toast.min.js 12 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(M,e,E){"use strict";M.isFunction=M.isFunction||function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},e=void 0!==e&&e.Math==Math?e:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")(),M.fn.toast=function(w){var k,e=M(this),x=e.selector||"",T=(new Date).getTime(),I=[],P=w,O="string"==typeof P,A=[].slice.call(arguments,1);return e.each(function(){var o,s,a,i,c,l,r,u=M.isPlainObject(w)?M.extend(!0,{},M.fn.toast.settings,w):M.extend({},M.fn.toast.settings),d=u.className,e=u.selector,p=u.error,n=u.namespace,f=u.fields,t="."+n,m=n+"-module",g=M(this),h=u.context?M(u.context):M("body"),v=g.hasClass("toast")||g.hasClass("message")||g.hasClass("card"),b=this,C=v?g.data(m):E,y={initialize:function(){y.verbose("Initializing element"),y.has.container()||y.create.container(),(v||""!==u.message||""!==u.title||""!==y.get.iconClass()||u.showImage||y.has.configActions())&&("string"==typeof u.showProgress&&-1!==[d.top,d.bottom].indexOf(u.showProgress)||(u.showProgress=!1),y.create.toast(),u.closeOnClick&&(u.closeIcon||0<M(s).find(e.input).length||y.has.configActions())&&(u.closeOnClick=!1),u.closeOnClick||o.addClass(d.unclickable),y.bind.events()),y.instantiate(),o&&y.show()},instantiate:function(){y.verbose("Storing instance of toast"),C=y,g.data(m,C)},destroy:function(){o&&(y.debug("Removing toast",o),y.unbind.events(),o.remove(),l=s=o=E,u.onRemove.call(o,b),r=c=i=E),g.removeData(m)},show:function(e){e=e||function(){},y.debug("Showing toast"),!1!==u.onShow.call(o,b)?y.animate.show(e):y.debug("onShow callback returned false, cancelling toast animation")},close:function(e){e=e||function(){},y.remove.visible(),y.unbind.events(),y.animate.close(e)},create:{container:function(){y.verbose("Creating container"),h.append(M("<div/>",{class:u.position+" "+d.container}))},toast:function(){var e,n,t;o=M("<div/>",{class:d.box}),v?(s=u.cloneModule?g.clone().removeAttr("id"):g,r=s.find("> i"+y.helpers.toClass(d.close)),u.closeIcon=0<r.length):(y.verbose("Creating toast"),s=M("<div/>"),e=M("<div/>",{class:d.content}),""!==(n=y.get.iconClass())&&s.append(M("<i/>",{class:n+" "+d.icon})),u.showImage&&s.append(M("<img>",{class:d.image+" "+u.classImage,src:u.showImage})),""!==u.title&&e.append(M("<div/>",{class:d.title,text:u.title})),e.append(M("<div/>",{html:y.helpers.escape(u.message,u.preserveHTML)})),s.addClass(u.class+" "+d.toast).append(e),s.css("opacity",u.opacity),u.closeIcon&&((r=M("<i/>",{class:d.close+" "+("string"==typeof u.closeIcon?u.closeIcon:"")})).hasClass(d.left)?s.prepend(r):s.append(r))),s.hasClass(d.compact)&&(u.compact=!0),s.hasClass("card")&&(u.compact=!1),a=s.find(".actions"),y.has.configActions()&&(0===a.length&&(a=M("<div/>",{class:d.actions+" "+(u.classActions||"")}).appendTo(s)),s.hasClass("card")&&!a.hasClass(d.attached)&&(a.addClass(d.extraContent),a.hasClass(d.vertical)&&(a.removeClass(d.vertical),y.error(p.verticalCard))),u.actions.forEach(function(e){var n=e[f.icon]?'<i class="'+y.helpers.deQuote(e[f.icon])+' icon"></i>':"",t=y.helpers.escape(e[f.text]||"",u.preserveHTML),o=y.helpers.deQuote(e[f.class]||""),s=e[f.click]&&M.isFunction(e[f.click])?e[f.click]:function(){};a.append(M("<button/>",{html:n+t,class:d.button+" "+o,click:function(){!1!==s.call(b,g)&&y.close()}}))})),a&&a.hasClass(d.vertical)&&s.addClass(d.vertical),0<a.length&&!a.hasClass(d.attached)&&(!a||a.hasClass(d.basic)&&!a.hasClass(d.left)||s.addClass(d.actions)),"auto"===u.displayTime&&(u.displayTime=Math.max(u.minDisplayTime,s.text().split(" ").length/u.wordsPerMinute*6e4)),o.append(s),0<a.length&&a.hasClass(d.attached)&&(a.addClass(d.buttons),a.detach(),s.addClass(d.attached),a.hasClass(d.vertical)?(s.wrap(M("<div/>",{class:d.vertical+" "+d.attached+" "+(u.compact?d.compact:"")})),a.hasClass(d.left)?s.addClass(d.left).parent().addClass(d.left).prepend(a):s.parent().append(a)):a.hasClass(d.top)?(o.prepend(a),s.addClass(d.bottom)):(o.append(a),s.addClass(d.top))),g!==s&&(b=(g=s)[0]),0<u.displayTime&&(t=d.progressing+" "+(u.pauseOnHover?d.pausable:""),u.showProgress&&(i=M("<div/>",{class:d.progress+" "+(u.classProgress||u.class),"data-percent":""}),u.classProgress||(s.hasClass("toast")&&!s.hasClass(d.inverted)?i.addClass(d.inverted):i.removeClass(d.inverted)),c=M("<div/>",{class:"bar "+(u.progressUp?"up ":"down ")+t}),i.addClass(u.showProgress).append(c),i.hasClass(d.top)?o.prepend(i):o.append(i),c.css("animation-duration",u.displayTime/1e3+"s")),(l=M("<span/>",{class:"wait "+t})).css("animation-duration",u.displayTime/1e3+"s"),l.appendTo(s)),u.compact&&(o.addClass(d.compact),s.addClass(d.compact),i&&i.addClass(d.compact)),u.newestOnTop?o.prependTo(y.get.container()):o.appendTo(y.get.container())}},bind:{events:function(){y.debug("Binding events to toast"),(u.closeOnClick||u.closeIcon)&&(u.closeIcon?r:s).on("click"+t,y.event.click),l&&l.on("animationend"+t,y.close),o.on("click"+t,e.approve,y.event.approve).on("click"+t,e.deny,y.event.deny)}},unbind:{events:function(){y.debug("Unbinding events to toast"),(u.closeOnClick||u.closeIcon)&&(u.closeIcon?r:s).off("click"+t),l&&l.off("animationend"+t),o.off("click"+t)}},animate:{show:function(e){e=M.isFunction(e)?e:function(){},u.transition&&y.can.useElement("transition")&&g.transition("is supported")&&(y.set.visible(),o.transition({animation:u.transition.showMethod+" in",queue:!1,debug:u.debug,verbose:u.verbose,duration:u.transition.showDuration,onComplete:function(){e.call(o,b),u.onVisible.call(o,b)}}))},close:function(e){e=M.isFunction(e)?e:function(){},y.debug("Closing toast"),!1!==u.onHide.call(o,b)?u.transition&&M.fn.transition!==E&&g.transition("is supported")?o.transition({animation:u.transition.hideMethod+" out",queue:!1,duration:u.transition.hideDuration,debug:u.debug,verbose:u.verbose,interval:50,onBeforeHide:function(e){e=M.isFunction(e)?e:function(){},""!==u.transition.closeEasing?o&&(o.css("opacity",0),o.wrap("<div/>").parent().slideUp(500,u.transition.closeEasing,function(){o&&(o.parent().remove(),e.call(o))})):e.call(o)},onComplete:function(){e.call(o,b),u.onHidden.call(o,b),y.destroy()}}):y.error(p.noTransition):y.debug("onHide callback returned false, cancelling toast animation")},pause:function(){l.css("animationPlayState","paused"),c&&c.css("animationPlayState","paused")},continue:function(){l.css("animationPlayState","running"),c&&c.css("animationPlayState","running")}},has:{container:function(){return y.verbose("Determining if there is already a container"),0<h.find(y.helpers.toClass(u.position)+e.container).length},toast:function(){return!!y.get.toast()},toasts:function(){return 0<y.get.toasts().length},configActions:function(){return Array.isArray(u.actions)&&0<u.actions.length}},get:{container:function(){return h.find(y.helpers.toClass(u.position)+e.container)[0]},toastBox:function(){return o||null},toast:function(){return s||null},toasts:function(){return M(y.get.container()).find(e.box)},iconClass:function(){return"string"==typeof u.showIcon?u.showIcon:u.showIcon&&u.icons[u.class]?u.icons[u.class]:""},remainingTime:function(){return l?l.css("opacity")*u.displayTime:0}},set:{visible:function(){s.addClass(d.visible)}},remove:{visible:function(){s.removeClass(d.visible)}},event:{click:function(e){0===M(e.target).closest("a").length&&(u.onClick.call(o,b),y.close())},approve:function(){!1!==u.onApprove.call(b,g)?y.close():y.verbose("Approve callback returned false cancelling close")},deny:function(){!1!==u.onDeny.call(b,g)?y.close():y.verbose("Deny callback returned false cancelling close")}},helpers:{toClass:function(e){var n=e.split(" "),t="";return n.forEach(function(e){t+="."+e}),t},deQuote:function(e){return String(e).replace(/"/g,"")},escape:function(e,n){if(n)return e;var t={"<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","`":"&#x60;"};return/[&<>"'`]/.test(e)?(e=e.replace(/&(?![a-z0-9#]{1,6};)/,"&amp;")).replace(/[<>"'`]/g,function(e){return t[e]}):e}},can:{useElement:function(e){return M.fn[e]!==E||(y.error(p.noElement.replace("{element}",e)),!1)}},setting:function(e,n){if(y.debug("Changing setting",e,n),M.isPlainObject(e))M.extend(!0,u,e);else{if(n===E)return u[e];M.isPlainObject(u[e])?M.extend(!0,u[e],n):u[e]=n}},internal:function(e,n){if(M.isPlainObject(e))M.extend(!0,y,e);else{if(n===E)return y[e];y[e]=n}},debug:function(){!u.silent&&u.debug&&(u.performance?y.performance.log(arguments):(y.debug=Function.prototype.bind.call(console.info,console,u.name+":"),y.debug.apply(console,arguments)))},verbose:function(){!u.silent&&u.verbose&&u.debug&&(u.performance?y.performance.log(arguments):(y.verbose=Function.prototype.bind.call(console.info,console,u.name+":"),y.verbose.apply(console,arguments)))},error:function(){u.silent||(y.error=Function.prototype.bind.call(console.error,console,u.name+":"),y.error.apply(console,arguments))},performance:{log:function(e){var n,t;u.performance&&(t=(n=(new Date).getTime())-(T||n),T=n,I.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:b,"Execution Time":t})),clearTimeout(y.performance.timer),y.performance.timer=setTimeout(y.performance.display,500)},display:function(){var e=u.name+":",t=0;T=!1,clearTimeout(y.performance.timer),M.each(I,function(e,n){t+=n["Execution Time"]}),e+=" "+t+"ms",x&&(e+=" '"+x+"'"),(console.group!==E||console.table!==E)&&0<I.length&&(console.groupCollapsed(e),console.table?console.table(I):M.each(I,function(e,n){console.log(n.Name+": "+n["Execution Time"]+"ms")}),console.groupEnd()),I=[]}},invoke:function(o,e,n){var s,a,t,i=C;return e=e||A,n=b||n,"string"==typeof o&&i!==E&&(o=o.split(/[\. ]/),s=o.length-1,M.each(o,function(e,n){var t=e!=s?n+o[e+1].charAt(0).toUpperCase()+o[e+1].slice(1):o;if(M.isPlainObject(i[t])&&e!=s)i=i[t];else{if(i[t]!==E)return a=i[t],!1;if(!M.isPlainObject(i[n])||e==s)return i[n]!==E?a=i[n]:y.error(p.method,o),!1;i=i[n]}})),M.isFunction(a)?t=a.apply(n,e):a!==E&&(t=a),Array.isArray(k)?k.push(t):k!==E?k=[k,t]:t!==E&&(k=t),a}};O?(C===E&&y.initialize(),y.invoke(P)):(C!==E&&C.invoke("destroy"),y.initialize(),k=g)}),k!==E?k:this},M.fn.toast.settings={name:"Toast",namespace:"toast",silent:!1,debug:!1,verbose:!1,performance:!0,context:"body",position:"top right",class:"neutral",classProgress:!1,classActions:!1,classImage:"mini",title:"",message:"",displayTime:3e3,minDisplayTime:1e3,wordsPerMinute:120,showIcon:!1,newestOnTop:!1,showProgress:!1,pauseOnHover:!0,progressUp:!1,opacity:1,compact:!0,closeIcon:!1,closeOnClick:!0,cloneModule:!0,actions:!1,preserveHTML:!0,showImage:!1,transition:{showMethod:"scale",showDuration:500,hideMethod:"scale",hideDuration:500,closeEasing:"easeOutCubic"},error:{method:"The method you called is not defined.",noElement:"This module requires ui {element}",verticalCard:"Vertical but not attached actions are not supported for card layout"},className:{container:"ui toast-container",box:"floating toast-box",progress:"ui attached active progress",toast:"ui toast",icon:"centered icon",visible:"visible",content:"content",title:"ui header",actions:"actions",extraContent:"extra content",button:"ui button",buttons:"ui buttons",close:"close icon",image:"ui image",vertical:"vertical",attached:"attached",inverted:"inverted",compact:"compact",pausable:"pausable",progressing:"progressing",top:"top",bottom:"bottom",left:"left",basic:"basic",unclickable:"unclickable"},icons:{info:"info",success:"checkmark",warning:"warning",error:"times"},selector:{container:".ui.toast-container",box:".toast-box",toast:".ui.toast",input:'input:not([type="hidden"]), textarea, select, button, .ui.button, ui.dropdown',approve:".actions .positive, .actions .approve, .actions .ok",deny:".actions .negative, .actions .deny, .actions .cancel"},fields:{class:"class",text:"text",icon:"icon",click:"click"},onShow:function(){},onVisible:function(){},onClick:function(){},onHide:function(){},onHidden:function(){},onRemove:function(){},onApprove:function(){},onDeny:function(){}},M.extend(M.easing,{easeOutBounce:function(e,n,t,o,s){return(n/=s)<1/2.75?o*(7.5625*n*n)+t:n<2/2.75?o*(7.5625*(n-=1.5/2.75)*n+.75)+t:n<2.5/2.75?o*(7.5625*(n-=2.25/2.75)*n+.9375)+t:o*(7.5625*(n-=2.625/2.75)*n+.984375)+t},easeOutCubic:function(e){return--e*e*e+1}})}(jQuery,window,void document);