aboutsummaryrefslogtreecommitdiff
path: root/html/dialog.js
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-01-11 15:43:40 +0100
committerWojtek Kosior <koszko@koszko.org>2022-01-11 15:43:40 +0100
commitdd8de100acda322f2124c58163ecde6f1b37d61d (patch)
treecbb73c92f04a83e259ea9b53ca653c6891970024 /html/dialog.js
parent19304cd1ae4e4ba4f6dcf4f1db14de1e4e70c250 (diff)
downloadbrowser-extension-dd8de100acda322f2124c58163ecde6f1b37d61d.tar.gz
browser-extension-dd8de100acda322f2124c58163ecde6f1b37d61d.zip
add missing payload_create test and styling
Diffstat (limited to 'html/dialog.js')
-rw-r--r--html/dialog.js64
1 files changed, 33 insertions, 31 deletions
diff --git a/html/dialog.js b/html/dialog.js
index 22e8aa9..c4bba5d 100644
--- a/html/dialog.js
+++ b/html/dialog.js
@@ -42,7 +42,6 @@
*/
#FROM html/DOM_helpers.js IMPORT clone_template
-#FROM common/lock.js IMPORT make_lock, lock, try_lock, unlock
function make(on_dialog_show, on_dialog_hide)
{
@@ -51,9 +50,7 @@ function make(on_dialog_show, on_dialog_hide)
on_dialog_show,
on_dialog_hide,
shown: false,
- queue: 0,
- lock: make_lock(),
- callback: null
+ queue: [],
});
for (const [id, val] of [["yes", true], ["no", false], ["ok", undefined]]) {
@@ -68,33 +65,30 @@ function make(on_dialog_show, on_dialog_hide)
function close_dialog(dialog_context, event)
{
- if (event && event.target.parentElement.classList.contains("hide"))
+ if ((event && event.target.parentElement.classList.contains("hide")) ||
+ dialog_context.queue.length === 0)
return;
- const result = event ? event.target.haketilo_dialog_result : undefined;
+ const [[shown_buts_id, msg, resolve]] = dialog_context.queue.splice(0, 1);
- if (dialog_context.queue > 0)
- dialog_context.callback(result);
+ if (dialog_context.queue.length > 0) {
+ process_queue_item(dialog_context);
+ } else {
+ dialog_context.shown = false;
+ try {
+ dialog_context.on_dialog_hide();
+ } catch(e) {
+ console.error(e);
+ }
+ }
+
+ resolve(event ? event.target.haketilo_dialog_result : undefined);
}
#EXPORT close_dialog AS close
-async function show_dialog(dialog_context, shown_buts_id, msg)
+function process_queue_item(dialog_context)
{
- dialog_context.queue++;
-
- if (!dialog_context.shown) {
- dialog_context.shown = true;
- dialog_context.on_dialog_show();
- }
-
- /*
- * 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);
+ const [shown_buts_id, msg, resolve] = dialog_context.queue[0];
[...dialog_context.msg.childNodes].forEach(n => n.remove());
dialog_context.msg.append(...msg);
@@ -102,17 +96,25 @@ async function show_dialog(dialog_context, shown_buts_id, msg)
const action = buts_id === shown_buts_id ? "remove" : "add";
dialog_context[buts_id].classList[action]("hide");
}
+}
- const result = await new Promise(cb => dialog_context.callback = cb);
+async function show_dialog(dialog_context, shown_buts_id, msg)
+{
+ let resolve;
+ const result_prom = new Promise(cb => resolve = cb);
+ dialog_context.queue.push([shown_buts_id, msg, resolve]);
- if (--dialog_context.queue == 0) {
- dialog_context.shown = false;
- dialog_context.on_dialog_hide();
+ if (!dialog_context.shown) {
+ process_queue_item(dialog_context);
+ dialog_context.shown = true;
+ try {
+ dialog_context.on_dialog_show();
+ } catch(e) {
+ console.error(e);
+ }
}
- unlock(dialog_context.lock);
-
- return result;
+ return await result_prom;
}
const error = (ctx, ...msg) => show_dialog(ctx, "conf_buts", msg);