UNPKG

jnoty

Version:

jnoty is a jQuery plugin that show OS X's Growl like notification in web app.

2 lines (1 loc) 5.42 kB
!function(t){t.jnoty=function(e,n){0===t("#jnoty").length&&t('<div id="jnoty"></div>').addClass(n&&n.position?n.position:t.jnoty.defaults.position).appendTo(n&&n.appendTo?n.appendTo:t.jnoty.defaults.appendTo),t("#jnoty").jnoty(e,n)},t.fn.jnoty=function(e,n){if(void 0===n&&t.isPlainObject(e)&&(e=(n=e).mesage),t.isFunction(this.each)){var o=arguments;return this.each(function(){void 0===t(this).data("jnoty.instance")&&(t(this).data("jnoty.instance",t.extend(new t.fn.jnoty,{notifications:[],element:null,interval:null})),t(this).data("jnoty.instance").startup(this)),t.isFunction(t(this).data("jnoty.instance")[e])?t(this).data("jnoty.instance")[e].apply(t(this).data("jnoty.instance"),t.makeArray(o).slice(1)):t(this).data("jnoty.instance").create(e,n)})}},t.extend(t.fn.jnoty.prototype,{defaults:{pool:0,header:"",group:"",sticky:!1,position:"top-right",appendTo:"body",glue:"after",theme:"default",themeState:"highlight",corners:"10px",check:250,life:3e3,closeDuration:"normal",openDuration:"normal",easing:"swing",closer:!1,closeTemplate:"",closerTemplate:"<div>[ close all ]</div>",log:function(){},beforeOpen:function(){},afterOpen:function(){},open:function(){},beforeClose:function(){},close:function(){},click:function(){},animateOpen:{opacity:"show"},animateClose:{opacity:"hide"}},notifications:[],element:null,interval:null,create:function(e,n){var o=t.extend({},this.defaults,n);void 0!==o.speed&&(o.openDuration=o.speed,o.closeDuration=o.speed),this.notifications.push({message:e,options:o}),o.log.apply(this.element,[this.element,e,o])},render:function(e){var n=this,o=e.message,i=e.options;i.themeState=""===i.themeState?"":"ui-state-"+i.themeState;var a=t("<div/>").addClass("jnoty-container "+i.themeState).append(t("<button/>").addClass("jnoty-close").html(i.closeTemplate)).append(t("<div/>").addClass("jnoty-icon").append(t("<div/>").addClass("icon"))).append(t("<div/>").addClass("jnoty-content").append(t("<div/>").addClass("jnoty-header").append(t("<span/>").addClass("jnoty-title").html(i.header))).append(t("<div/>").addClass("jnoty-message").html(o))).data("jnoty",i).addClass(i.theme).children(".jnoty-close").bind("click.jnoty",function(){return t(this).parent().trigger("jnoty.beforeClose"),!1}).parent();t(a).bind("mouseover.jnoty",function(){t(".jnoty-notification",n.element).data("jnoty.pause",!0)}).bind("mouseout.jnoty",function(){t(".jnoty-notification",n.element).data("jnoty.pause",!1)}).bind("jnoty.beforeOpen",function(){!1!==i.beforeOpen.apply(a,[a,o,i,n.element])&&t(this).trigger("jnoty.open")}).bind("jnoty.open",function(){!1!==i.open.apply(a,[a,o,i,n.element])&&("after"==i.glue?t(".jnoty-container:last",n.element).after(a):t(".jnoty-container:first",n.element).before(a),t(this).animate(i.animateOpen,i.openDuration,i.easing,function(){!1===t.support.opacity&&this.style.removeAttribute("filter"),null!==t(this).data("jnoty")&&void 0!==t(this).data("jnoty")&&(t(this).data("jnoty").created=new Date),t(this).trigger("jnoty.afterOpen")}))}).bind("jnoty.afterOpen",function(){i.afterOpen.apply(a,[a,o,i,n.element])}).bind("click",function(){i.click.apply(a,[a,o,i,n.element])}).bind("jnoty.beforeClose",function(){!1!==i.beforeClose.apply(a,[a,o,i,n.element])&&t(this).trigger("jnoty.close")}).bind("jnoty.close",function(){t(this).data("jnoty.pause",!0),t(this).animate(i.animateClose,i.closeDuration,i.easing,function(){t.isFunction(i.close)?!1!==i.close.apply(a,[a,o,i,n.element])&&t(this).remove():t(this).remove()})}).trigger("jnoty.beforeOpen"),""!==i.corners&&void 0!==t.fn.corner&&t(a).corner(i.corners),t(".jnoty-container:parent",n.element).length>1&&0===t(".jnoty-closer",n.element).length&&!1!==this.defaults.closer&&t(this.defaults.closerTemplate).addClass("jnoty-closer "+this.defaults.themeState+" ui-corner-all").addClass(this.defaults.theme).appendTo(n.element).animate(this.defaults.animateOpen,this.defaults.speed,this.defaults.easing).bind("click.jnoty",function(){t(this).siblings().trigger("jnoty.beforeClose"),t.isFunction(n.defaults.closer)&&n.defaults.closer.apply(t(this).parent()[0],[t(this).parent()[0]])})},update:function(){t(this.element).find(".jnoty-container:parent").each(function(){void 0!==t(this).data("jnoty")&&void 0!==t(this).data("jnoty").created&&t(this).data("jnoty").created.getTime()+parseInt(t(this).data("jnoty").life,10)<(new Date).getTime()&&!0!==t(this).data("jnoty").sticky&&(void 0===t(this).data("jnoty.pause")||!0!==t(this).data("jnoty.pause"))&&t(this).trigger("jnoty.beforeClose")}),this.notifications.length>0&&(0===this.defaults.pool||t(this.element).find(".jnoty-container:parent").length<this.defaults.pool)&&this.render(this.notifications.shift()),t(this.element).find(".jnoty-container:parent").length<2&&t(this.element).find(".jnoty-closer").animate(this.defaults.animateClose,this.defaults.speed,this.defaults.easing,function(){t(this).remove()})},startup:function(e){this.element=t(e).addClass("jnoty").append('<div class="jnoty-container"></div>'),this.interval=setInterval(function(){var n=t(e).data("jnoty.instance");void 0!==n&&n.update()},parseInt(this.defaults.check,10))},shutdown:function(){t(this.element).removeClass("jnoty").find(".jnoty-container").trigger("jnoty.close").parent().empty(),clearInterval(this.interval)},close:function(){t(this.element).find(".jnoty-container").each(function(){t(this).trigger("jnoty.beforeClose")})}}),t.jnoty.defaults=t.fn.jnoty.prototype.defaults}(jQuery);