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