blob: f53c2ca5cc86112c850ec4b04883023f123d9162 [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
Paul Lewis9950e182019-12-16 16:06:0731import {SoftContextMenu} from './SoftContextMenu.js';
32
Blink Reformat4c46d092018-04-07 15:32:3733/**
34 * @unrestricted
35 */
Tim van der Lippe0830b3d2019-10-03 13:20:0736export class Item {
Blink Reformat4c46d092018-04-07 15:32:3737 /**
Tim van der Lippe0830b3d2019-10-03 13:20:0738 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:3739 * @param {string} type
40 * @param {string=} label
41 * @param {boolean=} disabled
42 * @param {boolean=} checked
43 */
44 constructor(contextMenu, type, label, disabled, checked) {
45 this._type = type;
46 this._label = label;
47 this._disabled = disabled;
48 this._checked = checked;
49 this._contextMenu = contextMenu;
Tim van der Lippe1d6e57a2019-09-30 11:55:3450 if (type === 'item' || type === 'checkbox') {
Blink Reformat4c46d092018-04-07 15:32:3751 this._id = contextMenu ? contextMenu._nextId() : 0;
Tim van der Lippe1d6e57a2019-09-30 11:55:3452 }
Blink Reformat4c46d092018-04-07 15:32:3753 }
54
55 /**
56 * @return {number}
57 */
58 id() {
59 return this._id;
60 }
61
62 /**
63 * @return {string}
64 */
65 type() {
66 return this._type;
67 }
68
69 /**
70 * @return {boolean}
71 */
72 isEnabled() {
73 return !this._disabled;
74 }
75
76 /**
77 * @param {boolean} enabled
78 */
79 setEnabled(enabled) {
80 this._disabled = !enabled;
81 }
82
83 /**
84 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
85 */
86 _buildDescriptor() {
87 switch (this._type) {
88 case 'item':
89 const result = {type: 'item', id: this._id, label: this._label, enabled: !this._disabled};
Tim van der Lippe1d6e57a2019-09-30 11:55:3490 if (this._customElement) {
Blink Reformat4c46d092018-04-07 15:32:3791 result.element = this._customElement;
Tim van der Lippe1d6e57a2019-09-30 11:55:3492 }
93 if (this._shortcut) {
Blink Reformat4c46d092018-04-07 15:32:3794 result.shortcut = this._shortcut;
Tim van der Lippe1d6e57a2019-09-30 11:55:3495 }
Blink Reformat4c46d092018-04-07 15:32:3796 return result;
97 case 'separator':
98 return {type: 'separator'};
99 case 'checkbox':
100 return {type: 'checkbox', id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled};
101 }
102 throw new Error('Invalid item type:' + this._type);
103 }
104
105 /**
106 * @param {string} shortcut
107 */
108 setShortcut(shortcut) {
109 this._shortcut = shortcut;
110 }
Tim van der Lippe0830b3d2019-10-03 13:20:07111}
Blink Reformat4c46d092018-04-07 15:32:37112
113/**
114 * @unrestricted
115 */
Tim van der Lippe0830b3d2019-10-03 13:20:07116export class Section {
Blink Reformat4c46d092018-04-07 15:32:37117 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07118 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37119 */
120 constructor(contextMenu) {
121 this._contextMenu = contextMenu;
Tim van der Lippe0830b3d2019-10-03 13:20:07122 /** @type {!Array<!Item>} */
Blink Reformat4c46d092018-04-07 15:32:37123 this._items = [];
124 }
125
126 /**
127 * @param {string} label
128 * @param {function(?)} handler
129 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07130 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37131 */
132 appendItem(label, handler, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07133 const item = new Item(this._contextMenu, 'item', label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37134 this._items.push(item);
135 this._contextMenu._setHandler(item.id(), handler);
136 return item;
137 }
138
139 /**
140 * @param {!Element} element
Tim van der Lippe0830b3d2019-10-03 13:20:07141 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37142 */
143 appendCustomItem(element) {
Tim van der Lippe0830b3d2019-10-03 13:20:07144 const item = new Item(this._contextMenu, 'item', '<custom>');
Blink Reformat4c46d092018-04-07 15:32:37145 item._customElement = element;
146 this._items.push(item);
147 return item;
148 }
149
150 /**
Paul Lewis9950e182019-12-16 16:06:07151 * @return {!Item}
Jan Scheffler7dfab7e2019-10-23 11:25:15152 */
153 appendSeparator() {
Paul Lewis9950e182019-12-16 16:06:07154 const item = new Item(this._contextMenu, 'separator');
Jan Scheffler7dfab7e2019-10-23 11:25:15155 this._items.push(item);
156 return item;
157 }
158
159 /**
Blink Reformat4c46d092018-04-07 15:32:37160 * @param {string} actionId
161 * @param {string=} label
Pavel Feldmanf5b981a2018-11-30 03:42:08162 * @param {boolean=} optional
Blink Reformat4c46d092018-04-07 15:32:37163 */
Pavel Feldmanf5b981a2018-11-30 03:42:08164 appendAction(actionId, label, optional) {
Blink Reformat4c46d092018-04-07 15:32:37165 const action = UI.actionRegistry.action(actionId);
166 if (!action) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34167 if (!optional) {
Pavel Feldmanf5b981a2018-11-30 03:42:08168 console.error(`Action ${actionId} was not defined`);
Tim van der Lippe1d6e57a2019-09-30 11:55:34169 }
Blink Reformat4c46d092018-04-07 15:32:37170 return;
171 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34172 if (!label) {
Blink Reformat4c46d092018-04-07 15:32:37173 label = action.title();
Tim van der Lippe1d6e57a2019-09-30 11:55:34174 }
Blink Reformat4c46d092018-04-07 15:32:37175 const result = this.appendItem(label, action.execute.bind(action));
176 const shortcut = UI.shortcutRegistry.shortcutTitleForAction(actionId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 if (shortcut) {
Blink Reformat4c46d092018-04-07 15:32:37178 result.setShortcut(shortcut);
Tim van der Lippe1d6e57a2019-09-30 11:55:34179 }
Blink Reformat4c46d092018-04-07 15:32:37180 }
181
182 /**
183 * @param {string} label
184 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07185 * @return {!SubMenu}
Blink Reformat4c46d092018-04-07 15:32:37186 */
187 appendSubMenuItem(label, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07188 const item = new SubMenu(this._contextMenu, label, disabled);
Blink Reformat4c46d092018-04-07 15:32:37189 item._init();
190 this._items.push(item);
191 return item;
192 }
193
194 /**
195 * @param {string} label
196 * @param {function()} handler
197 * @param {boolean=} checked
198 * @param {boolean=} disabled
Tim van der Lippe0830b3d2019-10-03 13:20:07199 * @return {!Item}
Blink Reformat4c46d092018-04-07 15:32:37200 */
201 appendCheckboxItem(label, handler, checked, disabled) {
Tim van der Lippe0830b3d2019-10-03 13:20:07202 const item = new Item(this._contextMenu, 'checkbox', label, disabled, checked);
Blink Reformat4c46d092018-04-07 15:32:37203 this._items.push(item);
204 this._contextMenu._setHandler(item.id(), handler);
205 return item;
206 }
Tim van der Lippe0830b3d2019-10-03 13:20:07207}
Blink Reformat4c46d092018-04-07 15:32:37208
209/**
210 * @unrestricted
211 */
Brandon Goddardf7bccf22019-12-11 21:49:16212export class SubMenu extends Item {
Blink Reformat4c46d092018-04-07 15:32:37213 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07214 * @param {?ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37215 * @param {string=} label
216 * @param {boolean=} disabled
217 */
218 constructor(contextMenu, label, disabled) {
219 super(contextMenu, 'subMenu', label, disabled);
Tim van der Lippe0830b3d2019-10-03 13:20:07220 /** @type {!Map<string, !Section>} */
Blink Reformat4c46d092018-04-07 15:32:37221 this._sections = new Map();
Tim van der Lippe0830b3d2019-10-03 13:20:07222 /** @type {!Array<!Section>} */
Blink Reformat4c46d092018-04-07 15:32:37223 this._sectionList = [];
224 }
225
226 _init() {
Tim van der Lippe0830b3d2019-10-03 13:20:07227 _groupWeights.forEach(name => this.section(name));
Blink Reformat4c46d092018-04-07 15:32:37228 }
229
230 /**
231 * @param {string=} name
Tim van der Lippe0830b3d2019-10-03 13:20:07232 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37233 */
234 section(name) {
235 let section = name ? this._sections.get(name) : null;
236 if (!section) {
Tim van der Lippe0830b3d2019-10-03 13:20:07237 section = new Section(this._contextMenu);
Blink Reformat4c46d092018-04-07 15:32:37238 if (name) {
239 this._sections.set(name, section);
240 this._sectionList.push(section);
241 } else {
Tim van der Lippe0830b3d2019-10-03 13:20:07242 this._sectionList.splice(ContextMenu._groupWeights.indexOf('default'), 0, section);
Blink Reformat4c46d092018-04-07 15:32:37243 }
244 }
245 return section;
246 }
247
248 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07249 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37250 */
251 headerSection() {
252 return this.section('header');
253 }
254
255 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07256 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37257 */
258 newSection() {
259 return this.section('new');
260 }
261
262 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07263 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37264 */
265 revealSection() {
266 return this.section('reveal');
267 }
268
269 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07270 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37271 */
272 clipboardSection() {
273 return this.section('clipboard');
274 }
275
276 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07277 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37278 */
279 editSection() {
280 return this.section('edit');
281 }
282
283 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07284 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37285 */
286 debugSection() {
287 return this.section('debug');
288 }
289
290 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07291 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37292 */
293 viewSection() {
294 return this.section('view');
295 }
296
297 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07298 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37299 */
300 defaultSection() {
301 return this.section('default');
302 }
303
304 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07305 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37306 */
307 saveSection() {
308 return this.section('save');
309 }
310
311 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07312 * @return {!Section}
Blink Reformat4c46d092018-04-07 15:32:37313 */
314 footerSection() {
315 return this.section('footer');
316 }
317
318 /**
319 * @override
320 * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor}
321 */
322 _buildDescriptor() {
323 /** @type {!InspectorFrontendHostAPI.ContextMenuDescriptor} */
324 const result = {type: 'subMenu', label: this._label, enabled: !this._disabled, subItems: []};
325
326 const nonEmptySections = this._sectionList.filter(section => !!section._items.length);
327 for (const section of nonEmptySections) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34328 for (const item of section._items) {
Blink Reformat4c46d092018-04-07 15:32:37329 result.subItems.push(item._buildDescriptor());
Tim van der Lippe1d6e57a2019-09-30 11:55:34330 }
331 if (section !== nonEmptySections.peekLast()) {
Blink Reformat4c46d092018-04-07 15:32:37332 result.subItems.push({type: 'separator'});
Tim van der Lippe1d6e57a2019-09-30 11:55:34333 }
Blink Reformat4c46d092018-04-07 15:32:37334 }
335 return result;
336 }
337
338 /**
339 * @param {string} location
340 */
341 appendItemsAtLocation(location) {
342 for (const extension of self.runtime.extensions('context-menu-item')) {
343 const itemLocation = extension.descriptor()['location'] || '';
Tim van der Lippe1d6e57a2019-09-30 11:55:34344 if (!itemLocation.startsWith(location + '/')) {
Blink Reformat4c46d092018-04-07 15:32:37345 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34346 }
Blink Reformat4c46d092018-04-07 15:32:37347
348 const section = itemLocation.substr(location.length + 1);
Tim van der Lippe1d6e57a2019-09-30 11:55:34349 if (!section || section.includes('/')) {
Blink Reformat4c46d092018-04-07 15:32:37350 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34351 }
Blink Reformat4c46d092018-04-07 15:32:37352
353 this.section(section).appendAction(extension.descriptor()['actionId']);
354 }
355 }
Tim van der Lippe0830b3d2019-10-03 13:20:07356}
Blink Reformat4c46d092018-04-07 15:32:37357
Tim van der Lippe0830b3d2019-10-03 13:20:07358Item._uniqueSectionName = 0;
Blink Reformat4c46d092018-04-07 15:32:37359
360/**
361 * @unrestricted
362 */
Paul Lewis9950e182019-12-16 16:06:07363export class ContextMenu extends SubMenu {
Blink Reformat4c46d092018-04-07 15:32:37364 /**
365 * @param {!Event} event
366 * @param {boolean=} useSoftMenu
367 * @param {number=} x
368 * @param {number=} y
369 */
370 constructor(event, useSoftMenu, x, y) {
371 super(null);
372 this._contextMenu = this;
373 super._init();
374 this._defaultSection = this.defaultSection();
Tim van der Lippe0830b3d2019-10-03 13:20:07375 /** @type {!Array.<!Promise.<!Array.<!Provider>>>} */
Blink Reformat4c46d092018-04-07 15:32:37376 this._pendingPromises = [];
377 /** @type {!Array<!Object>} */
378 this._pendingTargets = [];
379 this._event = event;
380 this._useSoftMenu = !!useSoftMenu;
381 this._x = x === undefined ? event.x : x;
382 this._y = y === undefined ? event.y : y;
383 this._handlers = {};
384 this._id = 0;
385
386 const target = event.deepElementFromPoint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34387 if (target) {
Blink Reformat4c46d092018-04-07 15:32:37388 this.appendApplicableItems(/** @type {!Object} */ (target));
Tim van der Lippe1d6e57a2019-09-30 11:55:34389 }
Blink Reformat4c46d092018-04-07 15:32:37390 }
391
392 static initialize() {
Tim van der Lippe50cfa9b2019-10-01 10:40:58393 Host.InspectorFrontendHost.events.addEventListener(
394 Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu);
Blink Reformat4c46d092018-04-07 15:32:37395 /**
396 * @param {!Common.Event} event
397 */
398 function setUseSoftMenu(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07399 ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data);
Blink Reformat4c46d092018-04-07 15:32:37400 }
401 }
402
403 /**
404 * @param {!Document} doc
405 */
406 static installHandler(doc) {
407 doc.body.addEventListener('contextmenu', handler, false);
408
409 /**
410 * @param {!Event} event
411 */
412 function handler(event) {
Tim van der Lippe0830b3d2019-10-03 13:20:07413 const contextMenu = new ContextMenu(event);
Blink Reformat4c46d092018-04-07 15:32:37414 contextMenu.show();
415 }
416 }
417
418 /**
419 * @return {number}
420 */
421 _nextId() {
422 return this._id++;
423 }
424
425 show() {
426 Promise.all(this._pendingPromises).then(populate.bind(this)).then(this._innerShow.bind(this));
Tim van der Lippe0830b3d2019-10-03 13:20:07427 ContextMenu._pendingMenu = this;
Blink Reformat4c46d092018-04-07 15:32:37428
429 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07430 * @param {!Array.<!Array.<!Provider>>} appendCallResults
431 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37432 */
433 function populate(appendCallResults) {
Tim van der Lippe0830b3d2019-10-03 13:20:07434 if (ContextMenu._pendingMenu !== this) {
Blink Reformat4c46d092018-04-07 15:32:37435 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34436 }
Tim van der Lippe0830b3d2019-10-03 13:20:07437 delete ContextMenu._pendingMenu;
Blink Reformat4c46d092018-04-07 15:32:37438
439 for (let i = 0; i < appendCallResults.length; ++i) {
440 const providers = appendCallResults[i];
441 const target = this._pendingTargets[i];
442
443 for (let j = 0; j < providers.length; ++j) {
Tim van der Lippe0830b3d2019-10-03 13:20:07444 const provider = /** @type {!Provider} */ (providers[j]);
Blink Reformat4c46d092018-04-07 15:32:37445 provider.appendApplicableItems(this._event, this, target);
446 }
447 }
448
449 this._pendingPromises = [];
450 this._pendingTargets = [];
451 }
452
453 this._event.consume(true);
454 }
455
456 discard() {
Tim van der Lippe1d6e57a2019-09-30 11:55:34457 if (this._softMenu) {
Blink Reformat4c46d092018-04-07 15:32:37458 this._softMenu.discard();
Tim van der Lippe1d6e57a2019-09-30 11:55:34459 }
Blink Reformat4c46d092018-04-07 15:32:37460 }
461
462 _innerShow() {
463 const menuObject = this._buildMenuDescriptors();
Tim van der Lippe0830b3d2019-10-03 13:20:07464 if (this._useSoftMenu || ContextMenu._useSoftMenu || Host.InspectorFrontendHost.isHostedMode()) {
Paul Lewis9950e182019-12-16 16:06:07465 this._softMenu = new SoftContextMenu(menuObject, this._itemSelected.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37466 this._softMenu.show(this._event.target.ownerDocument, new AnchorBox(this._x, this._y, 0, 0));
467 } else {
Tim van der Lippe50cfa9b2019-10-01 10:40:58468 Host.InspectorFrontendHost.showContextMenuAtPoint(this._x, this._y, menuObject, this._event.target.ownerDocument);
Blink Reformat4c46d092018-04-07 15:32:37469
470 /**
Tim van der Lippe0830b3d2019-10-03 13:20:07471 * @this {ContextMenu}
Blink Reformat4c46d092018-04-07 15:32:37472 */
473 function listenToEvents() {
Tim van der Lippe50cfa9b2019-10-01 10:40:58474 Host.InspectorFrontendHost.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44475 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Tim van der Lippe50cfa9b2019-10-01 10:40:58476 Host.InspectorFrontendHost.events.addEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44477 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37478 }
479
480 // showContextMenuAtPoint call above synchronously issues a clear event for previous context menu (if any),
481 // so we skip it before subscribing to the clear event.
482 setImmediate(listenToEvents.bind(this));
483 }
484 }
485
486 /**
Brandon Goddard49ef6f12019-10-16 18:50:13487 * @param {number} x
488 */
489 setX(x) {
490 this._x = x;
491 }
492
493 /**
494 * @param {number} y
495 */
496 setY(y) {
497 this._y = y;
498 }
499
500 /**
Blink Reformat4c46d092018-04-07 15:32:37501 * @param {number} id
502 * @param {function(?)} handler
503 */
504 _setHandler(id, handler) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34505 if (handler) {
Blink Reformat4c46d092018-04-07 15:32:37506 this._handlers[id] = handler;
Tim van der Lippe1d6e57a2019-09-30 11:55:34507 }
Blink Reformat4c46d092018-04-07 15:32:37508 }
509
510 /**
511 * @return {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>}
512 */
513 _buildMenuDescriptors() {
514 return /** @type {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} */ (super._buildDescriptor().subItems);
515 }
516
517 /**
518 * @param {!Common.Event} event
519 */
520 _onItemSelected(event) {
521 this._itemSelected(/** @type {string} */ (event.data));
522 }
523
524 /**
525 * @param {string} id
526 */
527 _itemSelected(id) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34528 if (this._handlers[id]) {
Blink Reformat4c46d092018-04-07 15:32:37529 this._handlers[id].call(this);
Tim van der Lippe1d6e57a2019-09-30 11:55:34530 }
Blink Reformat4c46d092018-04-07 15:32:37531 this._menuCleared();
532 }
533
534 _menuCleared() {
Tim van der Lippe50cfa9b2019-10-01 10:40:58535 Host.InspectorFrontendHost.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44536 Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this);
Tim van der Lippe50cfa9b2019-10-01 10:40:58537 Host.InspectorFrontendHost.events.removeEventListener(
Tim van der Lippe7b190162019-09-27 15:10:44538 Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this);
Blink Reformat4c46d092018-04-07 15:32:37539 }
540
541 /**
542 * @param {!Object} target
Junyi Xiao6e3798d2019-09-23 19:12:27543 * @return {boolean}
544 */
545 containsTarget(target) {
546 return this._pendingTargets.indexOf(target) >= 0;
547 }
548
549 /**
550 * @param {!Object} target
Blink Reformat4c46d092018-04-07 15:32:37551 */
552 appendApplicableItems(target) {
Tim van der Lippe0830b3d2019-10-03 13:20:07553 this._pendingPromises.push(self.runtime.allInstances(Provider, target));
Blink Reformat4c46d092018-04-07 15:32:37554 this._pendingTargets.push(target);
555 }
Tim van der Lippe0830b3d2019-10-03 13:20:07556}
Blink Reformat4c46d092018-04-07 15:32:37557
Tim van der Lippe0830b3d2019-10-03 13:20:07558export const _groupWeights =
Blink Reformat4c46d092018-04-07 15:32:37559 ['header', 'new', 'reveal', 'edit', 'clipboard', 'debug', 'view', 'default', 'save', 'footer'];
560
561/**
562 * @interface
563 */
Tim van der Lippe0830b3d2019-10-03 13:20:07564export class Provider {
Blink Reformat4c46d092018-04-07 15:32:37565 /**
566 * @param {!Event} event
Tim van der Lippe0830b3d2019-10-03 13:20:07567 * @param {!ContextMenu} contextMenu
Blink Reformat4c46d092018-04-07 15:32:37568 * @param {!Object} target
569 */
570 appendApplicableItems(event, contextMenu, target) {}
Tim van der Lippe0830b3d2019-10-03 13:20:07571}
572
Tim van der Lippe0830b3d2019-10-03 13:20:07573ContextMenu._groupWeights = _groupWeights;