modal.min.js 11.3 KB
!function(E,j,N,P){"use strict";j=void 0!==j&&j.Math==Math?j:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),E.fn.modal=function(S){var C,e=E(this),M=E(j),H=E(N),F=E("body"),D=e.selector||"",A=(new Date).getTime(),x=[],T=S,O="string"==typeof T,z=[].slice.call(arguments,1),q=j.requestAnimationFrame||j.mozRequestAnimationFrame||j.webkitRequestAnimationFrame||j.msRequestAnimationFrame||function(e){setTimeout(e,0)};return e.each(function(){var n,t,e,o,a,i,r,s,c,l=E.isPlainObject(S)?E.extend(!0,{},E.fn.modal.settings,S):E.extend({},E.fn.modal.settings),d=l.selector,u=l.className,m=l.namespace,f=l.error,g="."+m,h="module-"+m,v=E(this),b=E(l.context),p=v.find(d.close),y=this,k=v.data(h),w=!1;c={initialize:function(){c.verbose("Initializing dimmer",b),c.create.id(),c.create.dimmer(),c.refreshModals(),c.bind.events(),l.observeChanges&&c.observeChanges(),c.instantiate()},instantiate:function(){c.verbose("Storing instance of modal"),k=c,v.data(h,k)},create:{dimmer:function(){var e={debug:l.debug,variation:!l.centered&&"top aligned",dimmerName:"modals"},i=E.extend(!0,e,l.dimmerSettings);E.fn.dimmer!==P?(c.debug("Creating dimmer"),o=b.dimmer(i),l.detachable?(c.verbose("Modal is detachable, moving content into dimmer"),o.dimmer("add content",v)):c.set.undetached(),a=o.dimmer("get dimmer")):c.error(f.dimmer)},id:function(){r=(Math.random().toString(16)+"000000000").substr(2,8),i="."+r,c.verbose("Creating unique id for element",r)}},destroy:function(){c.verbose("Destroying previous modal"),v.removeData(h).off(g),M.off(i),a.off(i),p.off(g),b.dimmer("destroy")},observeChanges:function(){"MutationObserver"in j&&((s=new MutationObserver(function(e){c.debug("DOM tree modified, refreshing"),c.refresh()})).observe(y,{childList:!0,subtree:!0}),c.debug("Setting up mutation observer",s))},refresh:function(){c.remove.scrolling(),c.cacheSizes(),c.set.screenHeight(),c.set.type()},refreshModals:function(){t=v.siblings(d.modal),n=t.add(v)},attachEvents:function(e,i){var n=E(e);i=E.isFunction(c[i])?c[i]:c.toggle,0<n.length?(c.debug("Attaching modal events to element",e,i),n.off(g).on("click"+g,i)):c.error(f.notFound,e)},bind:{events:function(){c.verbose("Attaching events"),v.on("click"+g,d.close,c.event.close).on("click"+g,d.approve,c.event.approve).on("click"+g,d.deny,c.event.deny),M.on("resize"+i,c.event.resize)}},get:{id:function(){return(Math.random().toString(16)+"000000000").substr(2,8)}},event:{approve:function(){w||!1===l.onApprove.call(y,E(this))?c.verbose("Approve callback returned false cancelling hide"):(w=!0,c.hide(function(){w=!1}))},deny:function(){w||!1===l.onDeny.call(y,E(this))?c.verbose("Deny callback returned false cancelling hide"):(w=!0,c.hide(function(){w=!1}))},close:function(){c.hide()},click:function(e){if(l.closable){var i=0<E(e.target).closest(d.modal).length,n=E.contains(N.documentElement,e.target);!i&&n&&c.is.active()&&(c.debug("Dimmer clicked, hiding all modals"),c.remove.clickaway(),l.allowMultiple?c.hide():c.hideAll())}else c.verbose("Dimmer clicked but closable setting is disabled")},debounce:function(e,i){clearTimeout(c.timer),c.timer=setTimeout(e,i)},keyboard:function(e){27==e.which&&(l.closable?(c.debug("Escape key pressed hiding modal"),c.hide()):c.debug("Escape key pressed, but closable is set to false"),e.preventDefault())},resize:function(){o.dimmer("is active")&&(c.is.animating()||c.is.active())&&q(c.refresh)}},toggle:function(){c.is.active()||c.is.animating()?c.hide():c.show()},show:function(e){e=E.isFunction(e)?e:function(){},c.refreshModals(),c.set.dimmerSettings(),c.showModal(e)},hide:function(e){e=E.isFunction(e)?e:function(){},c.refreshModals(),c.hideModal(e)},showModal:function(e){e=E.isFunction(e)?e:function(){},c.is.animating()||!c.is.active()?(c.showDimmer(),c.cacheSizes(),c.set.screenHeight(),c.set.type(),c.set.clickaway(),!l.allowMultiple&&c.others.active()?c.hideOthers(c.showModal):(l.allowMultiple&&l.detachable&&v.detach().appendTo(a),l.onShow.call(y),l.transition&&E.fn.transition!==P&&v.transition("is supported")?(c.debug("Showing modal with css animations"),v.transition({debug:l.debug,animation:l.transition+" in",queue:l.queue,duration:l.duration,useFailSafe:!0,onComplete:function(){l.onVisible.apply(y),l.keyboardShortcuts&&c.add.keyboardShortcuts(),c.save.focus(),c.set.active(),l.autofocus&&c.set.autofocus(),e()}})):c.error(f.noTransition))):c.debug("Modal is already visible")},hideModal:function(e,i){e=E.isFunction(e)?e:function(){},c.debug("Hiding modal"),!1!==l.onHide.call(y,E(this))?(c.is.animating()||c.is.active())&&(l.transition&&E.fn.transition!==P&&v.transition("is supported")?(c.remove.active(),v.transition({debug:l.debug,animation:l.transition+" out",queue:l.queue,duration:l.duration,useFailSafe:!0,onStart:function(){c.others.active()||i||c.hideDimmer(),l.keyboardShortcuts&&c.remove.keyboardShortcuts()},onComplete:function(){l.onHidden.call(y),c.restore.focus(),e()}})):c.error(f.noTransition)):c.verbose("Hide callback returned false cancelling hide")},showDimmer:function(){o.dimmer("is animating")||!o.dimmer("is active")?(c.debug("Showing dimmer"),o.dimmer("show")):c.debug("Dimmer already visible")},hideDimmer:function(){o.dimmer("is animating")||o.dimmer("is active")?o.dimmer("hide",function(){c.remove.clickaway(),c.remove.screenHeight()}):c.debug("Dimmer is not visible cannot hide")},hideAll:function(e){var i=n.filter("."+u.active+", ."+u.animating);e=E.isFunction(e)?e:function(){},0<i.length&&(c.debug("Hiding all visible modals"),c.hideDimmer(),i.modal("hide modal",e))},hideOthers:function(e){var i=t.filter("."+u.active+", ."+u.animating);e=E.isFunction(e)?e:function(){},0<i.length&&(c.debug("Hiding other modals",t),i.modal("hide modal",e,!0))},others:{active:function(){return 0<t.filter("."+u.active).length},animating:function(){return 0<t.filter("."+u.animating).length}},add:{keyboardShortcuts:function(){c.verbose("Adding keyboard shortcuts"),H.on("keyup"+g,c.event.keyboard)}},save:{focus:function(){0<E(N.activeElement).closest(v).length||(e=E(N.activeElement).blur())}},restore:{focus:function(){e&&0<e.length&&e.focus()}},remove:{active:function(){v.removeClass(u.active)},clickaway:function(){a.off("click"+i)},bodyStyle:function(){""===F.attr("style")&&(c.verbose("Removing style attribute"),F.removeAttr("style"))},screenHeight:function(){c.debug("Removing page height"),F.css("height","")},keyboardShortcuts:function(){c.verbose("Removing keyboard shortcuts"),H.off("keyup"+g)},scrolling:function(){o.removeClass(u.scrolling),v.removeClass(u.scrolling)}},cacheSizes:function(){v.addClass(u.loading);var e=v.prop("scrollHeight"),i=v.outerHeight();c.cache!==P&&0===i||(c.cache={pageHeight:E(N).outerHeight(),height:i+l.offset,scrollHeight:e+l.offset,contextHeight:"body"==l.context?E(j).height():o.height()},c.cache.topOffset=-c.cache.height/2),v.removeClass(u.loading),c.debug("Caching modal and container sizes",c.cache)},can:{fit:function(){var e=c.cache.contextHeight,i=c.cache.contextHeight/2,n=c.cache.topOffset,t=c.cache.scrollHeight,o=c.cache.height,a=l.padding;return o<t?i+n+t+a<e:o+2*a<e}},is:{active:function(){return v.hasClass(u.active)},animating:function(){return v.transition("is supported")?v.transition("is animating"):v.is(":visible")},scrolling:function(){return o.hasClass(u.scrolling)},modernBrowser:function(){return!(j.ActiveXObject||"ActiveXObject"in j)}},set:{autofocus:function(){var e=v.find("[tabindex], :input").filter(":visible"),i=e.filter("[autofocus]"),n=0<i.length?i.first():e.first();0<n.length&&n.focus()},clickaway:function(){a.on("click"+i,c.event.click)},dimmerSettings:function(){if(E.fn.dimmer!==P){var e={debug:l.debug,dimmerName:"modals",closable:"auto",variation:!l.centered&&"top aligned",duration:{show:l.duration,hide:l.duration}},i=E.extend(!0,e,l.dimmerSettings);l.inverted?(i.variation=i.variation!==P?i.variation+" inverted":"inverted",a.addClass(u.inverted)):a.removeClass(u.inverted),l.blurring?o.addClass(u.blurring):o.removeClass(u.blurring),b.dimmer("setting",i)}else c.error(f.dimmer)},screenHeight:function(){c.can.fit()?F.css("height",""):(c.debug("Modal is taller than page content, resizing page height"),F.css("height",c.cache.height+2*l.padding))},active:function(){v.addClass(u.active)},scrolling:function(){o.addClass(u.scrolling),v.addClass(u.scrolling)},type:function(){c.can.fit()?(c.verbose("Modal fits on screen"),c.others.active()||c.others.animating()||c.remove.scrolling()):(c.verbose("Modal cannot fit on screen setting to scrolling"),c.set.scrolling())},undetached:function(){o.addClass(u.undetached)}},setting:function(e,i){if(c.debug("Changing setting",e,i),E.isPlainObject(e))E.extend(!0,l,e);else{if(i===P)return l[e];E.isPlainObject(l[e])?E.extend(!0,l[e],i):l[e]=i}},internal:function(e,i){if(E.isPlainObject(e))E.extend(!0,c,e);else{if(i===P)return c[e];c[e]=i}},debug:function(){!l.silent&&l.debug&&(l.performance?c.performance.log(arguments):(c.debug=Function.prototype.bind.call(console.info,console,l.name+":"),c.debug.apply(console,arguments)))},verbose:function(){!l.silent&&l.verbose&&l.debug&&(l.performance?c.performance.log(arguments):(c.verbose=Function.prototype.bind.call(console.info,console,l.name+":"),c.verbose.apply(console,arguments)))},error:function(){l.silent||(c.error=Function.prototype.bind.call(console.error,console,l.name+":"),c.error.apply(console,arguments))},performance:{log:function(e){var i,n;l.performance&&(n=(i=(new Date).getTime())-(A||i),A=i,x.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:y,"Execution Time":n})),clearTimeout(c.performance.timer),c.performance.timer=setTimeout(c.performance.display,500)},display:function(){var e=l.name+":",n=0;A=!1,clearTimeout(c.performance.timer),E.each(x,function(e,i){n+=i["Execution Time"]}),e+=" "+n+"ms",D&&(e+=" '"+D+"'"),(console.group!==P||console.table!==P)&&0<x.length&&(console.groupCollapsed(e),console.table?console.table(x):E.each(x,function(e,i){console.log(i.Name+": "+i["Execution Time"]+"ms")}),console.groupEnd()),x=[]}},invoke:function(t,e,i){var o,a,n,r=k;return e=e||z,i=y||i,"string"==typeof t&&r!==P&&(t=t.split(/[\. ]/),o=t.length-1,E.each(t,function(e,i){var n=e!=o?i+t[e+1].charAt(0).toUpperCase()+t[e+1].slice(1):t;if(E.isPlainObject(r[n])&&e!=o)r=r[n];else{if(r[n]!==P)return a=r[n],!1;if(!E.isPlainObject(r[i])||e==o)return r[i]!==P&&(a=r[i]),!1;r=r[i]}})),E.isFunction(a)?n=a.apply(i,e):a!==P&&(n=a),E.isArray(C)?C.push(n):C!==P?C=[C,n]:n!==P&&(C=n),a}},O?(k===P&&c.initialize(),c.invoke(T)):(k!==P&&k.invoke("destroy"),c.initialize())}),C!==P?C:this},E.fn.modal.settings={name:"Modal",namespace:"modal",silent:!1,debug:!1,verbose:!1,performance:!0,observeChanges:!1,allowMultiple:!1,detachable:!0,closable:!0,autofocus:!0,inverted:!1,blurring:!1,centered:!0,dimmerSettings:{closable:!1,useCSS:!0},keyboardShortcuts:!0,context:"body",queue:!1,duration:500,offset:0,transition:"scale",padding:50,onShow:function(){},onVisible:function(){},onHide:function(){return!0},onHidden:function(){},onApprove:function(){return!0},onDeny:function(){return!0},selector:{close:"> .close",approve:".actions .positive, .actions .approve, .actions .ok",deny:".actions .negative, .actions .deny, .actions .cancel",modal:".ui.modal"},error:{dimmer:"UI Dimmer, a required component is not included in this page",method:"The method you called is not defined.",notFound:"The element you specified could not be found"},className:{active:"active",animating:"animating",blurring:"blurring",inverted:"inverted",loading:"loading",scrolling:"scrolling",undetached:"undetached"}}}(jQuery,window,document);