2 * noty - jQuery Notification Plugin v1.2.1
3 * Contributors: https://github.com/needim/noty/graphs/contributors
5 * Examples and Documentation - http://needim.github.com/noty/
7 * Licensed under the MIT licenses:
8 * http://www.opensource.org/licenses/mit-license.php
12 $.noty = function(options, customContainer) {
18 base.init = function(options) {
19 base.options = $.extend({}, $.noty.defaultOptions, options);
20 base.options.type = base.options.cssPrefix+base.options.type;
21 base.options.id = base.options.type+'_'+new Date().getTime();
22 base.options.layout = base.options.cssPrefix+'layout_'+base.options.layout;
24 if (base.options.custom.container) customContainer = base.options.custom.container;
25 isCustom = ($.type(customContainer) === 'object') ? true : false;
27 return base.addQueue();
30 // Push notification to queue
31 base.addQueue = function() {
32 var isGrowl = ($.inArray(base.options.layout, $.noty.growls) == -1) ? false : true;
33 if (!isGrowl) (base.options.force) ? $.noty.queue.unshift({options: base.options}) : $.noty.queue.push({options: base.options});
34 return base.render(isGrowl);
38 base.render = function(isGrowl) {
40 // Layout spesific container settings
41 var container = (isCustom) ? customContainer.addClass(base.options.theme+' '+base.options.layout+' noty_custom_container') : $('body');
43 if ($('ul.noty_cont.' + base.options.layout).length == 0)
44 container.prepend($('<ul/>').addClass('noty_cont ' + base.options.layout));
45 container = $('ul.noty_cont.' + base.options.layout);
47 if ($.noty.available) {
48 var fromQueue = $.noty.queue.shift(); // Get noty from queue
49 if ($.type(fromQueue) === 'object') {
50 $.noty.available = false;
51 base.options = fromQueue.options;
53 $.noty.available = true; // Queue is over
54 return base.options.id;
57 return base.options.id;
60 base.container = container;
62 // Generating noty bar
63 base.bar = $('<div class="noty_bar"/>').attr('id', base.options.id).addClass(base.options.theme+' '+base.options.layout+' '+base.options.type);
65 $noty.append(base.options.template).find('.noty_text').html(base.options.text);
66 $noty.data('noty_options', base.options);
68 // Close button display
69 (base.options.closeButton) ? $noty.addClass('noty_closable').find('.noty_close').show() : $noty.find('.noty_close').remove();
71 // Bind close event to button
72 $noty.find('.noty_close').bind('click', function() { $noty.trigger('noty.close'); });
74 // If we have a button we must disable closeOnSelfClick and closeOnSelfOver option
75 if (base.options.buttons) base.options.closeOnSelfClick = base.options.closeOnSelfOver = false;
76 // Close on self click
77 if (base.options.closeOnSelfClick) $noty.bind('click', function() { $noty.trigger('noty.close'); }).css('cursor', 'pointer');
78 // Close on self mouseover
79 if (base.options.closeOnSelfOver) $noty.bind('mouseover', function() { $noty.trigger('noty.close'); }).css('cursor', 'pointer');
81 // Set buttons if available
82 if (base.options.buttons) {
83 $buttons = $('<div/>').addClass('noty_buttons');
84 $noty.find('.noty_message').append($buttons);
85 $.each(base.options.buttons, function(i, button) {
86 bclass = (button.type) ? button.type : 'gray';
87 $button = $('<button/>').addClass(bclass).html(button.text).appendTo($noty.find('.noty_buttons'))
88 .bind('click', function() {
89 if ($.isFunction(button.click)) {
90 button.click.call($button, $noty);
96 return base.show(isGrowl);
99 base.show = function(isGrowl) {
102 if (base.options.modal) $('<div/>').addClass('noty_modal').addClass(base.options.theme).prependTo($('body')).fadeIn('fast');
104 $noty.close = function() { return this.trigger('noty.close'); };
106 // Prepend noty to container
107 (isGrowl) ? base.container.prepend($('<li/>').append($noty)) : base.container.prepend($noty);
109 // topCenter and center specific options
110 if (base.options.layout == 'noty_layout_topCenter' || base.options.layout == 'noty_layout_center') {
111 $.noty.reCenter($noty);
114 $noty.bind('noty.setText', function(event, text) {
115 $noty.find('.noty_text').html(text);
117 if (base.options.layout == 'noty_layout_topCenter' || base.options.layout == 'noty_layout_center') {
118 $.noty.reCenter($noty);
122 $noty.bind('noty.setType', function(event, type) {
123 $noty.removeClass($noty.data('noty_options').type);
125 type = $noty.data('noty_options').cssPrefix+type;
127 $noty.data('noty_options').type = type;
129 $noty.addClass(type);
131 if (base.options.layout == 'noty_layout_topCenter' || base.options.layout == 'noty_layout_center') {
132 $.noty.reCenter($noty);
136 $noty.bind('noty.getId', function(event) {
137 return $noty.data('noty_options').id;
141 $noty.one('noty.close', function(event) {
142 var options = $noty.data('noty_options');
143 if(options.onClose){options.onClose();}
146 if (options.modal) $('.noty_modal').fadeOut('fast', function() { $(this).remove(); });
148 $noty.clearQueue().stop().animate(
149 $noty.data('noty_options').animateClose,
150 $noty.data('noty_options').speed,
151 $noty.data('noty_options').easing,
152 $noty.data('noty_options').onClosed)
153 .promise().done(function() {
155 // Layout spesific cleaning
156 if ($.inArray($noty.data('noty_options').layout, $.noty.growls) > -1) {
157 $noty.parent().remove();
162 $.noty.available = true;
170 if(base.options.onShow){base.options.onShow();}
171 $noty.animate(base.options.animateOpen, base.options.speed, base.options.easing, base.options.onShown);
173 // If noty is have a timeout option
174 if (base.options.timeout) $noty.delay(base.options.timeout).promise().done(function() { $noty.trigger('noty.close'); });
175 return base.options.id;
179 return base.init(options);
183 $.noty.get = function(id) { return $('#'+id); };
184 $.noty.close = function(id) {
185 //remove from queue if not already visible
186 for(var i=0;i<$.noty.queue.length;) {
187 if($.noty.queue[i].options.id==id)
188 $.noty.queue.splice(id,1);
192 //close if already visible
193 $.noty.get(id).trigger('noty.close');
195 $.noty.setText = function(id, text) {
196 $.noty.get(id).trigger('noty.setText', text);
198 $.noty.setType = function(id, type) {
199 $.noty.get(id).trigger('noty.setType', type);
201 $.noty.closeAll = function() {
203 $('.noty_bar').trigger('noty.close');
205 $.noty.reCenter = function(noty) {
206 noty.css({'left': ($(window).width() - noty.outerWidth()) / 2 + 'px'});
208 $.noty.clearQueue = function() {
212 var windowAlert = window.alert;
213 $.noty.consumeAlert = function(options){
214 window.alert = function(text){
215 if(options){options.text = text;}
216 else{options = {text:text};}
220 $.noty.stopConsumeAlert = function(){
221 window.alert = windowAlert;
225 $.noty.growls = ['noty_layout_topLeft', 'noty_layout_topRight', 'noty_layout_bottomLeft', 'noty_layout_bottomRight'];
226 $.noty.available = true;
227 $.noty.defaultOptions = {
229 theme: 'noty_theme_default',
230 animateOpen: {height: 'toggle'},
231 animateClose: {height: 'toggle'},
238 closeOnSelfClick: true,
239 closeOnSelfOver: false,
247 template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
254 $.fn.noty = function(options) {
255 return this.each(function() {
256 (new $.noty(options, $(this)));
263 function noty(options) {
264 return jQuery.noty(options); // returns an id