popup.min.js 19 KB

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