Linux multi-monitor fullscreen support
[ryzomcore.git] / web / public_php / ams / js / jquery.noty.js
blobcd20fd16c38f403fe7f6f4986745aa872bb82c49
1 /**
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
10 **/
11 (function($) {
12         $.noty = function(options, customContainer) {
14                 var base = {};
15                 var $noty = null;
16                 var isCustom = false;
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();
28                 };
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);
35                 };
37                 // Render the noty
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');
42                 if (isGrowl) {
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);
46                 } else {
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;
52                                         } else {
53                                                 $.noty.available = true; // Queue is over
54                                                 return base.options.id;
55                                         }
56                         } else {
57                                 return base.options.id;
58                         }
59                 }
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);
64                 $noty = base.bar;
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);
91                                                 }
92                                         });
93                                 });
94                         }
96                 return base.show(isGrowl);
97                 };
99                 base.show = function(isGrowl) {
101                         // is Modal?
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);
112                         }
114                 $noty.bind('noty.setText', function(event, text) {
115                         $noty.find('.noty_text').html(text); 
116                         
117                         if (base.options.layout == 'noty_layout_topCenter' || base.options.layout == 'noty_layout_center') {
118                                 $.noty.reCenter($noty);
119                         }
120                 });
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);
130                         
131                         if (base.options.layout == 'noty_layout_topCenter' || base.options.layout == 'noty_layout_center') {
132                                 $.noty.reCenter($noty);
133                         }
134                 });
136                 $noty.bind('noty.getId', function(event) {
137                         return $noty.data('noty_options').id;
138                 });
140                 // Bind close event
141                 $noty.one('noty.close', function(event) {
142                                 var options = $noty.data('noty_options');
143         if(options.onClose){options.onClose();}
145                                 // Modal Cleaning
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();
158                                         } else {
159                                                 $noty.remove();
161                                                 // queue render
162                                                 $.noty.available = true;
163                                                 base.render(false);
164                                         }
166                                 });
167                         });
169                 // Start the show
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;
176                 };
178                 // Run initializer
179                 return base.init(options);
180         };
182         // API
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);
189                         else
190                                 i++;
191                 }
192                 //close if already visible
193                 $.noty.get(id).trigger('noty.close');
194         };
195         $.noty.setText = function(id, text) {
196                 $.noty.get(id).trigger('noty.setText', text);
197         };
198         $.noty.setType = function(id, type) {
199                 $.noty.get(id).trigger('noty.setType', type);
200         };
201         $.noty.closeAll = function() {
202                 $.noty.clearQueue();
203                 $('.noty_bar').trigger('noty.close');
204         };
205         $.noty.reCenter = function(noty) {
206                 noty.css({'left': ($(window).width() - noty.outerWidth()) / 2 + 'px'});
207         };
208         $.noty.clearQueue = function() {
209                 $.noty.queue = [];
210         };
211   
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};}
217       $.noty(options);
218     };
219   }
220   $.noty.stopConsumeAlert = function(){
221     window.alert = windowAlert;
222   }
224         $.noty.queue = [];
225         $.noty.growls = ['noty_layout_topLeft', 'noty_layout_topRight', 'noty_layout_bottomLeft', 'noty_layout_bottomRight'];
226         $.noty.available = true;
227         $.noty.defaultOptions = {
228                 layout: 'top',
229                 theme: 'noty_theme_default',
230                 animateOpen: {height: 'toggle'},
231                 animateClose: {height: 'toggle'},
232                 easing: 'swing',
233                 text: '',
234                 type: 'alert',
235                 speed: 500,
236                 timeout: 5000,
237                 closeButton: false,
238                 closeOnSelfClick: true,
239                 closeOnSelfOver: false,
240                 force: false,
241                 onShow: false,
242                 onShown: false,
243                 onClose: false,
244                 onClosed: false,
245                 buttons: false,
246                 modal: false,
247                 template: '<div class="noty_message"><span class="noty_text"></span><div class="noty_close"></div></div>',
248                 cssPrefix: 'noty_',
249                 custom: {
250                         container: null
251                 }
252         };
254         $.fn.noty = function(options) {
255                 return this.each(function() {
256                          (new $.noty(options, $(this)));
257                 });
258         };
260 })(jQuery);
262 //Helper
263 function noty(options) {
264         return jQuery.noty(options); // returns an id