-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.json-notifications.js
52 lines (40 loc) · 1.26 KB
/
jquery.json-notifications.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
(function ($) {
$.fn.notifications = function ( options ) {
// Vars
var _this = this, // HTML object
msgs = []; // Current messages
// Set defaults
var options = $.extend({
kind : 'kind',
msg : 'msg',
delay : 1600,
speed : 200
}, options);
_this.notify = function() {
if (msgs.length > 0) {
var msg = msgs.shift();
var html = $('<p class="notification ' + msg.kind + '">' + msg.msg + '</p>');
var element = html.appendTo(_this);
element.slideToggle( options.speed ).delay( options.delay ).slideToggle( options.speed );
}
};
_this.init = function() {
$(document).ajaxComplete(function(evt, xhr) {
var res = jQuery.parseJSON(xhr.responseText);
if (res != null && res[options.kind] != null && res[options.msg] != null) {
var item = { kind : res[options.kind], msg : res[options.msg] };
msgs.push(item);
}
_this.notify();
});
$(document).ajaxError(function(evt, xhr) {
var msg = xhr.status + ' Error: ' + xhr.statusText;
var item = { kind : 'error', msg : msg };
msgs.push(item);
_this.notify();
});
};
// Build
_this.init();
};
})(jQuery);