summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/content/mail-tab-button.mjs
blob: a0eeee227905ee5de13eb6419a87dc488e1ca91c (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* 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 { UnifiedToolbarButton } from "./unified-toolbar-button.mjs";

/* import-globals-from ../../../base/content/globalOverlay.js */

/**
 * Mail tab specific unified toolbar button. Instead of tracking a global
 * command, its state gets re-evaluated every time the state of about:3pane or
 * about:message tab changes in a relevant way.
 */
export class MailTabButton extends UnifiedToolbarButton {
  /**
   * Array of events to listen for on the about:3pane document.
   *
   * @type {string[]}
   */
  observed3PaneEvents = ["folderURIChanged", "select"];

  /**
   * Array of events to listen for on the message browser.
   *
   * @type {string[]}
   */
  observedAboutMessageEvents = ["load"];

  /**
   * Listeners we've added in tabs.
   *
   * @type {{tabId: any, target: EventTarget, event: string, callback: function}[]}
   */
  #listeners = [];

  connectedCallback() {
    super.connectedCallback();
    this.#addTabListeners();
    this.onCommandContextChange();
  }

  disconnectedCallback() {
    super.disconnectedCallback();
    for (const listener of this.#listeners) {
      listener.target.removeEventListener(listener.event, listener.callback);
    }
    this.#listeners.length = 0;
  }

  /**
   * Callback for customizable-element when the current tab is switched while
   * this button is visible.
   */
  onTabSwitched() {
    this.#addTabListeners();
    this.onCommandContextChange();
  }

  /**
   * Callback for customizable-element when a tab is closed.
   *
   * @param {TabInfo} tab
   */
  onTabClosing(tab) {
    this.#removeListenersForTab(tab.tabId);
  }

  /**
   * Remove all event listeners this button has for a given tab.
   *
   * @param {*} tabId - ID of the tab to remove listeners for.
   */
  #removeListenersForTab(tabId) {
    for (const listener of this.#listeners) {
      if (listener.tabId === tabId) {
        listener.target.removeEventListener(listener.event, listener.callback);
      }
    }
    this.#listeners = this.#listeners.filter(
      listener => listener.tabId !== tabId
    );
  }

  /**
   * Add missing event listeners for the current tab.
   */
  #addTabListeners() {
    const tabmail = document.getElementById("tabmail");
    const tabId = tabmail.currentTabInfo.tabId;
    const existingListeners = this.#listeners.filter(
      listener => listener.tabId === tabId
    );
    let expectedEventListeners = [];
    switch (tabmail.currentTabInfo.mode.name) {
      case "mail3PaneTab":
        expectedEventListeners = this.observed3PaneEvents.concat(
          this.observedAboutMessageEvents
        );
        break;
      case "mailMessageTab":
        expectedEventListeners = this.observedAboutMessageEvents.concat();
        break;
    }
    const missingListeners = expectedEventListeners.filter(event =>
      existingListeners.every(listener => listener.event !== event)
    );
    if (!missingListeners.length) {
      return;
    }
    const contentWindow = tabmail.currentTabInfo.chromeBrowser.contentWindow;
    for (const event of missingListeners) {
      const listener = {
        event,
        tabId,
        callback: this.#handle3PaneChange,
        target: contentWindow,
      };
      if (
        this.observedAboutMessageEvents.includes(event) &&
        contentWindow.messageBrowser
      ) {
        listener.target = contentWindow.messageBrowser.contentWindow;
      }
      listener.target.addEventListener(listener.event, listener.callback);
      this.#listeners.push(listener);
    }
  }

  /**
   * Event handling callback when an event by a tab is fired.
   */
  #handle3PaneChange = () => {
    this.onCommandContextChange();
  };

  /**
   * Handle the context changing, updating the disabled state for the button
   * etc.
   */
  onCommandContextChange() {
    if (!this.observedCommand) {
      return;
    }
    try {
      this.disabled = !getEnabledControllerForCommand(this.observedCommand);
    } catch {
      this.disabled = true;
    }
  }
}
customElements.define("mail-tab-button", MailTabButton, {
  extends: "button",
});