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 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 31 | import {SoftContextMenu} from './SoftContextMenu.js'; |
| 32 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 33 | /** |
| 34 | * @unrestricted |
| 35 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 36 | export class Item { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 38 | * @param {?ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | * @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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 50 | if (type === 'item' || type === 'checkbox') { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 51 | this._id = contextMenu ? contextMenu._nextId() : 0; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 52 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | } |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 90 | if (this._customElement) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 91 | result.element = this._customElement; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 92 | } |
| 93 | if (this._shortcut) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 94 | result.shortcut = this._shortcut; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 95 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 96 | 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 Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 111 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 112 | |
| 113 | /** |
| 114 | * @unrestricted |
| 115 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 116 | export class Section { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 117 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 118 | * @param {?ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 119 | */ |
| 120 | constructor(contextMenu) { |
| 121 | this._contextMenu = contextMenu; |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 122 | /** @type {!Array<!Item>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 123 | this._items = []; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @param {string} label |
| 128 | * @param {function(?)} handler |
| 129 | * @param {boolean=} disabled |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 130 | * @return {!Item} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 131 | */ |
| 132 | appendItem(label, handler, disabled) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 133 | const item = new Item(this._contextMenu, 'item', label, disabled); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 134 | this._items.push(item); |
| 135 | this._contextMenu._setHandler(item.id(), handler); |
| 136 | return item; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @param {!Element} element |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 141 | * @return {!Item} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 142 | */ |
| 143 | appendCustomItem(element) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 144 | const item = new Item(this._contextMenu, 'item', '<custom>'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 145 | item._customElement = element; |
| 146 | this._items.push(item); |
| 147 | return item; |
| 148 | } |
| 149 | |
| 150 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 151 | * @return {!Item} |
Jan Scheffler | 7dfab7e | 2019-10-23 11:25:15 | [diff] [blame] | 152 | */ |
| 153 | appendSeparator() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 154 | const item = new Item(this._contextMenu, 'separator'); |
Jan Scheffler | 7dfab7e | 2019-10-23 11:25:15 | [diff] [blame] | 155 | this._items.push(item); |
| 156 | return item; |
| 157 | } |
| 158 | |
| 159 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 160 | * @param {string} actionId |
| 161 | * @param {string=} label |
Pavel Feldman | f5b981a | 2018-11-30 03:42:08 | [diff] [blame] | 162 | * @param {boolean=} optional |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 163 | */ |
Pavel Feldman | f5b981a | 2018-11-30 03:42:08 | [diff] [blame] | 164 | appendAction(actionId, label, optional) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 165 | const action = UI.actionRegistry.action(actionId); |
| 166 | if (!action) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 167 | if (!optional) { |
Pavel Feldman | f5b981a | 2018-11-30 03:42:08 | [diff] [blame] | 168 | console.error(`Action ${actionId} was not defined`); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 169 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 170 | return; |
| 171 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 172 | if (!label) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 173 | label = action.title(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 174 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 175 | const result = this.appendItem(label, action.execute.bind(action)); |
| 176 | const shortcut = UI.shortcutRegistry.shortcutTitleForAction(actionId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 177 | if (shortcut) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | result.setShortcut(shortcut); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 179 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @param {string} label |
| 184 | * @param {boolean=} disabled |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 185 | * @return {!SubMenu} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 186 | */ |
| 187 | appendSubMenuItem(label, disabled) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 188 | const item = new SubMenu(this._contextMenu, label, disabled); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 189 | 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 Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 199 | * @return {!Item} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | */ |
| 201 | appendCheckboxItem(label, handler, checked, disabled) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 202 | const item = new Item(this._contextMenu, 'checkbox', label, disabled, checked); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 203 | this._items.push(item); |
| 204 | this._contextMenu._setHandler(item.id(), handler); |
| 205 | return item; |
| 206 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 207 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * @unrestricted |
| 211 | */ |
Brandon Goddard | f7bccf2 | 2019-12-11 21:49:16 | [diff] [blame] | 212 | export class SubMenu extends Item { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 213 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 214 | * @param {?ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 215 | * @param {string=} label |
| 216 | * @param {boolean=} disabled |
| 217 | */ |
| 218 | constructor(contextMenu, label, disabled) { |
| 219 | super(contextMenu, 'subMenu', label, disabled); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 220 | /** @type {!Map<string, !Section>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 221 | this._sections = new Map(); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 222 | /** @type {!Array<!Section>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 223 | this._sectionList = []; |
| 224 | } |
| 225 | |
| 226 | _init() { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 227 | _groupWeights.forEach(name => this.section(name)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @param {string=} name |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 232 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 233 | */ |
| 234 | section(name) { |
| 235 | let section = name ? this._sections.get(name) : null; |
| 236 | if (!section) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 237 | section = new Section(this._contextMenu); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 238 | if (name) { |
| 239 | this._sections.set(name, section); |
| 240 | this._sectionList.push(section); |
| 241 | } else { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 242 | this._sectionList.splice(ContextMenu._groupWeights.indexOf('default'), 0, section); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | return section; |
| 246 | } |
| 247 | |
| 248 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 249 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 250 | */ |
| 251 | headerSection() { |
| 252 | return this.section('header'); |
| 253 | } |
| 254 | |
| 255 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 256 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 257 | */ |
| 258 | newSection() { |
| 259 | return this.section('new'); |
| 260 | } |
| 261 | |
| 262 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 263 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 264 | */ |
| 265 | revealSection() { |
| 266 | return this.section('reveal'); |
| 267 | } |
| 268 | |
| 269 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 270 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 271 | */ |
| 272 | clipboardSection() { |
| 273 | return this.section('clipboard'); |
| 274 | } |
| 275 | |
| 276 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 277 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 278 | */ |
| 279 | editSection() { |
| 280 | return this.section('edit'); |
| 281 | } |
| 282 | |
| 283 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 284 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 285 | */ |
| 286 | debugSection() { |
| 287 | return this.section('debug'); |
| 288 | } |
| 289 | |
| 290 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 291 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 292 | */ |
| 293 | viewSection() { |
| 294 | return this.section('view'); |
| 295 | } |
| 296 | |
| 297 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 298 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 299 | */ |
| 300 | defaultSection() { |
| 301 | return this.section('default'); |
| 302 | } |
| 303 | |
| 304 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 305 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 306 | */ |
| 307 | saveSection() { |
| 308 | return this.section('save'); |
| 309 | } |
| 310 | |
| 311 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 312 | * @return {!Section} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 313 | */ |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 328 | for (const item of section._items) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 329 | result.subItems.push(item._buildDescriptor()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 330 | } |
| 331 | if (section !== nonEmptySections.peekLast()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 332 | result.subItems.push({type: 'separator'}); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 333 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 334 | } |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 344 | if (!itemLocation.startsWith(location + '/')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 345 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 346 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 347 | |
| 348 | const section = itemLocation.substr(location.length + 1); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 349 | if (!section || section.includes('/')) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 350 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 351 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 352 | |
| 353 | this.section(section).appendAction(extension.descriptor()['actionId']); |
| 354 | } |
| 355 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 356 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 357 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 358 | Item._uniqueSectionName = 0; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 359 | |
| 360 | /** |
| 361 | * @unrestricted |
| 362 | */ |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 363 | export class ContextMenu extends SubMenu { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 364 | /** |
| 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 Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 375 | /** @type {!Array.<!Promise.<!Array.<!Provider>>>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 376 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 387 | if (target) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 388 | this.appendApplicableItems(/** @type {!Object} */ (target)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 389 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | static initialize() { |
Tim van der Lippe | 50cfa9b | 2019-10-01 10:40:58 | [diff] [blame] | 393 | Host.InspectorFrontendHost.events.addEventListener( |
| 394 | Host.InspectorFrontendHostAPI.Events.SetUseSoftMenu, setUseSoftMenu); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 395 | /** |
| 396 | * @param {!Common.Event} event |
| 397 | */ |
| 398 | function setUseSoftMenu(event) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 399 | ContextMenu._useSoftMenu = /** @type {boolean} */ (event.data); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 400 | } |
| 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 Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 413 | const contextMenu = new ContextMenu(event); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 414 | 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 Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 427 | ContextMenu._pendingMenu = this; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 428 | |
| 429 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 430 | * @param {!Array.<!Array.<!Provider>>} appendCallResults |
| 431 | * @this {ContextMenu} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 432 | */ |
| 433 | function populate(appendCallResults) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 434 | if (ContextMenu._pendingMenu !== this) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 435 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 436 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 437 | delete ContextMenu._pendingMenu; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 438 | |
| 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 Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 444 | const provider = /** @type {!Provider} */ (providers[j]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 445 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 457 | if (this._softMenu) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 458 | this._softMenu.discard(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 459 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | _innerShow() { |
| 463 | const menuObject = this._buildMenuDescriptors(); |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 464 | if (this._useSoftMenu || ContextMenu._useSoftMenu || Host.InspectorFrontendHost.isHostedMode()) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame^] | 465 | this._softMenu = new SoftContextMenu(menuObject, this._itemSelected.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 466 | this._softMenu.show(this._event.target.ownerDocument, new AnchorBox(this._x, this._y, 0, 0)); |
| 467 | } else { |
Tim van der Lippe | 50cfa9b | 2019-10-01 10:40:58 | [diff] [blame] | 468 | Host.InspectorFrontendHost.showContextMenuAtPoint(this._x, this._y, menuObject, this._event.target.ownerDocument); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 469 | |
| 470 | /** |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 471 | * @this {ContextMenu} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 472 | */ |
| 473 | function listenToEvents() { |
Tim van der Lippe | 50cfa9b | 2019-10-01 10:40:58 | [diff] [blame] | 474 | Host.InspectorFrontendHost.events.addEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 475 | Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this); |
Tim van der Lippe | 50cfa9b | 2019-10-01 10:40:58 | [diff] [blame] | 476 | Host.InspectorFrontendHost.events.addEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 477 | Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 478 | } |
| 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 Goddard | 49ef6f1 | 2019-10-16 18:50:13 | [diff] [blame] | 487 | * @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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 501 | * @param {number} id |
| 502 | * @param {function(?)} handler |
| 503 | */ |
| 504 | _setHandler(id, handler) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 505 | if (handler) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 506 | this._handlers[id] = handler; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 507 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 508 | } |
| 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 528 | if (this._handlers[id]) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 529 | this._handlers[id].call(this); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 530 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 531 | this._menuCleared(); |
| 532 | } |
| 533 | |
| 534 | _menuCleared() { |
Tim van der Lippe | 50cfa9b | 2019-10-01 10:40:58 | [diff] [blame] | 535 | Host.InspectorFrontendHost.events.removeEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 536 | Host.InspectorFrontendHostAPI.Events.ContextMenuCleared, this._menuCleared, this); |
Tim van der Lippe | 50cfa9b | 2019-10-01 10:40:58 | [diff] [blame] | 537 | Host.InspectorFrontendHost.events.removeEventListener( |
Tim van der Lippe | 7b19016 | 2019-09-27 15:10:44 | [diff] [blame] | 538 | Host.InspectorFrontendHostAPI.Events.ContextMenuItemSelected, this._onItemSelected, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /** |
| 542 | * @param {!Object} target |
Junyi Xiao | 6e3798d | 2019-09-23 19:12:27 | [diff] [blame] | 543 | * @return {boolean} |
| 544 | */ |
| 545 | containsTarget(target) { |
| 546 | return this._pendingTargets.indexOf(target) >= 0; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * @param {!Object} target |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 551 | */ |
| 552 | appendApplicableItems(target) { |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 553 | this._pendingPromises.push(self.runtime.allInstances(Provider, target)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 554 | this._pendingTargets.push(target); |
| 555 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 556 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 557 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 558 | export const _groupWeights = |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 559 | ['header', 'new', 'reveal', 'edit', 'clipboard', 'debug', 'view', 'default', 'save', 'footer']; |
| 560 | |
| 561 | /** |
| 562 | * @interface |
| 563 | */ |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 564 | export class Provider { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 565 | /** |
| 566 | * @param {!Event} event |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 567 | * @param {!ContextMenu} contextMenu |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 568 | * @param {!Object} target |
| 569 | */ |
| 570 | appendApplicableItems(event, contextMenu, target) {} |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 571 | } |
| 572 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 573 | ContextMenu._groupWeights = _groupWeights; |