-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvanilla-toast.js
308 lines (258 loc) · 8.51 KB
/
vanilla-toast.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
307
308
(function() {
"use strict";
// VanillaToast class
var VanillaToast = (function() {
function VanillaToast() {
this.queue = new TaskQueue();
this.cancellationTokens = [];
this.element = null;
}
var constants = {
default: {
className: 'default',
fadeDuration: 400,
fadeInterval: 16,
duration: 2000,
closeButton: false,
immediately: false
},
success: {
className: 'success'
},
info: {
className: 'info'
},
warning: {
className: 'warning'
},
error: {
className: 'error',
duration: 3000,
closeButton: true
}
};
// create elements.
VanillaToast.prototype.initElement = function(selector) {
var container = document.createElement('div');
var toastBox = document.createElement('div');
var text = document.createElement('div');
var closeButton = document.createElement('span');
container.setAttribute("id", "vanilla-toast-container");
toastBox.setAttribute("id", "vanilla-toast");
text.setAttribute("id", "vanilla-toast-text");
closeButton.setAttribute("id", "vanilla-toast-close-button");
closeButton.innerHTML = '✖';
toastBox.appendChild(text);
toastBox.appendChild(closeButton);
container.appendChild(toastBox);
if (selector) {
document.getElementById(seletor).appendChild(containter);
} else {
document.body.appendChild(container);
}
this.element = {
container: container,
toastBox: toastBox,
text: text,
closeButton: closeButton
};
_setStyle(this, constants.default);
};
// cancel current showing toast.
VanillaToast.prototype.cancel = function() {
if (this.cancellationTokens.length) this.cancellationTokens[0].cancel();
};
// cancel all enqueued toasts.
VanillaToast.prototype.cancelAll = function() {
var length = this.cancellationTokens.length;
for (var i = 0; i < length; ++i) {
(function(token) {
token.cancel();
})(this.cancellationTokens[length - i - 1]);
}
};
// show toast
VanillaToast.prototype.show = function(text, option, callback) {
var self = this;
if (!self.element) self.initElement();
if (!option) option = {};
// if immediately show option is on, cancel all previous toasts.
if (option.immediately) self.cancelAll();
var cancellationToken = new CancellationToken();
// enqueue
self.queue.enqueue(function(next) {
// time setting
var fadeDuration = option.fadeDuration || constants.default.fadeDuration;
var fadeInterval = option.fadeInterval || constants.default.fadeInterval;
var fadeStep = Math.min(fadeInterval / fadeDuration, 1);
var duration = option.duration || constants.default.duration;
// close button setting
self.element.closeButton.style.display =
option.closeButton ? 'inline' : 'none';
// set text
self.element.text.innerHTML = text;
// set visible
var s = self.element.toastBox.style;
s.opacity = 0;
s.display = 'inline-block';
// set styles
_setStyle(self, option);
// timeoutId
var timeoutId = null;
// duration timeout callback.
var timeoutCallback = function() {
timeoutId = null;
// release click clickHandler
self.element.toastBox.removeEventListener('click', cancelHandler);
_hide(self, option, cancellationToken, function() {
if (callback) callback();
self.cancellationTokens.shift().dispose();
next();
});
};
// click for close handler
var cancelHandler = function() {
if (!timeoutId) return;
clearTimeout(timeoutId);
timeoutCallback();
};
// start fade in.
_fade(s, fadeStep, fadeInterval, cancellationToken, function() {
// show while duration time and hide.
self.element.toastBox.addEventListener('click', cancelHandler);
if (cancellationToken.isCancellationRequested) {
timeoutCallback();
} else {
timeoutId = setTimeout(timeoutCallback, duration);
cancellationToken.register(function() {
cancelHandler();
});
}
});
});
self.cancellationTokens.push(cancellationToken);
return self;
};
// create preset methods
for (var item in constants) {
(function(preset) {
VanillaToast.prototype[preset] = function(text, option, callback) {
if (!option) option = {};
// copy preset options
for (var propertyName in constants[preset]) {
if (option[propertyName] === undefined)
option[propertyName] = constants[preset][propertyName];
}
return this.show(text, option, callback);
};
})(item);
}
// private methods.
// set style
function _setStyle(self, option) {
self.element.toastBox.className = option.className || constants.default.className;
};
// hide toast
function _hide(self, option, cancellationToken, callback) {
if (!option) option = {};
// time setting
var fadeDuration = option.fadeDuration || constants.default.fadeDuration;
var fadeInterval = option.fadeInterval || constants.default.fadeInterval;
var fadeStep = Math.min(fadeInterval / fadeDuration, 1);
// set visible
var s = self.element.toastBox.style;
s.opacity = 1;
// start fade out and call callback function.
_fade(s, -fadeStep, fadeInterval, cancellationToken, function() {
s.display = 'none';
if (callback) callback();
});
return self;
};
// run fade animation
function _fade(style, step, interval, cancellationToken, callback) {
(function fade() {
if (cancellationToken.isCancellationRequested) {
style.opacity = step < 0 ? 0 : 1;
if (callback) callback();
return;
}
style.opacity = Number(style.opacity) + step;
if (step < 0 && style.opacity < 0) {
if (callback) callback();
} else if (step > 0 && style.opacity >= 1) {
if (callback) callback();
} else {
var timeoutId = setTimeout(function() {
timeoutId = null;
fade();
}, interval);
cancellationToken.register(function() {
if (!timeoutId) return;
clearTimeout(timeoutId);
timeoutId = null;
if (callback) callback();
});
}
})();
};
return VanillaToast;
})();
// CancellationToken class
var CancellationToken = (function() {
function CancellationToken() {
this.isCancellationRequested = false;
this.cancelCallbacks = [];
}
CancellationToken.prototype.cancel = function() {
this.isCancellationRequested = true;
var copiedCallbacks = this.cancelCallbacks.slice();
while (copiedCallbacks.length) copiedCallbacks.shift()();
};
CancellationToken.prototype.register = function(callback) {
this.cancelCallbacks.push(callback);
};
CancellationToken.prototype.dispose = function() {
while (this.cancelCallbacks.length) this.cancelCallbacks.shift();
};
return CancellationToken;
})();
// TaskQueue class from https://github.com/talsu/async-task-queue
var TaskQueue = (function() {
function TaskQueue() {
this.queue = [];
this.isExecuting = false;
}
// enqueue job. run immediately.
TaskQueue.prototype.enqueue = function(job) {
// enqueue.
this.queue.push(job);
// call execute.
dequeueAndExecute(this);
};
// Dequeue and execute job.
function dequeueAndExecute(self) {
if (self.isExecuting) return;
// Dequeue Job.
var job = self.queue.shift();
if (!job) return;
//Execute Job.
self.isExecuting = true;
// Pass next job execute callback.
job(function() {
self.isExecuting = false;
dequeueAndExecute(self);
});
}
return TaskQueue;
})();
// export
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = new VanillaToast();
}
exports.vanillaToast = new VanillaToast();
} else {
this.vanillaToast = new VanillaToast();
}
}.call(this));