54 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 {
2017-07-12 18:37:34 +00:00
container: HTMLElement
text: HTMLElement
constructor(container: HTMLElement, text: HTMLElement) {
this.container = container
this.text = text
}
2017-07-21 08:10:48 +00:00
show(message: string, duration: number) {
2017-07-12 18:37:34 +00:00
let messageId = String(Date.now())
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()
})
}
2017-07-21 10:55:36 +00:00
clearStyle() {
this.container.classList.remove("info-message")
this.container.classList.remove("error-message")
}
2018-04-02 15:20:25 +00:00
showError(message: string | Error, duration?: number) {
2017-07-21 10:55:36 +00:00
this.clearStyle()
2018-04-02 15:20:25 +00:00
this.show(message.toString(), duration || 4000)
2017-07-12 18:37:34 +00:00
this.container.classList.add("error-message")
}
2017-07-21 08:10:48 +00:00
showInfo(message: string, duration?: number) {
2017-07-21 10:55:36 +00:00
this.clearStyle()
2017-07-21 08:10:48 +00:00
this.show(message, duration || 2000)
this.container.classList.add("info-message")
}
2017-07-12 18:37:34 +00:00
close() {
this.container.classList.add("fade-out")
}
}