/**
* Toast Component
*/
globalThis.Component.register('Toast', container => {
container.toast = new bootstrap.Toast(container, { autohide: container.state.delay > 0 })
globalThis.Util.copyFunction(container, container.toast, 'show', 'hide')
container.addEventListener('show.bs.toast', () => {
if (container.state.delay > 0) {
let timer
const startTimer = () => {
container.state.left = container.state.delay / 1000
timer = setInterval(() => {
if (!container.isConnected || --container.state.left <= 0) clearInterval(timer)
}, 1000)
}
startTimer()
container.addEventListener('mouseenter', () => { clearInterval(timer); container.state.left = undefined })
container.addEventListener('mouseleave', startTimer)
}
})
}, globalThis.Util.makeDom(/*html*/`
`), globalThis.Util.makeDom(/*html*/``))
globalThis.Toast = {
show(message, options = {}) {
const delay = options.delay ?? 5000
const toast = document.createElement('Toast')
toast.state = { delay, left: delay ? delay / 1000 : undefined, type: options.type || 'primary', message, buttons: options.buttons ?? ['{#Close#}'] }
globalThis.$(`[toast-container="${options.container || 'default'}"]`).appendChild(toast)
return Promise.resolve().then(() => { toast.show(); return toast })
},
confirm(message, options = {}) {
return new Promise(resolve => this.show(message, { buttons: ['{#Confirm#}'], ...options }).then(toast => {
toast.addEventListener('hidden.bs.toast', () => resolve(toast.result === 1), { once: true })
}).catch(() => resolve(false)))
}
}