Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | /* |
| 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 Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame^] | 31 | import * as Common from '../common/common.js'; // eslint-disable-line no-unused-vars |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 32 | import * as Host from '../host/host.js'; |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame^] | 33 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 34 | import {SoftContextMenu} from './SoftContextMenu.js'; |
| 35 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | /** |
| 37 | * @unrestricted |
| 38 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 39 | export class Item { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 40 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 41 | * @param {?ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | * @param {string} type |
| 43 | * @param {string=} label |
| 44 | * @param {boolean=} disabled |
| 45 | * @param {boolean=} checked |
| 46 | */ |
| 47 | constructor(contextMenu, type, label, disabled, checked) { |
| 48 | this._type = type; |
| 49 | this._label = label; |
| 50 | this._disabled = disabled; |
| 51 | this._checked = checked; |
| 52 | this._contextMenu = contextMenu; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 53 | if (type === 'item' || type === 'checkbox') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 54 | this._id = contextMenu ? contextMenu._nextId() : 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 55 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return {number} |
| 60 | */ |
| 61 | id() { |
| 62 | return this._id; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return {string} |
| 67 | */ |
| 68 | type() { |
| 69 | return this._type; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return {boolean} |
| 74 | */ |
| 75 | isEnabled() { |
| 76 | return !this._disabled; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param {boolean} enabled |
| 81 | */ |
| 82 | setEnabled(enabled) { |
| 83 | this._disabled = !enabled; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor} |
| 88 | */ |
| 89 | _buildDescriptor() { |
| 90 | switch (this._type) { |
| 91 | case 'item': |
| 92 | const result = {type: 'item', id: this._id, label: this._label, enabled: !this._disabled}; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 93 | if (this._customElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 94 | result.element = this._customElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 95 | } |
| 96 | if (this._shortcut) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 97 | result.shortcut = this._shortcut; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 98 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 99 | return result; |
| 100 | case 'separator': |
| 101 | return {type: 'separator'}; |
| 102 | case 'checkbox': |
| 103 | return {type: 'checkbox', id: this._id, label: this._label, checked: !!this._checked, enabled: !this._disabled}; |
| 104 | } |
| 105 | throw new Error('Invalid item type:' + this._type); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param {string} shortcut |
| 110 | */ |
| 111 | setShortcut(shortcut) { |
| 112 | this._shortcut = shortcut; |
| 113 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 114 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * @unrestricted |
| 118 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 119 | export class Section { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 120 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 121 | * @param {?ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | */ |
| 123 | constructor(contextMenu) { |
| 124 | this._contextMenu = contextMenu; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 125 | /** @type {!Array<!Item>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 126 | this._items = []; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param {string} label |
| 131 | * @param {function(?)} handler |
| 132 | * @param {boolean=} disabled |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 133 | * @return {!Item} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 134 | */ |
| 135 | appendItem(label, handler, disabled) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 136 | const item = new Item(this._contextMenu, 'item', label, disabled); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 137 | this._items.push(item); |
| 138 | this._contextMenu._setHandler(item.id(), handler); |
| 139 | return item; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param {!Element} element |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 144 | * @return {!Item} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 145 | */ |
| 146 | appendCustomItem(element) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 147 | const item = new Item(this._contextMenu, 'item', '<custom>'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | item._customElement = element; |
| 149 | this._items.push(item); |
| 150 | return item; |
| 151 | } |
| 152 | |
| 153 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 154 | * @return {!Item} |
Jan Scheffler | 7dfab7e | 2019-10-23 11:25:15 | [diff] [blame] | 155 | */ |
| 156 | appendSeparator() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 157 | const item = new Item(this._contextMenu, 'separator'); |
Jan Scheffler | 7dfab7e | 2019-10-23 11:25:15 | [diff] [blame] | 158 | this._items.push(item); |
| 159 | return item; |
| 160 | } |
| 161 | |
| 162 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 163 | * @param {string} actionId |
| 164 | * @param {string=} label |
Pavel Feldman | f5b981a | 2018-11-30 03:42:08 | [diff] [blame] | 165 | * @param {boolean=} optional |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 166 | */ |
Pavel Feldman | f5b981a | 2018-11-30 03:42:08 | [diff] [blame] | 167 | appendAction(actionId, label, optional) { |
Paul Lewis | 24cb740 | 2020-01-24 13:46:35 | [diff] [blame] | 168 | const action = self.UI.actionRegistry.action(actionId); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 169 | if (!action) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 170 | if (!optional) { |
Pavel Feldman | f5b981a | 2018-11-30 03:42:08 | [diff] [blame] | 171 | console.error(`Action ${actionId} was not defined`); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 172 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 173 | return; |
| 174 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 175 | if (!label) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 176 | label = action.title(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 177 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | const result = this.appendItem(label, action.execute.bind(action)); |
Paul Lewis | 05eb37f | 2020-01-24 14:31:40 | [diff] [blame] | 179 | const shortcut = self.UI.shortcutRegistry.shortcutTitleForAction(actionId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 180 | if (shortcut) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 181 | result.setShortcut(shortcut); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 182 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @param {string} label |
| 187 | * @param {boolean=} disabled |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 188 | * @return {!SubMenu} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 189 | */ |
| 190 | appendSubMenuItem(label, disabled) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 191 | const item = new SubMenu(this._contextMenu, label, disabled); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 192 | item._init(); |
| 193 | this._items.push(item); |
| 194 | return item; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @param {string} label |
| 199 | * @param {function()} handler |
| 200 | * @param {boolean=} checked |
| 201 | * @param {boolean=} disabled |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 202 | * @return {!Item} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 203 | */ |
| 204 | appendCheckboxItem(label, handler, checked, disabled) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 205 | const item = new Item(this._contextMenu, 'checkbox', label, disabled, checked); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 206 | this._items.push(item); |
| 207 | this._contextMenu._setHandler(item.id(), handler); |
| 208 | return item; |
| 209 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 210 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * @unrestricted |
| 214 | */ |
Brandon Goddard | f7bccf2 | 2019-12-11 21:49:16 | [diff] [blame] | 215 | export class SubMenu extends Item { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 216 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 217 | * @param {?ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 218 | * @param {string=} label |
| 219 | * @param {boolean=} disabled |
| 220 | */ |
| 221 | constructor(contextMenu, label, disabled) { |
| 222 | super(contextMenu, 'subMenu', label, disabled); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 223 | /** @type {!Map<string, !Section>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 224 | this._sections = new Map(); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 225 | /** @type {!Array<!Section>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 226 | this._sectionList = []; |
| 227 | } |
| 228 | |
| 229 | _init() { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 230 | _groupWeights.forEach(name => this.section(name)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @param {string=} name |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 235 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 236 | */ |
| 237 | section(name) { |
| 238 | let section = name ? this._sections.get(name) : null; |
| 239 | if (!section) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 240 | section = new Section(this._contextMenu); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 241 | if (name) { |
| 242 | this._sections.set(name, section); |
| 243 | this._sectionList.push(section); |
| 244 | } else { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 245 | this._sectionList.splice(ContextMenu._groupWeights.indexOf('default'), 0, section); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | return section; |
| 249 | } |
| 250 | |
| 251 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 252 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 253 | */ |
| 254 | headerSection() { |
| 255 | return this.section('header'); |
| 256 | } |
| 257 | |
| 258 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 259 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 260 | */ |
| 261 | newSection() { |
| 262 | return this.section('new'); |
| 263 | } |
| 264 | |
| 265 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 266 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 267 | */ |
| 268 | revealSection() { |
| 269 | return this.section('reveal'); |
| 270 | } |
| 271 | |
| 272 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 273 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 274 | */ |
| 275 | clipboardSection() { |
| 276 | return this.section('clipboard'); |
| 277 | } |
| 278 | |
| 279 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 280 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 281 | */ |
| 282 | editSection() { |
| 283 | return this.section('edit'); |
| 284 | } |
| 285 | |
| 286 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 287 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 288 | */ |
| 289 | debugSection() { |
| 290 | return this.section('debug'); |
| 291 | } |
| 292 | |
| 293 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 294 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 295 | */ |
| 296 | viewSection() { |
| 297 | return this.section('view'); |
| 298 | } |
| 299 | |
| 300 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 301 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 302 | */ |
| 303 | defaultSection() { |
| 304 | return this.section('default'); |
| 305 | } |
| 306 | |
| 307 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 308 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | */ |
| 310 | saveSection() { |
| 311 | return this.section('save'); |
| 312 | } |
| 313 | |
| 314 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 315 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 316 | */ |
| 317 | footerSection() { |
| 318 | return this.section('footer'); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @override |
| 323 | * @return {!InspectorFrontendHostAPI.ContextMenuDescriptor} |
| 324 | */ |
| 325 | _buildDescriptor() { |
| 326 | /** @type {!InspectorFrontendHostAPI.ContextMenuDescriptor} */ |
| 327 | const result = {type: 'subMenu', label: this._label, enabled: !this._disabled, subItems: []}; |
| 328 | |
| 329 | const nonEmptySections = this._sectionList.filter(section => !!section._items.length); |
| 330 | for (const section of nonEmptySections) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 331 | for (const item of section._items) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 332 | result.subItems.push(item._buildDescriptor()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 333 | } |
| 334 | if (section !== nonEmptySections.peekLast()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 335 | result.subItems.push({type: 'separator'}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 336 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 337 | } |
| 338 | return result; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * @param {string} location |
| 343 | */ |
| 344 | appendItemsAtLocation(location) { |
| 345 | for (const extension of self.runtime.extensions('context-menu-item')) { |
| 346 | const itemLocation = extension.descriptor()['location'] || ''; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 347 | if (!itemLocation.startsWith(location + '/')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 348 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 349 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 350 | |
| 351 | const section = itemLocation.substr(location.length + 1); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 352 | if (!section || section.includes('/')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 353 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 354 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 355 | |
| 356 | this.section(section).appendAction(extension.descriptor()['actionId']); |
| 357 | } |
| 358 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 359 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 360 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 361 | Item._uniqueSectionName = 0; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * @unrestricted |
| 365 | */ |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 366 | export class ContextMenu extends SubMenu { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 367 | /** |
| 368 | * @param {!Event} event |
| 369 | * @param {boolean=} useSoftMenu |
| 370 | * @param {number=} x |
| 371 | * @param {number=} y |
| 372 | */ |
| 373 | constructor(event, useSoftMenu, x, y) { |
| 374 | super(null); |
| 375 | this._contextMenu = this; |
| 376 | super._init(); |
| 377 | this._defaultSection = this.defaultSection(); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 378 | /** @type {!Array.<!Promise.<!Array.<!Provider>>>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 379 | this._pendingPromises = []; |
| 380 | /** @type {!Array<!Object>} */ |
| 381 | this._pendingTargets = []; |
| 382 | this._event = event; |
| 383 | this._useSoftMenu = !!useSoftMenu; |
| 384 | this._x = x === undefined ? event.x : x; |
| 385 | this._y = y === undefined ? event.y : y; |
| 386 | this._handlers = {}; |
| 387 | this._id = 0; |
| 388 | |
| 389 | const target = event.deepElementFromPoint(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 390 | if (target) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 391 | this.appendApplicableItems(/** @type {!Object} */ (target)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 392 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static initialize() { |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 396 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener( |
Tim van der Lippe | 50cfa9b | 2019-10-01 10:40:58 | [diff] [blame] | 397 | Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 398 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame^] | 399 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 400 | */ |
| 401 | function setUseSoftMenu(event) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 402 | ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @param {!Document} doc |
| 408 | */ |
| 409 | static installHandler(doc) { |
| 410 | doc.body.addEventListener('contextmenu', handler, false); |
| 411 | |
| 412 | /** |
| 413 | * @param {!Event} event |
| 414 | */ |
| 415 | function handler(event) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 416 | const contextMenu = new ContextMenu(event); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 417 | contextMenu.show(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @return {number} |
| 423 | */ |
| 424 | _nextId() { |
| 425 | return this._id++; |
| 426 | } |
| 427 | |
| 428 | show() { |
| 429 | Promise.all(this._pendingPromises).then(populate.bind(this)).then(this._innerShow.bind(this)); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 430 | ContextMenu._pendingMenu = this; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 431 | |
| 432 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 433 | * @param {!Array.<!Array.<!Provider>>} appendCallResults |
| 434 | * @this {ContextMenu} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 435 | */ |
| 436 | function populate(appendCallResults) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 437 | if (ContextMenu._pendingMenu !== this) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 438 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 439 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 440 | delete ContextMenu._pendingMenu; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 441 | |
| 442 | for (let i = 0; i < appendCallResults.length; ++i) { |
| 443 | const providers = appendCallResults[i]; |
| 444 | const target = this._pendingTargets[i]; |
| 445 | |
| 446 | for (let j = 0; j < providers.length; ++j) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 447 | const provider = /** @type {!Provider} */ (providers[j]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 448 | provider.appendApplicableItems(this._event, this, target); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | this._pendingPromises = []; |
| 453 | this._pendingTargets = []; |
| 454 | } |
| 455 | |
| 456 | this._event.consume(true); |
| 457 | } |
| 458 | |
| 459 | discard() { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 460 | if (this._softMenu) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 461 | this._softMenu.discard(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 462 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | _innerShow() { |
| 466 | const menuObject = this._buildMenuDescriptors(); |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 467 | if (this._useSoftMenu || ContextMenu._useSoftMenu || |
| 468 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 469 | this._softMenu = new SoftContextMenu(menuObject, this._itemSelected.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 470 | this._softMenu.show(this._event.target.ownerDocument, new AnchorBox(this._x, this._y, 0, 0)); |
| 471 | } else { |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 472 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.showContextMenuAtPoint( |
| 473 | this._x, this._y, menuObject, this._event.target.ownerDocument); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 474 | |
| 475 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 476 | * @this {ContextMenu} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 477 | */ |
| 478 | function listenToEvents() { |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 479 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 480 | Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this); |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 481 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 482 | Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // showContextMenuAtPoint call above synchronously issues a clear event for previous context menu (if any), |
| 486 | // so we skip it before subscribing to the clear event. |
| 487 | setImmediate(listenToEvents.bind(this)); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /** |
Brandon Goddard | 49ef6f1 | 2019-10-16 18:50:13 | [diff] [blame] | 492 | * @param {number} x |
| 493 | */ |
| 494 | setX(x) { |
| 495 | this._x = x; |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * @param {number} y |
| 500 | */ |
| 501 | setY(y) { |
| 502 | this._y = y; |
| 503 | } |
| 504 | |
| 505 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 506 | * @param {number} id |
| 507 | * @param {function(?)} handler |
| 508 | */ |
| 509 | _setHandler(id, handler) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 510 | if (handler) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 511 | this._handlers[id] = handler; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 512 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /** |
| 516 | * @return {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} |
| 517 | */ |
| 518 | _buildMenuDescriptors() { |
| 519 | return /** @type {!Array.<!InspectorFrontendHostAPI.ContextMenuDescriptor>} */ (super._buildDescriptor().subItems); |
| 520 | } |
| 521 | |
| 522 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame^] | 523 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 524 | */ |
| 525 | _onItemSelected(event) { |
| 526 | this._itemSelected(/** @type {string} */ (event.data)); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * @param {string} id |
| 531 | */ |
| 532 | _itemSelected(id) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 533 | if (this._handlers[id]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 534 | this._handlers[id].call(this); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 535 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 536 | this._menuCleared(); |
| 537 | } |
| 538 | |
| 539 | _menuCleared() { |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 540 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 541 | Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this); |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 542 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.removeEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 543 | Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /** |
| 547 | * @param {!Object} target |
Junyi Xiao | 6e3798d | 2019-09-23 19:12:27 | [diff] [blame] | 548 | * @return {boolean} |
| 549 | */ |
| 550 | containsTarget(target) { |
| 551 | return this._pendingTargets.indexOf(target) >= 0; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * @param {!Object} target |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 556 | */ |
| 557 | appendApplicableItems(target) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 558 | this._pendingPromises.push(self.runtime.allInstances(Provider, target)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 559 | this._pendingTargets.push(target); |
| 560 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 561 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 562 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 563 | export const _groupWeights = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 564 | ['header', 'new', 'reveal', 'edit', 'clipboard', 'debug', 'view', 'default', 'save', 'footer']; |
| 565 | |
| 566 | /** |
| 567 | * @interface |
| 568 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 569 | export class Provider { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | /** |
| 571 | * @param {!Event} event |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 572 | * @param {!ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 573 | * @param {!Object} target |
| 574 | */ |
| 575 | appendApplicableItems(event, contextMenu, target) {} |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 576 | } |
| 577 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 578 | ContextMenu._groupWeights = _groupWeights; |