blob: 2f03b24500d30a3927c7075fe10445ca648d0687 [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 Lippec02a97c2020-02-14 14:39:2736
Tim van der Lippe177c0b22020-08-19 14:56:0237import {ActionRegistry} from './ActionRegistry.js';
Tim van der Lippe9c9fb122020-09-08 15:06:1738import {ShortcutRegistry} from './ShortcutRegistry.js';
Paul Lewis9950e182019-12-16 16:06:0739import {SoftContextMenu} from './SoftContextMenu.js';
40
Blink Reformat4c46d092018-04-07 15:32:3741/**
42 * @unrestricted
43 */
Tim van der Lippe0830b3d2019-10-03 13:20:0744export class Item {
Blink Reformat4c46d092018-04-07 15:32:3745 /**
Tim van der Lippe0830b3d2019-10-03 13:20:0746 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:3747 * @param {string} type
48 * @param {string=} label
49 * @param {boolean=} disabled
50 * @param {boolean=} checked
51 */
52 constructor(contextMenu, type, label, disabled, checked) {
53 this._type = type;
54 this._label = label;
55 this._disabled = disabled;
56 this._checked = checked;
57 this._contextMenu = contextMenu;
Tim van der Lippe1d6e57a2019-09-30 11:55:3458 if (type === 'item' || type === 'checkbox') {
Blink Reformat4c46d092018-04-07 15:32:3759 this._id = contextMenu ? contextMenu._nextId() : 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:3460 }
Blink Reformat4c46d092018-04-07 15:32:3761 }
62
63 /**
64 * @return {number}
65 */
66 id() {
67 return this._id;
68 }
69
70 /**
71 * @return {string}
72 */
73 type() {
74 return this._type;
75 }
76
77 /**
78 * @return {boolean}
79 */
80 isEnabled() {
81 return !this._disabled;
82 }
83
84 /**
85 * @param {boolean} enabled
86 */
87 setEnabled(enabled) {
88 this._disabled = !enabled;
89 }
90
91 /**
92 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
93 */
94 _buildDescriptor() {
95 switch (this._type) {
Mathias Bynens88e8f152020-03-25 14:33:1296 case 'item': {
Blink Reformat4c46d092018-04-07 15:32:3797 const result = {type: 'item', id: this._id, label: this._label, enabled: !this._disabled};
Tim van der Lippe1d6e57a2019-09-30 11:55:3498 if (this._customElement) {
Blink Reformat4c46d092018-04-07 15:32:3799 result.element = this._customElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:34100 }
101 if (this._shortcut) {
Blink Reformat4c46d092018-04-07 15:32:37102 result.shortcut = this._shortcut;
Tim van der Lippe1d6e57a2019-09-30 11:55:34103 }
Blink Reformat4c46d092018-04-07 15:32:37104 return result;
Mathias Bynens88e8f152020-03-25 14:33:12105 }
106 case 'separator': {
Blink Reformat4c46d092018-04-07 15:32:37107 return {type: 'separator'};
Mathias Bynens88e8f152020-03-25 14:33:12108 }
109 case 'checkbox': {
Blink Reformat4c46d092018-04-07 15:32:37110 return {type: 'checkbox', id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled};
Mathias Bynens88e8f152020-03-25 14:33:12111 }
Blink Reformat4c46d092018-04-07 15:32:37112 }
113 throw new Error('Invalid item type:' + this._type);
114 }
115
116 /**
117 * @param {string} shortcut
118 */
119 setShortcut(shortcut) {
120 this._shortcut = shortcut;
121 }
Tim van der Lippe0830b3d2019-10-03 13:20:07122}
Blink Reformat4c46d092018-04-07 15:32:37123
Sigurd Schneider46da7db2020-05-20 13:45:11124
Tim van der Lippe0830b3d2019-10-03 13:20:07125export class Section {
Blink Reformat4c46d092018-04-07 15:32:37126 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07127 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37128 */
129 constructor(contextMenu) {
130 this._contextMenu = contextMenu;
Tim van der Lippe0830b3d2019-10-03 13:20:07131 /** @type {!Array<!Item>} */
Blink Reformat4c46d092018-04-07 15:32:37132 this._items = [];
133 }
134
135 /**
136 * @param {string} label
Tim van der Lippe403a88d2020-05-13 11:51:32137 * @param {function(?):*} handler
Blink Reformat4c46d092018-04-07 15:32:37138 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07139 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37140 */
141 appendItem(label, handler, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07142 const item = new Item(this._contextMenu, 'item', label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37143 this._items.push(item);
144 this._contextMenu._setHandler(item.id(), handler);
145 return item;
146 }
147
148 /**
149 * @param {!Element} element
Tim van der Lippe0830b3d2019-10-03 13:20:07150 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37151 */
152 appendCustomItem(element) {
Tim van der Lippe0830b3d2019-10-03 13:20:07153 const item = new Item(this._contextMenu, 'item', '<custom>');
Blink Reformat4c46d092018-04-07 15:32:37154 item._customElement = element;
155 this._items.push(item);
156 return item;
157 }
158
159 /**
Paul Lewis9950e182019-12-16 16:06:07160 * @return {!Item}
Jan Scheffler7dfab7e2019-10-23 11:25:15161 */
162 appendSeparator() {
Paul Lewis9950e182019-12-16 16:06:07163 const item = new Item(this._contextMenu, 'separator');
Jan Scheffler7dfab7e2019-10-23 11:25:15164 this._items.push(item);
165 return item;
166 }
167
168 /**
Blink Reformat4c46d092018-04-07 15:32:37169 * @param {string} actionId
170 * @param {string=} label
Pavel Feldmanf5b981a2018-11-30 03:42:08171 * @param {boolean=} optional
Blink Reformat4c46d092018-04-07 15:32:37172 */
Pavel Feldmanf5b981a2018-11-30 03:42:08173 appendAction(actionId, label, optional) {
Tim van der Lippe177c0b22020-08-19 14:56:02174 const action = ActionRegistry.instance().action(actionId);
Blink Reformat4c46d092018-04-07 15:32:37175 if (!action) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34176 if (!optional) {
Pavel Feldmanf5b981a2018-11-30 03:42:08177 console.error(`Action ${actionId} was not defined`);
Tim van der Lippe1d6e57a2019-09-30 11:55:34178 }
Blink Reformat4c46d092018-04-07 15:32:37179 return;
180 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34181 if (!label) {
Blink Reformat4c46d092018-04-07 15:32:37182 label = action.title();
Tim van der Lippe1d6e57a2019-09-30 11:55:34183 }
Blink Reformat4c46d092018-04-07 15:32:37184 const result = this.appendItem(label, action.execute.bind(action));
Tim van der Lippe9c9fb122020-09-08 15:06:17185 const shortcut = ShortcutRegistry.instance().shortcutTitleForAction(actionId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34186 if (shortcut) {
Blink Reformat4c46d092018-04-07 15:32:37187 result.setShortcut(shortcut);
Tim van der Lippe1d6e57a2019-09-30 11:55:34188 }
Blink Reformat4c46d092018-04-07 15:32:37189 }
190
191 /**
192 * @param {string} label
193 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07194 * @return {!SubMenu}
Blink Reformat4c46d092018-04-07 15:32:37195 */
196 appendSubMenuItem(label, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07197 const item = new SubMenu(this._contextMenu, label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37198 item._init();
199 this._items.push(item);
200 return item;
201 }
202
203 /**
204 * @param {string} label
Tim van der Lippe403a88d2020-05-13 11:51:32205 * @param {function():*} handler
Blink Reformat4c46d092018-04-07 15:32:37206 * @param {boolean=} checked
207 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07208 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37209 */
210 appendCheckboxItem(label, handler, checked, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07211 const item = new Item(this._contextMenu, 'checkbox', label, disabled, checked);
Blink Reformat4c46d092018-04-07 15:32:37212 this._items.push(item);
213 this._contextMenu._setHandler(item.id(), handler);
214 return item;
215 }
Tim van der Lippe0830b3d2019-10-03 13:20:07216}
Blink Reformat4c46d092018-04-07 15:32:37217
Sigurd Schneider46da7db2020-05-20 13:45:11218
Brandon Goddardf7bccf22019-12-11 21:49:16219export class SubMenu extends Item {
Blink Reformat4c46d092018-04-07 15:32:37220 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07221 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37222 * @param {string=} label
223 * @param {boolean=} disabled
224 */
225 constructor(contextMenu, label, disabled) {
226 super(contextMenu, 'subMenu', label, disabled);
Tim van der Lippe0830b3d2019-10-03 13:20:07227 /** @type {!Map<string, !Section>} */
Blink Reformat4c46d092018-04-07 15:32:37228 this._sections = new Map();
Tim van der Lippe0830b3d2019-10-03 13:20:07229 /** @type {!Array<!Section>} */
Blink Reformat4c46d092018-04-07 15:32:37230 this._sectionList = [];
231 }
232
233 _init() {
Tim van der Lippe0830b3d2019-10-03 13:20:07234 _groupWeights.forEach(name => this.section(name));
Blink Reformat4c46d092018-04-07 15:32:37235 }
236
237 /**
238 * @param {string=} name
Tim van der Lippe0830b3d2019-10-03 13:20:07239 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37240 */
241 section(name) {
242 let section = name ? this._sections.get(name) : null;
243 if (!section) {
Tim van der Lippe0830b3d2019-10-03 13:20:07244 section = new Section(this._contextMenu);
Blink Reformat4c46d092018-04-07 15:32:37245 if (name) {
246 this._sections.set(name, section);
247 this._sectionList.push(section);
248 } else {
Tim van der Lippe0830b3d2019-10-03 13:20:07249 this._sectionList.splice(ContextMenu._groupWeights.indexOf('default'), 0, section);
Blink Reformat4c46d092018-04-07 15:32:37250 }
251 }
252 return section;
253 }
254
255 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07256 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37257 */
258 headerSection() {
259 return this.section('header');
260 }
261
262 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07263 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37264 */
265 newSection() {
266 return this.section('new');
267 }
268
269 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07270 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37271 */
272 revealSection() {
273 return this.section('reveal');
274 }
275
276 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07277 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37278 */
279 clipboardSection() {
280 return this.section('clipboard');
281 }
282
283 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07284 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37285 */
286 editSection() {
287 return this.section('edit');
288 }
289
290 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07291 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37292 */
293 debugSection() {
294 return this.section('debug');
295 }
296
297 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07298 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37299 */
300 viewSection() {
301 return this.section('view');
302 }
303
304 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07305 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37306 */
307 defaultSection() {
308 return this.section('default');
309 }
310
311 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07312 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37313 */
314 saveSection() {
315 return this.section('save');
316 }
317
318 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07319 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37320 */
321 footerSection() {
322 return this.section('footer');
323 }
324
325 /**
326 * @override
327 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
328 */
329 _buildDescriptor() {
330 /** @type {!InspectorFrontendHostAPI.ContextMenuDescriptor} */
331 const result = {type: 'subMenu', label: this._label, enabled: !this._disabled, subItems: []};
332
333 const nonEmptySections = this._sectionList.filter(section => !!section._items.length);
334 for (const section of nonEmptySections) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34335 for (const item of section._items) {
Blink Reformat4c46d092018-04-07 15:32:37336 result.subItems.push(item._buildDescriptor());
Tim van der Lippe1d6e57a2019-09-30 11:55:34337 }
338 if (section !== nonEmptySections.peekLast()) {
Blink Reformat4c46d092018-04-07 15:32:37339 result.subItems.push({type: 'separator'});
Tim van der Lippe1d6e57a2019-09-30 11:55:34340 }
Blink Reformat4c46d092018-04-07 15:32:37341 }
342 return result;
343 }
344
345 /**
346 * @param {string} location
347 */
348 appendItemsAtLocation(location) {
349 for (const extension of self.runtime.extensions('context-menu-item')) {
350 const itemLocation = extension.descriptor()['location'] || '';
Tim van der Lippe1d6e57a2019-09-30 11:55:34351 if (!itemLocation.startsWith(location + '/')) {
Blink Reformat4c46d092018-04-07 15:32:37352 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34353 }
Blink Reformat4c46d092018-04-07 15:32:37354
355 const section = itemLocation.substr(location.length + 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34356 if (!section || section.includes('/')) {
Blink Reformat4c46d092018-04-07 15:32:37357 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34358 }
Blink Reformat4c46d092018-04-07 15:32:37359
360 this.section(section).appendAction(extension.descriptor()['actionId']);
361 }
362 }
Tim van der Lippe0830b3d2019-10-03 13:20:07363}
Blink Reformat4c46d092018-04-07 15:32:37364
Tim van der Lippe0830b3d2019-10-03 13:20:07365Item._uniqueSectionName = 0;
Blink Reformat4c46d092018-04-07 15:32:37366
367/**
368 * @unrestricted
369 */
Paul Lewis9950e182019-12-16 16:06:07370export class ContextMenu extends SubMenu {
Blink Reformat4c46d092018-04-07 15:32:37371 /**
372 * @param {!Event} event
373 * @param {boolean=} useSoftMenu
374 * @param {number=} x
375 * @param {number=} y
376 */
377 constructor(event, useSoftMenu, x, y) {
378 super(null);
379 this._contextMenu = this;
380 super._init();
381 this._defaultSection = this.defaultSection();
Tim van der Lippe0830b3d2019-10-03 13:20:07382 /** @type {!Array.<!Promise.<!Array.<!Provider>>>} */
Blink Reformat4c46d092018-04-07 15:32:37383 this._pendingPromises = [];
384 /** @type {!Array<!Object>} */
385 this._pendingTargets = [];
386 this._event = event;
387 this._useSoftMenu = !!useSoftMenu;
388 this._x = x === undefined ? event.x : x;
389 this._y = y === undefined ? event.y : y;
390 this._handlers = {};
391 this._id = 0;
392
393 const target = event.deepElementFromPoint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34394 if (target) {
Blink Reformat4c46d092018-04-07 15:32:37395 this.appendApplicableItems(/** @type {!Object} */ (target));
Tim van der Lippe1d6e57a2019-09-30 11:55:34396 }
Blink Reformat4c46d092018-04-07 15:32:37397 }
398
399 static initialize() {
Paul Lewis0fd43712020-01-08 17:07:36400 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe50cfa9b2019-10-01 10:40:58401 Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu);
Blink Reformat4c46d092018-04-07 15:32:37402 /**
Tim van der Lippec02a97c2020-02-14 14:39:27403 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37404 */
405 function setUseSoftMenu(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07406 ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data);
Blink Reformat4c46d092018-04-07 15:32:37407 }
408 }
409
410 /**
411 * @param {!Document} doc
412 */
413 static installHandler(doc) {
414 doc.body.addEventListener('contextmenu', handler, false);
415
416 /**
417 * @param {!Event} event
418 */
419 function handler(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07420 const contextMenu = new ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37421 contextMenu.show();
422 }
423 }
424
425 /**
426 * @return {number}
427 */
428 _nextId() {
429 return this._id++;
430 }
431
432 show() {
433 Promise.all(this._pendingPromises).then(populate.bind(this)).then(this._innerShow.bind(this));
Tim van der Lippe0830b3d2019-10-03 13:20:07434 ContextMenu._pendingMenu = this;
Blink Reformat4c46d092018-04-07 15:32:37435
436 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07437 * @param {!Array.<!Array.<!Provider>>} appendCallResults
438 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37439 */
440 function populate(appendCallResults) {
Tim van der Lippe0830b3d2019-10-03 13:20:07441 if (ContextMenu._pendingMenu !== this) {
Blink Reformat4c46d092018-04-07 15:32:37442 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34443 }
Tim van der Lippe0830b3d2019-10-03 13:20:07444 delete ContextMenu._pendingMenu;
Blink Reformat4c46d092018-04-07 15:32:37445
446 for (let i = 0; i < appendCallResults.length; ++i) {
447 const providers = appendCallResults[i];
448 const target = this._pendingTargets[i];
449
450 for (let j = 0; j < providers.length; ++j) {
Tim van der Lippe0830b3d2019-10-03 13:20:07451 const provider = /** @type {!Provider} */ (providers[j]);
Blink Reformat4c46d092018-04-07 15:32:37452 provider.appendApplicableItems(this._event, this, target);
453 }
454 }
455
456 this._pendingPromises = [];
457 this._pendingTargets = [];
458 }
459
460 this._event.consume(true);
461 }
462
463 discard() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34464 if (this._softMenu) {
Blink Reformat4c46d092018-04-07 15:32:37465 this._softMenu.discard();
Tim van der Lippe1d6e57a2019-09-30 11:55:34466 }
Blink Reformat4c46d092018-04-07 15:32:37467 }
468
469 _innerShow() {
470 const menuObject = this._buildMenuDescriptors();
Paul Lewis0fd43712020-01-08 17:07:36471 if (this._useSoftMenu || ContextMenu._useSoftMenu ||
472 Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) {
Paul Lewis9950e182019-12-16 16:06:07473 this._softMenu = new SoftContextMenu(menuObject, this._itemSelected.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37474 this._softMenu.show(this._event.target.ownerDocument, new AnchorBox(this._x, this._y, 0, 0));
475 } else {
Paul Lewis0fd43712020-01-08 17:07:36476 Host.InspectorFrontendHost.InspectorFrontendHostInstance.showContextMenuAtPoint(
477 this._x, this._y, menuObject, this._event.target.ownerDocument);
Blink Reformat4c46d092018-04-07 15:32:37478
479 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07480 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37481 */
482 function listenToEvents() {
Paul Lewis0fd43712020-01-08 17:07:36483 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44484 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36485 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44486 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37487 }
488
489 // showContextMenuAtPoint call above synchronously issues a clear event for previous context menu (if any),
490 // so we skip it before subscribing to the clear event.
491 setImmediate(listenToEvents.bind(this));
492 }
493 }
494
495 /**
Brandon Goddard49ef6f12019-10-16 18:50:13496 * @param {number} x
497 */
498 setX(x) {
499 this._x = x;
500 }
501
502 /**
503 * @param {number} y
504 */
505 setY(y) {
506 this._y = y;
507 }
508
509 /**
Blink Reformat4c46d092018-04-07 15:32:37510 * @param {number} id
Tim van der Lippe403a88d2020-05-13 11:51:32511 * @param {function(?):*} handler
Blink Reformat4c46d092018-04-07 15:32:37512 */
513 _setHandler(id, handler) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34514 if (handler) {
Blink Reformat4c46d092018-04-07 15:32:37515 this._handlers[id] = handler;
Tim van der Lippe1d6e57a2019-09-30 11:55:34516 }
Blink Reformat4c46d092018-04-07 15:32:37517 }
518
519 /**
520 * @return {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>}
521 */
522 _buildMenuDescriptors() {
523 return /** @type {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} */ (super._buildDescriptor().subItems);
524 }
525
526 /**
Tim van der Lippec02a97c2020-02-14 14:39:27527 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37528 */
529 _onItemSelected(event) {
530 this._itemSelected(/** @type {string} */ (event.data));
531 }
532
533 /**
534 * @param {string} id
535 */
536 _itemSelected(id) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34537 if (this._handlers[id]) {
Blink Reformat4c46d092018-04-07 15:32:37538 this._handlers[id].call(this);
Tim van der Lippe1d6e57a2019-09-30 11:55:34539 }
Blink Reformat4c46d092018-04-07 15:32:37540 this._menuCleared();
541 }
542
543 _menuCleared() {
Paul Lewis0fd43712020-01-08 17:07:36544 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44545 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36546 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44547 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37548 }
549
550 /**
551 * @param {!Object} target
Junyi Xiao6e3798d2019-09-23 19:12:27552 * @return {boolean}
553 */
554 containsTarget(target) {
555 return this._pendingTargets.indexOf(target) >= 0;
556 }
557
558 /**
559 * @param {!Object} target
Blink Reformat4c46d092018-04-07 15:32:37560 */
561 appendApplicableItems(target) {
Tim van der Lippe0830b3d2019-10-03 13:20:07562 this._pendingPromises.push(self.runtime.allInstances(Provider, target));
Blink Reformat4c46d092018-04-07 15:32:37563 this._pendingTargets.push(target);
564 }
Tim van der Lippe0830b3d2019-10-03 13:20:07565}
Blink Reformat4c46d092018-04-07 15:32:37566
Tim van der Lippe0830b3d2019-10-03 13:20:07567export const _groupWeights =
Blink Reformat4c46d092018-04-07 15:32:37568 ['header', 'new', 'reveal', 'edit', 'clipboard', 'debug', 'view', 'default', 'save', 'footer'];
569
570/**
571 * @interface
572 */
Tim van der Lippe0830b3d2019-10-03 13:20:07573export class Provider {
Blink Reformat4c46d092018-04-07 15:32:37574 /**
575 * @param {!Event} event
Tim van der Lippe0830b3d2019-10-03 13:20:07576 * @param {!ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37577 * @param {!Object} target
578 */
579 appendApplicableItems(event, contextMenu, target) {}
Tim van der Lippe0830b3d2019-10-03 13:20:07580}
581
Tim van der Lippe0830b3d2019-10-03 13:20:07582ContextMenu._groupWeights = _groupWeights;