-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjquery.notifications-1.1.js
120 lines (105 loc) · 3.31 KB
/
jquery.notifications-1.1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* jQuery Notifications plugin 1.1
*
* http://programmingmind.com
*
* Copyright (c) 2009 David Ang
*
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Version Changes
*
* 1.1 8/17/2008
* - Allow users to choose between slide or fade effect
* - Fixed fadeSpeed option settings
*
* 1.0 8/9/2008
* - Initial release
*/
(function($){
var template;
var counter = 0;
$.notifications = function(msg, options) {
counter++;
var settings = $.extend({}, $.notifications.defaults, options);
if (!template) {
template = $('<div id="jquery-notifications"></div>').prependTo(document.body);
}
var n = $( '<p class="' + settings.type + '" id="jquery-notifications-' + counter + '">' + msg + '</p>').hide().appendTo("#jquery-notifications");
if( settings.effect == "fade" ) {
n.fadeIn( settings.fadeSpeed );
} else {
n.slideDown( settings.fadeSpeed );
}
if (settings.stick) {
var close = $('<a href="javascript:void(0);">' + settings.close + '</a>').click(function() {
if (settings.effect == "fade") {
$(this.parentNode).fadeOut( settings.fadeSpeed, function() {
$(this).remove();
});
}
else {
$(this.parentNode).slideUp( settings.fadeSpeed, function() {
$(this).remove();
});
}
});
close.appendTo(n);
}
if (!settings.stick) {
var notificationsDelayer = delayTimer(settings.timeout);
notificationsDelayer(update, { counter: counter, effect: settings.effect, fadeSpeed : settings.fadeSpeed } );
}
if ($("#errorExplanation").length) {
// if there exists an errorExplanation div from rails 3.0,
// hide the errors and list them down as notifications
$("#errorExplanation").hide();
$("#errorExplanation li").each(function(index) {
$.n.error($(this).text());
})
}
};
$.notifications.success = function( msg, options ){
return $.notifications( msg, $.extend( {}, options, { type : "success"}) );
};
$.notifications.error = function( msg, options ){
return $.notifications( msg, $.extend( { stick: true }, options, { type : "error" }) );
};
$.notifications.warning = function( msg, options ){
return $.notifications( msg, $.extend( {}, options, { type : "warning" }) );
};
function update(params) {
if (params.effect == "fade") {
$("#jquery-notifications-" + params.counter).fadeOut( params.fadeSpeed, function(){
$(this).remove();
});
} else {
$("#jquery-notifications-" + params.counter).slideUp( params.fadeSpeed, function(){
$(this).remove();
});
}
}
function delayTimer(delay) {
var timer;
return function(fn, params) {
timer = clearTimeout(timer);
if (fn)
timer = setTimeout(function() {
fn(params);
}, delay);
return timer;
};
}
$.notifications.defaults = {
type: "notice",
timeout: 10000,
stick: false,
fadeSpeed : 800,
close : "x",
effect : "fade"
};
$.n = $.notifications;
})(jQuery);