summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/megalist/content/Dialog.mjs
blob: f2eca6376cc1be88d60925e6f0b697a0d0502882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

const GENERIC_DIALOG_TEMPLATE = document.querySelector("#dialog-template");

const DIALOGS = {
  "remove-login": {
    template: "#remove-login-dialog-template",
  },
  "export-logins": {
    template: "#export-logins-dialog-template",
  },
  "remove-logins": {
    template: "#remove-logins-dialog-template",
    callback: dialog => {
      const primaryButton = dialog.querySelector("button.primary");
      const checkbox = dialog.querySelector(".confirm-checkbox");
      const toggleButton = () => (primaryButton.disabled = !checkbox.checked);
      checkbox.addEventListener("change", toggleButton);
      toggleButton();
    },
  },
  "import-logins": {
    template: "#import-logins-dialog-template",
  },
  "import-error": {
    template: "#import-error-dialog-template",
  },
};

/**
 * Setup dismiss and command handling logic for the dialog overlay.
 *
 * @param {Element} overlay - The overlay element containing the dialog
 * @param {Function} messageHandler - Function to send message back to view model.
 */
const setupControls = (overlay, messageHandler) => {
  const dialog = overlay.querySelector(".dialog-container");
  const commandButtons = dialog.querySelectorAll("[data-command]");
  for (const commandButton of commandButtons) {
    const commandId = commandButton.dataset.command;
    commandButton.addEventListener("click", () => messageHandler(commandId));
  }

  dialog.querySelectorAll("[close-dialog]").forEach(element => {
    element.addEventListener("click", cancelDialog, { once: true });
  });

  document.addEventListener("keyup", function handleKeyUp(ev) {
    if (ev.key === "Escape") {
      cancelDialog();
      document.removeEventListener("keyup", handleKeyUp);
    }
  });

  document.addEventListener("click", function handleClickOutside(ev) {
    if (!dialog.contains(ev.target)) {
      cancelDialog();
      document.removeEventListener("click", handleClickOutside);
    }
  });
  dialog.querySelector("[autofocus]")?.focus();
};

/**
 * Add data-l10n-args to elements with localizable attribute
 *
 * @param {Element} dialog - The dialog element.
 * @param {Array<object>} l10nArgs - List of localization arguments.
 */
const populateL10nArgs = (dialog, l10nArgs) => {
  const localizableElements = dialog.querySelectorAll("[localizable]");
  for (const [index, localizableElement] of localizableElements.entries()) {
    localizableElement.dataset.l10nArgs = JSON.stringify(l10nArgs[index]) ?? "";
  }
};

/**
 * Remove the currently displayed dialog overlay from the DOM.
 */
export const cancelDialog = () =>
  document.querySelector(".dialog-overlay")?.remove();

/**
 * Create a new dialog overlay and populate it using the specified template and data.
 *
 * @param {object} dialogData - Data required to populate the dialog, includes template and localization args.
 * @param {Function} messageHandler - Function to send message back to view model.
 */
export const createDialog = (dialogData, messageHandler) => {
  const templateData = DIALOGS[dialogData?.id];

  const genericTemplateClone = document.importNode(
    GENERIC_DIALOG_TEMPLATE.content,
    true
  );

  const overlay = genericTemplateClone.querySelector(".dialog-overlay");
  const dialog = genericTemplateClone.querySelector(".dialog-container");

  const overrideTemplate = document.querySelector(templateData.template);
  const overrideTemplateClone = document.importNode(
    overrideTemplate.content,
    true
  );

  genericTemplateClone
    .querySelector(".dialog-wrapper")
    .appendChild(overrideTemplateClone);

  populateL10nArgs(genericTemplateClone, dialogData.l10nArgs);
  setupControls(overlay, messageHandler);
  document.body.appendChild(genericTemplateClone);
  templateData?.callback?.(dialog, messageHandler);
};