-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjnoty.js
252 lines (216 loc) · 9.35 KB
/
jnoty.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
(function($) {
/** jnoty Wrapper - Establish a base jnoty Container for compatibility with older releases. **/
$.jnoty = function(m, o) {
// To maintain compatibility with older version that only supported one instance we'll create the base container.
if ($('#jnoty').length === 0)
$('<div id="jnoty"></div>').addClass((o && o.position) ? o.position : $.jnoty.defaults.position).appendTo((o && o.appendTo) ? o.appendTo : $.jnoty.defaults.appendTo);
// Create a notification on the container.
$('#jnoty').jnoty(m, o);
};
/** Raise jnoty Notification on a jnoty Container **/
$.fn.jnoty = function(m, o) {
// Short hand for passing in just an object to this method
if (o === undefined && $.isPlainObject(m)) {
o = m;
m = o.mesage;
}
if ($.isFunction(this.each)) {
var args = arguments;
return this.each(function() {
/** Create a jnoty Instance on the Container if it does not exist **/
if ($(this).data('jnoty.instance') === undefined) {
$(this).data('jnoty.instance', $.extend(new $.fn.jnoty(), {
notifications: [],
element: null,
interval: null
}));
$(this).data('jnoty.instance').startup(this);
}
/** Optionally call jnoty instance methods, or just raise a normal notification **/
if ($.isFunction($(this).data('jnoty.instance')[m])) {
$(this).data('jnoty.instance')[m].apply($(this).data('jnoty.instance'), $.makeArray(args).slice(1));
} else {
$(this).data('jnoty.instance').create(m, o);
}
});
}
};
$.extend($.fn.jnoty.prototype, {
/** Default jnoty Settings **/
defaults: {
pool: 0,
header: '',
group: '',
sticky: false,
position: 'top-right',
appendTo: 'body',
glue: 'after',
theme: 'default',
themeState: 'highlight',
corners: '10px',
check: 250,
life: 3000,
closeDuration: 'normal',
openDuration: 'normal',
easing: 'swing',
closer: false,
closeTemplate: '',
closerTemplate: '<div>[ close all ]</div>',
log: function() {},
beforeOpen: function() {},
afterOpen: function() {},
open: function() {},
beforeClose: function() {},
close: function() {},
click: function() {},
animateOpen: {
opacity: 'show'
},
animateClose: {
opacity: 'hide'
}
},
notifications: [],
/** jnoty Container Node **/
element: null,
/** Interval Function **/
interval: null,
/** Create a Notification **/
create: function(message, options) {
var o = $.extend({}, this.defaults, options);
/* To keep backward compatibility with 1.24 and earlier, honor 'speed' if the user has set it */
if (typeof o.speed !== 'undefined') {
o.openDuration = o.speed;
o.closeDuration = o.speed;
}
this.notifications.push({
message: message,
options: o
});
o.log.apply(this.element, [this.element, message, o]);
},
render: function(n) {
var self = this;
var message = n.message;
var o = n.options;
// Support for jQuery theme-states, if this is not used it displays a widget header
o.themeState = (o.themeState === '') ? '' : 'ui-state-' + o.themeState;
var notification = $('<div/>')
.addClass('jnoty-container ' + o.themeState + '')
.append($('<button/>').addClass('jnoty-close').html(o.closeTemplate))
.append($('<div/>').addClass('jnoty-icon')
.append($('<div/>').addClass('icon')))
.append($('<div/>').addClass('jnoty-content')
.append($('<div/>').addClass('jnoty-header')
.append($('<span/>').addClass('jnoty-title').html(o.header)))
.append($('<div/>').addClass('jnoty-message').html(message)))
.data("jnoty", o).addClass(o.theme).children('.jnoty-close').bind("click.jnoty", function() {
$(this).parent().trigger('jnoty.beforeClose');
return !1
}).parent();
/** Notification Actions **/
$(notification).bind("mouseover.jnoty", function() {
$('.jnoty-notification', self.element).data("jnoty.pause", true);
}).bind("mouseout.jnoty", function() {
$('.jnoty-notification', self.element).data("jnoty.pause", false);
}).bind('jnoty.beforeOpen', function() {
if (o.beforeOpen.apply(notification, [notification, message, o, self.element]) !== false) {
$(this).trigger('jnoty.open');
}
}).bind('jnoty.open', function() {
if (o.open.apply(notification, [notification, message, o, self.element]) !== false) {
if (o.glue == 'after') {
$('.jnoty-container:last', self.element).after(notification);
} else {
$('.jnoty-container:first', self.element).before(notification);
}
$(this).animate(o.animateOpen, o.openDuration, o.easing, function() {
// Fixes some anti-aliasing issues with IE filters.
if ($.support.opacity === false)
this.style.removeAttribute('filter');
if ($(this).data("jnoty") !== null && typeof $(this).data("jnoty") !== 'undefined') // Happens when a notification is closing before it's open.
$(this).data("jnoty").created = new Date();
$(this).trigger('jnoty.afterOpen');
});
}
}).bind('jnoty.afterOpen', function() {
o.afterOpen.apply(notification, [notification, message, o, self.element]);
}).bind('click', function() {
o.click.apply(notification, [notification, message, o, self.element]);
}).bind('jnoty.beforeClose', function() {
if (o.beforeClose.apply(notification, [notification, message, o, self.element]) !== false)
$(this).trigger('jnoty.close');
}).bind('jnoty.close', function() {
// Pause the notification, lest during the course of animation another close event gets called.
$(this).data('jnoty.pause', true);
$(this).animate(o.animateClose, o.closeDuration, o.easing, function() {
if ($.isFunction(o.close)) {
if (o.close.apply(notification, [notification, message, o, self.element]) !== false)
$(this).remove();
} else {
$(this).remove();
}
});
}).trigger('jnoty.beforeOpen');
/** Optional Corners Plugin **/
if (o.corners !== '' && $.fn.corner !== undefined) $(notification).corner(o.corners);
/** Add a Global Closer if more than one notification exists **/
if ($('.jnoty-container:parent', self.element).length > 1 &&
$('.jnoty-closer', self.element).length === 0 && this.defaults.closer !== false) {
$(this.defaults.closerTemplate).addClass('jnoty-closer ' + this.defaults.themeState + ' ui-corner-all').addClass(this.defaults.theme)
.appendTo(self.element).animate(this.defaults.animateOpen, this.defaults.speed, this.defaults.easing)
.bind("click.jnoty", function() {
$(this).siblings().trigger("jnoty.beforeClose");
if ($.isFunction(self.defaults.closer)) {
self.defaults.closer.apply($(this).parent()[0], [$(this).parent()[0]]);
}
});
}
},
/** Update the jnoty Container, removing old jnoty notifications **/
update: function() {
$(this.element).find('.jnoty-container:parent').each(function() {
if ($(this).data("jnoty") !== undefined && $(this).data("jnoty").created !== undefined &&
($(this).data("jnoty").created.getTime() + parseInt($(this).data("jnoty").life, 10)) < (new Date()).getTime() &&
$(this).data("jnoty").sticky !== true &&
($(this).data("jnoty.pause") === undefined || $(this).data("jnoty.pause") !== true)) {
// Pause the notification, lest during the course of animation another close event gets called.
$(this).trigger('jnoty.beforeClose');
}
});
if (this.notifications.length > 0 &&
(this.defaults.pool === 0 || $(this.element).find('.jnoty-container:parent').length < this.defaults.pool))
this.render(this.notifications.shift());
if ($(this.element).find('.jnoty-container:parent').length < 2) {
$(this.element).find('.jnoty-closer').animate(this.defaults.animateClose, this.defaults.speed, this.defaults.easing, function() {
$(this).remove();
});
}
},
/** Setup the jnoty Notification Container **/
startup: function(e) {
this.element = $(e).addClass('jnoty').append('<div class="jnoty-container"></div>');
this.interval = setInterval(function() {
// some error in chage ^^
var instance = $(e).data('jnoty.instance');
if (undefined !== instance) {
instance.update();
}
}, parseInt(this.defaults.check, 10));
},
/** Shutdown jnoty, removing it and clearing the interval **/
shutdown: function() {
$(this.element).removeClass('jnoty')
.find('.jnoty-container').trigger('jnoty.close')
.parent().empty();
clearInterval(this.interval);
},
close: function() {
$(this.element).find('.jnoty-container').each(function() {
$(this).trigger('jnoty.beforeClose');
});
}
});
/** Reference the Defaults Object for compatibility with older versions of jnoty **/
$.jnoty.defaults = $.fn.jnoty.prototype.defaults;
})(jQuery);