-
Notifications
You must be signed in to change notification settings - Fork 6
/
jquery.jnotify.js
208 lines (185 loc) · 7.51 KB
/
jquery.jnotify.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Simply creates stacking up notifications after calling
* $.jnotify('zOMG jnotify is nice!');
*
* You may remove a single notification by clicking anywhere on it or make
* them all disapear by double clicking on any of them.
*
* It's possible to create presets of notifications so that when you specify a
* special type of message ($.jnotify('message', 'error'), it will automatically
* use the set parameters for that preset type (for ex. timeout of 5 secs,
* 'mega-error' css class etc.). You may add or remove presets with :
* $.jnotify('addPreset', 'error', {timeout: 5, type: 'error', css: 'mega-error');
* $.jnotify('removePreset', 'error');
*
* options may be : {
* 'timeout' : 2, // time in seconds after wich the jnotify will disapear ; 0 to make it stay
* 'type' : 'success', // may be anything having a meaning for you ; if null will use the name of the preset
* 'css' : null // the css class used ; be sure to have a '.jnotify-'+css class set ; if null, uses type value
* }
*
* You may also change the default preset used if no preset is specified with :
* $.jnotify('setDefaultPreset', 'success');
* Just make sure to have that preset 1st before setting it as default or it'll go back to 'notice'.
*
* Different ways to use $.jnotify :
* $.jnotify('This is a sweet notice message!'); > creates a notification with default preset
* $.jnotify('This is a sweet success message!', 'success'); > creates a notification with 'success' preset ;
* if that preset does not exit, take default preset but still use the 'success' css class ;
* so you don't have to always create a preset if default behavior works for you
* $.jnotify('This is a sweet long notice message!', {timeout: 50}); > creates a notification with default preset but a timeout of 50 seconds
* $.jnotify('This is a sweet quick success message!', 'success', {timeout: 0.5}); > creates a notification with 'success' preset but a timeout of 0.5 seconds
*
* Notifications themselves may totally be modified to suit your needs by
* changing css classes accordingly. The plugin creates a div#jnotify-stack that
* contains the div.jnotify that also have a '.jnotify-'+css class depending on
* the css or type given in options (ie: div.jnotify-success).
*
* @author Jérémy Huet
*/
(function($) {
var statics = {
version: '1.2.0',
defaultPreset : 'notice',
presets : {
notice : { // Do not remove this preset as it's used for the <i>backup</i> default behavior
timeout : 2,
type : 'notice',
css : null
},
success : {
timeout : 2,
type : 'success',
css : null
},
error : {
timeout : 5,
type : 'error',
css : null
},
warning : {
timeout : 5,
type : 'warning',
css : null
}
}
};
var methods = {
init : function(options) {
},
/**
* @param string text : Text for the message
* @param mixed opts : Either a full set of options such as statics.presets.notice ; or the name of a preset or just the name of a css class
*/
jnotify: function(text, opts, optss) {
var options = $.extend({}, statics.presets[statics.defaultPreset]);
if (typeof opts == 'string') {
if (typeof statics.presets[opts] == 'object') {
opts = $.extend({}, statics.presets[opts]);
if (typeof optss == 'object') {
opts = $.extend(opts, optss);
}
}
else {
opts = {type: opts, css: opts};
}
}
if (typeof opts == 'object') {
$.extend(options, opts);
}
// Retreive the stack, create it if it isn't yet
var $stack = $('body').find('#jnotify-stack');
if ($stack.length == 0) {
$stack = $('<div />');
$stack.dblclick(function() {
$(this).empty();
} );
$stack.attr('id', 'jnotify-stack');
$('body').prepend($stack);
}
// Create the notice
var $notice = $('<div />');
$notice.addClass('jnotify jnotify-' + (options.css ? options.css : options.type));
$notice.html(text);
$notice.hide();
var $close = $('<div />');
$close.addClass('close');
$close.hide();
$notice.mouseenter(function() {
$(this).find('.close').show();
} ).mouseleave(function() {
$(this).find('.close').hide();
} );
$notice.prepend($close);
$stack.append($notice);
$notice.slideDown('fast');
if (options.timeout > 0) {
$notice.delay(options.timeout * 1000).fadeOut('slow', function() {
$(this).detach().remove();
} );
}
$notice.click(function() {
$(this).fadeOut('fast', function() {
$(this).detach().remove();
} );
} );
},
addPreset: function(preset, options) {
statics.presets[preset] = options;
if (! statics.presets[preset].type) {
statics.presets[preset].type = preset;
}
if (! statics.presets[preset].css) {
statics.presets[preset].css = statics.presets[preset].type;
}
},
updatePreset: function(preset, options) {
if (statics.presets[preset]) {
methods.addPreset(preset, $.extend(statics.presets[preset], options));
}
},
removePreset: function(preset) {
if (statics.presets[preset] && preset != 'notice') {
delete statics.presets[preset];
if (statics.defaultPreset == preset) {
methods.setDefaultPreset('notice');
}
} else {
$.error('"notice" preset is not allowed to be removed.');
}
},
getPreset: function(preset) {
if (statics.presets[preset]) {
return statics.presets[preset];
}
},
getPresets: function() {
return statics.presets;
},
setDefaultPreset: function(preset) {
if (statics.presets[preset]) {
statics.defaultPreset = preset;
}
},
getDefaultPreset: function() {
return methods.getPreset(statics.defaultPreset);
},
getDefaultPresetName: function() {
return statics.defaultPreset;
}
};
$.jnotify = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'string') {
return methods.jnotify.apply(this, arguments);
}
else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments );
}
else {
$.error('Method "' + method + '" does not exist on jQuery.jnotify');
}
}
})(jQuery);