-
Notifications
You must be signed in to change notification settings - Fork 21
/
notifications.js
306 lines (270 loc) · 11.7 KB
/
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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* global Notifications:true */
"use strict";
var constructor = (function() {
/***
* Creates an instance of Notifications
* @constructor
*/
function Notifications(settings) {
settings = settings || {};
_.defaults(settings, this.defaultSettings);
this._notificationsCollection = new Mongo.Collection(null);
this._notificationTimeout = undefined;
this.settings = settings;
}
/***
* Adds a notification
* @param {String} title of the notification
* @param {String} message of the notification
* @param {Object} [options={}] Options object to use for notification
* @param {String} [options.type=defaultOptions.type] the type of the notification
* @param {Boolean} [options.userCloseable=defaultOptions.userCloseable] Whether the notification is user closeable
* @param {Boolean} [options.clickBodyToClose=defaultOptions.clickBodyToClose] Whether the notification can be closed by clicking anywhere within its body. If turned off then the close button must clicked.
* @param {Function} [options.closed] Call this handler (passing data context) on notification close
* @param {Function} [options.onBodyClick] Call this handler (passing data context) on notification body click
* @param {Function} [options.timeout] No. of milliseconds to show this notification for.
*/
Notifications.prototype.addNotification = function (title, message, options) {
options = options || {};
_.defaults(options, this.getDefaultOptions(options.type));
var notification = {};
notification.title = title;
notification.message = message;
notification.type = options.type;
notification.userCloseable = options.userCloseable;
notification.clickBodyToClose = options.clickBodyToClose;
notification.closed = options.closed;
notification.onBodyClick = options.onBodyClick;
notification.onExpires = options.onExpires;
if (options.timeout) {
notification.expires = new Date().getTime() + options.timeout;
}
return this._add(notification);
};
/***
* Wraps addNotification, sets type to error
* @param {String} title of the notification
* @param {String} message of the notification
* @param {Object} [options={}] Options object to use for notification
* @param {Boolean} [options.userCloseable=defaultOptions.userCloseable] Whether the notification is user closeable
* @param {Function} [options.closed] Call this handler (passing data context) on notification close
* @param {Function} [options.onBodyClick] Call this handler (passing data context) on notification body click
* @returns {*}
*/
Notifications.prototype.error = function (title, message, options) {
options = options || {};
options.type = this.TYPES.ERROR;
return this.addNotification(title, message, options);
};
/***
* Wraps addNotification, sets type to warning
* @param {String} title of the notification
* @param {String} message of the notification
* @param {Object} [options={}] Options object to use for notification
* @param {Boolean} [options.userCloseable=defaultOptions.userCloseable] Whether the notification is user closeable
* @param {Function} [options.closed] Call this handler (passing data context) on notification close
* @param {Function} [options.onBodyClick] Call this handler (passing data context) on notification body click
* @returns {*}
*/
Notifications.prototype.warn = function (title, message, options) {
options = options || {};
options.type = this.TYPES.WARNING;
return this.addNotification(title, message, options);
};
/***
* Wraps addNotification, sets type to info
* @param {String} title of the notification
* @param {String} message of the notification
* @param {Object} [options={}] Options object to use for notification
* @param {Boolean} [options.userCloseable=defaultOptions.userCloseable] Whether the notification is user closeable
* @param {Function} [options.closed] Call this handler (passing data context) on notification close
* @param {Function} [options.onBodyClick] Call this handler (passing data context) on notification body click
* @returns {*}
*/
Notifications.prototype.info = function (title, message, options) {
options = options || {};
options.type = this.TYPES.INFO;
return this.addNotification(title, message, options);
};
/***
* Wraps addNotification, sets type to success
* @param {String} title of the notification
* @param {String} message of the notification
* @param {Object} [options={}] Options object to use for notification
* @param {Boolean} [options.userCloseable=defaultOptions.userCloseable] Whether the notification is user closeable
* @param {Function} [options.closed] Call this handler (passing data context) on notification close
* @param {Function} [options.onBodyClick] Call this handler (passing data context) on notification body click
* @returns {*}
*/
Notifications.prototype.success = function (title, message, options) {
options = options || {};
options.type = this.TYPES.SUCCESS;
return this.addNotification(title, message, options);
};
/***
* Returns the NotificationsCollection Meteor.Collection
* @returns {object} NotificationsCollection
* @private
*/
Notifications.prototype._getNotificationsCollection = function () {
return this._notificationsCollection;
};
/***
* Does the actual add to the collection. And creates a Timeout if necessary.
* @param {object} notification the object to be inserted into the collection
* @private
*/
Notifications.prototype._add = function (notification) {
var notificationsCollection = this._getNotificationsCollection();
var firstExpiration = this._getFirstExpiredTimestamp();
var notificationID = notificationsCollection.insert(notification);
if (notification.expires) {
if (this._notificationTimeout) {
if (firstExpiration > notification.expires) {
Meteor.clearTimeout(this._notificationTimeout);
this._notificationTimeout = undefined;
}
}
if (!this._notificationTimeout) {
this._createTimeout();
}
}
return notificationID;
};
/***
* Returns the timestamp of the notification from the notificationsCollection that is first to expire
* @returns {string} first to expire timestamp
* @private
*/
Notifications.prototype._getFirstExpiredTimestamp = function () {
var notificationsCollection = this._getNotificationsCollection();
var firstNotification = notificationsCollection.findOne({expires: {$gt: 0}}, {sort:[['expires', 'asc']]}, { reactive: false });
var firstExpiredTimestamp = firstNotification ? firstNotification.expires : 0;
return firstExpiredTimestamp;
};
/***
* creates a timeout for the first to expire notification.
* @private
*/
Notifications.prototype._createTimeout = function () {
var self = this;
var firstExpiration = this._getFirstExpiredTimestamp();
if (firstExpiration) {
this._notificationTimeout = Meteor.setTimeout(function () {
// queue up the onExpires
var needCallback = _ .filter(self._getNotificationsCollection().find({expires: {$lte: firstExpiration}}).fetch(),
function(notification) { return notification.onExpires != undefined; }
);
if(needCallback.length > 0) {
Meteor.setTimeout(function () {
_.each(needCallback, function (notification) {
if (notification.onExpires) notification.onExpires(notification); // check is redundant
});
}, 500);
}
self.remove({expires: {$lte: firstExpiration}});
self._createTimeout();
}, firstExpiration - new Date().getTime());
} else {
this._notificationTimeout = undefined;
}
};
/***
* gets the proper notification defaults based on type
* @param {String} notificationType the type of the notification for which to get the defaultOptions
*/
Notifications.prototype.getDefaultOptions = function (notificationType) {
return this.defaultOptionsByType[notificationType] || this.defaultOptions;
};
/***
* Gets the class containing the color for the notification
* @param {String} notificationType
* @returns {string} classname to use for the notification
*/
Notifications.prototype.getNotificationClass = function (notificationType) {
var notificationClass;
_.each(this.TYPES, function (value, key) {
if(value === notificationType) {
notificationClass = key.toLowerCase();
}
});
return notificationClass;
};
/***
* Removes the notifications matching the selector
* @param selector
*/
Notifications.prototype.remove = function (selector) {
this._getNotificationsCollection().remove(selector);
if (this._notificationTimeout) {
Meteor.clearTimeout(this._notificationTimeout);
this._notificationTimeout = undefined;
this._createTimeout();
}
};
/***
* Stores constants for the different notification types
* @type {{ERROR: number, WARNING: number, INFO: number, SUCCESS: number}}
*/
Notifications.prototype.TYPES = {
'ERROR': 1,
'WARNING': 2,
'INFO': 3,
'SUCCESS': 4
};
/***
* Object with the default options for the notifications
* @type {{type: number, userCloseable: boolean, timeout: number, closed: function}}
*/
Notifications.prototype.defaultOptions = {
type: Notifications.prototype.TYPES.INFO,
userCloseable: true,
clickBodyToClose: true,
timeout: 0
};
/***
* Object with the default options for the notifications for specific types
* @type {{type: number, userCloseable: boolean, timeout: number, closed: function}}
*/
Notifications.prototype.defaultOptionsByType = {};
Notifications.prototype.defaultSettings = {
hideAnimationProperties: {
height: 0,
opacity: 0,
paddingTop: 0,
paddingBottom: 0,
marginTop: 0
},
animationSpeed: 400
};
return Notifications;
})();
Notifications = new constructor();
Template.notifications.helpers({
notifications: function() {
return Notifications._getNotificationsCollection().find();
}
});
Template.notifications.rendered = function () {
this.firstNode._uihooks = {
insertElement: function (node, next) {
var settings = Notifications.settings;
$(node)
.addClass('notificationHidden')
.insertBefore(next)
.fadeIn({duration: settings.animationSpeed})
.promise()
.done(function () {
$(this).removeClass('notificationHidden');
});
},
removeElement: function (node) {
var settings = Notifications.settings;
$(node).animate(settings.hideAnimationProperties, {
duration: settings.animationSpeed,
complete: function () {
$(node).remove();
}});
}
};
};