2022-11-15 06:24:31 -05:00
|
|
|
import {Component} from "./component";
|
2017-08-06 16:08:03 -04:00
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
export class Notification extends Component {
|
2017-08-06 16:08:03 -04:00
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.type = this.$opts.type;
|
|
|
|
this.textElem = this.container.querySelector('span');
|
|
|
|
this.autoHide = this.$opts.autoHide === 'true';
|
|
|
|
this.initialShow = this.$opts.show === 'true'
|
|
|
|
this.container.style.display = 'grid';
|
2018-07-29 10:34:54 -04:00
|
|
|
|
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);
|
|
|
|
});
|
2022-11-15 06:24:31 -05:00
|
|
|
this.container.addEventListener('click', this.hide.bind(this));
|
2018-07-29 10:34:54 -04:00
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
if (this.initialShow) {
|
2018-07-29 10:34:54 -04:00
|
|
|
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 = '') {
|
2022-11-15 06:24:31 -05:00
|
|
|
this.container.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-06 16:08:03 -04:00
|
|
|
this.textElem.textContent = textToShow;
|
2022-11-15 06:24:31 -05:00
|
|
|
this.container.style.display = 'grid';
|
2017-08-06 16:08:03 -04:00
|
|
|
setTimeout(() => {
|
2022-11-15 06:24:31 -05:00
|
|
|
this.container.classList.add('showing');
|
2017-08-06 16:08:03 -04:00
|
|
|
}, 1);
|
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
if (this.autoHide) {
|
2020-04-10 08:38:08 -04:00
|
|
|
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() {
|
2022-11-15 06:24:31 -05:00
|
|
|
this.container.classList.remove('showing');
|
|
|
|
this.container.addEventListener('transitionend', this.hideCleanup);
|
2017-08-09 16:33:00 -04:00
|
|
|
}
|
2017-08-06 16:08:03 -04:00
|
|
|
|
2017-08-09 16:33:00 -04:00
|
|
|
hideCleanup() {
|
2022-11-15 06:24:31 -05:00
|
|
|
this.container.style.display = 'none';
|
|
|
|
this.container.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-06 16:08:03 -04:00
|
|
|
}
|
|
|
|
|
2022-11-15 06:24:31 -05:00
|
|
|
}
|