2020-06-07 19:04:43 +03:00
|
|
|
Function.noop = () => {};
|
|
|
|
|
2024-11-03 21:28:32 +03:00
|
|
|
class CMessageBox {
|
|
|
|
constructor(options = {}) {
|
|
|
|
const title = options.title ?? 'Untitled'
|
|
|
|
const body = options.body ?? '<hr>'
|
|
|
|
const buttons = options.buttons ?? []
|
|
|
|
const callbacks = options.callbacks ?? []
|
|
|
|
const close_on_buttons = options.close_on_buttons ?? true
|
|
|
|
const unique_name = options.unique_name ?? null
|
2024-11-06 18:19:11 +03:00
|
|
|
const warn_on_exit = options.warn_on_exit ?? false
|
|
|
|
if(unique_name && window.messagebox_stack.find(item => item.unique_name == unique_name) != null) {
|
|
|
|
return
|
|
|
|
}
|
2024-11-03 21:28:32 +03:00
|
|
|
|
|
|
|
this.title = title
|
|
|
|
this.body = body
|
|
|
|
this.id = random_int(0, 10000)
|
|
|
|
this.close_on_buttons = close_on_buttons
|
|
|
|
this.unique_name = unique_name
|
2024-11-06 18:19:11 +03:00
|
|
|
this.warn_on_exit = warn_on_exit
|
2024-11-03 21:28:32 +03:00
|
|
|
|
|
|
|
u('body').addClass('dimmed').append(this.__getTemplate())
|
|
|
|
u('html').attr('style', 'overflow-y:hidden')
|
|
|
|
|
|
|
|
buttons.forEach((text, callback) => {
|
|
|
|
this.getNode().find('.ovk-diag-action').append(u(`<button class="button">${text}</button>`))
|
|
|
|
let button = u(this.getNode().find('.ovk-diag-action > button.button').last())
|
|
|
|
button.on("click", (e) => {
|
|
|
|
callbacks[callback]()
|
|
|
|
|
|
|
|
if(close_on_buttons) {
|
|
|
|
this.close()
|
2023-10-03 19:40:13 +03:00
|
|
|
}
|
2024-11-03 21:28:32 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
window.messagebox_stack.push(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
__getTemplate() {
|
|
|
|
return u(
|
|
|
|
`<div class="ovk-diag-cont" data-id="${this.id}">
|
|
|
|
<div class="ovk-diag">
|
|
|
|
<div class="ovk-diag-head">${this.title}</div>
|
|
|
|
<div class="ovk-diag-body">${this.body}</div>
|
|
|
|
<div class="ovk-diag-action"></div>
|
|
|
|
</div>
|
|
|
|
</div>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
getNode() {
|
|
|
|
return u(`.ovk-diag-cont[data-id='${this.id}']`)
|
|
|
|
}
|
|
|
|
|
|
|
|
async __showCloseConfirmationDialog() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const msg = new CMessageBox({
|
|
|
|
title: tr('exit_noun'),
|
|
|
|
body: tr('exit_confirmation'),
|
|
|
|
warn_on_exit: false,
|
|
|
|
unique_name: 'close_confirmation',
|
|
|
|
buttons: [tr('no'), tr('yes')],
|
|
|
|
callbacks: [() => {
|
|
|
|
msg.close()
|
|
|
|
resolve(false)
|
|
|
|
}, () => {
|
|
|
|
this.__exitDialog()
|
|
|
|
resolve(true)
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
__exitDialog() {
|
|
|
|
u(`.ovk-diag-cont[data-id='${this.id}']`).remove()
|
|
|
|
if(u('.ovk-diag-cont').length < 1) {
|
|
|
|
u('body').removeClass('dimmed')
|
|
|
|
u('html').attr('style', 'overflow-y:scroll')
|
|
|
|
}
|
|
|
|
|
|
|
|
const current_item = window.messagebox_stack.find(item => item.id == this.id)
|
|
|
|
const index_of_item = window.messagebox_stack.indexOf(current_item)
|
|
|
|
window.messagebox_stack = array_splice(window.messagebox_stack, index_of_item)
|
2020-06-07 19:04:43 +03:00
|
|
|
|
2024-11-03 21:28:32 +03:00
|
|
|
delete this
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
this.__exitDialog()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.messagebox_stack = []
|
|
|
|
|
|
|
|
function MessageBox(title, body, buttons, callbacks, return_msg = false) {
|
|
|
|
const msg = new CMessageBox({
|
|
|
|
title: title,
|
|
|
|
body: body,
|
|
|
|
buttons: buttons,
|
|
|
|
callbacks: callbacks,
|
|
|
|
})
|
|
|
|
|
|
|
|
if(return_msg) {
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg.getNode()
|
2020-07-17 19:25:55 +03:00
|
|
|
}
|
2024-11-03 21:28:32 +03:00
|
|
|
|
|
|
|
// Close on 'Escape' key
|
|
|
|
u(document).on('keyup', async (e) => {
|
|
|
|
if(e.keyCode == 27 && window.messagebox_stack.length > 0) {
|
|
|
|
const msg = window.messagebox_stack[window.messagebox_stack.length - 1]
|
|
|
|
if(!msg) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg.close_on_buttons) {
|
|
|
|
msg.close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-06 18:19:11 +03:00
|
|
|
if(msg.warn_on_exit) {
|
|
|
|
const res = await msg.__showCloseConfirmationDialog()
|
|
|
|
if(res === true) {
|
|
|
|
msg.close()
|
|
|
|
}
|
2024-11-03 21:28:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Close when clicking on shadow
|
|
|
|
u(document).on('click', 'body.dimmed .dimmer', async (e) => {
|
|
|
|
if(u(e.target).hasClass('dimmer')) {
|
|
|
|
const msg = window.messagebox_stack[window.messagebox_stack.length - 1]
|
|
|
|
if(!msg) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if(msg.close_on_buttons) {
|
|
|
|
msg.close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-06 18:19:11 +03:00
|
|
|
if(msg.warn_on_exit) {
|
|
|
|
const res = await msg.__showCloseConfirmationDialog()
|
|
|
|
if(res === true) {
|
|
|
|
msg.close()
|
|
|
|
}
|
2024-11-03 21:28:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|