summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/content/unified-toolbar-button.mjs
blob: 466a83f0c11dd923895586314877f0618a05329d (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* 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/. */

//TODO keyboard handling, keyboard + commands

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

/**
 * Toolbar button implementation for the unified toolbar.
 * Template ID: unifiedToolbarButtonTemplate
 * Attributes:
 * - command: ID string of the command to execute when the button is pressed.
 * - observes: ID of command to observe for disabled state. Defaults to value of
 *   command attribute.
 * - popup: ID of the popup to open when the button is pressed. The popup is
 *   anchored to the button. Overrides any other click handling.
 * - disabled: When set the button is disabled.
 * - title: Tooltip to show on the button.
 * - label: Label text of the button. Observed for changes.
 * - label-id: A fluent ID for the label instead of the label attribute.
 *   Observed for changes.
 * - badge: When set, the value of the attribute is shown as badge.
 * - aria-pressed: set to "false" to make the button behave like a toggle.
 * Events:
 * - buttondisabled: Fired when the button gets disabled while it is keyboard
 *   navigable.
 * - buttonenabled: Fired when the button gets enabled again but isn't marked to
 *   be keyboard navigable.
 */
export class UnifiedToolbarButton extends HTMLButtonElement {
  static get observedAttributes() {
    return ["label", "label-id", "disabled"];
  }

  /**
   * Container for the button label.
   *
   * @type {?HTMLSpanElement}
   */
  label = null;

  /**
   * Name of the command this button follows the disabled (and if it is a toggle
   * button the checked) state of.
   *
   * @type {string?}
   */
  observedCommand;

  /**
   * The mutation observer observing the command this button follows the state
   * of.
   *
   * @type {MutationObserver?}
   */
  #observer = null;

  connectedCallback() {
    // We remove the mutation overserver when the element is disconnected, thus
    // we have to add it every time the element is connected.
    this.observedCommand =
      this.getAttribute("observes") || this.getAttribute("command");
    if (this.observedCommand) {
      const command = document.getElementById(this.observedCommand);
      if (command) {
        if (!this.#observer) {
          this.#observer = new MutationObserver(this.#handleCommandMutation);
        }
        const observedAttributes = ["disabled"];
        if (this.hasAttribute("aria-pressed")) {
          observedAttributes.push("checked");

          // Update the pressed state from the command
          this.setAttribute(
            "aria-pressed",
            command.getAttribute("checked") ?? "false"
          );
        }
        this.#observer.observe(command, {
          attributes: true,
          attributeFilter: observedAttributes,
        });
      }
      // Update the disabled state to match the current state of the command.
      try {
        this.disabled = !getEnabledControllerForCommand(this.observedCommand);
      } catch {
        this.disabled = true;
      }
    }
    if (this.hasConnected) {
      return;
    }
    this.hasConnected = true;
    this.classList.add("unified-toolbar-button", "button");

    const template = document
      .getElementById("unifiedToolbarButtonTemplate")
      .content.cloneNode(true);
    this.label = template.querySelector("span");
    this.#updateLabel();
    this.appendChild(template);
    this.addEventListener("click", event => this.handleClick(event));
  }

  disconnectedCallback() {
    if (this.#observer) {
      this.#observer.disconnect();
    }
  }

  attributeChangedCallback(attribute) {
    switch (attribute) {
      case "label":
      case "label-id":
        this.#updateLabel();
        break;
      case "disabled":
        if (!this.hasConnected) {
          return;
        }
        if (this.disabled && this.tabIndex !== -1) {
          this.tabIndex = -1;
          this.dispatchEvent(new CustomEvent("buttondisabled"));
        } else if (!this.disabled && this.tabIndex === -1) {
          this.dispatchEvent(new CustomEvent("buttonenabled"));
        }
        break;
    }
  }

  /**
   * Default handling for clicks on the button. Shows the associated popup,
   * executes the given command and toggles the button state.
   *
   * @param {MouseEvent} event - Click event.
   */
  handleClick(event) {
    if (this.hasAttribute("popup")) {
      event.preventDefault();
      event.stopPropagation();
      const popup = document.getElementById(this.getAttribute("popup"));
      popup.openPopup(this, {
        position: "after_start",
        triggerEvent: event,
      });
      this.setAttribute("aria-pressed", "true");
      const hideListener = () => {
        if (popup.state === "open") {
          return;
        }
        this.removeAttribute("aria-pressed");
        popup.removeEventListener("popuphiding", hideListener);
      };
      popup.addEventListener("popuphiding", hideListener);
      return;
    }
    if (this.hasAttribute("aria-pressed")) {
      const isPressed = this.getAttribute("aria-pressed") === "true";
      this.setAttribute("aria-pressed", (!isPressed).toString());
    }
    if (this.hasAttribute("command")) {
      const command = this.getAttribute("command");
      let controller = getEnabledControllerForCommand(command);
      if (controller) {
        event.preventDefault();
        event.stopPropagation();
        controller = controller.wrappedJSObject ?? controller;
        controller.doCommand(command, event);
        return;
      }
      const commandElement = document.getElementById(command);
      if (!commandElement) {
        return;
      }
      event.preventDefault();
      event.stopPropagation();
      commandElement.doCommand();
    }
  }

  /**
   * Callback for the mutation observer on the command this button follows.
   *
   * @param {Mutation[]} mutationList - List of mutations the observer saw.
   */
  #handleCommandMutation = mutationList => {
    for (const mutation of mutationList) {
      if (mutation.type !== "attributes") {
        continue;
      }
      if (mutation.attributeName === "disabled") {
        this.disabled = mutation.target.getAttribute("disabled") === "true";
      } else if (mutation.attributeName === "checked") {
        this.setAttribute(
          "aria-pressed",
          mutation.target.getAttribute("checked")
        );
      }
    }
  };

  /**
   * Update the contents of the label from the attributes of this element.
   */
  #updateLabel() {
    if (!this.label) {
      return;
    }
    if (this.hasAttribute("label")) {
      this.label.textContent = this.getAttribute("label");
      return;
    }
    if (this.hasAttribute("label-id")) {
      document.l10n.setAttributes(this.label, this.getAttribute("label-id"));
    }
  }

  /**
   * Badge displayed on the button. To clear the badge, set to empty string or
   * nullish value.
   *
   * @type {string}
   */
  set badge(badgeText) {
    if (badgeText === "" || badgeText == null) {
      this.removeAttribute("badge");
      return;
    }
    this.setAttribute("badge", badgeText);
  }

  get badge() {
    return this.getAttribute("badge");
  }
}
customElements.define("unified-toolbar-button", UnifiedToolbarButton, {
  extends: "button",
});