blob: c2d882aece176352aeacf9087b705630500fc967 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371/*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
Tim van der Lippec02a97c2020-02-14 14:39:2731import * as Common from '../common/common.js'; // eslint-disable-line no-unused-vars
Paul Lewis0fd43712020-01-08 17:07:3632import * as Host from '../host/host.js';
Tim van der Lippec02a97c2020-02-14 14:39:2733
Paul Lewis9950e182019-12-16 16:06:0734import {SoftContextMenu} from './SoftContextMenu.js';
35
Blink Reformat4c46d092018-04-07 15:32:3736/**
37 * @unrestricted
38 */
Tim van der Lippe0830b3d2019-10-03 13:20:0739export class Item {
Blink Reformat4c46d092018-04-07 15:32:3740 /**
Tim van der Lippe0830b3d2019-10-03 13:20:0741 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:3742 * @param {string} type
43 * @param {string=} label
44 * @param {boolean=} disabled
45 * @param {boolean=} checked
46 */
47 constructor(contextMenu, type, label, disabled, checked) {
48 this._type = type;
49 this._label = label;
50 this._disabled = disabled;
51 this._checked = checked;
52 this._contextMenu = contextMenu;
Tim van der Lippe1d6e57a2019-09-30 11:55:3453 if (type === 'item' || type === 'checkbox') {
Blink Reformat4c46d092018-04-07 15:32:3754 this._id = contextMenu ? contextMenu._nextId() : 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:3455 }
Blink Reformat4c46d092018-04-07 15:32:3756 }
57
58 /**
59 * @return {number}
60 */
61 id() {
62 return this._id;
63 }
64
65 /**
66 * @return {string}
67 */
68 type() {
69 return this._type;
70 }
71
72 /**
73 * @return {boolean}
74 */
75 isEnabled() {
76 return !this._disabled;
77 }
78
79 /**
80 * @param {boolean} enabled
81 */
82 setEnabled(enabled) {
83 this._disabled = !enabled;
84 }
85
86 /**
87 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
88 */
89 _buildDescriptor() {
90 switch (this._type) {
91 case 'item':
92 const result = {type: 'item', id: this._id, label: this._label, enabled: !this._disabled};
Tim van der Lippe1d6e57a2019-09-30 11:55:3493 if (this._customElement) {
Blink Reformat4c46d092018-04-07 15:32:3794 result.element = this._customElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:3495 }
96 if (this._shortcut) {
Blink Reformat4c46d092018-04-07 15:32:3797 result.shortcut = this._shortcut;
Tim van der Lippe1d6e57a2019-09-30 11:55:3498 }
Blink Reformat4c46d092018-04-07 15:32:3799 return result;
100 case 'separator':
101 return {type: 'separator'};
102 case 'checkbox':
103 return {type: 'checkbox', id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled};
104 }
105 throw new Error('Invalid item type:' + this._type);
106 }
107
108 /**
109 * @param {string} shortcut
110 */
111 setShortcut(shortcut) {
112 this._shortcut = shortcut;
113 }
Tim van der Lippe0830b3d2019-10-03 13:20:07114}
Blink Reformat4c46d092018-04-07 15:32:37115
116/**
117 * @unrestricted
118 */
Tim van der Lippe0830b3d2019-10-03 13:20:07119export class Section {
Blink Reformat4c46d092018-04-07 15:32:37120 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07121 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37122 */
123 constructor(contextMenu) {
124 this._contextMenu = contextMenu;
Tim van der Lippe0830b3d2019-10-03 13:20:07125 /** @type {!Array<!Item>} */
Blink Reformat4c46d092018-04-07 15:32:37126 this._items = [];
127 }
128
129 /**
130 * @param {string} label
131 * @param {function(?)} handler
132 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07133 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37134 */
135 appendItem(label, handler, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07136 const item = new Item(this._contextMenu, 'item', label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37137 this._items.push(item);
138 this._contextMenu._setHandler(item.id(), handler);
139 return item;
140 }
141
142 /**
143 * @param {!Element} element
Tim van der Lippe0830b3d2019-10-03 13:20:07144 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37145 */
146 appendCustomItem(element) {
Tim van der Lippe0830b3d2019-10-03 13:20:07147 const item = new Item(this._contextMenu, 'item', '<custom>');
Blink Reformat4c46d092018-04-07 15:32:37148 item._customElement = element;
149 this._items.push(item);
150 return item;
151 }
152
153 /**
Paul Lewis9950e182019-12-16 16:06:07154 * @return {!Item}
Jan Scheffler7dfab7e2019-10-23 11:25:15155 */
156 appendSeparator() {
Paul Lewis9950e182019-12-16 16:06:07157 const item = new Item(this._contextMenu, 'separator');
Jan Scheffler7dfab7e2019-10-23 11:25:15158 this._items.push(item);
159 return item;
160 }
161
162 /**
Blink Reformat4c46d092018-04-07 15:32:37163 * @param {string} actionId
164 * @param {string=} label
Pavel Feldmanf5b981a2018-11-30 03:42:08165 * @param {boolean=} optional
Blink Reformat4c46d092018-04-07 15:32:37166 */
Pavel Feldmanf5b981a2018-11-30 03:42:08167 appendAction(actionId, label, optional) {
Paul Lewis24cb7402020-01-24 13:46:35168 const action = self.UI.actionRegistry.action(actionId);
Blink Reformat4c46d092018-04-07 15:32:37169 if (!action) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34170 if (!optional) {
Pavel Feldmanf5b981a2018-11-30 03:42:08171 console.error(`Action ${actionId} was not defined`);
Tim van der Lippe1d6e57a2019-09-30 11:55:34172 }
Blink Reformat4c46d092018-04-07 15:32:37173 return;
174 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34175 if (!label) {
Blink Reformat4c46d092018-04-07 15:32:37176 label = action.title();
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 }
Blink Reformat4c46d092018-04-07 15:32:37178 const result = this.appendItem(label, action.execute.bind(action));
Paul Lewis05eb37f2020-01-24 14:31:40179 const shortcut = self.UI.shortcutRegistry.shortcutTitleForAction(actionId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34180 if (shortcut) {
Blink Reformat4c46d092018-04-07 15:32:37181 result.setShortcut(shortcut);
Tim van der Lippe1d6e57a2019-09-30 11:55:34182 }
Blink Reformat4c46d092018-04-07 15:32:37183 }
184
185 /**
186 * @param {string} label
187 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07188 * @return {!SubMenu}
Blink Reformat4c46d092018-04-07 15:32:37189 */
190 appendSubMenuItem(label, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07191 const item = new SubMenu(this._contextMenu, label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37192 item._init();
193 this._items.push(item);
194 return item;
195 }
196
197 /**
198 * @param {string} label
199 * @param {function()} handler
200 * @param {boolean=} checked
201 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07202 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37203 */
204 appendCheckboxItem(label, handler, checked, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07205 const item = new Item(this._contextMenu, 'checkbox', label, disabled, checked);
Blink Reformat4c46d092018-04-07 15:32:37206 this._items.push(item);
207 this._contextMenu._setHandler(item.id(), handler);
208 return item;
209 }
Tim van der Lippe0830b3d2019-10-03 13:20:07210}
Blink Reformat4c46d092018-04-07 15:32:37211
212/**
213 * @unrestricted
214 */
Brandon Goddardf7bccf22019-12-11 21:49:16215export class SubMenu extends Item {
Blink Reformat4c46d092018-04-07 15:32:37216 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07217 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37218 * @param {string=} label
219 * @param {boolean=} disabled
220 */
221 constructor(contextMenu, label, disabled) {
222 super(contextMenu, 'subMenu', label, disabled);
Tim van der Lippe0830b3d2019-10-03 13:20:07223 /** @type {!Map<string, !Section>} */
Blink Reformat4c46d092018-04-07 15:32:37224 this._sections = new Map();
Tim van der Lippe0830b3d2019-10-03 13:20:07225 /** @type {!Array<!Section>} */
Blink Reformat4c46d092018-04-07 15:32:37226 this._sectionList = [];
227 }
228
229 _init() {
Tim van der Lippe0830b3d2019-10-03 13:20:07230 _groupWeights.forEach(name => this.section(name));
Blink Reformat4c46d092018-04-07 15:32:37231 }
232
233 /**
234 * @param {string=} name
Tim van der Lippe0830b3d2019-10-03 13:20:07235 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37236 */
237 section(name) {
238 let section = name ? this._sections.get(name) : null;
239 if (!section) {
Tim van der Lippe0830b3d2019-10-03 13:20:07240 section = new Section(this._contextMenu);
Blink Reformat4c46d092018-04-07 15:32:37241 if (name) {
242 this._sections.set(name, section);
243 this._sectionList.push(section);
244 } else {
Tim van der Lippe0830b3d2019-10-03 13:20:07245 this._sectionList.splice(ContextMenu._groupWeights.indexOf('default'), 0, section);
Blink Reformat4c46d092018-04-07 15:32:37246 }
247 }
248 return section;
249 }
250
251 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07252 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37253 */
254 headerSection() {
255 return this.section('header');
256 }
257
258 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07259 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37260 */
261 newSection() {
262 return this.section('new');
263 }
264
265 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07266 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37267 */
268 revealSection() {
269 return this.section('reveal');
270 }
271
272 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07273 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37274 */
275 clipboardSection() {
276 return this.section('clipboard');
277 }
278
279 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07280 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37281 */
282 editSection() {
283 return this.section('edit');
284 }
285
286 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07287 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37288 */
289 debugSection() {
290 return this.section('debug');
291 }
292
293 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07294 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37295 */
296 viewSection() {
297 return this.section('view');
298 }
299
300 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07301 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37302 */
303 defaultSection() {
304 return this.section('default');
305 }
306
307 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07308 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37309 */
310 saveSection() {
311 return this.section('save');
312 }
313
314 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07315 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37316 */
317 footerSection() {
318 return this.section('footer');
319 }
320
321 /**
322 * @override
323 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
324 */
325 _buildDescriptor() {
326 /** @type {!InspectorFrontendHostAPI.ContextMenuDescriptor} */
327 const result = {type: 'subMenu', label: this._label, enabled: !this._disabled, subItems: []};
328
329 const nonEmptySections = this._sectionList.filter(section => !!section._items.length);
330 for (const section of nonEmptySections) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34331 for (const item of section._items) {
Blink Reformat4c46d092018-04-07 15:32:37332 result.subItems.push(item._buildDescriptor());
Tim van der Lippe1d6e57a2019-09-30 11:55:34333 }
334 if (section !== nonEmptySections.peekLast()) {
Blink Reformat4c46d092018-04-07 15:32:37335 result.subItems.push({type: 'separator'});
Tim van der Lippe1d6e57a2019-09-30 11:55:34336 }
Blink Reformat4c46d092018-04-07 15:32:37337 }
338 return result;
339 }
340
341 /**
342 * @param {string} location
343 */
344 appendItemsAtLocation(location) {
345 for (const extension of self.runtime.extensions('context-menu-item')) {
346 const itemLocation = extension.descriptor()['location'] || '';
Tim van der Lippe1d6e57a2019-09-30 11:55:34347 if (!itemLocation.startsWith(location + '/')) {
Blink Reformat4c46d092018-04-07 15:32:37348 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34349 }
Blink Reformat4c46d092018-04-07 15:32:37350
351 const section = itemLocation.substr(location.length + 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34352 if (!section || section.includes('/')) {
Blink Reformat4c46d092018-04-07 15:32:37353 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34354 }
Blink Reformat4c46d092018-04-07 15:32:37355
356 this.section(section).appendAction(extension.descriptor()['actionId']);
357 }
358 }
Tim van der Lippe0830b3d2019-10-03 13:20:07359}
Blink Reformat4c46d092018-04-07 15:32:37360
Tim van der Lippe0830b3d2019-10-03 13:20:07361Item._uniqueSectionName = 0;
Blink Reformat4c46d092018-04-07 15:32:37362
363/**
364 * @unrestricted
365 */
Paul Lewis9950e182019-12-16 16:06:07366export class ContextMenu extends SubMenu {
Blink Reformat4c46d092018-04-07 15:32:37367 /**
368 * @param {!Event} event
369 * @param {boolean=} useSoftMenu
370 * @param {number=} x
371 * @param {number=} y
372 */
373 constructor(event, useSoftMenu, x, y) {
374 super(null);
375 this._contextMenu = this;
376 super._init();
377 this._defaultSection = this.defaultSection();
Tim van der Lippe0830b3d2019-10-03 13:20:07378 /** @type {!Array.<!Promise.<!Array.<!Provider>>>} */
Blink Reformat4c46d092018-04-07 15:32:37379 this._pendingPromises = [];
380 /** @type {!Array<!Object>} */
381 this._pendingTargets = [];
382 this._event = event;
383 this._useSoftMenu = !!useSoftMenu;
384 this._x = x === undefined ? event.x : x;
385 this._y = y === undefined ? event.y : y;
386 this._handlers = {};
387 this._id = 0;
388
389 const target = event.deepElementFromPoint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34390 if (target) {
Blink Reformat4c46d092018-04-07 15:32:37391 this.appendApplicableItems(/** @type {!Object} */ (target));
Tim van der Lippe1d6e57a2019-09-30 11:55:34392 }
Blink Reformat4c46d092018-04-07 15:32:37393 }
394
395 static initialize() {
Paul Lewis0fd43712020-01-08 17:07:36396 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe50cfa9b2019-10-01 10:40:58397 Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu);
Blink Reformat4c46d092018-04-07 15:32:37398 /**
Tim van der Lippec02a97c2020-02-14 14:39:27399 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37400 */
401 function setUseSoftMenu(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07402 ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data);
Blink Reformat4c46d092018-04-07 15:32:37403 }
404 }
405
406 /**
407 * @param {!Document} doc
408 */
409 static installHandler(doc) {
410 doc.body.addEventListener('contextmenu', handler, false);
411
412 /**
413 * @param {!Event} event
414 */
415 function handler(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07416 const contextMenu = new ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37417 contextMenu.show();
418 }
419 }
420
421 /**
422 * @return {number}
423 */
424 _nextId() {
425 return this._id++;
426 }
427
428 show() {
429 Promise.all(this._pendingPromises).then(populate.bind(this)).then(this._innerShow.bind(this));
Tim van der Lippe0830b3d2019-10-03 13:20:07430 ContextMenu._pendingMenu = this;
Blink Reformat4c46d092018-04-07 15:32:37431
432 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07433 * @param {!Array.<!Array.<!Provider>>} appendCallResults
434 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37435 */
436 function populate(appendCallResults) {
Tim van der Lippe0830b3d2019-10-03 13:20:07437 if (ContextMenu._pendingMenu !== this) {
Blink Reformat4c46d092018-04-07 15:32:37438 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34439 }
Tim van der Lippe0830b3d2019-10-03 13:20:07440 delete ContextMenu._pendingMenu;
Blink Reformat4c46d092018-04-07 15:32:37441
442 for (let i = 0; i < appendCallResults.length; ++i) {
443 const providers = appendCallResults[i];
444 const target = this._pendingTargets[i];
445
446 for (let j = 0; j < providers.length; ++j) {
Tim van der Lippe0830b3d2019-10-03 13:20:07447 const provider = /** @type {!Provider} */ (providers[j]);
Blink Reformat4c46d092018-04-07 15:32:37448 provider.appendApplicableItems(this._event, this, target);
449 }
450 }
451
452 this._pendingPromises = [];
453 this._pendingTargets = [];
454 }
455
456 this._event.consume(true);
457 }
458
459 discard() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34460 if (this._softMenu) {
Blink Reformat4c46d092018-04-07 15:32:37461 this._softMenu.discard();
Tim van der Lippe1d6e57a2019-09-30 11:55:34462 }
Blink Reformat4c46d092018-04-07 15:32:37463 }
464
465 _innerShow() {
466 const menuObject = this._buildMenuDescriptors();
Paul Lewis0fd43712020-01-08 17:07:36467 if (this._useSoftMenu || ContextMenu._useSoftMenu ||
468 Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) {
Paul Lewis9950e182019-12-16 16:06:07469 this._softMenu = new SoftContextMenu(menuObject, this._itemSelected.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37470 this._softMenu.show(this._event.target.ownerDocument, new AnchorBox(this._x, this._y, 0, 0));
471 } else {
Paul Lewis0fd43712020-01-08 17:07:36472 Host.InspectorFrontendHost.InspectorFrontendHostInstance.showContextMenuAtPoint(
473 this._x, this._y, menuObject, this._event.target.ownerDocument);
Blink Reformat4c46d092018-04-07 15:32:37474
475 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07476 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37477 */
478 function listenToEvents() {
Paul Lewis0fd43712020-01-08 17:07:36479 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44480 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36481 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44482 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37483 }
484
485 // showContextMenuAtPoint call above synchronously issues a clear event for previous context menu (if any),
486 // so we skip it before subscribing to the clear event.
487 setImmediate(listenToEvents.bind(this));
488 }
489 }
490
491 /**
Brandon Goddard49ef6f12019-10-16 18:50:13492 * @param {number} x
493 */
494 setX(x) {
495 this._x = x;
496 }
497
498 /**
499 * @param {number} y
500 */
501 setY(y) {
502 this._y = y;
503 }
504
505 /**
Blink Reformat4c46d092018-04-07 15:32:37506 * @param {number} id
507 * @param {function(?)} handler
508 */
509 _setHandler(id, handler) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34510 if (handler) {
Blink Reformat4c46d092018-04-07 15:32:37511 this._handlers[id] = handler;
Tim van der Lippe1d6e57a2019-09-30 11:55:34512 }
Blink Reformat4c46d092018-04-07 15:32:37513 }
514
515 /**
516 * @return {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>}
517 */
518 _buildMenuDescriptors() {
519 return /** @type {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} */ (super._buildDescriptor().subItems);
520 }
521
522 /**
Tim van der Lippec02a97c2020-02-14 14:39:27523 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37524 */
525 _onItemSelected(event) {
526 this._itemSelected(/** @type {string} */ (event.data));
527 }
528
529 /**
530 * @param {string} id
531 */
532 _itemSelected(id) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34533 if (this._handlers[id]) {
Blink Reformat4c46d092018-04-07 15:32:37534 this._handlers[id].call(this);
Tim van der Lippe1d6e57a2019-09-30 11:55:34535 }
Blink Reformat4c46d092018-04-07 15:32:37536 this._menuCleared();
537 }
538
539 _menuCleared() {
Paul Lewis0fd43712020-01-08 17:07:36540 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44541 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36542 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44543 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37544 }
545
546 /**
547 * @param {!Object} target
Junyi Xiao6e3798d2019-09-23 19:12:27548 * @return {boolean}
549 */
550 containsTarget(target) {
551 return this._pendingTargets.indexOf(target) >= 0;
552 }
553
554 /**
555 * @param {!Object} target
Blink Reformat4c46d092018-04-07 15:32:37556 */
557 appendApplicableItems(target) {
Tim van der Lippe0830b3d2019-10-03 13:20:07558 this._pendingPromises.push(self.runtime.allInstances(Provider, target));
Blink Reformat4c46d092018-04-07 15:32:37559 this._pendingTargets.push(target);
560 }
Tim van der Lippe0830b3d2019-10-03 13:20:07561}
Blink Reformat4c46d092018-04-07 15:32:37562
Tim van der Lippe0830b3d2019-10-03 13:20:07563export const _groupWeights =
Blink Reformat4c46d092018-04-07 15:32:37564 ['header', 'new', 'reveal', 'edit', 'clipboard', 'debug', 'view', 'default', 'save', 'footer'];
565
566/**
567 * @interface
568 */
Tim van der Lippe0830b3d2019-10-03 13:20:07569export class Provider {
Blink Reformat4c46d092018-04-07 15:32:37570 /**
571 * @param {!Event} event
Tim van der Lippe0830b3d2019-10-03 13:20:07572 * @param {!ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37573 * @param {!Object} target
574 */
575 appendApplicableItems(event, contextMenu, target) {}
Tim van der Lippe0830b3d2019-10-03 13:20:07576}
577
Tim van der Lippe0830b3d2019-10-03 13:20:07578ContextMenu._groupWeights = _groupWeights;