]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/notification.js
Added support for Pascal language
[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         this.elem.style.display = 'grid';
10
11         window.$events.listen(this.type, text => {
12             this.show(text);
13         });
14         elem.addEventListener('click', this.hide.bind(this));
15
16         if (elem.hasAttribute('data-show')) {
17             setTimeout(() => this.show(this.textElem.textContent), 100);
18         }
19
20         this.hideCleanup = this.hideCleanup.bind(this);
21     }
22
23     show(textToShow = '') {
24         this.elem.removeEventListener('transitionend', this.hideCleanup);
25         this.textElem.textContent = textToShow;
26         this.elem.style.display = 'grid';
27         setTimeout(() => {
28             this.elem.classList.add('showing');
29         }, 1);
30
31         if (this.autohide) setTimeout(this.hide.bind(this), 2000);
32     }
33
34     hide() {
35         this.elem.classList.remove('showing');
36         this.elem.addEventListener('transitionend', this.hideCleanup);
37     }
38
39     hideCleanup() {
40         this.elem.style.display = 'none';
41         this.elem.removeEventListener('transitionend', this.hideCleanup);
42     }
43
44 }
45
46 export default Notification;