From 19304cd1ae4e4ba4f6dcf4f1db14de1e4e70c250 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Mon, 10 Jan 2022 23:38:56 +0100 Subject: improve item list styling; add payload creation form; exend dialog mechanism --- html/dialog.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'html/dialog.js') diff --git a/html/dialog.js b/html/dialog.js index 6345b2d..22e8aa9 100644 --- a/html/dialog.js +++ b/html/dialog.js @@ -42,7 +42,7 @@ */ #FROM html/DOM_helpers.js IMPORT clone_template -#FROM common/lock.js IMPORT make_lock, lock, unlock +#FROM common/lock.js IMPORT make_lock, lock, try_lock, unlock function make(on_dialog_show, on_dialog_hide) { @@ -68,9 +68,15 @@ function make(on_dialog_show, on_dialog_hide) function close_dialog(dialog_context, event) { + if (event && event.target.parentElement.classList.contains("hide")) + return; + + const result = event ? event.target.haketilo_dialog_result : undefined; + if (dialog_context.queue > 0) - dialog_context.callback(event.target.haketilo_dialog_result); + dialog_context.callback(result); } +#EXPORT close_dialog AS close async function show_dialog(dialog_context, shown_buts_id, msg) { @@ -81,11 +87,19 @@ async function show_dialog(dialog_context, shown_buts_id, msg) dialog_context.on_dialog_show(); } - await lock(dialog_context.lock); - - dialog_context.msg.innerText = msg; + /* + * We want the dialog to be ready for calling close() right after + * show_dialog() gets called. For this, we want locking to happen + * synchronously if possible. If impossible (lock taken), this means dialog + * is already open, hence it's also ready for close()'ing. + */ + if (!try_lock(dialog_context.lock)) + await lock(dialog_context.lock); + + [...dialog_context.msg.childNodes].forEach(n => n.remove()); + dialog_context.msg.append(...msg); for (const buts_id of ["ask_buts", "conf_buts"]) { - const action = buts_id == shown_buts_id ? "remove" : "add"; + const action = buts_id === shown_buts_id ? "remove" : "add"; dialog_context[buts_id].classList[action]("hide"); } @@ -101,12 +115,15 @@ async function show_dialog(dialog_context, shown_buts_id, msg) return result; } -const error = (ctx, msg) => show_dialog(ctx, "conf_buts", msg); +const error = (ctx, ...msg) => show_dialog(ctx, "conf_buts", msg); #EXPORT error /* info() and error() are the same for now, we might later change that. */ const info = error; #EXPORT info -const ask = (ctx, msg) => show_dialog(ctx, "ask_buts", msg); +const ask = (ctx, ...msg) => show_dialog(ctx, "ask_buts", msg); #EXPORT ask + +const loader = (ctx, ...msg) => show_dialog(ctx, null, msg); +#EXPORT loader -- cgit v1.2.3