-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotif.js
45 lines (37 loc) · 1.09 KB
/
notif.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
var Notif = (function () {
'use strict';
function Notif (opts) {
this.text = opts.text;
this.target = document.body;
this.show();
return this;
}
Notif.prototype.render = function () {
var container = document.createElement('div');
container.className = 'notif-container';
var textEl = document.createElement('div');
textEl.className = 'notif-text';
textEl.textContent = this.text;
var closeBtn = document.createElement('div');
closeBtn.className = 'notif-close-btn';
closeBtn.textContent = 'close';
closeBtn.addEventListener('click', this.close.bind(this));
container.appendChild(textEl);
container.appendChild(closeBtn);
return container;
};
Notif.prototype.show = function () {
this.notifEl = this.render();
this.target.appendChild(this.notifEl);
return this;
};
Notif.prototype.close = function (opts) {
if (opts && opts.duration > 0) {
window.setTimeout(this.close.bind(this), opts.duration);
} else {
this.target.removeChild(this.notifEl);
}
return this;
};
return Notif;
}());