summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/content/named-deck.js
blob: a5fdd590133056dc5d9d060fa6276db2bdcb0ed2 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* 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/. */
/* eslint max-len: ["error", 80] */

"use strict";

/**
 * This element is for use with the <named-deck> element. Set the target
 * <named-deck>'s ID in the "deck" attribute and the button's selected state
 * will reflect the deck's state. When the button is clicked, it will set the
 * view in the <named-deck> to the button's "name" attribute.
 *
 * The "tab" role will be added unless a different role is provided. Wrapping
 * a set of these buttons in a <button-group> element will add the key handling
 * for a tablist.
 *
 * NOTE: This does not observe changes to the "deck" or "name" attributes, so
 * changing them likely won't work properly.
 *
 * <button is="named-deck-button" deck="pet-deck" name="dogs">Dogs</button>
 * <named-deck id="pet-deck">
 *   <p name="cats">I like cats.</p>
 *   <p name="dogs">I like dogs.</p>
 * </named-deck>
 *
 * let btn = document.querySelector('button[name="dogs"]');
 * let deck = document.querySelector("named-deck");
 * deck.selectedViewName == "cats";
 * btn.selected == false; // Selected was pulled from the related deck.
 * btn.click();
 * deck.selectedViewName == "dogs";
 * btn.selected == true; // Selected updated when view changed.
 */
class NamedDeckButton extends HTMLButtonElement {
  connectedCallback() {
    this.id = `${this.deckId}-button-${this.name}`;
    if (!this.hasAttribute("role")) {
      this.setAttribute("role", "tab");
    }
    this.setSelectedFromDeck();
    this.addEventListener("click", this);
    document.addEventListener("view-changed", this, { capture: true });
  }

  disconnectedCallback() {
    this.removeEventListener("click", this);
    document.removeEventListener("view-changed", this, { capture: true });
  }

  attributeChangedCallback(name, oldVal, newVal) {
    if (name == "selected") {
      this.selected = newVal;
    }
  }

  get deckId() {
    return this.getAttribute("deck");
  }

  set deckId(val) {
    this.setAttribute("deck", val);
  }

  get deck() {
    return document.getElementById(this.deckId);
  }

  handleEvent(e) {
    if (e.type == "view-changed" && e.target.id == this.deckId) {
      this.setSelectedFromDeck();
    } else if (e.type == "click") {
      let { deck } = this;
      if (deck) {
        deck.selectedViewName = this.name;
      }
    }
  }

  get name() {
    return this.getAttribute("name");
  }

  get selected() {
    return this.hasAttribute("selected");
  }

  set selected(val) {
    if (this.selected != val) {
      this.toggleAttribute("selected", val);
    }
    this.setAttribute("aria-selected", !!val);
  }

  setSelectedFromDeck() {
    let { deck } = this;
    this.selected = deck && deck.selectedViewName == this.name;
    if (this.selected) {
      this.dispatchEvent(
        new CustomEvent("button-group:selected", { bubbles: true })
      );
    }
  }
}
customElements.define("named-deck-button", NamedDeckButton, {
  extends: "button",
});

class ButtonGroup extends HTMLElement {
  static get observedAttributes() {
    return ["orientation"];
  }

  connectedCallback() {
    this.setAttribute("role", "tablist");

    if (!this.observer) {
      this.observer = new MutationObserver(changes => {
        for (let change of changes) {
          this.setChildAttributes(change.addedNodes);
          for (let node of change.removedNodes) {
            if (this.activeChild == node) {
              // Ensure there's still an active child.
              this.activeChild = this.firstElementChild;
            }
          }
        }
      });
    }
    this.observer.observe(this, { childList: true });

    // Set the role and tabindex for the current children.
    this.setChildAttributes(this.children);

    // Try assigning the active child again, this will run through the checks
    // to ensure it's still valid.
    this.activeChild = this._activeChild;

    this.addEventListener("button-group:selected", this);
    this.addEventListener("keydown", this);
    this.addEventListener("mousedown", this);
    document.addEventListener("keypress", this);
  }

  disconnectedCallback() {
    this.observer.disconnect();
    this.removeEventListener("button-group:selected", this);
    this.removeEventListener("keydown", this);
    this.removeEventListener("mousedown", this);
    document.removeEventListener("keypress", this);
  }

  attributeChangedCallback(name, oldVal, newVal) {
    if (name == "orientation") {
      if (this.isVertical) {
        this.setAttribute("aria-orientation", this.orientation);
      } else {
        this.removeAttribute("aria-orientation");
      }
    }
  }

  setChildAttributes(nodes) {
    for (let node of nodes) {
      if (node.nodeType == Node.ELEMENT_NODE && node != this.activeChild) {
        node.setAttribute("tabindex", "-1");
      }
    }
  }

  // The activeChild is the child that can be focused with tab.
  get activeChild() {
    return this._activeChild;
  }

  set activeChild(node) {
    let prevActiveChild = this._activeChild;
    let newActiveChild;

    if (node && this.contains(node)) {
      newActiveChild = node;
    } else {
      newActiveChild = this.firstElementChild;
    }

    this._activeChild = newActiveChild;

    if (newActiveChild) {
      newActiveChild.setAttribute("tabindex", "0");
    }

    if (prevActiveChild && prevActiveChild != newActiveChild) {
      prevActiveChild.setAttribute("tabindex", "-1");
    }
  }

