notifications.js
17.8 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//Author : @arboshiki
/**
* Generates random string of n length.
* String contains only letters and numbers
*
* @param {int} n
* @returns {String}
*/
Math.randomString = function (n) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < n; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
};
var Lobibox = Lobibox || {};
(function () {
var LobiboxNotify = function (type, options) {
//------------------------------------------------------------------------------
//----------------PROTOTYPE VARIABLES-------------------------------------------
//------------------------------------------------------------------------------
this.$type = null;
this.$options = null;
this.$el = null;
//------------------------------------------------------------------------------
//-----------------PRIVATE VARIABLES--------------------------------------------
//------------------------------------------------------------------------------
var me = this;
//------------------------------------------------------------------------------
//-----------------PRIVATE FUNCTIONS--------------------------------------------
//------------------------------------------------------------------------------
var _processInput = function (options) {
if (options.size === 'mini' || options.size === 'large') {
options = $.extend({}, Lobibox.notify.OPTIONS[options.size], options);
}
options = $.extend({}, Lobibox.notify.OPTIONS[me.$type], Lobibox.notify.DEFAULTS, options);
if (options.size !== 'mini' && options.title === true) {
options.title = Lobibox.notify.OPTIONS[me.$type].title;
} else if (options.size === 'mini' && options.title === true) {
options.title = false;
}
if (options.icon === true) {
options.icon = Lobibox.notify.OPTIONS.icons[options.iconSource][me.$type];
}
if (options.sound === true) {
options.sound = Lobibox.notify.OPTIONS[me.$type].sound;
}
if (options.sound) {
options.sound = options.soundPath + options.sound + options.soundExt;
}
return options;
};
var _appendInWrapper = function ($el, $wrapper) {
if (me.$options.size === 'normal') {
if ($wrapper.hasClass('bottom')) {
$wrapper.prepend($el);
} else {
$wrapper.append($el);
}
} else if (me.$options.size === 'mini') {
if ($wrapper.hasClass('bottom')) {
$wrapper.prepend($el);
} else {
$wrapper.append($el);
}
} else if (me.$options.size === 'large') {
var tabPane = _createTabPane().append($el);
var $li = _createTabControl(tabPane.attr('id'));
$wrapper.find('.lb-notify-wrapper').append(tabPane);
$wrapper.find('.lb-notify-tabs').append($li);
_activateTab($li);
$li.find('>a').click(function () {
_activateTab($li);
});
}
};
var _activateTab = function ($li) {
$li.closest('.lb-notify-tabs').find('>li').removeClass('active');
$li.addClass('active');
var $current = $($li.find('>a').attr('href'));
$current.closest('.lb-notify-wrapper').find('>.lb-tab-pane').removeClass('active');
$current.addClass('active')
};
var _createTabControl = function (tabPaneId) {
var $li = $('<li></li>', {
'class': Lobibox.notify.OPTIONS[me.$type]['class']
});
$('<a></a>', {
'href': '#' + tabPaneId
}).append('<i class="tab-control-icon ' + me.$options.icon + '"></i>')
.appendTo($li);
return $li;
};
var _createTabPane = function () {
return $('<div></div>', {
'class': 'lb-tab-pane',
'id': Math.randomString(10)
})
};
var _createNotifyWrapper = function () {
var selector = (me.$options.size === 'large' ? '.lobibox-notify-wrapper-large' : '.lobibox-notify-wrapper')
+ "." + me.$options.position.replace(/\s/gi, '.'),
$wrapper;
//var classes = me.$options.position.split(" ");
$wrapper = $(selector);
if ($wrapper.length === 0) {
$wrapper = $('<div></div>')
.addClass(selector.replace(/\./g, ' ').trim())
.appendTo($('body'));
if (me.$options.size === 'large') {
$wrapper.append($('<ul class="lb-notify-tabs"></ul>'))
.append($('<div class="lb-notify-wrapper"></div>'));
}
}
return $wrapper;
};
var _createNotify = function () {
var OPTS = Lobibox.notify.OPTIONS,
$iconEl,
$innerIconEl,
$iconWrapper,
$body,
$msg,
$notify = $('<div></div>', {
'class': 'lobibox-notify ' + OPTS[me.$type]['class'] + ' ' + OPTS['class'] + ' ' + me.$options.showClass
});
$iconWrapper = $('<div class="lobibox-notify-icon-wrapper"></div>').appendTo($notify);
$iconEl = $('<div class="lobibox-notify-icon"></div>').appendTo($iconWrapper);
$innerIconEl = $('<div></div>').appendTo($iconEl);
// Add image or icon depending on given parameters
if (me.$options.img) {
$innerIconEl.append('<img src="' + me.$options.img + '"/>');
} else if (me.$options.icon) {
$innerIconEl.append('<div class="icon-el"><i class="' + me.$options.icon + '"></i></div>');
} else {
$notify.addClass('without-icon');
}
// Create body, append title and message in body and append body in notification
$msg = $('<div class="lobibox-notify-msg">' + me.$options.msg + '</div>');
if (me.$options.messageHeight !== false) {
$msg.css('max-height', me.$options.messageHeight);
}
$body = $('<div></div>', {
'class': 'lobibox-notify-body'
}).append($msg).appendTo($notify);
if (me.$options.title) {
$body.prepend('<div class="lobibox-notify-title">' + me.$options.title + '<div>');
}
_addCloseButton($notify);
if (me.$options.size === 'normal' || me.$options.size === 'mini') {
_addCloseOnClick($notify);
_addDelay($notify);
}
// Give width to notification
if (me.$options.width) {
$notify.css('width', _calculateWidth(me.$options.width));
}
return $notify;
};
var _addCloseButton = function ($el) {
if (!me.$options.closable) {
return;
}
$('<span class="lobibox-close">×</span>').click(function () {
if (me.$options.onClose && typeof me.$options.onClose === 'function'){
me.$options.onClose.call(me);
}else{
me.remove();
}
}).appendTo($el);
};
var _addCloseOnClick = function ($el) {
if (!me.$options.closeOnClick) {
return;
}
$el.click(function () {
me.remove();
});
};
var _addDelay = function ($el) {
if (!me.$options.delay) {
return;
}
if (me.$options.delayIndicator) {
var delay = $('<div class="lobibox-delay-indicator"><div></div></div>');
$el.append(delay);
}
var time = 0;
var interval = 1000 / 30;
var currentTime = new Date().getTime();
var timer = setInterval(function () {
if (me.$options.continueDelayOnInactiveTab){
time = new Date().getTime() - currentTime;
} else {
time += interval;
}
var width = 100 * time / me.$options.delay;
if (width >= 100) {
width = 100;
me.remove();
timer = clearInterval(timer);
}
if (me.$options.delayIndicator) {
delay.find('div').css('width', width + "%");
}
}, interval);
if (me.$options.pauseDelayOnHover) {
$el.on('mouseenter.lobibox', function () {
interval = 0;
}).on('mouseleave.lobibox', function () {
interval = 1000 / 30;
});
}
};
var _findTabToActivate = function ($li) {
var $itemToActivate = $li.prev();
if ($itemToActivate.length === 0) {
$itemToActivate = $li.next();
}
if ($itemToActivate.length === 0) {
return null;
}
return $itemToActivate;
};
var _calculateWidth = function (width) {
width = Math.min($(window).outerWidth(), width);
return width;
};
//------------------------------------------------------------------------------
//----------------PROTOTYPE FUNCTIONS-------------------------------------------
//------------------------------------------------------------------------------
/**
* Delete the notification
*
* @returns {LobiboxNotify}
*/
this.remove = function () {
me.$el.removeClass(me.$options.showClass)
.addClass(me.$options.hideClass);
var parent = me.$el.parent();
var wrapper = parent.closest('.lobibox-notify-wrapper-large');
var href = '#' + parent.attr('id');
var $li = wrapper.find('>.lb-notify-tabs>li:has(a[href="' + href + '"])');
$li.addClass(Lobibox.notify.OPTIONS['class'])
.addClass(me.$options.hideClass);
setTimeout(function () {
if (me.$options.size === 'normal' || me.$options.size === 'mini') {
me.$el.remove();
} else if (me.$options.size === 'large') {
var $newLi = _findTabToActivate($li);
if ($newLi) {
_activateTab($newLi);
}
$li.remove();
parent.remove();
}
var list = Lobibox.notify.list;
var ind = list.indexOf(me);
list.splice(ind, 1);
var next = list[ind];
if (next && next.$options.showAfterPrevious){
next._init();
}
}, 500);
return me;
};
me._init = function () {
// Create notification
var $notify = _createNotify();
if (me.$options.size === 'mini') {
$notify.addClass('notify-mini');
}
if (typeof me.$options.position === 'string') {
var $wrapper = _createNotifyWrapper();
_appendInWrapper($notify, $wrapper);
if ($wrapper.hasClass('center')) {
$wrapper.css('margin-left', '-' + ($wrapper.width() / 2) + "px");
}
} else {
$('body').append($notify);
$notify.css({
'position': 'fixed',
left: me.$options.position.left,
top: me.$options.position.top
})
}
me.$el = $notify;
if (me.$options.sound) {
var snd = new Audio(me.$options.sound); // buffers automatically when created
snd.play();
}
if (me.$options.rounded) {
me.$el.addClass('rounded');
}
me.$el.on('click.lobibox', function(ev){
if (me.$options.onClickUrl){
window.location.href = me.$options.onClickUrl;
}
if (me.$options.onClick && typeof me.$options.onClick === 'function'){
me.$options.onClick.call(me, ev);
}
});
me.$el.data('lobibox', me);
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
this.$type = type;
this.$options = _processInput(options);
if (!me.$options.showAfterPrevious || Lobibox.notify.list.length === 0){
this._init();
}
};
Lobibox.notify = function (type, options) {
if (["default", "info", "warning", "error", "success"].indexOf(type) > -1) {
var lobibox = new LobiboxNotify(type, options);
Lobibox.notify.list.push(lobibox);
return lobibox;
}
};
Lobibox.notify.list = [];
Lobibox.notify.closeAll = function () {
var list = Lobibox.notify.list;
for (var i in list){
list[i].remove();
}
};
//User can set default options to this variable
Lobibox.notify.DEFAULTS = {
title: true, // Title of notification. If you do not include the title in options it will automatically takes its value
//from Lobibox.notify.OPTIONS object depending of the type of the notifications or set custom string. Set this false to disable title
size: 'normal', // normal, mini, large
soundPath: 'sounds/', // The folder path where sounds are located
soundExt: '.ogg', // Default extension for all sounds
showClass: 'fadeInDown', // Show animation class.
hideClass: 'zoomOut', // Hide animation class.
icon: true, // Icon of notification. Leave as is for default icon or set custom string
msg: '', // Message of notification
img: null, // Image source string
closable: true, // Make notifications closable
hideCloseButton: false, // Notification may be closable but you can hide close button and it will be closed by clicking on notification itsef
delay: 5000, // Hide notification after this time (in miliseconds)
delayIndicator: true, // Show timer indicator
closeOnClick: true, // Close notifications by clicking on them
width: 400, // Width of notification box
sound: true, // Sound of notification. Set this false to disable sound. Leave as is for default sound or set custom soud path
// Place to show notification. Available options: "top left", "top right", "bottom left", "bottom right", "center top", "center bottom"
// It can also be object {left: number, top: number} to position notification at any place
position: "bottom right",
iconSource: 'bootstrap', // "bootstrap" or "fontAwesome" the library which will be used for icons
rounded: false, // Whether to make notification corners rounded
messageHeight: 60, // Notification message maximum height. This is not for notification itself, this is for <code>.lobibox-notify-msg</code>
pauseDelayOnHover: true, // When you mouse over on notification delay (if it is enabled) will be paused.
onClickUrl: null, // The url which will be opened when notification is clicked
showAfterPrevious: false, // Set this to true if you want notification not to be shown until previous notification is closed. This is useful for notification queues
continueDelayOnInactiveTab: true, // Continue delay when browser tab is inactive
// Events
onClick: null,
onClose: null
};
//This variable is necessary.
Lobibox.notify.OPTIONS = {
'class': 'animated-fast',
large: {
width: 500,
messageHeight: 96
},
mini: {
'class': 'notify-mini',
messageHeight: 32
},
default: {
'class': 'lobibox-notify-default',
'title': 'Default',
sound: false
},
success: {
'class': 'lobibox-notify-success',
'title': 'Success',
sound: 'sound2'
},
error: {
'class': 'lobibox-notify-error',
'title': 'Error',
sound: 'sound4'
},
warning: {
'class': 'lobibox-notify-warning',
'title': 'Warning',
sound: 'sound5'
},
info: {
'class': 'lobibox-notify-info',
'title': 'Information',
sound: 'sound6'
},
icons: {
bootstrap: {
success: 'glyphicon glyphicon-ok-sign',
error: 'glyphicon glyphicon-remove-sign',
warning: 'glyphicon glyphicon-exclamation-sign',
info: 'glyphicon glyphicon-info-sign'
},
fontAwesome: {
success: 'fa fa-check-circle',
error: 'fa fa-times-circle',
warning: 'fa fa-exclamation-circle',
info: 'fa fa-info-circle'
}
}
};
})();