2017-08-06 22:08:03 +02: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 16:34:54 +02:00
|
|
|
this.elem.style.display = 'grid';
|
|
|
|
|
2017-08-27 15:31:34 +02:00
|
|
|
window.$events.listen(this.type, text => {
|
2017-08-06 22:08:03 +02:00
|
|
|
this.show(text);
|
|
|
|
});
|
|
|
|
elem.addEventListener('click', this.hide.bind(this));
|
2018-07-29 16:34:54 +02:00
|
|
|
|
|
|
|
if (elem.hasAttribute('data-show')) {
|
|
|
|
setTimeout(() => this.show(this.textElem.textContent), 100);
|
|
|
|
}
|
2017-08-09 22:33:00 +02:00
|
|
|
|
|
|
|
this.hideCleanup = this.hideCleanup.bind(this);
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
show(textToShow = '') {
|
2017-08-09 22:33:00 +02:00
|
|
|
this.elem.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-06 22:08:03 +02:00
|
|
|
this.textElem.textContent = textToShow;
|
2018-03-18 12:58:45 +01:00
|
|
|
this.elem.style.display = 'grid';
|
2017-08-06 22:08:03 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
this.elem.classList.add('showing');
|
|
|
|
}, 1);
|
|
|
|
|
2020-04-10 14:38:08 +02:00
|
|
|
if (this.autohide) {
|
|
|
|
const words = textToShow.split(' ').length;
|
|
|
|
const timeToShow = Math.max(2000, 1000 + (250 * words));
|
|
|
|
setTimeout(this.hide.bind(this), timeToShow);
|
|
|
|
}
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
|
|
|
this.elem.classList.remove('showing');
|
2017-08-09 22:33:00 +02:00
|
|
|
this.elem.addEventListener('transitionend', this.hideCleanup);
|
|
|
|
}
|
2017-08-06 22:08:03 +02:00
|
|
|
|
2017-08-09 22:33:00 +02:00
|
|
|
hideCleanup() {
|
|
|
|
this.elem.style.display = 'none';
|
|
|
|
this.elem.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-06 22:08:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-09 22:17:35 +01:00
|
|
|
export default Notification;
|