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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// NOTE: This file depends on ui.js (or the autogenerated ui.m.js module
// version). These files and all files that depend on them are deprecated, and
// should only be used by legacy UIs that have not yet been updated to new
// patterns. Use Web Components in any new code.
import {assert, assertInstanceof} from 'chrome://resources/js/assert.js';
import {getPropertyDescriptor, PropertyKind} from 'chrome://resources/js/cr.m.js';
import {define as crUiDefine, decorate} from 'chrome://resources/js/cr/ui.js';
import {MenuItem} from './menu_item.js';
/**
* Creates a new menu element. Menu dispatches all commands on the element it
* was shown for.
*
* @param {Object=} opt_propertyBag Optional properties.
* @constructor
* @extends {HTMLElement}
*/
export const Menu = crUiDefine('cr-menu');
Menu.prototype = {
__proto__: HTMLElement.prototype,
selectedIndex_: -1,
/**
* Element for which menu is being shown.
*/
contextElement: null,
/**
* Initializes the menu element.
*/
decorate() {
this.addEventListener('mouseover', this.handleMouseOver_);
this.addEventListener('mouseout', this.handleMouseOut_);
this.addEventListener('mouseup', this.handleMouseUp_, true);
this.classList.add('decorated');
this.setAttribute('role', 'menu');
this.hidden = true; // Hide the menu by default.
// Decorate the children as menu items.
const menuItems = this.menuItems;
for (let i = 0, menuItem; menuItem = menuItems[i]; i++) {
decorate(menuItem, MenuItem);
}
},
/**
* Adds menu item at the end of the list.
* @param {Object} item Menu item properties.
* @return {!MenuItem} The created menu item.
*/
addMenuItem(item) {
const menuItem = /** @type {!MenuItem} */ (
this.ownerDocument.createElement('cr-menu-item'));
this.appendChild(menuItem);
decorate(menuItem, MenuItem);
if (item.label) {
menuItem.label = item.label;
}
if (item.iconUrl) {
menuItem.iconUrl = item.iconUrl;
}
return menuItem;
},
/**
* Adds separator at the end of the list.
*/
addSeparator() {
const separator = this.ownerDocument.createElement('hr');
decorate(separator, MenuItem);
this.appendChild(separator);
},
/**
* Clears menu.
*/
clear() {
this.selectedItem = null;
this.textContent = '';
},
/**
* Walks up the ancestors of |node| until a menu item belonging to this menu
* is found.
* @param {Node} node The node to start searching from.
* @return {MenuItem} The found menu item or null.
* @private
*/
findMenuItem_(node) {
while (node && node.parentNode !== this && !(node instanceof MenuItem)) {
node = node.parentNode;
}
return node ? assertInstanceof(node, MenuItem) : null;
},
/**
* Handles mouseover events and selects the hovered item.
* @param {Event} e The mouseover event.
* @private
*/
handleMouseOver_(e) {
const overItem = this.findMenuItem_(/** @type {Element} */ (e.target));
this.selectedItem = overItem;
},
/**
* Handles mouseout events and deselects any selected item.
* @param {Event} e The mouseout event.
* @private
*/
handleMouseOut_(e) {
this.selectedItem = null;
},
/**
* If there's a mouseup that happens quickly in about the same position,
* stop it from propagating to items. This is to prevent accidentally
* selecting a menu item that's created under the mouse cursor.
* @param {Event} e A mouseup event on the menu (in capturing phase).
* @private
*/
handleMouseUp_(e) {
assert(this.contains(/** @type {Element} */ (e.target)));
if (!this.trustEvent_(e) || Date.now() - this.shown_.time > 200) {
return;
}
const pos = this.shown_.mouseDownPos;
if (!pos || Math.abs(pos.x - e.screenX) + Math.abs(pos.y - e.screenY) > 4) {
return;
}
e.preventDefault();
e.stopPropagation();
},
/**
* @param {!Event} e
* @return {boolean} Whether |e| can be trusted.
* @private
* @suppress {checkTypes}
*/
trustEvent_(e) {
return e.isTrusted || e.isTrustedForTesting;
},
get menuItems() {
return this.querySelectorAll(this.menuItemSelector || '*');
},
/**
* The selected menu item or null if none.
* @type {MenuItem}
*/
get selectedItem() {
return this.menuItems[this.selectedIndex];
},
set selectedItem(item) {
const index = Array.prototype.indexOf.call(this.menuItems, item);
this.selectedIndex = index;
},
/**
* Focuses the selected item. If selectedIndex is invalid, set it to 0
* first.
*/
focusSelectedItem() {
const items = this.menuItems;
if (this.selectedIndex < 0 || this.selectedIndex > items.length) {
// Find first visible item to focus by default.
for (let idx = 0; idx < items.length; idx++) {
const item = items[idx];
if (item.hasAttribute('hidden') || item.isSeparator()) {
continue;
}
// If the item is disabled we accept it, but try to find the next
// enabled item, but keeping the first disabled item.
if (!item.disabled) {
this.selectedIndex = idx;
break;
} else if (this.selectedIndex === -1) {
this.selectedIndex = idx;
}
}
}
if (this.selectedItem) {
this.selectedItem.focus();
this.setAttribute('aria-activedescendant', this.selectedItem.id);
}
},
/**
* Menu length
*/
get length() {
return this.menuItems.length;
},
/**
* Returns whether the given menu item is visible.
* @param {!MenuItem} menuItem
* @return {boolean}
* @private
*/
isItemVisible_(menuItem) {
if (menuItem.hidden) {
return false;
}
if (menuItem.offsetParent) {
return true;
}
// A "position: fixed" element won't have an offsetParent, so we have to
// do the full style computation.
return window.getComputedStyle(menuItem).display !== 'none';
},
/**
* Returns whether the menu has any visible items.
* @return {boolean} True if the menu has visible item. Otherwise, false.
*/
hasVisibleItems() {
// Inspect items in reverse order to determine if the separator above each
// set of items is required.
for (const menuItem of this.menuItems) {
if (this.isItemVisible_(menuItem)) {
return true;
}
}
return false;
},
/**
* This is the function that handles keyboard navigation. This is usually
* called by the element responsible for managing the menu.
* @param {Event} e The keydown event object.
* @return {boolean} Whether the event was handled be the menu.
*/
handleKeyDown(e) {
let item = this.selectedItem;
const self = this;
const selectNextAvailable = function(m) {
const menuItems = self.menuItems;
const len = menuItems.length;
if (!len) {
// Edge case when there are no items.
return;
}
let i = self.selectedIndex;
if (i === -1 && m === -1) {
// Edge case when needed to go the last item first.
i = 0;
}
// "i" may be negative(-1), so modulus operation and cycle below
// wouldn't work as assumed. This trick makes startPosition positive
// without altering it's modulo.
const startPosition = (i + len) % len;
while (true) {
i = (i + m + len) % len;
// Check not to enter into infinite loop if all items are hidden or
// disabled.
if (i === startPosition) {
break;
}
item = menuItems[i];
if (item && !item.isSeparator() && !item.disabled &&
this.isItemVisible_(item)) {
break;
}
}
if (item && !item.disabled) {
self.selectedIndex = i;
}
}.bind(this);
switch (e.key) {
case 'ArrowDown':
selectNextAvailable(1);
this.focusSelectedItem();
return true;
case 'ArrowUp':
selectNextAvailable(-1);
this.focusSelectedItem();
return true;
case 'Enter':
case ' ':
if (item) {
// Store |contextElement| since it'll be removed when handling the
// 'activate' event.
const contextElement = this.contextElement;
const activationEvent = document.createEvent('Event');
activationEvent.initEvent('activate', true, true);
activationEvent.originalEvent = e;
if (item.dispatchEvent(activationEvent)) {
if (item.command) {
item.command.execute(contextElement);
}
}
}
return true;
}
return false;
},
hide() {
this.hidden = true;
delete this.shown_;
},
/** @param {{x: number, y: number}=} opt_mouseDownPos */
show(opt_mouseDownPos) {
this.shown_ = {mouseDownPos: opt_mouseDownPos, time: Date.now()};
this.hidden = false;
},
/**
* Updates menu items command according to context.
* @param {Node=} node Node for which to actuate commands state.
*/
updateCommands(node) {
const menuItems = this.menuItems;
for (const menuItem of menuItems) {
if (!menuItem.isSeparator()) {
menuItem.updateCommand(node);
}
}
let separatorRequired = false;
let lastSeparator = null;
// Hide any separators without a visible item between them and the next
// separator or the end of the menu.
for (const menuItem of menuItems) {
if (menuItem.isSeparator()) {
if (separatorRequired) {
lastSeparator = menuItem;
}
menuItem.hidden = true;
separatorRequired = false;
continue;
}
if (this.isItemVisible_(menuItem)) {
if (lastSeparator) {
lastSeparator.hidden = false;
}
separatorRequired = true;
}
}
},
};
/** @suppress {globalThis} This standalone function is used like method. */
function selectedIndexChanged(selectedIndex, oldSelectedIndex) {
const oldSelectedItem = this.menuItems[oldSelectedIndex];
if (oldSelectedItem) {
oldSelectedItem.selected = false;
oldSelectedItem.blur();
}
const item = this.selectedItem;
if (item) {
item.selected = true;
}
}
/**
* The selected menu item.
* @type {number}
*/
Menu.prototype.selectedIndex;
Object.defineProperty(
Menu.prototype, 'selectedIndex',
getPropertyDescriptor(
'selectedIndex', PropertyKind.JS, selectedIndexChanged));
/**
* Selector for children which are menu items.
* @type {string}
*/
Menu.prototype.menuItemSelector;
Object.defineProperty(
Menu.prototype, 'menuItemSelector',
getPropertyDescriptor('menuItemSelector', PropertyKind.ATTR));
|