2017-08-07 04:08:03 +08:00
|
|
|
|
|
|
|
class Notification {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
this.type = elem.getAttribute('notification');
|
|
|
|
this.textElem = elem.querySelector('span');
|
|
|
|
this.autohide = this.elem.hasAttribute('data-autohide');
|
2018-07-29 22:34:54 +08:00
|
|
|
this.elem.style.display = 'grid';
|
|
|
|
|
2017-08-27 21:31:34 +08:00
|
|
|
window.$events.listen(this.type, text => {
|
2017-08-07 04:08:03 +08:00
|
|
|
this.show(text);
|
|
|
|
});
|
|
|
|
elem.addEventListener('click', this.hide.bind(this));
|
2018-07-29 22:34:54 +08:00
|
|
|
|
|
|
|
if (elem.hasAttribute('data-show')) {
|
|
|
|
setTimeout(() => this.show(this.textElem.textContent), 100);
|
|
|
|
}
|
2017-08-10 04:33:00 +08:00
|
|
|
|
|
|
|
this.hideCleanup = this.hideCleanup.bind(this);
|
2017-08-07 04:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
show(textToShow = '') {
|
2017-08-10 04:33:00 +08:00
|
|
|
this.elem.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-07 04:08:03 +08:00
|
|
|
this.textElem.textContent = textToShow;
|
2018-03-18 19:58:45 +08:00
|
|
|
this.elem.style.display = 'grid';
|
2017-08-07 04:08:03 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
this.elem.classList.add('showing');
|
|
|
|
}, 1);
|
|
|
|
|
|
|
|
if (this.autohide) setTimeout(this.hide.bind(this), 2000);
|
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
|
|
|
this.elem.classList.remove('showing');
|
2017-08-10 04:33:00 +08:00
|
|
|
this.elem.addEventListener('transitionend', this.hideCleanup);
|
|
|
|
}
|
2017-08-07 04:08:03 +08:00
|
|
|
|
2017-08-10 04:33:00 +08:00
|
|
|
hideCleanup() {
|
|
|
|
this.elem.style.display = 'none';
|
|
|
|
this.elem.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-07 04:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-10 05:17:35 +08:00
|
|
|
export default Notification;
|