summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/content/items/folder-location-button.mjs
blob: 9d99dbbf3039a9d394c79f16a8c76035848b2ece (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
/* 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 { MailTabButton } from "chrome://messenger/content/unifiedtoolbar/mail-tab-button.mjs";

const lazy = {};
ChromeUtils.defineModuleGetter(
  lazy,
  "FolderUtils",
  "resource:///modules/FolderUtils.jsm"
);

class FolderLocationButton extends MailTabButton {
  /**
   * Image element displaying the icon on the button.
   *
   * @type {Image?}
   */
  #icon = null;

  /**
   * If we've added our event listeners, especially to the current about3pane.
   *
   * @type {boolean}
   */
  #addedListeners = false;

  observed3PaneEvents = ["folderURIChanged"];

  observedAboutMessageEvents = [];

  connectedCallback() {
    super.connectedCallback();
    if (this.#addedListeners) {
      return;
    }
    this.#icon = this.querySelector(".button-icon");
    this.onCommandContextChange();
    this.#addedListeners = true;
    const popup = document.getElementById(this.getAttribute("popup"));
    popup.addEventListener("command", this.#handlePopupCommand);
  }

  disconnectedCallback() {
    super.disconnectedCallback();
    if (this.#addedListeners) {
      const popup = document.getElementById(this.getAttribute("popup"));
      popup.removeEventListener("command", this.#handlePopupCommand);
    }
  }

  #handlePopupCommand = event => {
    const about3Pane = document.getElementById("tabmail").currentAbout3Pane;
    about3Pane.displayFolder(event.target._folder.URI);
  };

  /**
   * Update the label and icon of the button from the currently selected folder
   * in the local 3pane.
   */
  onCommandContextChange() {
    if (!this.#icon) {
      return;
    }
    const { gFolder } =
      document.getElementById("tabmail").currentAbout3Pane ?? {};
    if (!gFolder) {
      this.disabled = true;
      return;
    }
    this.disabled = false;
    this.label.textContent = gFolder.name;
    this.#icon.style = `content: url(${lazy.FolderUtils.getFolderIcon(
      gFolder
    )});`;
  }
}
customElements.define("folder-location-button", FolderLocationButton, {
  extends: "button",
});