]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/notification.js
Converted jQuery bits into raw JS components
[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             console.log('show', text);
11             this.show(text);
12         });
13         elem.addEventListener('click', this.hide.bind(this));
14         if (elem.hasAttribute('data-show')) this.show(this.textElem.textContent);
15     }
16
17     show(textToShow = '') {
18         this.textElem.textContent = textToShow;
19         this.elem.style.display = 'block';
20         setTimeout(() => {
21             this.elem.classList.add('showing');
22         }, 1);
23
24         if (this.autohide) setTimeout(this.hide.bind(this), 2000);
25     }
26
27     hide() {
28         this.elem.classList.remove('showing');
29
30         function transitionEnd() {
31             this.elem.style.display = 'none';
32             this.elem.removeEventListener('transitionend', transitionEnd);
33         }
34
35         this.elem.addEventListener('transitionend', transitionEnd.bind(this));
36     }
37
38 }
39
40 module.exports = Notification;