]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/notification.js
add missing icon, fix name conventions
[bookstack] / resources / assets / js / components / notification.js
1
2 class Notification {
3
4     constructor(elem) {
5         this.elem = elem;
6         this.type = elem.getAttribute('notification');
7         this.textElem = elem.querySelector('span');
8         this.autohide = this.elem.hasAttribute('data-autohide');
9         window.$events.listen(this.type, text => {
10             this.show(text);
11         });
12         elem.addEventListener('click', this.hide.bind(this));
13         if (elem.hasAttribute('data-show')) this.show(this.textElem.textContent);
14
15         this.hideCleanup = this.hideCleanup.bind(this);
16     }
17
18     show(textToShow = '') {
19         this.elem.removeEventListener('transitionend', this.hideCleanup);
20         this.textElem.textContent = textToShow;
21         this.elem.style.display = 'block';
22         setTimeout(() => {
23             this.elem.classList.add('showing');
24         }, 1);
25
26         if (this.autohide) setTimeout(this.hide.bind(this), 2000);
27     }
28
29     hide() {
30         this.elem.classList.remove('showing');
31         this.elem.addEventListener('transitionend', this.hideCleanup);
32     }
33
34     hideCleanup() {
35         this.elem.style.display = 'none';
36         this.elem.removeEventListener('transitionend', this.hideCleanup);
37     }
38
39 }
40
41 module.exports = Notification;