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