summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/content/components/generic-dialog.mjs
blob: 8d9ddc9d36c19b9f6580b01309707e05da42d73c (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
/* 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 http://mozilla.org/MPL/2.0/. */

import {
  setKeyboardAccessForNonDialogElements,
  initDialog,
} from "../aboutLoginsUtils.mjs";

export default class GenericDialog extends HTMLElement {
  constructor() {
    super();
    this._promise = null;
  }

  connectedCallback() {
    if (this.shadowRoot) {
      return;
    }
    const shadowRoot = initDialog(this, "#generic-dialog-template");
    this._dismissButton = this.querySelector(".dismiss-button");
    this._overlay = shadowRoot.querySelector(".overlay");
  }

  handleEvent(event) {
    switch (event.type) {
      case "keydown":
        if (event.key === "Escape" && !event.defaultPrevented) {
          this.hide();
        }
        break;
      case "click":
        if (
          event.currentTarget.classList.contains("dismiss-button") ||
          event.target.classList.contains("overlay")
        ) {
          this.hide();
        }
    }
  }

  show() {
    setKeyboardAccessForNonDialogElements(false);
    this.hidden = false;
    this.parentNode.host.hidden = false;

    this._dismissButton.addEventListener("click", this);
    this._overlay.addEventListener("click", this);
    window.addEventListener("keydown", this);
  }

  hide() {
    setKeyboardAccessForNonDialogElements(true);
    this._dismissButton.removeEventListener("click", this);
    this._overlay.removeEventListener("click", this);
    window.removeEventListener("keydown", this);

    this.hidden = true;
    this.parentNode.host.hidden = true;
  }
}

customElements.define("generic-dialog", GenericDialog);