55 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-07-12 18:37:34 +00:00
import { delay } from "./Utils"
2018-04-02 05:34:16 +00:00
export default class StatusMessage {
2019-11-17 09:09:39 +00:00
private container: HTMLElement
private text: HTMLElement
2017-07-12 18:37:34 +00:00
constructor(container: HTMLElement, text: HTMLElement) {
this.container = container
this.text = text
}
2019-11-17 09:09:39 +00: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 18:37:34 +00:00
2018-06-28 06:30:24 +00:00
this.text.textContent = message
2017-07-12 18:37:34 +00:00
this.container.classList.remove("fade-out")
this.container.dataset.messageId = messageId
2018-04-08 10:59:36 +00:00
// Negative duration means we're displaying it forever until the user manually closes it
if(duration === -1) {
return
}
2017-07-12 18:37:34 +00:00
delay(duration || 4000).then(() => {
if(this.container.dataset.messageId !== messageId) {
return
}
this.close()
})
}
2019-11-17 09:09:39 +00:00
private clearStyle() {
2017-07-21 10:55:36 +00:00
this.container.classList.remove("info-message")
this.container.classList.remove("error-message")
}
2019-11-17 09:09:39 +00:00
}