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