-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
notify.js
96 lines (81 loc) · 2.82 KB
/
notify.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
(function () {
// notify.js, by chapel; for sugarcube 2
// version 1.1.1
// requires notify.css / notify.min.css
var DEFAULT_TIME = 2000; // default notification time (in MS)
var isCssTime = /\d+m?s$/;
$(document.body).append("<div id='notify'></div>");
$(document).on(':notify', function (ev) {
if (ev.message && typeof ev.message === 'string') {
// trim message
ev.message.trim();
// classes
if (ev.class) {
if (typeof ev.class === 'string') {
ev.class = 'open macro-notify ' + ev.class;
} else if (Array.isArray(ev.class)) {
ev.class = 'open macro-notify ' + ev.class.join(' ');
} else {
ev.class = 'open macro-notify';
}
} else {
ev.class = 'open macro-notify';
}
// delay
if (ev.delay) {
if (typeof ev.delay !== 'number') {
ev.delay = Number(ev.delay);
}
if (Number.isNaN(ev.delay)) {
ev.delay = DEFAULT_TIME;
}
} else {
ev.delay = DEFAULT_TIME;
}
$('#notify')
.empty()
.wiki(ev.message)
.addClass(ev.class);
setTimeout(function () {
$('#notify').removeClass();
}, ev.delay);
}
});
function notify (message, time, classes) {
if (typeof message !== 'string') {
return;
}
if (typeof time !== 'number') {
time = false;
}
$(document).trigger({
type : ':notify',
message : message,
delay : time,
class : classes || ''
});
}
// <<notify delay 'classes'>> message <</notify>>
Macro.add('notify', {
tags : null,
handler : function () {
// set up
var msg = this.payload[0].contents,
time = false,
classes = false, i;
// arguments
if (this.args.length > 0) {
var cssTime = isCssTime.test(this.args[0]);
if (typeof this.args[0] === 'number' || cssTime) {
time = cssTime ? Util.fromCssTime(this.args[0]) : this.args[0];
classes = (this.args.length > 1) ? this.args.slice(1).flat(Infinity) : false;
} else {
classes = this.args.flat(Infinity).join(' ');
}
}
// fire event
notify(msg, time, classes);
}
});
setup.notify = notify;
}());