-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathToaster.js
73 lines (55 loc) · 1.36 KB
/
Toaster.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
// Do not import this module to the application! Import index.js instead.
/**
* @type {Toaster}
*/
export let toaster = new Toaster();
/**
* Toasts controller. Controls toasts that appear on the screen.
* @constructor
* @private
*/
function Toaster () {
/**
* @type {Toast[]}
*/
this.toasts = [];
/**
* Keeps the timeouts of toasts which are removed.
* @type {Map}
*/
this.timeouts = new Map();
}
/**
* @param {Toast} toast
* @param {number} timeout
*/
Toaster.prototype.push = function (toast, timeout) {
requestAnimationFrame(() => {
let height = toast.attach(0);
this.toasts.forEach((toast) => {
toast.seek(height);
});
this.toasts.push(toast);
this.timeouts.set(toast, setTimeout(() => this.remove(toast), timeout));
});
};
/**
* @param {Toast} toast
*/
Toaster.prototype.remove = function (toast) {
if (this.timeouts.has(toast)) {
clearTimeout(this.timeouts.get(toast));
this.timeouts.delete(toast);
} else {
return; // already deleted
}
const index = this.toasts.indexOf(toast);
const tst = this.toasts.splice(index, 1)[0];
const height = toast.element.offsetHeight;
tst.detach();
this.toasts.slice(0, index).forEach(t => t.seek(-height));
};
Toaster.prototype.removeAll = function () {
while (this.toasts.length > 0)
this.remove(this.toasts[0]);
};