  get isVertical() {
    return this.orientation == "vertical";
  }

  get orientation() {
    return this.getAttribute("orientation") == "vertical"
      ? "vertical"
      : "horizontal";
  }

  set orientation(val) {
    if (val == "vertical") {
      this.setAttribute("orientation", val);
    } else {
      this.removeAttribute("orientation");
    }
  }

  _navigationKeys() {
    if (this.isVertical) {
      return {
        previousKey: "ArrowUp",
        nextKey: "ArrowDown",
      };
    }
    if (document.dir == "rtl") {
      return {
        previousKey: "ArrowRight",
        nextKey: "ArrowLeft",
      };
    }
    return {
      previousKey: "ArrowLeft",
      nextKey: "ArrowRight",
    };
  }

  handleEvent(e) {
    let { previousKey, nextKey } = this._navigationKeys();
    if (e.type == "keydown" && (e.key == previousKey || e.key == nextKey)) {
      this.setAttribute("last-input-type", "keyboard");
      e.preventDefault();
      let oldFocus = this.activeChild;
      this.walker.currentNode = oldFocus;
      let newFocus;
      if (e.key == previousKey) {
        newFocus = this.walker.previousNode();
      } else {
        newFocus = this.walker.nextNode();
      }
      if (newFocus) {
        this.activeChild = newFocus;
        this.dispatchEvent(new CustomEvent("button-group:key-selected"));
      }
    } else if (e.type == "button-group:selected") {
      this.activeChild = e.target;
    } else if (e.type == "mousedown") {
      this.setAttribute("last-input-type", "mouse");
    } else if (e.type == "keypress" && e.key == "Tab") {
      this.setAttribute("last-input-type", "keyboard");
    }
  }

  get walker() {
    if (!this._walker) {
      this._walker = document.createTreeWalker(this, NodeFilter.SHOW_ELEMENT, {
        acceptNode: node => {
          if (node.hidden || node.disabled) {
            return NodeFilter.FILTER_REJECT;
          }
          node.focus();
          return document.activeElement == node
            ? NodeFilter.FILTER_ACCEPT
            : NodeFilter.FILTER_REJECT;
        },
      });
    }
    return this._walker;
  }
}
customElements.define("button-group", ButtonGroup);

/**
 * A deck that is indexed by the "name" attribute of its children. The
 * <named-deck-button> element is a companion element that can update its state
 * and change the view of a <named-deck>.
 *
 * When the deck is connected it will set the first child as the selected view
 * if a view is not already selected.
 *
 * The deck is implemented using a named slot. Setting a slot directly on a
 * child element of the deck is not supported.
 *
 * You can get or set the selected view by name with the `selectedViewName`
 * property or by setting the "selected-view" attribute.
 *
 * <named-deck>
 *   <section name="cats">Some info about cats.</section>
 *   <section name="dogs">Some dog stuff.</section>
 * </named-deck>
 *
 * let deck = document.querySelector("named-deck");
 * deck.selectedViewName == "cats"; // Cat info is shown.
 * deck.selectedViewName = "dogs";
 * deck.selectedViewName == "dogs"; // Dog stuff is shown.
 * deck.setAttribute("selected-view", "cats");
 * deck.selectedViewName == "cats"; // Cat info is shown.
 */
class NamedDeck extends HTMLElement {
  static get observedAttributes() {
    return ["selected-view"];
  }

  constructor() {
    super();
    this.attachShadow({ mode: "open" });

    // Create a slot for the visible content.
    let selectedSlot = document.createElement("slot");
    selectedSlot.setAttribute("name", "selected");
    this.shadowRoot.appendChild(selectedSlot);

    this.observer = new MutationObserver(() => {
      this._setSelectedViewAttributes();
    });
  }

  connectedCallback() {
    if (this.selectedViewName) {
      // Make sure the selected view is shown.
      this._setSelectedViewAttributes();
    } else {
      // If there's no selected view, default to the first.
      let firstView = this.firstElementChild;
      if (firstView) {
        // This will trigger showing the first view.
        this.selectedViewName = firstView.getAttribute("name");
      }
    }
    this.observer.observe(this, { childList: true });
  }

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

  attributeChangedCallback(attr, oldVal, newVal) {
    if (attr == "selected-view" && oldVal != newVal) {
      // Update the slot attribute on the views.
      this._setSelectedViewAttributes();

      // Notify that the selected view changed.
      this.dispatchEvent(new CustomEvent("view-changed"));
    }
  }

  get selectedViewName() {
    return this.getAttribute("selected-view");
  }

  set selectedViewName(name) {
    this.setAttribute("selected-view", name);
  }

  /**
   * Set the slot attribute on all of the views to ensure only the selected view
   * is shown.
   */
  _setSelectedViewAttributes() {
    let { selectedViewName } = this;
    for (let view of this.children) {
      let name = view.getAttribute("name");
      view.setAttribute("aria-labelledby", `${this.id}-button-${name}`);
      view.setAttribute("role", "tabpanel");

      if (name === selectedViewName) {
        view.slot = "selected";
      } else {
        view.slot = "";
      }
    }
  }
}
customElements.define("named-deck", NamedDeck);