blob: 19b9b6dce7494125c7f0e3dfb96c8a7cce47cfc5 [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
Paul Lewis9950e182019-12-16 16:06:0737import {SoftContextMenu} from './SoftContextMenu.js';
38
Blink Reformat4c46d092018-04-07 15:32:3739/**
40 * @unrestricted
41 */
Tim van der Lippe0830b3d2019-10-03 13:20:0742export class Item {
Blink Reformat4c46d092018-04-07 15:32:3743 /**
Tim van der Lippe0830b3d2019-10-03 13:20:0744 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:3745 * @param {string} type
46 * @param {string=} label
47 * @param {boolean=} disabled
48 * @param {boolean=} checked
49 */
50 constructor(contextMenu, type, label, disabled, checked) {
51 this._type = type;
52 this._label = label;
53 this._disabled = disabled;
54 this._checked = checked;
55 this._contextMenu = contextMenu;
Tim van der Lippe1d6e57a2019-09-30 11:55:3456 if (type === 'item' || type === 'checkbox') {
Blink Reformat4c46d092018-04-07 15:32:3757 this._id = contextMenu ? contextMenu._nextId() : 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:3458 }
Blink Reformat4c46d092018-04-07 15:32:3759 }
60
61 /**
62 * @return {number}
63 */
64 id() {
65 return this._id;
66 }
67
68 /**
69 * @return {string}
70 */
71 type() {
72 return this._type;
73 }
74
75 /**
76 * @return {boolean}
77 */
78 isEnabled() {
79 return !this._disabled;
80 }
81
82 /**
83 * @param {boolean} enabled
84 */
85 setEnabled(enabled) {
86 this._disabled = !enabled;
87 }
88
89 /**
90 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
91 */
92 _buildDescriptor() {
93 switch (this._type) {
Mathias Bynens88e8f152020-03-25 14:33:1294 case 'item': {
Blink Reformat4c46d092018-04-07 15:32:3795 const result = {type: 'item', id: this._id, label: this._label, enabled: !this._disabled};
Tim van der Lippe1d6e57a2019-09-30 11:55:3496 if (this._customElement) {
Blink Reformat4c46d092018-04-07 15:32:3797 result.element = this._customElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:3498 }
99 if (this._shortcut) {
Blink Reformat4c46d092018-04-07 15:32:37100 result.shortcut = this._shortcut;
Tim van der Lippe1d6e57a2019-09-30 11:55:34101 }
Blink Reformat4c46d092018-04-07 15:32:37102 return result;
Mathias Bynens88e8f152020-03-25 14:33:12103 }
104 case 'separator': {
Blink Reformat4c46d092018-04-07 15:32:37105 return {type: 'separator'};
Mathias Bynens88e8f152020-03-25 14:33:12106 }
107 case 'checkbox': {
Blink Reformat4c46d092018-04-07 15:32:37108 return {type: 'checkbox', id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled};
Mathias Bynens88e8f152020-03-25 14:33:12109 }
Blink Reformat4c46d092018-04-07 15:32:37110 }
111 throw new Error('Invalid item type:' + this._type);
112 }
113
114 /**
115 * @param {string} shortcut
116 */
117 setShortcut(shortcut) {
118 this._shortcut = shortcut;
119 }
Tim van der Lippe0830b3d2019-10-03 13:20:07120}
Blink Reformat4c46d092018-04-07 15:32:37121
122/**
123 * @unrestricted
124 */
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) {
Paul Lewis24cb7402020-01-24 13:46:35174 const action = self.UI.actionRegistry.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));
Paul Lewis05eb37f2020-01-24 14:31:40185 const shortcut = self.UI.shortcutRegistry.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
218/**
219 * @unrestricted
220 */
Brandon Goddardf7bccf22019-12-11 21:49:16221export class SubMenu extends Item {
Blink Reformat4c46d092018-04-07 15:32:37222 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07223 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37224 * @param {string=} label
225 * @param {boolean=} disabled
226 */
227 constructor(contextMenu, label, disabled) {
228 super(contextMenu, 'subMenu', label, disabled);
Tim van der Lippe0830b3d2019-10-03 13:20:07229 /** @type {!Map<string, !Section>} */
Blink Reformat4c46d092018-04-07 15:32:37230 this._sections = new Map();
Tim van der Lippe0830b3d2019-10-03 13:20:07231 /** @type {!Array<!Section>} */
Blink Reformat4c46d092018-04-07 15:32:37232 this._sectionList = [];
233 }
234
235 _init() {
Tim van der Lippe0830b3d2019-10-03 13:20:07236 _groupWeights.forEach(name => this.section(name));
Blink Reformat4c46d092018-04-07 15:32:37237 }
238
239 /**
240 * @param {string=} name
Tim van der Lippe0830b3d2019-10-03 13:20:07241 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37242 */
243 section(name) {
244 let section = name ? this._sections.get(name) : null;
245 if (!section) {
Tim van der Lippe0830b3d2019-10-03 13:20:07246 section = new Section(this._contextMenu);
Blink Reformat4c46d092018-04-07 15:32:37247 if (name) {
248 this._sections.set(name, section);
249 this._sectionList.push(section);
250 } else {
Tim van der Lippe0830b3d2019-10-03 13:20:07251 this._sectionList.splice(ContextMenu._groupWeights.indexOf('default'), 0, section);
Blink Reformat4c46d092018-04-07 15:32:37252 }
253 }
254 return section;
255 }
256
257 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07258 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37259 */
260 headerSection() {
261 return this.section('header');
262 }
263
264 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07265 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37266 */
267 newSection() {
268 return this.section('new');
269 }
270
271 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07272 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37273 */
274 revealSection() {
275 return this.section('reveal');
276 }
277
278 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07279 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37280 */
281 clipboardSection() {
282 return this.section('clipboard');
283 }
284
285 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07286 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37287 */
288 editSection() {
289 return this.section('edit');
290 }
291
292 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07293 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37294 */
295 debugSection() {
296 return this.section('debug');
297 }
298
299 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07300 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37301 */
302 viewSection() {
303 return this.section('view');
304 }
305
306 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07307 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37308 */
309 defaultSection() {
310 return this.section('default');
311 }
312
313 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07314 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37315 */
316 saveSection() {
317 return this.section('save');
318 }
319
320 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07321 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37322 */
323 footerSection() {
324 return this.section('footer');
325 }
326
327 /**
328 * @override
329 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
330 */
331 _buildDescriptor() {
332 /** @type {!InspectorFrontendHostAPI.ContextMenuDescriptor} */
333 const result = {type: 'subMenu', label: this._label, enabled: !this._disabled, subItems: []};
334
335 const nonEmptySections = this._sectionList.filter(section => !!section._items.length);
336 for (const section of nonEmptySections) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34337 for (const item of section._items) {
Blink Reformat4c46d092018-04-07 15:32:37338 result.subItems.push(item._buildDescriptor());
Tim van der Lippe1d6e57a2019-09-30 11:55:34339 }
340 if (section !== nonEmptySections.peekLast()) {
Blink Reformat4c46d092018-04-07 15:32:37341 result.subItems.push({type: 'separator'});
Tim van der Lippe1d6e57a2019-09-30 11:55:34342 }
Blink Reformat4c46d092018-04-07 15:32:37343 }
344 return result;
345 }
346
347 /**
348 * @param {string} location
349 */
350 appendItemsAtLocation(location) {
351 for (const extension of self.runtime.extensions('context-menu-item')) {
352 const itemLocation = extension.descriptor()['location'] || '';
Tim van der Lippe1d6e57a2019-09-30 11:55:34353 if (!itemLocation.startsWith(location + '/')) {
Blink Reformat4c46d092018-04-07 15:32:37354 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34355 }
Blink Reformat4c46d092018-04-07 15:32:37356
357 const section = itemLocation.substr(location.length + 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34358 if (!section || section.includes('/')) {
Blink Reformat4c46d092018-04-07 15:32:37359 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34360 }
Blink Reformat4c46d092018-04-07 15:32:37361
362 this.section(section).appendAction(extension.descriptor()['actionId']);
363 }
364 }
Tim van der Lippe0830b3d2019-10-03 13:20:07365}
Blink Reformat4c46d092018-04-07 15:32:37366
Tim van der Lippe0830b3d2019-10-03 13:20:07367Item._uniqueSectionName = 0;
Blink Reformat4c46d092018-04-07 15:32:37368
369/**
370 * @unrestricted
371 */
Paul Lewis9950e182019-12-16 16:06:07372export class ContextMenu extends SubMenu {
Blink Reformat4c46d092018-04-07 15:32:37373 /**
374 * @param {!Event} event
375 * @param {boolean=} useSoftMenu
376 * @param {number=} x
377 * @param {number=} y
378 */
379 constructor(event, useSoftMenu, x, y) {
380 super(null);
381 this._contextMenu = this;
382 super._init();
383 this._defaultSection = this.defaultSection();
Tim van der Lippe0830b3d2019-10-03 13:20:07384 /** @type {!Array.<!Promise.<!Array.<!Provider>>>} */
Blink Reformat4c46d092018-04-07 15:32:37385 this._pendingPromises = [];
386 /** @type {!Array<!Object>} */
387 this._pendingTargets = [];
388 this._event = event;
389 this._useSoftMenu = !!useSoftMenu;
390 this._x = x === undefined ? event.x : x;
391 this._y = y === undefined ? event.y : y;
392 this._handlers = {};
393 this._id = 0;
394
395 const target = event.deepElementFromPoint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34396 if (target) {
Blink Reformat4c46d092018-04-07 15:32:37397 this.appendApplicableItems(/** @type {!Object} */ (target));
Tim van der Lippe1d6e57a2019-09-30 11:55:34398 }
Blink Reformat4c46d092018-04-07 15:32:37399 }
400
401 static initialize() {
Paul Lewis0fd43712020-01-08 17:07:36402 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe50cfa9b2019-10-01 10:40:58403 Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu);
Blink Reformat4c46d092018-04-07 15:32:37404 /**
Tim van der Lippec02a97c2020-02-14 14:39:27405 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37406 */
407 function setUseSoftMenu(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07408 ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data);
Blink Reformat4c46d092018-04-07 15:32:37409 }
410 }
411
412 /**
413 * @param {!Document} doc
414 */
415 static installHandler(doc) {
416 doc.body.addEventListener('contextmenu', handler, false);
417
418 /**
419 * @param {!Event} event
420 */
421 function handler(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07422 const contextMenu = new ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37423 contextMenu.show();
424 }
425 }
426
427 /**
428 * @return {number}
429 */
430 _nextId() {
431 return this._id++;
432 }
433
434 show() {
435 Promise.all(this._pendingPromises).then(populate.bind(this)).then(this._innerShow.bind(this));
Tim van der Lippe0830b3d2019-10-03 13:20:07436 ContextMenu._pendingMenu = this;
Blink Reformat4c46d092018-04-07 15:32:37437
438 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07439 * @param {!Array.<!Array.<!Provider>>} appendCallResults
440 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37441 */
442 function populate(appendCallResults) {
Tim van der Lippe0830b3d2019-10-03 13:20:07443 if (ContextMenu._pendingMenu !== this) {
Blink Reformat4c46d092018-04-07 15:32:37444 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34445 }
Tim van der Lippe0830b3d2019-10-03 13:20:07446 delete ContextMenu._pendingMenu;
Blink Reformat4c46d092018-04-07 15:32:37447
448 for (let i = 0; i < appendCallResults.length; ++i) {
449 const providers = appendCallResults[i];
450 const target = this._pendingTargets[i];
451
452 for (let j = 0; j < providers.length; ++j) {
Tim van der Lippe0830b3d2019-10-03 13:20:07453 const provider = /** @type {!Provider} */ (providers[j]);
Blink Reformat4c46d092018-04-07 15:32:37454 provider.appendApplicableItems(this._event, this, target);
455 }
456 }
457
458 this._pendingPromises = [];
459 this._pendingTargets = [];
460 }
461
462 this._event.consume(true);
463 }
464
465 discard() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34466 if (this._softMenu) {
Blink Reformat4c46d092018-04-07 15:32:37467 this._softMenu.discard();
Tim van der Lippe1d6e57a2019-09-30 11:55:34468 }
Blink Reformat4c46d092018-04-07 15:32:37469 }
470
471 _innerShow() {
472 const menuObject = this._buildMenuDescriptors();
Paul Lewis0fd43712020-01-08 17:07:36473 if (this._useSoftMenu || ContextMenu._useSoftMenu ||
474 Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) {
Paul Lewis9950e182019-12-16 16:06:07475 this._softMenu = new SoftContextMenu(menuObject, this._itemSelected.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37476 this._softMenu.show(this._event.target.ownerDocument, new AnchorBox(this._x, this._y, 0, 0));
477 } else {
Paul Lewis0fd43712020-01-08 17:07:36478 Host.InspectorFrontendHost.InspectorFrontendHostInstance.showContextMenuAtPoint(
479 this._x, this._y, menuObject, this._event.target.ownerDocument);
Blink Reformat4c46d092018-04-07 15:32:37480
481 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07482 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37483 */
484 function listenToEvents() {
Paul Lewis0fd43712020-01-08 17:07:36485 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44486 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36487 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44488 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37489 }
490
491 // showContextMenuAtPoint call above synchronously issues a clear event for previous context menu (if any),
492 // so we skip it before subscribing to the clear event.
493 setImmediate(listenToEvents.bind(this));
494 }
495 }
496
497 /**
Brandon Goddard49ef6f12019-10-16 18:50:13498 * @param {number} x
499 */
500 setX(x) {
501 this._x = x;
502 }
503
504 /**
505 * @param {number} y
506 */
507 setY(y) {
508 this._y = y;
509 }
510
511 /**
Blink Reformat4c46d092018-04-07 15:32:37512 * @param {number} id
Tim van der Lippe403a88d2020-05-13 11:51:32513 * @param {function(?):*} handler
Blink Reformat4c46d092018-04-07 15:32:37514 */
515 _setHandler(id, handler) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34516 if (handler) {
Blink Reformat4c46d092018-04-07 15:32:37517 this._handlers[id] = handler;
Tim van der Lippe1d6e57a2019-09-30 11:55:34518 }
Blink Reformat4c46d092018-04-07 15:32:37519 }
520
521 /**
522 * @return {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>}
523 */
524 _buildMenuDescriptors() {
525 return /** @type {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} */ (super._buildDescriptor().subItems);
526 }
527
528 /**
Tim van der Lippec02a97c2020-02-14 14:39:27529 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37530 */
531 _onItemSelected(event) {
532 this._itemSelected(/** @type {string} */ (event.data));
533 }
534
535 /**
536 * @param {string} id
537 */
538 _itemSelected(id) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34539 if (this._handlers[id]) {
Blink Reformat4c46d092018-04-07 15:32:37540 this._handlers[id].call(this);
Tim van der Lippe1d6e57a2019-09-30 11:55:34541 }
Blink Reformat4c46d092018-04-07 15:32:37542 this._menuCleared();
543 }
544
545 _menuCleared() {
Paul Lewis0fd43712020-01-08 17:07:36546 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44547 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Paul Lewis0fd43712020-01-08 17:07:36548 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44549 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37550 }
551
552 /**
553 * @param {!Object} target
Junyi Xiao6e3798d2019-09-23 19:12:27554 * @return {boolean}
555 */
556 containsTarget(target) {
557 return this._pendingTargets.indexOf(target) >= 0;
558 }
559
560 /**
561 * @param {!Object} target
Blink Reformat4c46d092018-04-07 15:32:37562 */
563 appendApplicableItems(target) {
Tim van der Lippe0830b3d2019-10-03 13:20:07564 this._pendingPromises.push(self.runtime.allInstances(Provider, target));
Blink Reformat4c46d092018-04-07 15:32:37565 this._pendingTargets.push(target);
566 }
Tim van der Lippe0830b3d2019-10-03 13:20:07567}
Blink Reformat4c46d092018-04-07 15:32:37568
Tim van der Lippe0830b3d2019-10-03 13:20:07569export const _groupWeights =
Blink Reformat4c46d092018-04-07 15:32:37570 ['header', 'new', 'reveal', 'edit', 'clipboard', 'debug', 'view', 'default', 'save', 'footer'];
571
572/**
573 * @interface
574 */
Tim van der Lippe0830b3d2019-10-03 13:20:07575export class Provider {
Blink Reformat4c46d092018-04-07 15:32:37576 /**
577 * @param {!Event} event
Tim van der Lippe0830b3d2019-10-03 13:20:07578 * @param {!ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37579 * @param {!Object} target
580 */
581 appendApplicableItems(event, contextMenu, target) {}
Tim van der Lippe0830b3d2019-10-03 13:20:07582}
583
Tim van der Lippe0830b3d2019-10-03 13:20:07584ContextMenu._groupWeights = _groupWeights;