summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/unifiedtoolbar/content/customization-palette.mjs
blob: d3d0417f7ee98afab1948b126c144b1125169c38 (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
/* 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 ListBoxSelection from "./list-box-selection.mjs";
import "./customizable-element.mjs"; // eslint-disable-line import/no-unassigned-import

const { getAvailableItemIdsForSpace, MULTIPLE_ALLOWED_ITEM_IDS } =
  ChromeUtils.importESModule("resource:///modules/CustomizableItems.sys.mjs");

/**
 * Customization palette containing items that can be added to a customization
 * target.
 * Attributes:
 * - space: ID of the space the widgets are for. "all" for space agnostic
 *   widgets. Not observed.
 * - items-in-use: Comma-separated IDs of items that are in a target at the time
 *   this is initialized. When changed, initialize should be called.
 */
class CustomizationPalette extends ListBoxSelection {
  contextMenuId = "customizationPaletteMenu";

  /**
   * If this palette contains items (even if those items are currently all in a
   * target).
   *
   * @type {boolean}
   */
  isEmpty = false;

  /**
   * Array of item IDs allowed to be in this palette.
   *
   * @type {string[]}
   */
  #allAvailableItems = [];

  /**
   * If this palette contains items that can be added to all spaces.
   *
   * @type {boolean}
   */
  #allSpaces = false;

  connectedCallback() {
    if (super.connectedCallback()) {
      return;
    }

    this.#allSpaces = this.getAttribute("space") === "all";

    if (this.#allSpaces) {
      document
        .getElementById("customizationPaletteAddEverywhere")
        .addEventListener("command", this.#handleMenuAddEverywhere);
    }

    this.initialize();
  }

  /**
   * Initializes the contents of the palette from the current state. The
   * relevant state is defined by the space and items-in-use attributes.
   */
  initialize() {
    const itemIds = this.getAttribute("items-in-use").split(",");
    this.setItems(itemIds);
  }

  /**
   * Update the items currently removed from the palette with an array of item
   * IDs.
   *
   * @param {string[]} itemIds - Array of item IDs currently being used in a
   *   target.
   */
  setItems(itemIds) {
    let space = this.getAttribute("space");
    if (space === "all") {
      space = undefined;
    }
    const itemsInUse = new Set(itemIds);
    this.#allAvailableItems = getAvailableItemIdsForSpace(space);
    this.isEmpty = !this.#allAvailableItems.length;
    const items = this.#allAvailableItems.filter(
      itemId => !itemsInUse.has(itemId) || MULTIPLE_ALLOWED_ITEM_IDS.has(itemId)
    );
    this.replaceChildren(
      ...items.map(itemId => {
        const element = document.createElement("li", {
          is: "customizable-element",
        });
        element.setAttribute("item-id", itemId);
        element.draggable = true;
        return element;
      })
    );
  }

  /**
   * Overwritten context menu handler. Before showing the menu, initializes the
   * menu with items for all the target areas available.
   *
   * @param {MouseEvent} event
   */
  handleContextMenu = event => {
    const menu = document.getElementById(this.contextMenuId);
    const targets = this.getRootNode().querySelectorAll(
      '[is="customization-target"]'
    );
    const addEverywhereItem = document.getElementById(
      "customizationPaletteAddEverywhere"
    );
    addEverywhereItem.setAttribute("hidden", (!this.#allSpaces).toString());
    const menuItems = Array.from(targets, target => {
      const menuItem = document.createXULElement("menuitem");
      document.l10n.setAttributes(menuItem, "customize-palette-add-to", {
        target: target.name,
      });
      menuItem.addEventListener(
        "command",
        this.#makeAddToTargetHandler(target)
      );
      return menuItem;
    });
    menuItems.push(addEverywhereItem);
    menu.replaceChildren(...menuItems);
    this.initializeContextMenu(event);
  };

  #handleMenuAddEverywhere = () => {
    if (this.contextMenuFor) {
      this.primaryAction(this.contextMenuFor);
      this.dispatchEvent(
        new CustomEvent("additem", {
          detail: {
            itemId: this.contextMenuFor.getAttribute("item-id"),
          },
          bubbles: true,
          composed: true,
        })
      );
    }
  };

  /**
   * Generate a context menu item event handler that will add the right clicked
   * item to the target.
   *
   * @param {CustomizationTarget} target
   * @returns {function} Context menu item event handler curried with the given
   *   target.
   */
  #makeAddToTargetHandler(target) {
    return () => {
      if (this.contextMenuFor) {
        this.primaryAction(this.contextMenuFor, target);
      }
    };
  }

  handleDragSuccess(item) {
    if (item.allowMultiple) {
      return;
    }
    super.handleDragSuccess(item);
  }

  handleDrop(itemId, sibling, afterSibling) {
    if (this.querySelector(`li[item-id="${itemId}"]`)?.allowMultiple) {
      return;
    }
    super.handleDrop(itemId, sibling, afterSibling);
  }

  canAddElement(itemId) {
    return (
      this.#allAvailableItems.includes(itemId) &&
      (super.canAddElement(itemId) ||
        this.querySelector(`li[item-id="${itemId}"]`).allowMultiple)
    );
  }

  /**
   * The primary action for the palette is to add the item to a customization
   * target. Will pick the first target if none is provided.
   *
   * @param {CustomizableElement} item - Item to move to a target.
   * @param {CustomizationTarget} [target] - The target to move the item to.
   *   Defaults to the first target in the root.
   */
  primaryAction(item, target) {
    if (!target) {
      target = this.getRootNode().querySelector('[is="customization-target"]');
    }
    if (item?.allowMultiple) {
      target.addItem(item.cloneNode(true));
      return;
    }
    if (super.primaryAction(item)) {
      return;
    }
    target.addItem(item);
  }

  /**
   * Returns the item to this palette from some other place.
   *
   * @param {CustomizableElement} item - Item to return to this palette.
   */
  returnItem(item) {
    if (item.allowMultiple) {
      item.remove();
      return;
    }
    this.append(item);
  }

  /**
   * Filter the items in the palette for the given string based on their label.
   * The comparison is done on the lower cased label, and the filter string is
   * lower cased as well.
   *
   * @param {string} filterString - String to filter the items by.
   */
  filterItems(filterString) {
    const lowerFilterString = filterString.toLowerCase();
    for (const item of this.children) {
      item.hidden = !item.label.toLowerCase().includes(lowerFilterString);
    }
  }

  addItemById(itemId) {
    const item = this.querySelector(`[item-id="${itemId}"]`);
    if (!item) {
      return;
    }
    this.primaryAction(item);
  }
}
customElements.define("customization-palette", CustomizationPalette, {
  extends: "ul",
});