source: pro-violet-viettel/docs/Space/assets/js/uncompressed/jquery.gritter.js @ 290

Last change on this file since 290 was 290, checked in by lamdt, 11 years ago
File size: 11.0 KB
Line 
1/*
2 * Gritter for jQuery
3 * http://www.boedesign.com/
4 *
5 * Copyright (c) 2012 Jordan Boesch
6 * Dual licensed under the MIT and GPL licenses.
7 *
8 * Date: February 24, 2012
9 * Version: 1.7.4
10 */
11
12(function($){
13       
14        /**
15        * Set it up as an object under the jQuery namespace
16        */
17        $.gritter = {};
18       
19        /**
20        * Set up global options that the user can over-ride
21        */
22        $.gritter.options = {
23                position: '',
24                class_name: '', // could be set to 'gritter-light' to use white notifications
25                fade_in_speed: 'medium', // how fast notifications fade in
26                fade_out_speed: 1000, // how fast the notices fade out
27                time: 6000 // hang on the screen for...
28        }
29       
30        /**
31        * Add a gritter notification to the screen
32        * @see Gritter#add();
33        */
34        $.gritter.add = function(params){
35
36                try {
37                        return Gritter.add(params || {});
38                } catch(e) {
39               
40                        var err = 'Gritter Error: ' + e;
41                        (typeof(console) != 'undefined' && console.error) ?
42                                console.error(err, params) :
43                                alert(err);
44                               
45                }
46               
47        }
48       
49        /**
50        * Remove a gritter notification from the screen
51        * @see Gritter#removeSpecific();
52        */
53        $.gritter.remove = function(id, params){
54                Gritter.removeSpecific(id, params || {});
55        }
56       
57        /**
58        * Remove all notifications
59        * @see Gritter#stop();
60        */
61        $.gritter.removeAll = function(params){
62                Gritter.stop(params || {});
63        }
64       
65        /**
66        * Big fat Gritter object
67        * @constructor (not really since its object literal)
68        */
69        var Gritter = {
70               
71                // Public - options to over-ride with $.gritter.options in "add"
72                position: '',
73                fade_in_speed: '',
74                fade_out_speed: '',
75                time: '',
76               
77                // Private - no touchy the private parts
78                _custom_timer: 0,
79                _item_count: 0,
80                _is_setup: 0,
81                _tpl_close: '<div class="gritter-close"></div>',
82                _tpl_title: '<span class="gritter-title">[[title]]</span>',
83                _tpl_item: '<div id="gritter-item-[[number]]" class="gritter-item-wrapper [[item_class]]" style="display:none"><div class="gritter-top"></div><div class="gritter-item">[[close]][[image]]<div class="[[class_name]]">[[title]]<p>[[text]]</p></div><div style="clear:both"></div></div><div class="gritter-bottom"></div></div>',
84                _tpl_wrap: '<div id="gritter-notice-wrapper"></div>',
85               
86                /**
87                * Add a gritter notification to the screen
88                * @param {Object} params The object that contains all the options for drawing the notification
89                * @return {Integer} The specific numeric id to that gritter notification
90                */
91                add: function(params){
92                        // Handle straight text
93                        if(typeof(params) == 'string'){
94                                params = {text:params};
95                        }
96
97                        // We might have some issues if we don't have a title or text!
98                        if(params.text === null){
99                                throw 'You must supply "text" parameter.';
100                        }
101                       
102                        // Check the options and set them once
103                        if(!this._is_setup){
104                                this._runSetup();
105                        }
106                       
107                        // Basics
108                        var title = params.title,
109                                text = params.text,
110                                image = params.image || '',
111                                sticky = params.sticky || false,
112                                item_class = params.class_name || $.gritter.options.class_name,
113                                position = $.gritter.options.position,
114                                time_alive = params.time || '';
115
116                        this._verifyWrapper();
117                       
118                        this._item_count++;
119                        var number = this._item_count,
120                                tmp = this._tpl_item;
121                       
122                        // Assign callbacks
123                        $(['before_open', 'after_open', 'before_close', 'after_close']).each(function(i, val){
124                                Gritter['_' + val + '_' + number] = ($.isFunction(params[val])) ? params[val] : function(){}
125                        });
126
127                        // Reset
128                        this._custom_timer = 0;
129                       
130                        // A custom fade time set
131                        if(time_alive){
132                                this._custom_timer = time_alive;
133                        }
134                       
135                        var image_str = (image != '') ? '<img src="' + image + '" class="gritter-image" />' : '',
136                                class_name = (image != '') ? 'gritter-with-image' : 'gritter-without-image';
137                       
138                        // String replacements on the template
139                        if(title){
140                                title = this._str_replace('[[title]]',title,this._tpl_title);
141                        }else{
142                                title = '';
143                        }
144                       
145                        tmp = this._str_replace(
146                                ['[[title]]', '[[text]]', '[[close]]', '[[image]]', '[[number]]', '[[class_name]]', '[[item_class]]'],
147                                [title, text, this._tpl_close, image_str, this._item_count, class_name, item_class], tmp
148                        );
149
150                        // If it's false, don't show another gritter message
151                        if(this['_before_open_' + number]() === false){
152                                return false;
153                        }
154
155                        $('#gritter-notice-wrapper').addClass(position).append(tmp);
156                       
157                        var item = $('#gritter-item-' + this._item_count);
158                       
159                        item.fadeIn(this.fade_in_speed, function(){
160                                Gritter['_after_open_' + number]($(this));
161                        });
162                       
163                        if(!sticky){
164                                this._setFadeTimer(item, number);
165                        }
166                       
167                        // Bind the hover/unhover states
168                        $(item).bind('mouseenter mouseleave', function(event){
169                                if(event.type == 'mouseenter'){
170                                        if(!sticky){
171                                                Gritter._restoreItemIfFading($(this), number);
172                                        }
173                                }
174                                else {
175                                        if(!sticky){
176                                                Gritter._setFadeTimer($(this), number);
177                                        }
178                                }
179                                Gritter._hoverState($(this), event.type);
180                        });
181                       
182                        // Clicking (X) makes the perdy thing close
183                        $(item).find('.gritter-close').click(function(){
184                                Gritter.removeSpecific(number, {}, null, true);
185                        });
186                       
187                        return number;
188               
189                },
190               
191                /**
192                * If we don't have any more gritter notifications, get rid of the wrapper using this check
193                * @private
194                * @param {Integer} unique_id The ID of the element that was just deleted, use it for a callback
195                * @param {Object} e The jQuery element that we're going to perform the remove() action on
196                * @param {Boolean} manual_close Did we close the gritter dialog with the (X) button
197                */
198                _countRemoveWrapper: function(unique_id, e, manual_close){
199                       
200                        // Remove it then run the callback function
201                        e.remove();
202                        this['_after_close_' + unique_id](e, manual_close);
203                       
204                        // Check if the wrapper is empty, if it is.. remove the wrapper
205                        if($('.gritter-item-wrapper').length == 0){
206                                $('#gritter-notice-wrapper').remove();
207                        }
208               
209                },
210               
211                /**
212                * Fade out an element after it's been on the screen for x amount of time
213                * @private
214                * @param {Object} e The jQuery element to get rid of
215                * @param {Integer} unique_id The id of the element to remove
216                * @param {Object} params An optional list of params to set fade speeds etc.
217                * @param {Boolean} unbind_events Unbind the mouseenter/mouseleave events if they click (X)
218                */
219                _fade: function(e, unique_id, params, unbind_events){
220
221                        var params = params || {},
222                                fade = (typeof(params.fade) != 'undefined') ? params.fade : true,
223                                fade_out_speed = params.speed || this.fade_out_speed,
224                                manual_close = unbind_events;
225
226                        this['_before_close_' + unique_id](e, manual_close);
227                       
228                        // If this is true, then we are coming from clicking the (X)
229                        if(unbind_events){
230                                e.unbind('mouseenter mouseleave');
231                        }
232                       
233                        // Fade it out or remove it
234                        if(fade){
235                       
236                                e.animate({
237                                        opacity: 0
238                                }, fade_out_speed, function(){
239                                        e.animate({ height: 0 }, 300, function(){
240                                                Gritter._countRemoveWrapper(unique_id, e, manual_close);
241                                        })
242                                })
243                               
244                        }
245                        else {
246                               
247                                this._countRemoveWrapper(unique_id, e);
248                               
249                        }
250                                               
251                },
252               
253                /**
254                * Perform actions based on the type of bind (mouseenter, mouseleave)
255                * @private
256                * @param {Object} e The jQuery element
257                * @param {String} type The type of action we're performing: mouseenter or mouseleave
258                */
259                _hoverState: function(e, type){
260                       
261                        // Change the border styles and add the (X) close button when you hover
262                        if(type == 'mouseenter'){
263                               
264                                e.addClass('hover');
265                               
266                                // Show close button
267                                e.find('.gritter-close').show();
268                                               
269                        }
270                        // Remove the border styles and hide (X) close button when you mouse out
271                        else {
272                               
273                                e.removeClass('hover');
274                               
275                                // Hide close button
276                                e.find('.gritter-close').hide();
277                               
278                        }
279                       
280                },
281               
282                /**
283                * Remove a specific notification based on an ID
284                * @param {Integer} unique_id The ID used to delete a specific notification
285                * @param {Object} params A set of options passed in to determine how to get rid of it
286                * @param {Object} e The jQuery element that we're "fading" then removing
287                * @param {Boolean} unbind_events If we clicked on the (X) we set this to true to unbind mouseenter/mouseleave
288                */
289                removeSpecific: function(unique_id, params, e, unbind_events){
290                       
291                        if(!e){
292                                var e = $('#gritter-item-' + unique_id);
293                        }
294
295                        // We set the fourth param to let the _fade function know to
296                        // unbind the "mouseleave" event.  Once you click (X) there's no going back!
297                        this._fade(e, unique_id, params || {}, unbind_events);
298                       
299                },
300               
301                /**
302                * If the item is fading out and we hover over it, restore it!
303                * @private
304                * @param {Object} e The HTML element to remove
305                * @param {Integer} unique_id The ID of the element
306                */
307                _restoreItemIfFading: function(e, unique_id){
308                       
309                        clearTimeout(this['_int_id_' + unique_id]);
310                        e.stop().css({ opacity: '', height: '' });
311                       
312                },
313               
314                /**
315                * Setup the global options - only once
316                * @private
317                */
318                _runSetup: function(){
319               
320                        for(opt in $.gritter.options){
321                                this[opt] = $.gritter.options[opt];
322                        }
323                        this._is_setup = 1;
324                       
325                },
326               
327                /**
328                * Set the notification to fade out after a certain amount of time
329                * @private
330                * @param {Object} item The HTML element we're dealing with
331                * @param {Integer} unique_id The ID of the element
332                */
333                _setFadeTimer: function(e, unique_id){
334                       
335                        var timer_str = (this._custom_timer) ? this._custom_timer : this.time;
336                        this['_int_id_' + unique_id] = setTimeout(function(){
337                                Gritter._fade(e, unique_id);
338                        }, timer_str);
339               
340                },
341               
342                /**
343                * Bring everything to a halt
344                * @param {Object} params A list of callback functions to pass when all notifications are removed
345                */ 
346                stop: function(params){
347                       
348                        // callbacks (if passed)
349                        var before_close = ($.isFunction(params.before_close)) ? params.before_close : function(){};
350                        var after_close = ($.isFunction(params.after_close)) ? params.after_close : function(){};
351                       
352                        var wrap = $('#gritter-notice-wrapper');
353                        before_close(wrap);
354                        wrap.fadeOut(function(){
355                                $(this).remove();
356                                after_close();
357                        });
358               
359                },
360               
361                /**
362                * An extremely handy PHP function ported to JS, works well for templating
363                * @private
364                * @param {String/Array} search A list of things to search for
365                * @param {String/Array} replace A list of things to replace the searches with
366                * @return {String} sa The output
367                */ 
368                _str_replace: function(search, replace, subject, count){
369               
370                        var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
371                                f = [].concat(search),
372                                r = [].concat(replace),
373                                s = subject,
374                                ra = r instanceof Array, sa = s instanceof Array;
375                        s = [].concat(s);
376                       
377                        if(count){
378                                this.window[count] = 0;
379                        }
380               
381                        for(i = 0, sl = s.length; i < sl; i++){
382                               
383                                if(s[i] === ''){
384                                        continue;
385                                }
386                               
387                                for (j = 0, fl = f.length; j < fl; j++){
388                                       
389                                        temp = s[i] + '';
390                                        repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
391                                        s[i] = (temp).split(f[j]).join(repl);
392                                       
393                                        if(count && s[i] !== temp){
394                                                this.window[count] += (temp.length-s[i].length) / f[j].length;
395                                        }
396                                       
397                                }
398                        }
399                       
400                        return sa ? s : s[0];
401                       
402                },
403               
404                /**
405                * A check to make sure we have something to wrap our notices with
406                * @private
407                */ 
408                _verifyWrapper: function(){
409                 
410                        if($('#gritter-notice-wrapper').length == 0){
411                                $('body').append(this._tpl_wrap);
412                        }
413               
414                }
415               
416        }
417       
418})(jQuery);
Note: See TracBrowser for help on using the repository browser.