forked from mouse0270/bootstrap-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-notify.min.js
1 lines (1 loc) · 8.76 KB
/
bootstrap-notify.min.js
1
!function(t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof exports?t(require("jquery")):t(jQuery)}(function(d){var n={element:"body",position:null,type:"info",allow_dismiss:!0,allow_duplicates:!0,newest_on_top:!1,showProgressbar:!1,placement:{from:"top",align:"right"},offset:20,spacing:10,z_index:1031,delay:5e3,timer:1e3,url_target:"_blank",mouse_over:null,animate:{enter:"animated fadeInDown",exit:"animated fadeOutUp"},onShow:null,onShown:null,onClose:null,onClosed:null,onClick:null,icon_type:"class",template:'<div data-notify="container" class="col-xs-11 col-sm-4 alert alert-{0}" role="alert"><button type="button" aria-hidden="true" class="close" data-notify="dismiss">×</button><span data-notify="icon"></span> <span data-notify="title">{1}</span> <span data-notify="message">{2}</span><div class="progress" data-notify="progressbar"><div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div></div><a href="{3}" target="{4}" data-notify="url"></a></div>'};function s(t,i,s){var l,c,e={content:{message:"object"==typeof i?i.message:i,title:i.title?i.title:"",icon:i.icon?i.icon:"",url:i.url?i.url:"#",target:i.target?i.target:"-"}};s=d.extend(!0,{},e,s),this.settings=d.extend(!0,{},n,s),this._defaults=n,"-"===this.settings.content.target&&(this.settings.content.target=this.settings.url_target),this.animations={start:"webkitAnimationStart oanimationstart MSAnimationStart animationstart",end:"webkitAnimationEnd oanimationend MSAnimationEnd animationend"},"number"==typeof this.settings.offset&&(this.settings.offset={x:this.settings.offset,y:this.settings.offset}),(this.settings.allow_duplicates||!this.settings.allow_duplicates&&(l=this,c=!1,d('[data-notify="container"]').each(function(t,i){var s=d(i),e=s.find('[data-notify="title"]').html().trim(),n=s.find('[data-notify="message"]').html().trim(),a=e===d("<div>"+l.settings.content.title+"</div>").html().trim(),o=n===d("<div>"+l.settings.content.message+"</div>").html().trim(),r=s.hasClass("alert-"+l.settings.type);return a&&o&&r&&(c=!0),!c}),!c))&&this.init()}String.format=function(){var s=arguments;return arguments[0].replace(/(\{\{\d\}\}|\{\d\})/g,function(t){if("{{"===t.substring(0,2))return t;var i=parseInt(t.match(/\d/)[0]);return s[i+1]})},d.extend(s.prototype,{init:function(){var r=this;this.buildNotify(),this.settings.content.icon&&this.setIcon(),"#"!=this.settings.content.url&&this.styleURL(),this.placement(),this.bind(),this.notify={$ele:this.$ele,update:function(t,i){var s={};for(var e in"string"==typeof t?s[t]=i:s=t,s)switch(e){case"type":this.$ele.removeClass("alert-"+r.settings.type),this.$ele.find('[data-notify="progressbar"] > .progress-bar').removeClass("progress-bar-"+r.settings.type),r.settings.type=s[e],this.$ele.addClass("alert-"+s[e]).find('[data-notify="progressbar"] > .progress-bar').addClass("progress-bar-"+s[e]);break;case"icon":var n=this.$ele.find('[data-notify="icon"]');"class"===r.settings.icon_type.toLowerCase()?n.removeClass(r.settings.content.icon).addClass(s[e]):(n.is("img")||n.find("img"),n.attr("src",s[e])),r.settings.content.icon=s[t];break;case"progress":var a=r.settings.delay-r.settings.delay*(s[e]/100);this.$ele.data("notify-delay",a),this.$ele.find('[data-notify="progressbar"] > div').attr("aria-valuenow",s[e]).css("width",s[e]+"%");break;case"url":this.$ele.find('[data-notify="url"]').attr("href",s[e]);break;case"target":this.$ele.find('[data-notify="url"]').attr("target",s[e]);break;default:this.$ele.find('[data-notify="'+e+'"]').html(s[e])}var o=this.$ele.outerHeight()+parseInt(r.settings.spacing)+parseInt(r.settings.offset.y);r.reposition(o)},close:function(){r.close()}}},buildNotify:function(){var t=this.settings.content;this.$ele=d(String.format(this.settings.template,this.settings.type,t.title,t.message,t.url,t.target)),this.$ele.attr("data-notify-position",this.settings.placement.from+"-"+this.settings.placement.align),this.settings.allow_dismiss||this.$ele.find('[data-notify="dismiss"]').css("display","none"),(this.settings.delay<=0&&!this.settings.showProgressbar||!this.settings.showProgressbar)&&this.$ele.find('[data-notify="progressbar"]').remove()},setIcon:function(){"class"===this.settings.icon_type.toLowerCase()?this.$ele.find('[data-notify="icon"]').addClass(this.settings.content.icon):this.$ele.find('[data-notify="icon"]').is("img")?this.$ele.find('[data-notify="icon"]').attr("src",this.settings.content.icon):this.$ele.find('[data-notify="icon"]').append('<img src="'+this.settings.content.icon+'" alt="Notify Icon" />')},styleURL:function(){this.$ele.find('[data-notify="url"]').css({backgroundImage:"url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)",height:"100%",left:0,position:"absolute",top:0,width:"100%",zIndex:this.settings.z_index+1})},placement:function(){var s=this,t=this.settings.offset.y,i={display:"inline-block",margin:"0px auto",position:this.settings.position?this.settings.position:"body"===this.settings.element?"fixed":"absolute",transition:"all .5s ease-in-out",zIndex:this.settings.z_index},e=!1,n=this.settings;switch(d('[data-notify-position="'+this.settings.placement.from+"-"+this.settings.placement.align+'"]:not([data-closing="true"])').each(function(){t=Math.max(t,parseInt(d(this).css(n.placement.from))+parseInt(d(this).outerHeight())+parseInt(n.spacing))}),!0===this.settings.newest_on_top&&(t=this.settings.offset.y),i[this.settings.placement.from]=t+"px",this.settings.placement.align){case"left":case"right":i[this.settings.placement.align]=this.settings.offset.x+"px";break;case"center":i.left=0,i.right=0}this.$ele.css(i).addClass(this.settings.animate.enter),d.each(Array("webkit-","moz-","o-","ms-",""),function(t,i){s.$ele[0].style[i+"AnimationIterationCount"]=1}),d(this.settings.element).append(this.$ele),!0===this.settings.newest_on_top&&(t=parseInt(t)+parseInt(this.settings.spacing)+this.$ele.outerHeight(),this.reposition(t)),d.isFunction(s.settings.onShow)&&s.settings.onShow.call(this.$ele),this.$ele.one(this.animations.start,function(){e=!0}).one(this.animations.end,function(){s.$ele.removeClass(s.settings.animate.enter),d.isFunction(s.settings.onShown)&&s.settings.onShown.call(this)}),setTimeout(function(){e||d.isFunction(s.settings.onShown)&&s.settings.onShown.call(this)},600)},bind:function(){var s=this;this.$ele.find('[data-notify="dismiss"]').on("click",function(){s.close()}),d.isFunction(s.settings.onClick)&&this.$ele.on("click",function(t){t.target!=s.$ele.find('[data-notify="dismiss"]')[0]&&s.settings.onClick.call(this,t)});var e=!1;if(this.$ele.on("mouseenter.notify",function(){e=!0,s.$ele.one("mouseleave.notify",function(){e=!1})}),0<this.settings.delay){s.$ele.data("notify-delay",s.settings.delay);var n=setInterval(function(){var t=parseInt(s.$ele.data("notify-delay"))-s.settings.timer;if(e&&"pause"===s.settings.mouse_over||"pause"!=s.settings.mouse_over){var i=(s.settings.delay-t)/s.settings.delay*100;s.$ele.data("notify-delay",t),s.$ele.find('[data-notify="progressbar"] > div').attr("aria-valuenow",i).css("width",i+"%")}t<=-s.settings.timer&&(clearInterval(n),s.close())},s.settings.timer)}},close:function(){var t=this,i=parseInt(this.$ele.css(this.settings.placement.from)),s=!1;this.settings.animate.enter&&this.$ele.removeClass(this.settings.animate.enter),this.$ele.attr("data-closing","true").addClass(this.settings.animate.exit),t.reposition(i),d.isFunction(t.settings.onClose)&&t.settings.onClose.call(this.$ele),this.$ele.one(this.animations.start,function(){s=!0}).one(this.animations.end,function(){d(this).remove(),d.isFunction(t.settings.onClosed)&&t.settings.onClosed.call(this)}),setTimeout(function(){s||(t.$ele.remove(),d.isFunction(t.settings.onClosed)&&t.settings.onClosed.call(this))},600)},reposition:function(t){var i=this,s='[data-notify-position="'+this.settings.placement.from+"-"+this.settings.placement.align+'"]:not([data-closing="true"])',e=this.$ele.nextAll(s);!0===this.settings.newest_on_top&&(e=this.$ele.prevAll(s)),e.each(function(){d(this).css(i.settings.placement.from,t),t=parseInt(t)+parseInt(i.settings.spacing)+d(this).outerHeight()})}}),d.notify=function(t,i){return new s(this,t,i).notify},d.notifyDefaults=function(t){return n=d.extend(!0,{},n,t)},d.notifyClose=function(t){void 0===t||"all"===t?d("[data-notify]").find('[data-notify="dismiss"]').trigger("click"):"success"===t||"info"===t||"warning"===t||"danger"===t?d(".alert-"+t+"[data-notify]").find('[data-notify="dismiss"]').trigger("click"):t?d(t+"[data-notify]").find('[data-notify="dismiss"]').trigger("click"):d('[data-notify-position="'+t+'"]').find('[data-notify="dismiss"]').trigger("click")},d.notifyCloseExcept=function(t){"success"===t||"info"===t||"warning"===t||"danger"===t?d("[data-notify]").not(".alert-"+t).find('[data-notify="dismiss"]').trigger("click"):d("[data-notify]").not(t).find('[data-notify="dismiss"]').trigger("click")}});