2017-08-06 16:08:03 -04: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 10:34:54 -04:00
|
|
|
this.elem.style.display = 'grid';
|
|
|
|
|
2017-08-27 09:31:34 -04:00
|
|
|
window.$events.listen(this.type, text => {
|
2017-08-06 16:08:03 -04:00
|
|
|
this.show(text);
|
|
|
|
});
|
|
|
|
elem.addEventListener('click', this.hide.bind(this));
|
2018-07-29 10:34:54 -04:00
|
|
|
|
|
|
|
if (elem.hasAttribute('data-show')) {
|
|
|
|
setTimeout(() => this.show(this.textElem.textContent), 100);
|
|
|
|
}
|
2017-08-09 16:33:00 -04:00
|
|
|
|
|
|
|
this.hideCleanup = this.hideCleanup.bind(this);
|
2017-08-06 16:08:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
show(textToShow = '') {
|
2017-08-09 16:33:00 -04:00
|
|
|
this.elem.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-06 16:08:03 -04:00
|
|
|
this.textElem.textContent = textToShow;
|
2018-03-18 07:58:45 -04:00
|
|
|
this.elem.style.display = 'grid';
|
2017-08-06 16:08:03 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
this.elem.classList.add('showing');
|
|
|
|
}, 1);
|
|
|
|
|
2020-04-10 08:38:08 -04: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 16:08:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
|
|
|
this.elem.classList.remove('showing');
|
2017-08-09 16:33:00 -04:00
|
|
|
this.elem.addEventListener('transitionend', this.hideCleanup);
|
|
|
|
}
|
2017-08-06 16:08:03 -04:00
|
|
|
|
2017-08-09 16:33:00 -04:00
|
|
|
hideCleanup() {
|
|
|
|
this.elem.style.display = 'none';
|
|
|
|
this.elem.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-06 16:08:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default Notification;
|