blob: c85d9814516911c115905850675a70d4068fd76e [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 Lippeee97fa32020-04-23 15:20:5631// @ts-nocheck
32// TODO(crbug.com/1011811): Enable TypeScript compiler checks
33
Tim van der Lippec02a97c2020-02-14 14:39:2734import * as Common from '../common/common.js'; // eslint-disable-line no-unused-vars
Paul Lewis0fd43712020-01-08 17:07:3635import * as Host from '../host/host.js';
Tim van der Lippe7f2002c2020-09-28 14:54:0136import * as Root from '../root/root.js';
Tim van der Lippec02a97c2020-02-14 14:39:2737
Tim van der Lippe177c0b22020-08-19 14:56:0238import {ActionRegistry} from './ActionRegistry.js';
Tim van der Lippe9c9fb122020-09-08 15:06:1739import {ShortcutRegistry} from './ShortcutRegistry.js';
Paul Lewis9950e182019-12-16 16:06:0740import {SoftContextMenu} from './SoftContextMenu.js';
41
Blink Reformat4c46d092018-04-07 15:32:3742/**
43 * @unrestricted
44 */
Tim van der Lippe0830b3d2019-10-03 13:20:0745export class Item {
Blink Reformat4c46d092018-04-07 15:32:3746 /**
Tim van der Lippe0830b3d2019-10-03 13:20:0747 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:3748 * @param {string} type
49 * @param {string=} label
50 * @param {boolean=} disabled
51 * @param {boolean=} checked
52 */
53 constructor(contextMenu, type, label, disabled, checked) {
54 this._type = type;
55 this._label = label;
56 this._disabled = disabled;
57 this._checked = checked;
58 this._contextMenu = contextMenu;
Tim van der Lippe1d6e57a2019-09-30 11:55:3459 if (type === 'item' || type === 'checkbox') {
Blink Reformat4c46d092018-04-07 15:32:3760 this._id = contextMenu ? contextMenu._nextId() : 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:3461 }
Blink Reformat4c46d092018-04-07 15:32:3762 }
63
64 /**
65 * @return {number}
66 */
67 id() {
68 return this._id;
69 }
70
71 /**
72 * @return {string}
73 */
74 type() {
75 return this._type;
76 }
77
78 /**
79 * @return {boolean}
80 */
81 isEnabled() {
82 return !this._disabled;
83 }
84
85 /**
86 * @param {boolean} enabled
87 */
88 setEnabled(enabled) {
89 this._disabled = !enabled;
90 }
91
92 /**
93 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
94 */
95 _buildDescriptor() {
96 switch (this._type) {
Mathias Bynens88e8f152020-03-25 14:33:1297 case 'item': {
Blink Reformat4c46d092018-04-07 15:32:3798 const result = {type: 'item', id: this._id, label: this._label, enabled: !this._disabled};
Tim van der Lippe1d6e57a2019-09-30 11:55:3499 if (this._customElement) {
Blink Reformat4c46d092018-04-07 15:32:37100 result.element = this._customElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34101 }
102 if (this._shortcut) {
Blink Reformat4c46d092018-04-07 15:32:37103 result.shortcut = this._shortcut;
Tim van der Lippe1d6e57a2019-09-30 11:55:34104 }
Blink Reformat4c46d092018-04-07 15:32:37105 return result;
Mathias Bynens88e8f152020-03-25 14:33:12106 }
107 case 'separator': {
Blink Reformat4c46d092018-04-07 15:32:37108 return {type: 'separator'};
Mathias Bynens88e8f152020-03-25 14:33:12109 }
110 case 'checkbox': {
Blink Reformat4c46d092018-04-07 15:32:37111 return {type: 'checkbox', id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled};
Mathias Bynens88e8f152020-03-25 14:33:12112 }
Blink Reformat4c46d092018-04-07 15:32:37113 }
114 throw new Error('Invalid item type:' + this._type);
115 }
116
117 /**
118 * @param {string} shortcut
119 */
120 setShortcut(shortcut) {
121 this._shortcut = shortcut;
122 }
Tim van der Lippe0830b3d2019-10-03 13:20:07123}
Blink Reformat4c46d092018-04-07 15:32:37124
Sigurd Schneider46da7db2020-05-20 13:45:11125
Tim van der Lippe0830b3d2019-10-03 13:20:07126export class Section {
Blink Reformat4c46d092018-04-07 15:32:37127 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07128 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37129 */
130 constructor(contextMenu) {
131 this._contextMenu = contextMenu;
Tim van der Lippe0830b3d2019-10-03 13:20:07132 /** @type {!Array<!Item>} */
Blink Reformat4c46d092018-04-07 15:32:37133 this._items = [];
134 }
135
136 /**
137 * @param {string} label
Tim van der Lippe403a88d2020-05-13 11:51:32138 * @param {function(?):*} handler
Blink Reformat4c46d092018-04-07 15:32:37139 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07140 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37141 */
142 appendItem(label, handler, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07143 const item = new Item(this._contextMenu, 'item', label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37144 this._items.push(item);
145 this._contextMenu._setHandler(item.id(), handler);
146 return item;
147 }
148
149 /**
150 * @param {!Element} element
Tim van der Lippe0830b3d2019-10-03 13:20:07151 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37152 */
153 appendCustomItem(element) {
Tim van der Lippe0830b3d2019-10-03 13:20:07154 const item = new Item(this._contextMenu, 'item', '<custom>');
Blink Reformat4c46d092018-04-07 15:32:37155 item._customElement = element;
156 this._items.push(item);
157 return item;
158 }
159
160 /**
Paul Lewis9950e182019-12-16 16:06:07161 * @return {!Item}
Jan Scheffler7dfab7e2019-10-23 11:25:15162 */
163 appendSeparator() {
Paul Lewis9950e182019-12-16 16:06:07164 const item = new Item(this._contextMenu, 'separator');
Jan Scheffler7dfab7e2019-10-23 11:25:15165 this._items.push(item);
166 return item;
167 }
168
169 /**
Blink Reformat4c46d092018-04-07 15:32:37170 * @param {string} actionId
171 * @param {string=} label
Pavel Feldmanf5b981a2018-11-30 03:42:08172 * @param {boolean=} optional
Blink Reformat4c46d092018-04-07 15:32:37173 */
Pavel Feldmanf5b981a2018-11-30 03:42:08174 appendAction(actionId, label, optional) {
Tim van der Lippe177c0b22020-08-19 14:56:02175 const action = ActionRegistry.instance().action(actionId);
Blink Reformat4c46d092018-04-07 15:32:37176 if (!action) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 if (!optional) {
Pavel Feldmanf5b981a2018-11-30 03:42:08178 console.error(`Action ${actionId} was not defined`);
Tim van der Lippe1d6e57a2019-09-30 11:55:34179 }
Blink Reformat4c46d092018-04-07 15:32:37180 return;
181 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34182 if (!label) {
Blink Reformat4c46d092018-04-07 15:32:37183 label = action.title();
Tim van der Lippe1d6e57a2019-09-30 11:55:34184 }
Blink Reformat4c46d092018-04-07 15:32:37185 const result = this.appendItem(label, action.execute.bind(action));
Tim van der Lippe9c9fb122020-09-08 15:06:17186 const shortcut = ShortcutRegistry.instance().shortcutTitleForAction(actionId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34187 if (shortcut) {
Blink Reformat4c46d092018-04-07 15:32:37188 result.setShortcut(shortcut);
Tim van der Lippe1d6e57a2019-09-30 11:55:34189 }
Blink Reformat4c46d092018-04-07 15:32:37190 }
191
192 /**
193 * @param {string} label
194 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07195 * @return {!SubMenu}
Blink Reformat4c46d092018-04-07 15:32:37196 */
197 appendSubMenuItem(label, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07198 const item = new SubMenu(this._contextMenu, label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37199 item._init();
200 this._items.push(item);
201 return item;
202 }
203
204 /**
205 * @param {string} label
Tim van der Lippe403a88d2020-05-13 11:51:32206 * @param {function():*} handler
Blink Reformat4c46d092018-04-07 15:32:37207 * @param {boolean=} checked
208 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07209 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37210 */
211 appendCheckboxItem(label, handler, checked, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07212 const item = new Item(this._contextMenu, 'checkbox', label, disabled, checked);
Blink Reformat4c46d092018-04-07 15:32:37213 this._items.push(item);
214 this._contextMenu._setHandler(item.id(), handler);
215 return item;
216 }
Tim van der Lippe0830b3d2019-10-03 13:20:07217}
Blink Reformat4c46d092018-04-07 15:32:37218
Sigurd Schneider46da7db2020-05-20 13:45:11219
Brandon Goddardf7bccf22019-12-11 21:49:16220export class SubMenu extends Item {
Blink Reformat4c46d092018-04-07 15:32:37221 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07222 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37223 * @param {string=} label
224 * @param {boolean=} disabled
225 */
226 constructor(contextMenu, label, disabled) {
227 super(contextMenu, 'subMenu', label, disabled);
Tim van der Lippe0830b3d2019-10-03 13:20:07228 /** @type {!Map<string, !Section>} */
Blink Reformat4c46d092018-04-07 15:32:37229 this._sections = new Map();
Tim van der Lippe0830b3d2019-10-03 13:20:07230 /** @type {!Array<!Section>} */
Blink Reformat4c46d092018-04-07 15:32:37231 this._sectionList = [];
232 }
233
234 _init() {
Tim van der Lippe0830b3d2019-10-03 13:20:07235 _groupWeights.forEach(name => this.section(name));
Blink Reformat4c46d092018-04-07 15:32:37236 }
237
238 /**
239 * @param {string=} name
Tim van der Lippe0830b3d2019-10-03 13:20:07240 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37241 */
242 section(name) {
243 let section = name ? this._sections.get(name) : null;
244 if (!section) {
Tim van der Lippe0830b3d2019-10-03 13:20:07245 section = new Section(this._contextMenu);
Blink Reformat4c46d092018-04-07 15:32:37246 if (name) {
247 this._sections.set(name, section);
248 this._sectionList.push(section);
249 } else {
Tim van der Lippe0830b3d2019-10-03 13:20:07250 this._sectionList.splice(ContextMenu._groupWeights.indexOf('default'), 0, section);
Blink Reformat4c46d092018-04-07 15:32:37251 }
252 }
253 return section;
254 }
255
256 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07257 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37258 */
259 headerSection() {
260 return this.section('header');
261 }
262
263 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07264 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37265 */
266 newSection() {
267 return this.section('new');
268 }
269
270 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07271 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37272 */
273 revealSection() {
274 return this.section('reveal');
275 }
276
277 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07278 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37279 */
280 clipboardSection() {
281 return this.section('clipboard');
282 }
283
284 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07285 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37286 */
287 editSection() {
288 return this.section('edit');
289 }
290
291 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07292 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37293 */
294 debugSection() {
295 return this.section('debug');
296 }
297
298 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07299 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37300 */
301 viewSection() {
302 return this.section('view');
303 }
304
305 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07306 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37307 */
308 defaultSection() {
309 return this.section('default');
310 }
311
312 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07313 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37314 */
315 saveSection() {
316 return this.section('save');
317 }
318
319 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07320 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37321 */
322 footerSection() {
323 return this.section('footer');
324 }
325
326 /**
327 * @override
328 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
329 */
330 _buildDescriptor() {
331 /** @type {!InspectorFrontendHostAPI.ContextMenuDescriptor} */
332 const result = {type: 'subMenu', label: this._label, enabled: !this._disabled, subItems: []};
333
334 const nonEmptySections = this._sectionList.filter(section => !!section._items.length);
335 for (const section of nonEmptySections) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34336 for (const item of section._items) {
Blink Reformat4c46d092018-04-07 15:32:37337 result.subItems.push(item._buildDescriptor());
Tim van der Lippe1d6e57a2019-09-30 11:55:34338 }
339 if (section !== nonEmptySections.peekLast()) {
Blink Reformat4c46d092018-04-07 15:32:37340 result.subItems.push({type: 'separator'});
Tim van der Lippe1d6e57a2019-09-30 11:55:34341 }
Blink Reformat4c46d092018-04-07 15:32:37342 }
343 return result;
344 }
345
346 /**
347 * @param {string} location
348 */
349 appendItemsAtLocation(location) {
Tim van der Lippe7f2002c2020-09-28 14:54:01350 for (const extension of Root.Runtime.Runtime.instance().extensions('context-menu-item')) {
Blink Reformat4c46d092018-04-07 15:32:37351 const itemLocation = extension.descriptor()['location'] || '';
Tim van der Lippe1d6e57a2019-09-30 11:55:34352 if (!itemLocation.startsWith(location + '/')) {
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 const section = itemLocation.substr(location.length + 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34357 if (!section || section.includes('/')) {
Blink Reformat4c46d092018-04-07 15:32:37358 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34359 }
Blink Reformat4c46d092018-04-07 15:32:37360
361 this.section(section).appendAction(extension.descriptor()['actionId']);
362 }
363 }
Tim van der Lippe0830b3d2019-10-03 13:20:07364}
Blink Reformat4c46d092018-04-07 15:32:37365
Tim van der Lippe0830b3d2019-10-03 13:20:07366Item._uniqueSectionName = 0;
Blink Reformat4c46d092018-04-07 15:32:37367
368/**
369 * @unrestricted
370 */
Paul Lewis9950e182019-12-16 16:06:07371export class ContextMenu extends SubMenu {
Blink Reformat4c46d092018-04-07 15:32:37372 /**
373 * @param {!Event} event
374 * @param {boolean=} useSoftMenu
375 * @param {number=} x
376 * @param {number=} y
377 */
378 constructor(event, useSoftMenu, x, y) {
379 super(null);
380 this._contextMenu = this;
381 super._init();
382 this._defaultSection = this.defaultSection();
Tim van der Lippe0830b3d2019-10-03 13:20:07383 /** @type {!Array.<!Promise.<!Array.<!Provider>>>} */
Blink Reformat4c46d092018-04-07 15:32:37384 this._pendingPromises = [];
385 /** @type {!Array<!Object>} */
386 this._pendingTargets = [];
387 this._event = event;
388 this._useSoftMenu = !!useSoftMenu;
389 this._x = x === undefined ? event.x : x;
390 this._y = y === undefined ? event.y : y;
391 this._handlers = {};
392 this._id = 0;
393
394 const target = event.deepElementFromPoint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34395 if (target) {
Blink Reformat4c46d092018-04-07 15:32:37396 this.appendApplicableItems(/** @type {!Object} */ (target));
Tim van der Lippe1d6e57a2019-09-30 11:55:34397 }
Blink Reformat4c46d092018-04-07 15:32:37398 }
399
400 static initialize() {
Paul Lewis0fd43712020-01-08 17:07:36401 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe50cfa9b2019-10-01 10:40:58402 Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu);
Blink Reformat4c46d092018-04-07 15:32:37403 /**
Tim van der Lippec02a97c2020-02-14 14:39:27404 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37405 */
406 function setUseSoftMenu(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07407 ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data);
Blink Reformat4c46d092018-04-07 15:32:37408 }
409 }
410
411 /**
412 * @param {!Document} doc
413 */
414 static installHandler(doc) {
415 doc.body.addEventListener('contextmenu', handler, false);
416
417 /**
418 * @param {!Event} event
419 */
420 function handler(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07421 const contextMenu = new ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37422 contextMenu.show();
423 }
424 }
425
426 /**
427 * @return {number}
428 */
429 _nextId() {
430 return this._id++;
431 }
432
433 show() {
434 Promise.all(this._pendingPromises).then(populate.bind(this)).then(this._innerShow.bind(this));
Tim van der Lippe0830b3d2019-10-03 13:20:07435 ContextMenu._pendingMenu = this;
Blink Reformat4c46d092018-04-07 15:32:37436
437 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07438 * @param {!Array.<!Array.<!Provider>>} appendCallResults
439 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37440 */
441 function populate(appendCallResults) {
Tim van der Lippe0830b3d2019-10-03 13:20:07442 if (ContextMenu._pendingMenu !== this) {
Blink Reformat4c46d092018-04-07 15:32:37443 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34444 }
Tim van der Lippe0830b3d2019-10-03 13:20:07445 delete ContextMenu._pendingMenu;
Blink Reformat4c46d092018-04-07 15:32:37446
447 for (let i = 0; i < appendCallResults.length; ++i) {
448 const providers = appendCallResults[i];
449 const target = this._pendingTargets[i];
450
451 for (let j = 0; j < providers.length; ++j) {
Tim van der Lippe0830b3d2019-10-03 13:20:07452 const provider = /** @type {!Provider} */ (providers[j]);
Blink Reformat4c46d092018-04-07 15:32:37453 provider.appendApplicableItems(this._event, this, target);
454 }
455 }
456
457 this._pendingPromises = [];
458 this._pendingTargets = [];
459 }
460
461 this._event.consume(true);
462 }
463
464 discard() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34465 if (this._softMenu) {
Blink Reformat4c46d092018-04-07 15:32:37466 this._softMenu.discard();
Tim van der Lippe1d6e57a2019-09-30 11:55:34467 }
Blink Reformat4c46d092018-04-07 15:32:37468 }
469
470 _innerShow() {
471 const menuObject = this._buildMenuDescriptors();
Paul Lewis0fd43712020-01-08 17:07:36472 if (this._useSoftMenu || ContextMenu._useSoftMenu ||
473 Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) {
Paul Lewis9950e182019-12-16 16:06:07474 this._softMenu = new SoftContextMenu(menuObject, this._itemSelected.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37475 this._softMenu.show(this._event.target.ownerDocument, new AnchorBox(this._x, this._y, 0, 0));
476 } else {
Paul Lewis0fd43712020-01-08 17:07:36477 Host.InspectorFrontendHost.InspectorFrontendHostInstance.showContextMenuAtPoint(
478 this._x, this._y, menuObject, this._event.target.ownerDocument);
Blink Reformat4c46d092018-04-07 15:32:37479
480 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07481 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37482 */
483 function listenToEvents() {
Paul Lewis0fd43712020-01-08 17:07:36484 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44485 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36486 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44487 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37488 }
489
490 // showContextMenuAtPoint call above synchronously issues a clear event for previous context menu (if any),
491 // so we skip it before subscribing to the clear event.
492 setImmediate(listenToEvents.bind(this));
493 }
494 }
495
496 /**
Brandon Goddard49ef6f12019-10-16 18:50:13497 * @param {number} x
498 */
499 setX(x) {
500 this._x = x;
501 }
502
503 /**
504 * @param {number} y
505 */
506 setY(y) {
507 this._y = y;
508 }
509
510 /**
Blink Reformat4c46d092018-04-07 15:32:37511 * @param {number} id
Tim van der Lippe403a88d2020-05-13 11:51:32512 * @param {function(?):*} handler
Blink Reformat4c46d092018-04-07 15:32:37513 */
514 _setHandler(id, handler) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34515 if (handler) {
Blink Reformat4c46d092018-04-07 15:32:37516 this._handlers[id] = handler;
Tim van der Lippe1d6e57a2019-09-30 11:55:34517 }
Blink Reformat4c46d092018-04-07 15:32:37518 }
519
520 /**
521 * @return {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>}
522 */
523 _buildMenuDescriptors() {
524 return /** @type {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} */ (super._buildDescriptor().subItems);
525 }
526
527 /**
Tim van der Lippec02a97c2020-02-14 14:39:27528 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37529 */
530 _onItemSelected(event) {
531 this._itemSelected(/** @type {string} */ (event.data));
532 }
533
534 /**
535 * @param {string} id
536 */
537 _itemSelected(id) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34538 if (this._handlers[id]) {
Blink Reformat4c46d092018-04-07 15:32:37539 this._handlers[id].call(this);
Tim van der Lippe1d6e57a2019-09-30 11:55:34540 }
Blink Reformat4c46d092018-04-07 15:32:37541 this._menuCleared();
542 }
543
544 _menuCleared() {
Paul Lewis0fd43712020-01-08 17:07:36545 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44546 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36547 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44548 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37549 }
550
551 /**
552 * @param {!Object} target
Junyi Xiao6e3798d2019-09-23 19:12:27553 * @return {boolean}
554 */
555 containsTarget(target) {
556 return this._pendingTargets.indexOf(target) >= 0;
557 }
558
559 /**
560 * @param {!Object} target
Blink Reformat4c46d092018-04-07 15:32:37561 */
562 appendApplicableItems(target) {
Tim van der Lippe7f2002c2020-09-28 14:54:01563 this._pendingPromises.push(Root.Runtime.Runtime.instance().allInstances(Provider, target));
Blink Reformat4c46d092018-04-07 15:32:37564 this._pendingTargets.push(target);
565 }
Tim van der Lippe0830b3d2019-10-03 13:20:07566}
Blink Reformat4c46d092018-04-07 15:32:37567
Tim van der Lippe0830b3d2019-10-03 13:20:07568export const _groupWeights =
Blink Reformat4c46d092018-04-07 15:32:37569 ['header', 'new', 'reveal', 'edit', 'clipboard', 'debug', 'view', 'default', 'save', 'footer'];
570
571/**
572 * @interface
573 */
Tim van der Lippe0830b3d2019-10-03 13:20:07574export class Provider {
Blink Reformat4c46d092018-04-07 15:32:37575 /**
576 * @param {!Event} event
Tim van der Lippe0830b3d2019-10-03 13:20:07577 * @param {!ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37578 * @param {!Object} target
579 */
580 appendApplicableItems(event, contextMenu, target) {}
Tim van der Lippe0830b3d2019-10-03 13:20:07581}
582
Tim van der Lippe0830b3d2019-10-03 13:20:07583ContextMenu._groupWeights = _groupWeights;