1
0

modal.min.js 15 KB

1234567891011
  1. /*
  2. * # Fomantic UI - 2.7.8
  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(N,P,R,W){"use strict";N.isFunction=N.isFunction||function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},P=void 0!==P&&P.Math==Math?P:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),N.fn.modal=function(F){var x,e=N(this),D=N(P),A=N(R),H=N("body"),O=e.selector||"",T=(new Date).getTime(),E=[],z=F,j="string"==typeof z,q=[].slice.call(arguments,1),L=P.requestAnimationFrame||P.mozRequestAnimationFrame||P.webkitRequestAnimationFrame||P.msRequestAnimationFrame||function(e){setTimeout(e,0)};return e.each(function(){var o,r,e,t,n,s,a,i,c,l,d,u=N.isPlainObject(F)?N.extend(!0,{},N.fn.modal.settings,F):N.extend({},N.fn.modal.settings),m=u.selector,f=u.className,g=u.namespace,h=u.error,v="."+g,b="module-"+g,p=N(this),y=N(u.context),w=p.find(m.close),C=this,k=p.data(b),S=!1,M="";d={initialize:function(){d.verbose("Initializing dimmer",y),d.create.id(),d.create.dimmer(),u.allowMultiple&&d.create.innerDimmer(),u.centered||p.addClass("top aligned"),d.refreshModals(),d.bind.events(),u.observeChanges&&d.observeChanges(),d.instantiate()},instantiate:function(){d.verbose("Storing instance of modal"),k=d,p.data(b,k)},create:{dimmer:function(){var e={debug:u.debug,dimmerName:"modals"},i=N.extend(!0,e,u.dimmerSettings);N.fn.dimmer!==W?(d.debug("Creating dimmer"),t=y.dimmer(i),u.detachable?(d.verbose("Modal is detachable, moving content into dimmer"),t.dimmer("add content",p)):d.set.undetached(),n=t.dimmer("get dimmer")):d.error(h.dimmer)},id:function(){c=(Math.random().toString(16)+"000000000").substr(2,8),i="."+c,d.verbose("Creating unique id for element",c)},innerDimmer:function(){0==p.find(m.dimmer).length&&p.prepend('<div class="ui inverted dimmer"></div>')}},destroy:function(){l&&l.disconnect(),d.verbose("Destroying previous modal"),p.removeData(b).off(v),D.off(i),n.off(i),w.off(v),y.dimmer("destroy")},observeChanges:function(){"MutationObserver"in P&&((l=new MutationObserver(function(e){d.debug("DOM tree modified, refreshing"),d.refresh()})).observe(C,{childList:!0,subtree:!0}),d.debug("Setting up mutation observer",l))},refresh:function(){d.remove.scrolling(),d.cacheSizes(),d.can.useFlex()||d.set.modalOffset(),d.set.screenHeight(),d.set.type()},refreshModals:function(){r=p.siblings(m.modal),o=r.add(p)},attachEvents:function(e,i){var n=N(e);i=N.isFunction(d[i])?d[i]:d.toggle,0<n.length?(d.debug("Attaching modal events to element",e,i),n.off(v).on("click"+v,i)):d.error(h.notFound,e)},bind:{events:function(){d.verbose("Attaching events"),p.on("click"+v,m.close,d.event.close).on("click"+v,m.approve,d.event.approve).on("click"+v,m.deny,d.event.deny),D.on("resize"+i,d.event.resize)},scrollLock:function(){t.get(0).addEventListener("touchmove",d.event.preventScroll,{passive:!1})}},unbind:{scrollLock:function(){t.get(0).removeEventListener("touchmove",d.event.preventScroll,{passive:!1})}},get:{id:function(){return(Math.random().toString(16)+"000000000").substr(2,8)}},event:{approve:function(){S||!1===u.onApprove.call(C,N(this))?d.verbose("Approve callback returned false cancelling hide"):(S=!0,d.hide(function(){S=!1}))},preventScroll:function(e){-1!==e.target.className.indexOf("dimmer")&&e.preventDefault()},deny:function(){S||!1===u.onDeny.call(C,N(this))?d.verbose("Deny callback returned false cancelling hide"):(S=!0,d.hide(function(){S=!1}))},close:function(){d.hide()},mousedown:function(e){var i=N(e.target);(s=0<i.closest(m.modal).length)&&d.verbose("Mouse down event registered inside the modal"),(a=d.is.scrolling()&&N(P).outerWidth()-u.scrollbarWidth<=e.clientX)&&d.verbose("Mouse down event registered inside the scrollbar")},mouseup:function(e){if(u.closable)if(s)d.debug("Dimmer clicked but mouse down was initially registered inside the modal");else if(a)d.debug("Dimmer clicked but mouse down was initially registered inside the scrollbar");else{var i=0<N(e.target).closest(m.modal).length,n=N.contains(R.documentElement,e.target);if(!i&&n&&d.is.active()&&p.hasClass(f.front)){if(d.debug("Dimmer clicked, hiding all modals"),u.allowMultiple){if(!d.hideAll())return}else if(!d.hide())return;d.remove.clickaway()}}else d.verbose("Dimmer clicked but closable setting is disabled")},debounce:function(e,i){clearTimeout(d.timer),d.timer=setTimeout(e,i)},keyboard:function(e){27==e.which&&(u.closable?(d.debug("Escape key pressed hiding modal"),p.hasClass(f.front)&&d.hide()):d.debug("Escape key pressed, but closable is set to false"),e.preventDefault())},resize:function(){t.dimmer("is active")&&(d.is.animating()||d.is.active())&&L(d.refresh)}},toggle:function(){d.is.active()||d.is.animating()?d.hide():d.show()},show:function(e){e=N.isFunction(e)?e:function(){},d.refreshModals(),d.set.dimmerSettings(),d.set.dimmerStyles(),d.showModal(e)},hide:function(e){return e=N.isFunction(e)?e:function(){},d.refreshModals(),d.hideModal(e)},showModal:function(e){e=N.isFunction(e)?e:function(){},d.is.animating()||!d.is.active()?(d.showDimmer(),d.cacheSizes(),d.can.useFlex()?d.remove.legacy():(d.set.legacy(),d.set.modalOffset(),d.debug("Using non-flex legacy modal positioning.")),d.set.screenHeight(),d.set.type(),d.set.clickaway(),!u.allowMultiple&&d.others.active()?d.hideOthers(d.showModal):(S=!1,u.allowMultiple&&(d.others.active()&&r.filter("."+f.active).find(m.dimmer).addClass("active"),u.detachable&&p.detach().appendTo(n)),u.onShow.call(C),u.transition&&N.fn.transition!==W&&p.transition("is supported")?(d.debug("Showing modal with css animations"),p.transition({debug:u.debug,animation:u.transition+" in",queue:u.queue,duration:u.duration,useFailSafe:!0,onComplete:function(){u.onVisible.apply(C),u.keyboardShortcuts&&d.add.keyboardShortcuts(),d.save.focus(),d.set.active(),u.autofocus&&d.set.autofocus(),e()}})):d.error(h.noTransition))):d.debug("Modal is already visible")},hideModal:function(e,i,n){var t=r.filter("."+f.active).last();if(e=N.isFunction(e)?e:function(){},d.debug("Hiding modal"),!1===u.onHide.call(C,N(this)))return d.verbose("Hide callback returned false cancelling hide"),S=!1;(d.is.animating()||d.is.active())&&(u.transition&&N.fn.transition!==W&&p.transition("is supported")?(d.remove.active(),p.transition({debug:u.debug,animation:u.transition+" out",queue:u.queue,duration:u.duration,useFailSafe:!0,onStart:function(){d.others.active()||d.others.animating()||i||d.hideDimmer(),u.keyboardShortcuts&&!d.others.active()&&d.remove.keyboardShortcuts()},onComplete:function(){d.unbind.scrollLock(),u.allowMultiple&&(t.addClass(f.front),p.removeClass(f.front),n?o.find(m.dimmer).removeClass("active"):t.find(m.dimmer).removeClass("active")),u.onHidden.call(C),d.remove.dimmerStyles(),d.restore.focus(),e()}})):d.error(h.noTransition))},showDimmer:function(){t.dimmer("is animating")||!t.dimmer("is active")?(d.save.bodyMargin(),d.debug("Showing dimmer"),t.dimmer("show")):d.debug("Dimmer already visible")},hideDimmer:function(){t.dimmer("is animating")||t.dimmer("is active")?(d.unbind.scrollLock(),t.dimmer("hide",function(){d.restore.bodyMargin(),d.remove.clickaway(),d.remove.screenHeight()})):d.debug("Dimmer is not visible cannot hide")},hideAll:function(n){var e=o.filter("."+f.active+", ."+f.animating);if(n=N.isFunction(n)?n:function(){},0<e.length){d.debug("Hiding all visible modals");var t=!0;return N(e.get().reverse()).each(function(e,i){t=t&&N(i).modal("hide modal",n,!1,!0)}),t&&d.hideDimmer(),t}},hideOthers:function(e){var i=r.filter("."+f.active+", ."+f.animating);e=N.isFunction(e)?e:function(){},0<i.length&&(d.debug("Hiding other modals",r),i.modal("hide modal",e,!0))},others:{active:function(){return 0<r.filter("."+f.active).length},animating:function(){return 0<r.filter("."+f.animating).length}},add:{keyboardShortcuts:function(){d.verbose("Adding keyboard shortcuts"),A.on("keyup"+v,d.event.keyboard)}},save:{focus:function(){0<N(R.activeElement).closest(p).length||(e=N(R.activeElement).blur())},bodyMargin:function(){M=H.css("margin-right");var e=parseInt(M.replace(/[^\d.]/g,""))+(P.innerWidth-R.documentElement.clientWidth);H.css("margin-right",e+"px"),H.find(m.bodyFixed).css("padding-right",e+"px")}},restore:{focus:function(){e&&0<e.length&&u.restoreFocus&&e.focus()},bodyMargin:function(){H.css("margin-right",M),H.find(m.bodyFixed).css("padding-right",M)}},remove:{active:function(){p.removeClass(f.active)},legacy:function(){p.removeClass(f.legacy)},clickaway:function(){n.off("mousedown"+i),n.off("mouseup"+i)},dimmerStyles:function(){n.removeClass(f.inverted),t.removeClass(f.blurring)},bodyStyle:function(){""===H.attr("style")&&(d.verbose("Removing style attribute"),H.removeAttr("style"))},screenHeight:function(){d.debug("Removing page height"),H.css("height","")},keyboardShortcuts:function(){d.verbose("Removing keyboard shortcuts"),A.off("keyup"+v)},scrolling:function(){t.removeClass(f.scrolling),p.removeClass(f.scrolling)}},cacheSizes:function(){p.addClass(f.loading);var e=p.prop("scrollHeight"),i=p.outerWidth(),n=p.outerHeight();d.cache!==W&&0===n||(d.cache={pageHeight:N(R).outerHeight(),width:i,height:n+u.offset,scrollHeight:e+u.offset,contextHeight:"body"==u.context?N(P).height():t.height()},d.cache.topOffset=-d.cache.height/2),p.removeClass(f.loading),d.debug("Caching modal and container sizes",d.cache)},can:{useFlex:function(){return"auto"==u.useFlex?u.detachable&&!d.is.ie():u.useFlex},fit:function(){var e=d.cache.contextHeight,i=d.cache.contextHeight/2,n=d.cache.topOffset,t=d.cache.scrollHeight,o=d.cache.height,r=u.padding;return o<t?i+n+t+r<e:o+2*r<e}},is:{active:function(){return p.hasClass(f.active)},ie:function(){return!P.ActiveXObject&&"ActiveXObject"in P||"ActiveXObject"in P},animating:function(){return p.transition("is supported")?p.transition("is animating"):p.is(":visible")},scrolling:function(){return t.hasClass(f.scrolling)},modernBrowser:function(){return!(P.ActiveXObject||"ActiveXObject"in P)}},set:{autofocus:function(){var e=p.find("[tabindex], :input").filter(":visible").filter(function(){return 0===N(this).closest(".disabled").length}),i=e.filter("[autofocus]"),n=0<i.length?i.first():e.first();0<n.length&&n.focus()},clickaway:function(){n.on("mousedown"+i,d.event.mousedown),n.on("mouseup"+i,d.event.mouseup)},dimmerSettings:function(){if(N.fn.dimmer!==W){var e={debug:u.debug,dimmerName:"modals",closable:"auto",useFlex:d.can.useFlex(),duration:{show:u.duration,hide:u.duration}},i=N.extend(!0,e,u.dimmerSettings);u.inverted&&(i.variation=i.variation!==W?i.variation+" inverted":"inverted"),y.dimmer("setting",i)}else d.error(h.dimmer)},dimmerStyles:function(){u.inverted?n.addClass(f.inverted):n.removeClass(f.inverted),u.blurring?t.addClass(f.blurring):t.removeClass(f.blurring)},modalOffset:function(){var e=d.cache.width,i=d.cache.height;p.css({marginTop:!p.hasClass("aligned")&&d.can.fit()?-i/2:0,marginLeft:-e/2}),d.verbose("Setting modal offset for legacy mode")},screenHeight:function(){d.can.fit()?H.css("height",""):p.hasClass("bottom")||(d.debug("Modal is taller than page content, resizing page height"),H.css("height",d.cache.height+2*u.padding))},active:function(){p.addClass(f.active+" "+f.front),r.filter("."+f.active).removeClass(f.front)},scrolling:function(){t.addClass(f.scrolling),p.addClass(f.scrolling),d.unbind.scrollLock()},legacy:function(){p.addClass(f.legacy)},type:function(){d.can.fit()?(d.verbose("Modal fits on screen"),d.others.active()||d.others.animating()||(d.remove.scrolling(),d.bind.scrollLock())):p.hasClass("bottom")?d.verbose("Bottom aligned modal not fitting on screen is unsupported for scrolling"):(d.verbose("Modal cannot fit on screen setting to scrolling"),d.set.scrolling())},undetached:function(){t.addClass(f.undetached)}},setting:function(e,i){if(d.debug("Changing setting",e,i),N.isPlainObject(e))N.extend(!0,u,e);else{if(i===W)return u[e];N.isPlainObject(u[e])?N.extend(!0,u[e],i):u[e]=i}},internal:function(e,i){if(N.isPlainObject(e))N.extend(!0,d,e);else{if(i===W)return d[e];d[e]=i}},debug:function(){!u.silent&&u.debug&&(u.performance?d.performance.log(arguments):(d.debug=Function.prototype.bind.call(console.info,console,u.name+":"),d.debug.apply(console,arguments)))},verbose:function(){!u.silent&&u.verbose&&u.debug&&(u.performance?d.performance.log(arguments):(d.verbose=Function.prototype.bind.call(console.info,console,u.name+":"),d.verbose.apply(console,arguments)))},error:function(){u.silent||(d.error=Function.prototype.bind.call(console.error,console,u.name+":"),d.error.apply(console,arguments))},performance:{log:function(e){var i,n;u.performance&&(n=(i=(new Date).getTime())-(T||i),T=i,E.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:C,"Execution Time":n})),clearTimeout(d.performance.timer),d.performance.timer=setTimeout(d.performance.display,500)},display:function(){var e=u.name+":",n=0;T=!1,clearTimeout(d.performance.timer),N.each(E,function(e,i){n+=i["Execution Time"]}),e+=" "+n+"ms",O&&(e+=" '"+O+"'"),(console.group!==W||console.table!==W)&&0<E.length&&(console.groupCollapsed(e),console.table?console.table(E):N.each(E,function(e,i){console.log(i.Name+": "+i["Execution Time"]+"ms")}),console.groupEnd()),E=[]}},invoke:function(t,e,i){var o,r,n,s=k;return e=e||q,i=C||i,"string"==typeof t&&s!==W&&(t=t.split(/[\. ]/),o=t.length-1,N.each(t,function(e,i){var n=e!=o?i+t[e+1].charAt(0).toUpperCase()+t[e+1].slice(1):t;if(N.isPlainObject(s[n])&&e!=o)s=s[n];else{if(s[n]!==W)return r=s[n],!1;if(!N.isPlainObject(s[i])||e==o)return s[i]!==W&&(r=s[i]),!1;s=s[i]}})),N.isFunction(r)?n=r.apply(i,e):r!==W&&(n=r),Array.isArray(x)?x.push(n):x!==W?x=[x,n]:n!==W&&(x=n),r}},j?(k===W&&d.initialize(),d.invoke(z)):(k!==W&&k.invoke("destroy"),d.initialize())}),x!==W?x:this},N.fn.modal.settings={name:"Modal",namespace:"modal",useFlex:"auto",offset:0,silent:!1,debug:!1,verbose:!1,performance:!0,observeChanges:!1,allowMultiple:!1,detachable:!0,closable:!0,autofocus:!0,restoreFocus:!0,inverted:!1,blurring:!1,centered:!0,dimmerSettings:{closable:!1,useCSS:!0},keyboardShortcuts:!0,context:"body",queue:!1,duration:500,transition:"scale",padding:50,scrollbarWidth:10,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",dimmer:"> .ui.dimmer",bodyFixed:"> .ui.fixed.menu, > .ui.right.toast-container, > .ui.right.sidebar"},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",legacy:"legacy",loading:"loading",scrolling:"scrolling",undetached:"undetached",front:"front"}}}(jQuery,window,document);