55 lines
1.2 KiB
TypeScript
Raw Normal View History

2019-11-18 11:04:13 +09:00
import delay from "./Utils/delay"
2017-07-12 20:37:34 +02:00
2018-04-02 07:34:16 +02:00
export default class StatusMessage {
2019-11-17 18:09:39 +09:00
private container: HTMLElement
private text: HTMLElement
2017-07-12 20:37:34 +02:00
constructor(container: HTMLElement, text: HTMLElement) {
this.container = container
this.text = text
}
2019-11-17 18:09:39 +09:00
public showError(message: string | Error, duration?: number) {
this.clearStyle()
this.show(message.toString(), duration || 4000)
this.container.classList.add("error-message")
}
public showInfo(message: string, duration?: number) {
this.clearStyle()
this.show(message, duration || 2000)
this.container.classList.add("info-message")
}
public close() {
this.container.classList.add("fade-out")
}
private show(message: string, duration: number) {
const messageId = String(Date.now())
2017-07-12 20:37:34 +02:00
2018-06-28 15:30:24 +09:00
this.text.textContent = message
2017-07-12 20:37:34 +02:00
this.container.classList.remove("fade-out")
this.container.dataset.messageId = messageId
2018-04-08 12:59:36 +02:00
// Negative duration means we're displaying it forever until the user manually closes it
if(duration === -1) {
return
}
2017-07-12 20:37:34 +02:00
delay(duration || 4000).then(() => {
if(this.container.dataset.messageId !== messageId) {
return
}
this.close()
})
}
2019-11-17 18:09:39 +09:00
private clearStyle() {
2017-07-21 12:55:36 +02:00
this.container.classList.remove("info-message")
this.container.classList.remove("error-message")
}
2019-11-17 18:09:39 +09:00
}