2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2017-08-07 04:08:03 +08:00
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
export class Notification extends Component {
|
2017-08-07 04:08:03 +08:00
|
|
|
|
2022-11-15 19:24:31 +08:00
|
|
|
setup() {
|
|
|
|
this.container = this.$el;
|
|
|
|
this.type = this.$opts.type;
|
|
|
|
this.textElem = this.container.querySelector('span');
|
|
|
|
this.autoHide = this.$opts.autoHide === 'true';
|
2023-04-19 05:20:02 +08:00
|
|
|
this.initialShow = this.$opts.show === 'true';
|
2022-11-15 19:24:31 +08:00
|
|
|
this.container.style.display = 'grid';
|
2018-07-29 22:34:54 +08:00
|
|
|
|
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);
|
|
|
|
});
|
2022-11-15 19:24:31 +08:00
|
|
|
this.container.addEventListener('click', this.hide.bind(this));
|
2018-07-29 22:34:54 +08:00
|
|
|
|
2022-11-15 19:24:31 +08:00
|
|
|
if (this.initialShow) {
|
2018-07-29 22:34:54 +08:00
|
|
|
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 = '') {
|
2022-11-15 19:24:31 +08:00
|
|
|
this.container.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-07 04:08:03 +08:00
|
|
|
this.textElem.textContent = textToShow;
|
2022-11-15 19:24:31 +08:00
|
|
|
this.container.style.display = 'grid';
|
2017-08-07 04:08:03 +08:00
|
|
|
setTimeout(() => {
|
2022-11-15 19:24:31 +08:00
|
|
|
this.container.classList.add('showing');
|
2017-08-07 04:08:03 +08:00
|
|
|
}, 1);
|
|
|
|
|
2022-11-15 19:24:31 +08:00
|
|
|
if (this.autoHide) {
|
2020-04-10 20:38:08 +08:00
|
|
|
const words = textToShow.split(' ').length;
|
|
|
|
const timeToShow = Math.max(2000, 1000 + (250 * words));
|
|
|
|
setTimeout(this.hide.bind(this), timeToShow);
|
|
|
|
}
|
2017-08-07 04:08:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
2022-11-15 19:24:31 +08:00
|
|
|
this.container.classList.remove('showing');
|
|
|
|
this.container.addEventListener('transitionend', this.hideCleanup);
|
2017-08-10 04:33:00 +08:00
|
|
|
}
|
2017-08-07 04:08:03 +08:00
|
|
|
|
2017-08-10 04:33:00 +08:00
|
|
|
hideCleanup() {
|
2022-11-15 19:24:31 +08:00
|
|
|
this.container.style.display = 'none';
|
|
|
|
this.container.removeEventListener('transitionend', this.hideCleanup);
|
2017-08-07 04:08:03 +08:00
|
|
|
}
|
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|