osu-wayback/static/components/popup.min.js
2018-01-12 23:16:10 +01:00

1 line
18 KiB
JavaScript

!function(t,e,o,n){"use strict";e=void 0!==e&&e.Math==Math?e:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),t.fn.popup=function(n){var i,r=t(this),a=t(o),s=t(e),p=t("body"),l=r.selector||"",u=(new Date).getTime(),c=[],d=arguments[0],f="string"==typeof d,g=[].slice.call(arguments,1);return r.each(function(){var r,h,v,m,b,w,y=t.isPlainObject(n)?t.extend(!0,{},t.fn.popup.settings,n):t.extend({},t.fn.popup.settings),C=y.selector,T=y.className,P=y.error,x=y.metadata,k=y.namespace,S="."+y.namespace,E="module-"+k,O=t(this),A=t(y.context),D=t(y.scrollContext),j=t(y.boundary),F=y.target?t(y.target):O,R=0,H=!1,N=!1,V=this,M=O.data(E);w={initialize:function(){w.debug("Initializing",O),w.createID(),w.bind.events(),!w.exists()&&y.preserve&&w.create(),y.observeChanges&&w.observeChanges(),w.instantiate()},instantiate:function(){w.verbose("Storing instance",w),M=w,O.data(E,M)},observeChanges:function(){"MutationObserver"in e&&(v=new MutationObserver(w.event.documentChanged),v.observe(o,{childList:!0,subtree:!0}),w.debug("Setting up mutation observer",v))},refresh:function(){y.popup?r=t(y.popup).eq(0):y.inline&&(r=F.nextAll(C.popup).eq(0),y.popup=r),y.popup?(r.addClass(T.loading),h=w.get.offsetParent(),r.removeClass(T.loading),y.movePopup&&w.has.popup()&&w.get.offsetParent(r)[0]!==h[0]&&(w.debug("Moving popup to the same offset parent as target"),r.detach().appendTo(h))):h=y.inline?w.get.offsetParent(F):w.has.popup()?w.get.offsetParent(r):p,h.is("html")&&h[0]!==p[0]&&(w.debug("Setting page as offset parent"),h=p),w.get.variation()&&w.set.variation()},reposition:function(){w.refresh(),w.set.position()},destroy:function(){w.debug("Destroying previous module"),v&&v.disconnect(),r&&!y.preserve&&w.removePopup(),clearTimeout(w.hideTimer),clearTimeout(w.showTimer),w.unbind.close(),w.unbind.events(),O.removeData(E)},event:{start:function(e){var o=t.isPlainObject(y.delay)?y.delay.show:y.delay;clearTimeout(w.hideTimer),N||(w.showTimer=setTimeout(w.show,o))},end:function(){var e=t.isPlainObject(y.delay)?y.delay.hide:y.delay;clearTimeout(w.showTimer),w.hideTimer=setTimeout(w.hide,e)},touchstart:function(t){N=!0,w.show()},resize:function(){w.is.visible()&&w.set.position()},documentChanged:function(e){[].forEach.call(e,function(e){e.removedNodes&&[].forEach.call(e.removedNodes,function(e){(e==V||t(e).find(V).length>0)&&(w.debug("Element removed from DOM, tearing down events"),w.destroy())})})},hideGracefully:function(e){var n=t(e.target),i=t.contains(o.documentElement,e.target),r=n.closest(C.popup).length>0;e&&!r&&i?(w.debug("Click occurred outside popup hiding popup"),w.hide()):w.debug("Click was inside popup, keeping popup open")}},create:function(){var e=w.get.html(),o=w.get.title(),n=w.get.content();e||n||o?(w.debug("Creating pop-up html"),e||(e=y.templates.popup({title:o,content:n})),r=t("<div/>").addClass(T.popup).data(x.activator,O).html(e),y.inline?(w.verbose("Inserting popup element inline",r),r.insertAfter(O)):(w.verbose("Appending popup element to body",r),r.appendTo(A)),w.refresh(),w.set.variation(),y.hoverable&&w.bind.popup(),y.onCreate.call(r,V)):0!==F.next(C.popup).length?(w.verbose("Pre-existing popup found"),y.inline=!0,y.popup=F.next(C.popup).data(x.activator,O),w.refresh(),y.hoverable&&w.bind.popup()):y.popup?(t(y.popup).data(x.activator,O),w.verbose("Used popup specified in settings"),w.refresh(),y.hoverable&&w.bind.popup()):w.debug("No content specified skipping display",V)},createID:function(){b=(Math.random().toString(16)+"000000000").substr(2,8),m="."+b,w.verbose("Creating unique id for element",b)},toggle:function(){w.debug("Toggling pop-up"),w.is.hidden()?(w.debug("Popup is hidden, showing pop-up"),w.unbind.close(),w.show()):(w.debug("Popup is visible, hiding pop-up"),w.hide())},show:function(t){if(t=t||function(){},w.debug("Showing pop-up",y.transition),w.is.hidden()&&(!w.is.active()||!w.is.dropdown())){if(w.exists()||w.create(),!1===y.onShow.call(r,V))return void w.debug("onShow callback returned false, cancelling popup animation");y.preserve||y.popup||w.refresh(),r&&w.set.position()&&(w.save.conditions(),y.exclusive&&w.hideAll(),w.animate.show(t))}},hide:function(t){if(t=t||function(){},w.is.visible()||w.is.animating()){if(!1===y.onHide.call(r,V))return void w.debug("onHide callback returned false, cancelling popup animation");w.remove.visible(),w.unbind.close(),w.restore.conditions(),w.animate.hide(t)}},hideAll:function(){t(C.popup).filter("."+T.popupVisible).each(function(){t(this).data(x.activator).popup("hide")})},exists:function(){return!!r&&(y.inline||y.popup?w.has.popup():r.closest(A).length>=1)},removePopup:function(){w.has.popup()&&!y.popup&&(w.debug("Removing popup",r),r.remove(),r=void 0,y.onRemove.call(r,V))},save:{conditions:function(){w.cache={title:O.attr("title")},w.cache.title&&O.removeAttr("title"),w.verbose("Saving original attributes",w.cache.title)}},restore:{conditions:function(){return w.cache&&w.cache.title&&(O.attr("title",w.cache.title),w.verbose("Restoring original attributes",w.cache.title)),!0}},supports:{svg:function(){return"undefined"==typeof SVGGraphicsElement}},animate:{show:function(e){e=t.isFunction(e)?e:function(){},y.transition&&void 0!==t.fn.transition&&O.transition("is supported")?(w.set.visible(),r.transition({animation:y.transition+" in",queue:!1,debug:y.debug,verbose:y.verbose,duration:y.duration,onComplete:function(){w.bind.close(),e.call(r,V),y.onVisible.call(r,V)}})):w.error(P.noTransition)},hide:function(e){if(e=t.isFunction(e)?e:function(){},w.debug("Hiding pop-up"),!1===y.onHide.call(r,V))return void w.debug("onHide callback returned false, cancelling popup animation");y.transition&&void 0!==t.fn.transition&&O.transition("is supported")?r.transition({animation:y.transition+" out",queue:!1,duration:y.duration,debug:y.debug,verbose:y.verbose,onComplete:function(){w.reset(),e.call(r,V),y.onHidden.call(r,V)}}):w.error(P.noTransition)}},change:{content:function(t){r.html(t)}},get:{html:function(){return O.removeData(x.html),O.data(x.html)||y.html},title:function(){return O.removeData(x.title),O.data(x.title)||y.title},content:function(){return O.removeData(x.content),O.data(x.content)||O.attr("title")||y.content},variation:function(){return O.removeData(x.variation),O.data(x.variation)||y.variation},popup:function(){return r},popupOffset:function(){return r.offset()},calculations:function(){var t,o=F[0],n=j[0]==e,i=y.inline||y.popup&&y.movePopup?F.position():F.offset(),a=n?{top:0,left:0}:j.offset(),p={},l=n?{top:s.scrollTop(),left:s.scrollLeft()}:{top:0,left:0};return p={target:{element:F[0],width:F.outerWidth(),height:F.outerHeight(),top:i.top,left:i.left,margin:{}},popup:{width:r.outerWidth(),height:r.outerHeight()},parent:{width:h.outerWidth(),height:h.outerHeight()},screen:{top:a.top,left:a.left,scroll:{top:l.top,left:l.left},width:j.width(),height:j.height()}},y.setFluidWidth&&w.is.fluid()&&(p.container={width:r.parent().outerWidth()},p.popup.width=p.container.width),p.target.margin.top=y.inline?parseInt(e.getComputedStyle(o).getPropertyValue("margin-top"),10):0,p.target.margin.left=y.inline?w.is.rtl()?parseInt(e.getComputedStyle(o).getPropertyValue("margin-right"),10):parseInt(e.getComputedStyle(o).getPropertyValue("margin-left"),10):0,t=p.screen,p.boundary={top:t.top+t.scroll.top,bottom:t.top+t.scroll.top+t.height,left:t.left+t.scroll.left,right:t.left+t.scroll.left+t.width},p},id:function(){return b},startEvent:function(){return"hover"==y.on?"mouseenter":"focus"==y.on&&"focus"},scrollEvent:function(){return"scroll"},endEvent:function(){return"hover"==y.on?"mouseleave":"focus"==y.on&&"blur"},distanceFromBoundary:function(t,e){var o,n,i={};return e=e||w.get.calculations(),o=e.popup,n=e.boundary,t&&(i={top:t.top-n.top,left:t.left-n.left,right:n.right-(t.left+o.width),bottom:n.bottom-(t.top+o.height)},w.verbose("Distance from boundaries determined",t,i)),i},offsetParent:function(e){var o=void 0!==e?e[0]:O[0],n=o.parentNode,i=t(n);if(n)for(var r="none"===i.css("transform"),a="static"===i.css("position"),s=i.is("html");n&&!s&&a&&r;)n=n.parentNode,i=t(n),r="none"===i.css("transform"),a="static"===i.css("position"),s=i.is("html");return i&&i.length>0?i:t()},positions:function(){return{"top left":!1,"top center":!1,"top right":!1,"bottom left":!1,"bottom center":!1,"bottom right":!1,"left center":!1,"right center":!1}},nextPosition:function(t){var e=t.split(" "),o=e[0],n=e[1],i={top:"bottom",bottom:"top",left:"right",right:"left"},r={left:"center",center:"right",right:"left"},a={"top left":"top center","top center":"top right","top right":"right center","right center":"bottom right","bottom right":"bottom center","bottom center":"bottom left","bottom left":"left center","left center":"top left"},s="top"==o||"bottom"==o,p=!1,l=!1,u=!1;return H||(w.verbose("All available positions available"),H=w.get.positions()),w.debug("Recording last position tried",t),H[t]=!0,"opposite"===y.prefer&&(u=[i[o],n],u=u.join(" "),p=!0===H[u],w.debug("Trying opposite strategy",u)),"adjacent"===y.prefer&&s&&(u=[o,r[n]],u=u.join(" "),l=!0===H[u],w.debug("Trying adjacent strategy",u)),(l||p)&&(w.debug("Using backup position",u),u=a[t]),u}},set:{position:function(t,e){if(0===F.length||0===r.length)return void w.error(P.notFound);var o,n,i,a,s,p,l,u;if(e=e||w.get.calculations(),t=t||O.data(x.position)||y.position,o=O.data(x.offset)||y.offset,n=y.distanceAway,i=e.target,a=e.popup,s=e.parent,0===i.width&&0===i.height&&!w.is.svg(i.element))return w.debug("Popup target is hidden, no action taken"),!1;switch(y.inline&&(w.debug("Adding margin to calculation",i.margin),"left center"==t||"right center"==t?(o+=i.margin.top,n+=-i.margin.left):"top left"==t||"top center"==t||"top right"==t?(o+=i.margin.left,n-=i.margin.top):(o+=i.margin.left,n+=i.margin.top)),w.debug("Determining popup position from calculations",t,e),w.is.rtl()&&(t=t.replace(/left|right/g,function(t){return"left"==t?"right":"left"}),w.debug("RTL: Popup position updated",t)),R==y.maxSearchDepth&&"string"==typeof y.lastResort&&(t=y.lastResort),t){case"top left":p={top:"auto",bottom:s.height-i.top+n,left:i.left+o,right:"auto"};break;case"top center":p={bottom:s.height-i.top+n,left:i.left+i.width/2-a.width/2+o,top:"auto",right:"auto"};break;case"top right":p={bottom:s.height-i.top+n,right:s.width-i.left-i.width-o,top:"auto",left:"auto"};break;case"left center":p={top:i.top+i.height/2-a.height/2+o,right:s.width-i.left+n,left:"auto",bottom:"auto"};break;case"right center":p={top:i.top+i.height/2-a.height/2+o,left:i.left+i.width+n,bottom:"auto",right:"auto"};break;case"bottom left":p={top:i.top+i.height+n,left:i.left+o,bottom:"auto",right:"auto"};break;case"bottom center":p={top:i.top+i.height+n,left:i.left+i.width/2-a.width/2+o,bottom:"auto",right:"auto"};break;case"bottom right":p={top:i.top+i.height+n,right:s.width-i.left-i.width-o,left:"auto",bottom:"auto"}}if(void 0===p&&w.error(P.invalidPosition,t),w.debug("Calculated popup positioning values",p),r.css(p).removeClass(T.position).addClass(t).addClass(T.loading),l=w.get.popupOffset(),u=w.get.distanceFromBoundary(l,e),w.is.offstage(u,t)){if(w.debug("Position is outside viewport",t),R<y.maxSearchDepth)return R++,t=w.get.nextPosition(t),w.debug("Trying new position",t),!!r&&w.set.position(t,e);if(!y.lastResort)return w.debug("Popup could not find a position to display",r),w.error(P.cannotPlace,V),w.remove.attempts(),w.remove.loading(),w.reset(),y.onUnplaceable.call(r,V),!1;w.debug("No position found, showing with last position")}return w.debug("Position is on stage",t),w.remove.attempts(),w.remove.loading(),y.setFluidWidth&&w.is.fluid()&&w.set.fluidWidth(e),!0},fluidWidth:function(t){t=t||w.get.calculations(),w.debug("Automatically setting element width to parent width",t.parent.width),r.css("width",t.container.width)},variation:function(t){(t=t||w.get.variation())&&w.has.popup()&&(w.verbose("Adding variation to popup",t),r.addClass(t))},visible:function(){O.addClass(T.visible)}},remove:{loading:function(){r.removeClass(T.loading)},variation:function(t){(t=t||w.get.variation())&&(w.verbose("Removing variation",t),r.removeClass(t))},visible:function(){O.removeClass(T.visible)},attempts:function(){w.verbose("Resetting all searched positions"),R=0,H=!1}},bind:{events:function(){w.debug("Binding popup events to module"),"click"==y.on&&O.on("click"+S,w.toggle),"hover"==y.on&&O.on("touchstart"+S,w.event.touchstart),w.get.startEvent()&&O.on(w.get.startEvent()+S,w.event.start).on(w.get.endEvent()+S,w.event.end),y.target&&w.debug("Target set to element",F),s.on("resize"+m,w.event.resize)},popup:function(){w.verbose("Allowing hover events on popup to prevent closing"),r&&w.has.popup()&&r.on("mouseenter"+S,w.event.start).on("mouseleave"+S,w.event.end)},close:function(){(!0===y.hideOnScroll||"auto"==y.hideOnScroll&&"click"!=y.on)&&w.bind.closeOnScroll(),"hover"==y.on&&N&&w.bind.touchClose(),"click"==y.on&&y.closable&&w.bind.clickaway()},closeOnScroll:function(){w.verbose("Binding scroll close event to document"),D.one(w.get.scrollEvent()+m,w.event.hideGracefully)},touchClose:function(){w.verbose("Binding popup touchclose event to document"),a.on("touchstart"+m,function(t){w.verbose("Touched away from popup"),w.event.hideGracefully.call(V,t)})},clickaway:function(){w.verbose("Binding popup close event to document"),a.on("click"+m,function(t){w.verbose("Clicked away from popup"),w.event.hideGracefully.call(V,t)})}},unbind:{events:function(){s.off(m),O.off(S)},close:function(){a.off(m),D.off(m)}},has:{popup:function(){return r&&r.length>0}},is:{offstage:function(e,o){var n=[];return t.each(e,function(t,e){e<-y.jitter&&(w.debug("Position exceeds allowable distance from edge",t,e,o),n.push(t))}),n.length>0},svg:function(t){return w.supports.svg()&&t instanceof SVGGraphicsElement},active:function(){return O.hasClass(T.active)},animating:function(){return void 0!==r&&r.hasClass(T.animating)},fluid:function(){return void 0!==r&&r.hasClass(T.fluid)},visible:function(){return void 0!==r&&r.hasClass(T.popupVisible)},dropdown:function(){return O.hasClass(T.dropdown)},hidden:function(){return!w.is.visible()},rtl:function(){return"rtl"==O.css("direction")}},reset:function(){w.remove.visible(),y.preserve?void 0!==t.fn.transition&&r.transition("remove transition"):w.removePopup()},setting:function(e,o){if(t.isPlainObject(e))t.extend(!0,y,e);else{if(void 0===o)return y[e];y[e]=o}},internal:function(e,o){if(t.isPlainObject(e))t.extend(!0,w,e);else{if(void 0===o)return w[e];w[e]=o}},debug:function(){!y.silent&&y.debug&&(y.performance?w.performance.log(arguments):(w.debug=Function.prototype.bind.call(console.info,console,y.name+":"),w.debug.apply(console,arguments)))},verbose:function(){!y.silent&&y.verbose&&y.debug&&(y.performance?w.performance.log(arguments):(w.verbose=Function.prototype.bind.call(console.info,console,y.name+":"),w.verbose.apply(console,arguments)))},error:function(){y.silent||(w.error=Function.prototype.bind.call(console.error,console,y.name+":"),w.error.apply(console,arguments))},performance:{log:function(t){var e,o,n;y.performance&&(e=(new Date).getTime(),n=u||e,o=e-n,u=e,c.push({Name:t[0],Arguments:[].slice.call(t,1)||"",Element:V,"Execution Time":o})),clearTimeout(w.performance.timer),w.performance.timer=setTimeout(w.performance.display,500)},display:function(){var e=y.name+":",o=0;u=!1,clearTimeout(w.performance.timer),t.each(c,function(t,e){o+=e["Execution Time"]}),e+=" "+o+"ms",l&&(e+=" '"+l+"'"),(void 0!==console.group||void 0!==console.table)&&c.length>0&&(console.groupCollapsed(e),console.table?console.table(c):t.each(c,function(t,e){console.log(e.Name+": "+e["Execution Time"]+"ms")}),console.groupEnd()),c=[]}},invoke:function(e,o,n){var r,a,s,p=M;return o=o||g,n=V||n,"string"==typeof e&&void 0!==p&&(e=e.split(/[\. ]/),r=e.length-1,t.each(e,function(o,n){var i=o!=r?n+e[o+1].charAt(0).toUpperCase()+e[o+1].slice(1):e;if(t.isPlainObject(p[i])&&o!=r)p=p[i];else{if(void 0!==p[i])return a=p[i],!1;if(!t.isPlainObject(p[n])||o==r)return void 0!==p[n]&&(a=p[n],!1);p=p[n]}})),t.isFunction(a)?s=a.apply(n,o):void 0!==a&&(s=a),t.isArray(i)?i.push(s):void 0!==i?i=[i,s]:void 0!==s&&(i=s),a}},f?(void 0===M&&w.initialize(),w.invoke(d)):(void 0!==M&&M.invoke("destroy"),w.initialize())}),void 0!==i?i:this},t.fn.popup.settings={name:"Popup",silent:!1,debug:!1,verbose:!1,performance:!0,namespace:"popup",observeChanges:!0,onCreate:function(){},onRemove:function(){},onShow:function(){},onVisible:function(){},onHide:function(){},onUnplaceable:function(){},onHidden:function(){},on:"hover",boundary:e,addTouchEvents:!0,position:"top left",variation:"",movePopup:!0,target:!1,popup:!1,inline:!1,preserve:!1,hoverable:!1,content:!1,html:!1,title:!1,closable:!0,hideOnScroll:"auto",exclusive:!1,context:"body",scrollContext:e,prefer:"opposite",lastResort:!1,delay:{show:50,hide:70},setFluidWidth:!0,duration:200,transition:"scale",distanceAway:0,jitter:2,offset:0,maxSearchDepth:15,error:{invalidPosition:"The position you specified is not a valid position",cannotPlace:"Popup does not fit within the boundaries of the viewport",method:"The method you called is not defined.",noTransition:"This module requires ui transitions <https://github.com/Semantic-Org/UI-Transition>",notFound:"The target or popup you specified does not exist on the page"},metadata:{activator:"activator",content:"content",html:"html",offset:"offset",position:"position",title:"title",variation:"variation"},className:{active:"active",animating:"animating",dropdown:"dropdown",fluid:"fluid",loading:"loading",popup:"ui popup",position:"top left center bottom right",visible:"visible",popupVisible:"visible"},selector:{popup:".ui.popup"},templates:{escape:function(t){var e=/[&<>"'`]/g,o=/[&<>"'`]/,n={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","`":"&#x60;"},i=function(t){return n[t]};return o.test(t)?t.replace(e,i):t},popup:function(e){var o="",n=t.fn.popup.settings.templates.escape;return void 0!==typeof e&&(void 0!==typeof e.title&&e.title&&(e.title=n(e.title),o+='<div class="header">'+e.title+"</div>"),void 0!==typeof e.content&&e.content&&(e.content=n(e.content),o+='<div class="content">'+e.content+"</div>")),o}}}}(jQuery,window,document);