tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 5 | /** |
| 6 | * @fileoverview Element which shows context menus and handles keyboard |
| 7 | * shortcuts. |
| 8 | */ |
rbpotter | f0aa38c | 2019-11-19 01:21:31 | [diff] [blame] | 9 | import 'chrome://resources/cr_elements/cr_action_menu/cr_action_menu.m.js'; |
| 10 | import 'chrome://resources/cr_elements/cr_button/cr_button.m.js'; |
| 11 | import 'chrome://resources/cr_elements/cr_lazy_render/cr_lazy_render.m.js'; |
| 12 | import 'chrome://resources/cr_elements/shared_vars_css.m.js'; |
rbpotter | f0aa38c | 2019-11-19 01:21:31 | [diff] [blame] | 13 | import 'chrome://resources/polymer/v3_0/iron-a11y-keys-behavior/iron-a11y-keys-behavior.js'; |
rbpotter | f0aa38c | 2019-11-19 01:21:31 | [diff] [blame] | 14 | import './edit_dialog.js'; |
| 15 | import './shared_style.js'; |
rbpotter | f0aa38c | 2019-11-19 01:21:31 | [diff] [blame] | 16 | import './strings.m.js'; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 17 | |
Esmael El-Moslimany | 544ed11 | 2019-11-27 00:28:35 | [diff] [blame] | 18 | import {getToastManager} from 'chrome://resources/cr_elements/cr_toast/cr_toast_manager.m.js'; |
dpapad | c5e234c | 2020-09-23 21:31:55 | [diff] [blame] | 19 | import {assert, assertNotReached} from 'chrome://resources/js/assert.m.js'; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 20 | import {isMac} from 'chrome://resources/js/cr.m.js'; |
| 21 | import {KeyboardShortcutList} from 'chrome://resources/js/cr/ui/keyboard_shortcut_list.m.js'; |
| 22 | import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js'; |
dpapad | 34538a9 | 2020-09-22 17:16:23 | [diff] [blame] | 23 | import {PluralStringProxyImpl} from 'chrome://resources/js/plural_string_proxy.js'; |
Esmael El-Moslimany | 544ed11 | 2019-11-27 00:28:35 | [diff] [blame] | 24 | import {IronA11yAnnouncer} from 'chrome://resources/polymer/v3_0/iron-a11y-announcer/iron-a11y-announcer.js'; |
| 25 | import {afterNextRender, flush, html, Polymer} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js'; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 26 | |
| 27 | import {deselectItems, selectAll, selectFolder} from './actions.js'; |
| 28 | import {highlightUpdatedItems, trackUpdatedItems} from './api_listener.js'; |
| 29 | import {BrowserProxy} from './browser_proxy.js'; |
| 30 | import {Command, IncognitoAvailability, MenuSource, OPEN_CONFIRMATION_LIMIT, ROOT_NODE_ID} from './constants.js'; |
| 31 | import {DialogFocusManager} from './dialog_focus_manager.js'; |
| 32 | import {StoreClient} from './store_client.js'; |
rbpotter | f0aa38c | 2019-11-19 01:21:31 | [diff] [blame] | 33 | import {BookmarkNode} from './types.js'; |
| 34 | import {canEditNode, canReorderChildren, getDisplayedList} from './util.js'; |
rbpotter | f0aa38c | 2019-11-19 01:21:31 | [diff] [blame] | 35 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 36 | export const CommandManager = Polymer({ |
| 37 | is: 'bookmarks-command-manager', |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 38 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 39 | _template: html`{__html_template__}`, |
rbpotter | f0aa38c | 2019-11-19 01:21:31 | [diff] [blame] | 40 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 41 | behaviors: [ |
| 42 | StoreClient, |
| 43 | ], |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 44 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 45 | properties: { |
| 46 | /** @private {!Array<Command>} */ |
| 47 | menuCommands_: { |
| 48 | type: Array, |
| 49 | computed: 'computeMenuCommands_(menuSource_)', |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 50 | }, |
| 51 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 52 | /** @private {Set<string>} */ |
| 53 | menuIds_: Object, |
tsergeant | 7fb9e13f | 2017-06-26 06:35:19 | [diff] [blame] | 54 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 55 | /** |
| 56 | * Indicates where the context menu was opened from. Will be NONE if |
| 57 | * menu is not open, indicating that commands are from keyboard shortcuts |
| 58 | * or elsewhere in the UI. |
| 59 | * @private {MenuSource} |
| 60 | */ |
| 61 | menuSource_: { |
| 62 | type: Number, |
| 63 | value: MenuSource.NONE, |
| 64 | }, |
rbpotter | cd52168 | 2019-11-12 02:00:35 | [diff] [blame] | 65 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 66 | /** @private */ |
| 67 | canPaste_: Boolean, |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 68 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 69 | /** @private */ |
| 70 | globalCanEdit_: Boolean, |
| 71 | }, |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 72 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 73 | /** @private {?Function} */ |
| 74 | confirmOpenCallback_: null, |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 75 | |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 76 | attached() { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 77 | assert(CommandManager.instance_ === null); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 78 | CommandManager.instance_ = this; |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 79 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 80 | /** @private {!BrowserProxy} */ |
| 81 | this.browserProxy_ = BrowserProxy.getInstance(); |
tsergeant | 679159f | 2017-06-16 06:58:41 | [diff] [blame] | 82 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 83 | this.watch('globalCanEdit_', state => state.prefs.canEdit); |
| 84 | this.updateFromStore(); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 85 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 86 | /** @private {!Map<Command, KeyboardShortcutList>} */ |
| 87 | this.shortcuts_ = new Map(); |
Christopher Lam | f4a16fd | 2018-02-01 01:47:11 | [diff] [blame] | 88 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 89 | this.addShortcut_(Command.EDIT, 'F2', 'Enter'); |
| 90 | this.addShortcut_(Command.DELETE, 'Delete', 'Delete Backspace'); |
Christopher Lam | f4a16fd | 2018-02-01 01:47:11 | [diff] [blame] | 91 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 92 | this.addShortcut_(Command.OPEN, 'Enter', 'Meta|o'); |
| 93 | this.addShortcut_(Command.OPEN_NEW_TAB, 'Ctrl|Enter', 'Meta|Enter'); |
| 94 | this.addShortcut_(Command.OPEN_NEW_WINDOW, 'Shift|Enter'); |
Christopher Lam | f4a16fd | 2018-02-01 01:47:11 | [diff] [blame] | 95 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 96 | // Note: the undo shortcut is also defined in bookmarks_ui.cc |
| 97 | // TODO(b/893033): de-duplicate shortcut by moving all shortcut |
| 98 | // definitions from JS to C++. |
| 99 | this.addShortcut_(Command.UNDO, 'Ctrl|z', 'Meta|z'); |
| 100 | this.addShortcut_(Command.REDO, 'Ctrl|y Ctrl|Shift|Z', 'Meta|Shift|Z'); |
Christopher Lam | f4a16fd | 2018-02-01 01:47:11 | [diff] [blame] | 101 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 102 | this.addShortcut_(Command.SELECT_ALL, 'Ctrl|a', 'Meta|a'); |
| 103 | this.addShortcut_(Command.DESELECT_ALL, 'Escape'); |
| 104 | |
| 105 | this.addShortcut_(Command.CUT, 'Ctrl|x', 'Meta|x'); |
| 106 | this.addShortcut_(Command.COPY, 'Ctrl|c', 'Meta|c'); |
| 107 | this.addShortcut_(Command.PASTE, 'Ctrl|v', 'Meta|v'); |
| 108 | |
| 109 | /** @private {!Map<string, Function>} */ |
| 110 | this.boundListeners_ = new Map(); |
| 111 | |
| 112 | const addDocumentListener = (eventName, handler) => { |
| 113 | assert(!this.boundListeners_.has(eventName)); |
| 114 | const boundListener = handler.bind(this); |
| 115 | this.boundListeners_.set(eventName, boundListener); |
| 116 | document.addEventListener(eventName, boundListener); |
| 117 | }; |
| 118 | addDocumentListener('open-command-menu', this.onOpenCommandMenu_); |
| 119 | addDocumentListener('keydown', this.onKeydown_); |
| 120 | |
| 121 | const addDocumentListenerForCommand = (eventName, command) => { |
| 122 | addDocumentListener(eventName, (e) => { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 123 | if (e.path[0].tagName === 'INPUT') { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | const items = this.getState().selection.items; |
| 128 | if (this.canExecute(command, items)) { |
| 129 | this.handle(command, items); |
| 130 | } |
| 131 | }); |
| 132 | }; |
| 133 | addDocumentListenerForCommand('command-undo', Command.UNDO); |
| 134 | addDocumentListenerForCommand('cut', Command.CUT); |
| 135 | addDocumentListenerForCommand('copy', Command.COPY); |
| 136 | addDocumentListenerForCommand('paste', Command.PASTE); |
Esmael El-Moslimany | 544ed11 | 2019-11-27 00:28:35 | [diff] [blame] | 137 | |
| 138 | afterNextRender(this, function() { |
| 139 | IronA11yAnnouncer.requestAvailability(); |
| 140 | }); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 141 | }, |
| 142 | |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 143 | detached() { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 144 | CommandManager.instance_ = null; |
| 145 | this.boundListeners_.forEach( |
| 146 | (handler, eventName) => |
| 147 | document.removeEventListener(eventName, handler)); |
| 148 | }, |
| 149 | |
| 150 | /** |
| 151 | * Display the command context menu at (|x|, |y|) in window coordinates. |
| 152 | * Commands will execute on |items| if given, or on the currently selected |
| 153 | * items. |
| 154 | * @param {number} x |
| 155 | * @param {number} y |
| 156 | * @param {MenuSource} source |
| 157 | * @param {Set<string>=} items |
| 158 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 159 | openCommandMenuAtPosition(x, y, source, items) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 160 | this.menuSource_ = source; |
| 161 | this.menuIds_ = items || this.getState().selection.items; |
| 162 | |
| 163 | const dropdown = |
| 164 | /** @type {!CrActionMenuElement} */ (this.$.dropdown.get()); |
| 165 | // Ensure that the menu is fully rendered before trying to position it. |
| 166 | flush(); |
| 167 | DialogFocusManager.getInstance().showDialog( |
| 168 | dropdown.getDialog(), function() { |
| 169 | dropdown.showAtPosition({top: y, left: x}); |
Christopher Lam | f4a16fd | 2018-02-01 01:47:11 | [diff] [blame] | 170 | }); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 171 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 172 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 173 | /** |
| 174 | * Display the command context menu positioned to cover the |target| |
| 175 | * element. Commands will execute on the currently selected items. |
| 176 | * @param {!Element} target |
| 177 | * @param {MenuSource} source |
| 178 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 179 | openCommandMenuAtElement(target, source) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 180 | this.menuSource_ = source; |
| 181 | this.menuIds_ = this.getState().selection.items; |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 182 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 183 | const dropdown = |
| 184 | /** @type {!CrActionMenuElement} */ (this.$.dropdown.get()); |
| 185 | // Ensure that the menu is fully rendered before trying to position it. |
| 186 | flush(); |
| 187 | DialogFocusManager.getInstance().showDialog( |
| 188 | dropdown.getDialog(), function() { |
| 189 | dropdown.showAt(target); |
| 190 | }); |
| 191 | }, |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 192 | |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 193 | closeCommandMenu() { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 194 | this.menuIds_ = new Set(); |
| 195 | this.menuSource_ = MenuSource.NONE; |
| 196 | /** @type {!CrActionMenuElement} */ (this.$.dropdown.get()).close(); |
| 197 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 198 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 199 | //////////////////////////////////////////////////////////////////////////// |
| 200 | // Command handlers: |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 201 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 202 | /** |
| 203 | * Determine if the |command| can be executed with the given |itemIds|. |
| 204 | * Commands which appear in the context menu should be implemented |
| 205 | * separately using `isCommandVisible_` and `isCommandEnabled_`. |
| 206 | * @param {Command} command |
| 207 | * @param {!Set<string>} itemIds |
| 208 | * @return {boolean} |
| 209 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 210 | canExecute(command, itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 211 | const state = this.getState(); |
| 212 | switch (command) { |
| 213 | case Command.OPEN: |
| 214 | return itemIds.size > 0; |
| 215 | case Command.UNDO: |
| 216 | case Command.REDO: |
| 217 | return this.globalCanEdit_; |
| 218 | case Command.SELECT_ALL: |
| 219 | case Command.DESELECT_ALL: |
| 220 | return true; |
| 221 | case Command.COPY: |
| 222 | return itemIds.size > 0; |
| 223 | case Command.CUT: |
| 224 | return itemIds.size > 0 && |
| 225 | !this.containsMatchingNode_(itemIds, function(node) { |
| 226 | return !canEditNode(state, node.id); |
| 227 | }); |
| 228 | case Command.PASTE: |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 229 | return state.search.term === '' && |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 230 | canReorderChildren(state, state.selectedFolder); |
| 231 | default: |
| 232 | return this.isCommandVisible_(command, itemIds) && |
| 233 | this.isCommandEnabled_(command, itemIds); |
| 234 | } |
| 235 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 236 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 237 | /** |
| 238 | * @param {Command} command |
| 239 | * @param {!Set<string>} itemIds |
| 240 | * @return {boolean} True if the command should be visible in the context |
| 241 | * menu. |
| 242 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 243 | isCommandVisible_(command, itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 244 | switch (command) { |
| 245 | case Command.EDIT: |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 246 | return itemIds.size === 1 && this.globalCanEdit_; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 247 | case Command.PASTE: |
| 248 | return this.globalCanEdit_; |
| 249 | case Command.CUT: |
| 250 | case Command.COPY: |
| 251 | return itemIds.size >= 1 && this.globalCanEdit_; |
| 252 | case Command.COPY_URL: |
| 253 | return this.isSingleBookmark_(itemIds); |
| 254 | case Command.DELETE: |
| 255 | return itemIds.size > 0 && this.globalCanEdit_; |
| 256 | case Command.SHOW_IN_FOLDER: |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 257 | return this.menuSource_ === MenuSource.ITEM && itemIds.size === 1 && |
| 258 | this.getState().search.term !== '' && |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 259 | !this.containsMatchingNode_(itemIds, function(node) { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 260 | return !node.parentId || node.parentId === ROOT_NODE_ID; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 261 | }); |
| 262 | case Command.OPEN_NEW_TAB: |
| 263 | case Command.OPEN_NEW_WINDOW: |
| 264 | case Command.OPEN_INCOGNITO: |
| 265 | return itemIds.size > 0; |
| 266 | case Command.ADD_BOOKMARK: |
| 267 | case Command.ADD_FOLDER: |
| 268 | case Command.SORT: |
| 269 | case Command.EXPORT: |
| 270 | case Command.IMPORT: |
| 271 | case Command.HELP_CENTER: |
| 272 | return true; |
| 273 | } |
| 274 | return assert(false); |
| 275 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 276 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 277 | /** |
| 278 | * @param {Command} command |
| 279 | * @param {!Set<string>} itemIds |
| 280 | * @return {boolean} True if the command should be clickable in the context |
| 281 | * menu. |
| 282 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 283 | isCommandEnabled_(command, itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 284 | const state = this.getState(); |
| 285 | switch (command) { |
| 286 | case Command.EDIT: |
| 287 | case Command.DELETE: |
| 288 | return !this.containsMatchingNode_(itemIds, function(node) { |
| 289 | return !canEditNode(state, node.id); |
| 290 | }); |
| 291 | case Command.OPEN_NEW_TAB: |
| 292 | case Command.OPEN_NEW_WINDOW: |
| 293 | return this.expandUrls_(itemIds).length > 0; |
| 294 | case Command.OPEN_INCOGNITO: |
| 295 | return this.expandUrls_(itemIds).length > 0 && |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 296 | state.prefs.incognitoAvailability !== |
| 297 | IncognitoAvailability.DISABLED; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 298 | case Command.SORT: |
| 299 | return this.canChangeList_() && |
| 300 | state.nodes[state.selectedFolder].children.length > 1; |
| 301 | case Command.ADD_BOOKMARK: |
| 302 | case Command.ADD_FOLDER: |
| 303 | return this.canChangeList_(); |
| 304 | case Command.IMPORT: |
| 305 | return this.globalCanEdit_; |
| 306 | case Command.PASTE: |
| 307 | return this.canPaste_; |
| 308 | default: |
| 309 | return true; |
| 310 | } |
| 311 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 312 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 313 | /** |
| 314 | * Returns whether the currently displayed bookmarks list can be changed. |
| 315 | * @private |
| 316 | * @return {boolean} |
| 317 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 318 | canChangeList_() { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 319 | const state = this.getState(); |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 320 | return state.search.term === '' && |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 321 | canReorderChildren(state, state.selectedFolder); |
| 322 | }, |
| 323 | |
| 324 | /** |
| 325 | * @param {Command} command |
| 326 | * @param {!Set<string>} itemIds |
| 327 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 328 | handle(command, itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 329 | const state = this.getState(); |
| 330 | switch (command) { |
| 331 | case Command.EDIT: { |
| 332 | const id = Array.from(itemIds)[0]; |
| 333 | /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) |
| 334 | .showEditDialog(state.nodes[id]); |
| 335 | break; |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 336 | } |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 337 | case Command.COPY_URL: |
| 338 | case Command.COPY: { |
| 339 | const idList = Array.from(itemIds); |
| 340 | chrome.bookmarkManagerPrivate.copy(idList, () => { |
dpapad | 111a3490 | 2017-09-12 16:51:10 | [diff] [blame] | 341 | let labelPromise; |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 342 | if (command === Command.COPY_URL) { |
Christopher Lam | 75ca910 | 2017-07-18 02:15:18 | [diff] [blame] | 343 | labelPromise = |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 344 | Promise.resolve(loadTimeData.getString('toastUrlCopied')); |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 345 | } else if (idList.length === 1) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 346 | labelPromise = |
| 347 | Promise.resolve(loadTimeData.getString('toastItemCopied')); |
Christopher Lam | 75ca910 | 2017-07-18 02:15:18 | [diff] [blame] | 348 | } else { |
dpapad | 34538a9 | 2020-09-22 17:16:23 | [diff] [blame] | 349 | labelPromise = PluralStringProxyImpl.getInstance().getPluralString( |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 350 | 'toastItemsCopied', idList.length); |
Christopher Lam | 75ca910 | 2017-07-18 02:15:18 | [diff] [blame] | 351 | } |
| 352 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 353 | this.showTitleToast_( |
| 354 | labelPromise, state.nodes[idList[0]].title, false); |
Hector Carmona | 79b07f0 | 2019-09-12 00:40:53 | [diff] [blame] | 355 | }); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 356 | break; |
| 357 | } |
| 358 | case Command.SHOW_IN_FOLDER: { |
| 359 | const id = Array.from(itemIds)[0]; |
| 360 | this.dispatch( |
| 361 | selectFolder(assert(state.nodes[id].parentId), state.nodes)); |
| 362 | DialogFocusManager.getInstance().clearFocus(); |
| 363 | this.fire('highlight-items', [id]); |
| 364 | break; |
| 365 | } |
| 366 | case Command.DELETE: { |
| 367 | const idList = Array.from(this.minimizeDeletionSet_(itemIds)); |
| 368 | const title = state.nodes[idList[0]].title; |
| 369 | let labelPromise; |
Hector Carmona | 79b07f0 | 2019-09-12 00:40:53 | [diff] [blame] | 370 | |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 371 | if (idList.length === 1) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 372 | labelPromise = |
| 373 | Promise.resolve(loadTimeData.getString('toastItemDeleted')); |
| 374 | } else { |
dpapad | 34538a9 | 2020-09-22 17:16:23 | [diff] [blame] | 375 | labelPromise = PluralStringProxyImpl.getInstance().getPluralString( |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 376 | 'toastItemsDeleted', idList.length); |
| 377 | } |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 378 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 379 | chrome.bookmarkManagerPrivate.removeTrees(idList, () => { |
| 380 | this.showTitleToast_(labelPromise, title, true); |
| 381 | }); |
| 382 | break; |
| 383 | } |
| 384 | case Command.UNDO: |
| 385 | chrome.bookmarkManagerPrivate.undo(); |
Esmael El-Moslimany | 544ed11 | 2019-11-27 00:28:35 | [diff] [blame] | 386 | getToastManager().hide(); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 387 | break; |
| 388 | case Command.REDO: |
| 389 | chrome.bookmarkManagerPrivate.redo(); |
| 390 | break; |
| 391 | case Command.OPEN_NEW_TAB: |
| 392 | case Command.OPEN_NEW_WINDOW: |
| 393 | case Command.OPEN_INCOGNITO: |
| 394 | this.openUrls_(this.expandUrls_(itemIds), command); |
| 395 | break; |
| 396 | case Command.OPEN: |
| 397 | if (this.isFolder_(itemIds)) { |
| 398 | const folderId = Array.from(itemIds)[0]; |
| 399 | this.dispatch(selectFolder(folderId, state.nodes)); |
| 400 | } else { |
| 401 | this.openUrls_(this.expandUrls_(itemIds), command); |
| 402 | } |
| 403 | break; |
| 404 | case Command.SELECT_ALL: |
| 405 | const displayedIds = getDisplayedList(state); |
| 406 | this.dispatch(selectAll(displayedIds, state)); |
| 407 | break; |
| 408 | case Command.DESELECT_ALL: |
| 409 | this.dispatch(deselectItems()); |
John Lee | 3e46b10 | 2020-12-08 21:06:44 | [diff] [blame^] | 410 | IronA11yAnnouncer.requestAvailability(); |
| 411 | this.fire('iron-announce', { |
| 412 | text: loadTimeData.getString('itemsUnselected'), |
| 413 | }); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 414 | break; |
| 415 | case Command.CUT: |
| 416 | chrome.bookmarkManagerPrivate.cut(Array.from(itemIds)); |
| 417 | break; |
| 418 | case Command.PASTE: |
| 419 | const selectedFolder = state.selectedFolder; |
| 420 | const selectedItems = state.selection.items; |
| 421 | trackUpdatedItems(); |
| 422 | chrome.bookmarkManagerPrivate.paste( |
| 423 | selectedFolder, Array.from(selectedItems), highlightUpdatedItems); |
| 424 | break; |
| 425 | case Command.SORT: |
| 426 | chrome.bookmarkManagerPrivate.sortChildren( |
| 427 | assert(state.selectedFolder)); |
Esmael El-Moslimany | 544ed11 | 2019-11-27 00:28:35 | [diff] [blame] | 428 | getToastManager().querySelector('dom-if').if = true; |
| 429 | getToastManager().show(loadTimeData.getString('toastFolderSorted')); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 430 | break; |
| 431 | case Command.ADD_BOOKMARK: |
| 432 | /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) |
| 433 | .showAddDialog(false, assert(state.selectedFolder)); |
| 434 | break; |
| 435 | case Command.ADD_FOLDER: |
| 436 | /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) |
| 437 | .showAddDialog(true, assert(state.selectedFolder)); |
| 438 | break; |
| 439 | case Command.IMPORT: |
| 440 | chrome.bookmarks.import(); |
| 441 | break; |
| 442 | case Command.EXPORT: |
| 443 | chrome.bookmarks.export(); |
| 444 | break; |
| 445 | case Command.HELP_CENTER: |
| 446 | window.open('https://support.google.com/chrome/?p=bookmarks'); |
| 447 | break; |
| 448 | default: |
| 449 | assert(false); |
| 450 | } |
| 451 | this.recordCommandHistogram_( |
| 452 | itemIds, 'BookmarkManager.CommandExecuted', command); |
| 453 | }, |
| 454 | |
| 455 | /** |
| 456 | * @param {!Event} e |
| 457 | * @param {!Set<string>} itemIds |
| 458 | * @return {boolean} True if the event was handled, triggering a keyboard |
| 459 | * shortcut. |
| 460 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 461 | handleKeyEvent(e, itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 462 | for (const commandTuple of this.shortcuts_) { |
| 463 | const command = /** @type {Command} */ (commandTuple[0]); |
| 464 | const shortcut = |
| 465 | /** @type {KeyboardShortcutList} */ (commandTuple[1]); |
| 466 | if (shortcut.matchesEvent(e) && this.canExecute(command, itemIds)) { |
| 467 | this.handle(command, itemIds); |
| 468 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 469 | e.stopPropagation(); |
| 470 | e.preventDefault(); |
| 471 | return true; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | return false; |
| 476 | }, |
| 477 | |
| 478 | //////////////////////////////////////////////////////////////////////////// |
| 479 | // Private functions: |
| 480 | |
| 481 | /** |
| 482 | * Register a keyboard shortcut for a command. |
| 483 | * @param {Command} command Command that the shortcut will trigger. |
| 484 | * @param {string} shortcut Keyboard shortcut, using the syntax of |
| 485 | * cr/ui/command.js. |
| 486 | * @param {string=} macShortcut If set, enables a replacement shortcut for |
| 487 | * Mac. |
| 488 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 489 | addShortcut_(command, shortcut, macShortcut) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 490 | shortcut = (isMac && macShortcut) ? macShortcut : shortcut; |
| 491 | this.shortcuts_.set(command, new KeyboardShortcutList(shortcut)); |
| 492 | }, |
| 493 | |
| 494 | /** |
| 495 | * Minimize the set of |itemIds| by removing any node which has an ancestor |
| 496 | * node already in the set. This ensures that instead of trying to delete |
| 497 | * both a node and its descendant, we will only try to delete the topmost |
| 498 | * node, preventing an error in the bookmarkManagerPrivate.removeTrees API |
| 499 | * call. |
| 500 | * @param {!Set<string>} itemIds |
| 501 | * @return {!Set<string>} |
| 502 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 503 | minimizeDeletionSet_(itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 504 | const minimizedSet = new Set(); |
| 505 | const nodes = this.getState().nodes; |
| 506 | itemIds.forEach(function(itemId) { |
| 507 | let currentId = itemId; |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 508 | while (currentId !== ROOT_NODE_ID) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 509 | currentId = assert(nodes[currentId].parentId); |
| 510 | if (itemIds.has(currentId)) { |
| 511 | return; |
| 512 | } |
| 513 | } |
| 514 | minimizedSet.add(itemId); |
| 515 | }); |
| 516 | return minimizedSet; |
| 517 | }, |
| 518 | |
| 519 | /** |
| 520 | * Open the given |urls| in response to a |command|. May show a confirmation |
| 521 | * dialog before opening large numbers of URLs. |
| 522 | * @param {!Array<string>} urls |
| 523 | * @param {Command} command |
| 524 | * @private |
| 525 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 526 | openUrls_(urls, command) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 527 | assert( |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 528 | command === Command.OPEN || command === Command.OPEN_NEW_TAB || |
| 529 | command === Command.OPEN_NEW_WINDOW || |
| 530 | command === Command.OPEN_INCOGNITO); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 531 | |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 532 | if (urls.length === 0) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 533 | return; |
| 534 | } |
| 535 | |
| 536 | const openUrlsCallback = function() { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 537 | const incognito = command === Command.OPEN_INCOGNITO; |
| 538 | if (command === Command.OPEN_NEW_WINDOW || incognito) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 539 | chrome.windows.create({url: urls, incognito: incognito}); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 540 | } else { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 541 | if (command === Command.OPEN) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 542 | chrome.tabs.create({url: urls.shift(), active: true}); |
| 543 | } |
| 544 | urls.forEach(function(url) { |
| 545 | chrome.tabs.create({url: url, active: false}); |
| 546 | }); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 547 | } |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 548 | }; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 549 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 550 | if (urls.length <= OPEN_CONFIRMATION_LIMIT) { |
| 551 | openUrlsCallback(); |
| 552 | return; |
| 553 | } |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 554 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 555 | this.confirmOpenCallback_ = openUrlsCallback; |
| 556 | const dialog = this.$.openDialog.get(); |
| 557 | dialog.querySelector('[slot=body]').textContent = |
| 558 | loadTimeData.getStringF('openDialogBody', urls.length); |
| 559 | |
| 560 | DialogFocusManager.getInstance().showDialog(this.$.openDialog.get()); |
| 561 | }, |
| 562 | |
| 563 | /** |
| 564 | * Returns all URLs in the given set of nodes and their immediate children. |
| 565 | * Note that these will be ordered by insertion order into the |itemIds| |
| 566 | * set, and that it is possible to duplicate a URL by passing in both the |
| 567 | * parent ID and child ID. |
| 568 | * @param {!Set<string>} itemIds |
| 569 | * @return {!Array<string>} |
| 570 | * @private |
| 571 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 572 | expandUrls_(itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 573 | const urls = []; |
| 574 | const nodes = this.getState().nodes; |
| 575 | |
| 576 | itemIds.forEach(function(id) { |
| 577 | const node = nodes[id]; |
| 578 | if (node.url) { |
| 579 | urls.push(node.url); |
| 580 | } else { |
| 581 | node.children.forEach(function(childId) { |
| 582 | const childNode = nodes[childId]; |
| 583 | if (childNode.url) { |
| 584 | urls.push(childNode.url); |
| 585 | } |
| 586 | }); |
Esmael El-Moslimany | f50f28b | 2019-03-21 03:06:04 | [diff] [blame] | 587 | } |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 588 | }); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 589 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 590 | return urls; |
| 591 | }, |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 592 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 593 | /** |
| 594 | * @param {!Set<string>} itemIds |
| 595 | * @param {function(BookmarkNode):boolean} predicate |
| 596 | * @return {boolean} True if any node in |itemIds| returns true for |
| 597 | * |predicate|. |
| 598 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 599 | containsMatchingNode_(itemIds, predicate) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 600 | const nodes = this.getState().nodes; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 601 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 602 | return Array.from(itemIds).some(function(id) { |
| 603 | return predicate(nodes[id]); |
| 604 | }); |
| 605 | }, |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 606 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 607 | /** |
| 608 | * @param {!Set<string>} itemIds |
| 609 | * @return {boolean} True if |itemIds| is a single bookmark (non-folder) |
| 610 | * node. |
| 611 | * @private |
| 612 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 613 | isSingleBookmark_(itemIds) { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 614 | return itemIds.size === 1 && |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 615 | this.containsMatchingNode_(itemIds, function(node) { |
| 616 | return !!node.url; |
| 617 | }); |
| 618 | }, |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 619 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 620 | /** |
| 621 | * @param {!Set<string>} itemIds |
| 622 | * @return {boolean} |
| 623 | * @private |
| 624 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 625 | isFolder_(itemIds) { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 626 | return itemIds.size === 1 && |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 627 | this.containsMatchingNode_(itemIds, node => !node.url); |
| 628 | }, |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 629 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 630 | /** |
| 631 | * @param {Command} command |
| 632 | * @return {string} |
| 633 | * @private |
| 634 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 635 | getCommandLabel_(command) { |
dpapad | c5e234c | 2020-09-23 21:31:55 | [diff] [blame] | 636 | // Handle non-pluralized strings first. |
| 637 | let label = null; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 638 | switch (command) { |
| 639 | case Command.EDIT: |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 640 | if (this.menuIds_.size !== 1) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 641 | return ''; |
| 642 | } |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 643 | |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 644 | const id = Array.from(this.menuIds_)[0]; |
| 645 | const itemUrl = this.getState().nodes[id].url; |
| 646 | label = itemUrl ? 'menuEdit' : 'menuRename'; |
| 647 | break; |
| 648 | case Command.CUT: |
| 649 | label = 'menuCut'; |
| 650 | break; |
| 651 | case Command.COPY: |
| 652 | label = 'menuCopy'; |
| 653 | break; |
| 654 | case Command.COPY_URL: |
| 655 | label = 'menuCopyURL'; |
| 656 | break; |
| 657 | case Command.PASTE: |
| 658 | label = 'menuPaste'; |
| 659 | break; |
| 660 | case Command.DELETE: |
| 661 | label = 'menuDelete'; |
| 662 | break; |
| 663 | case Command.SHOW_IN_FOLDER: |
| 664 | label = 'menuShowInFolder'; |
| 665 | break; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 666 | case Command.SORT: |
| 667 | label = 'menuSort'; |
| 668 | break; |
| 669 | case Command.ADD_BOOKMARK: |
| 670 | label = 'menuAddBookmark'; |
| 671 | break; |
| 672 | case Command.ADD_FOLDER: |
| 673 | label = 'menuAddFolder'; |
| 674 | break; |
| 675 | case Command.IMPORT: |
| 676 | label = 'menuImport'; |
| 677 | break; |
| 678 | case Command.EXPORT: |
| 679 | label = 'menuExport'; |
| 680 | break; |
| 681 | case Command.HELP_CENTER: |
| 682 | label = 'menuHelpCenter'; |
| 683 | break; |
| 684 | } |
dpapad | c5e234c | 2020-09-23 21:31:55 | [diff] [blame] | 685 | if (label !== null) { |
| 686 | return loadTimeData.getString(assert(label)); |
| 687 | } |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 688 | |
dpapad | c5e234c | 2020-09-23 21:31:55 | [diff] [blame] | 689 | // Handle pluralized strings. |
| 690 | switch (command) { |
| 691 | case Command.OPEN_NEW_TAB: |
| 692 | return this.getPluralizedOpenAllString_( |
| 693 | 'menuOpenAllNewTab', 'menuOpenNewTab', |
| 694 | 'menuOpenAllNewTabWithCount'); |
| 695 | case Command.OPEN_NEW_WINDOW: |
| 696 | return this.getPluralizedOpenAllString_( |
| 697 | 'menuOpenAllNewWindow', 'menuOpenNewWindow', |
| 698 | 'menuOpenAllNewWindowWithCount'); |
| 699 | case Command.OPEN_INCOGNITO: |
| 700 | return this.getPluralizedOpenAllString_( |
| 701 | 'menuOpenAllIncognito', 'menuOpenIncognito', |
| 702 | 'menuOpenAllIncognitoWithCount'); |
| 703 | } |
| 704 | |
| 705 | assertNotReached(); |
| 706 | return ''; |
| 707 | }, |
| 708 | |
| 709 | /** |
| 710 | * @param {string} case0 String ID for the case of zero URLs. |
| 711 | * @param {string} case1 String ID for the case of 1 URL. |
| 712 | * @param {string} caseOther String ID for string that includes the URL count. |
| 713 | * @return {string} |
| 714 | * @private |
| 715 | */ |
| 716 | getPluralizedOpenAllString_(case0, case1, caseOther) { |
| 717 | const multipleNodes = this.menuIds_.size > 1 || |
| 718 | this.containsMatchingNode_(this.menuIds_, node => !node.url); |
| 719 | |
| 720 | const urls = this.expandUrls_(this.menuIds_); |
| 721 | if (urls.length === 0) { |
| 722 | return loadTimeData.getStringF(case0, urls.length); |
| 723 | } |
| 724 | |
| 725 | if (urls.length === 1 && !multipleNodes) { |
| 726 | return loadTimeData.getString(case1); |
| 727 | } |
| 728 | |
| 729 | return loadTimeData.getStringF(caseOther, urls.length); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 730 | }, |
| 731 | |
| 732 | /** |
| 733 | * @param {Command} command |
| 734 | * @return {string} |
| 735 | * @private |
| 736 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 737 | getCommandSublabel_(command) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 738 | const multipleNodes = this.menuIds_.size > 1 || |
| 739 | this.containsMatchingNode_(this.menuIds_, function(node) { |
| 740 | return !node.url; |
| 741 | }); |
| 742 | switch (command) { |
| 743 | case Command.OPEN_NEW_TAB: |
| 744 | const urls = this.expandUrls_(this.menuIds_); |
| 745 | return multipleNodes && urls.length > 0 ? String(urls.length) : ''; |
| 746 | default: |
| 747 | return ''; |
| 748 | } |
| 749 | }, |
| 750 | |
| 751 | /** @private */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 752 | computeMenuCommands_() { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 753 | switch (this.menuSource_) { |
| 754 | case MenuSource.ITEM: |
| 755 | case MenuSource.TREE: |
| 756 | return [ |
| 757 | Command.EDIT, |
| 758 | Command.SHOW_IN_FOLDER, |
| 759 | Command.DELETE, |
| 760 | // <hr> |
| 761 | Command.CUT, |
| 762 | Command.COPY, |
| 763 | Command.COPY_URL, |
| 764 | Command.PASTE, |
| 765 | // <hr> |
| 766 | Command.OPEN_NEW_TAB, |
| 767 | Command.OPEN_NEW_WINDOW, |
| 768 | Command.OPEN_INCOGNITO, |
| 769 | ]; |
| 770 | case MenuSource.TOOLBAR: |
| 771 | return [ |
| 772 | Command.SORT, |
| 773 | // <hr> |
| 774 | Command.ADD_BOOKMARK, |
| 775 | Command.ADD_FOLDER, |
| 776 | // <hr> |
| 777 | Command.IMPORT, |
| 778 | Command.EXPORT, |
| 779 | // <hr> |
| 780 | Command.HELP_CENTER, |
| 781 | ]; |
| 782 | case MenuSource.LIST: |
| 783 | return [ |
| 784 | Command.ADD_BOOKMARK, |
| 785 | Command.ADD_FOLDER, |
| 786 | ]; |
| 787 | case MenuSource.NONE: |
| 788 | return []; |
| 789 | } |
| 790 | assert(false); |
| 791 | }, |
| 792 | |
| 793 | /** |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 794 | * @param {Command} command |
| 795 | * @param {!Set<string>} itemIds |
| 796 | * @return {boolean} |
| 797 | * @private |
| 798 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 799 | showDividerAfter_(command, itemIds) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 800 | switch (command) { |
| 801 | case Command.SORT: |
| 802 | case Command.ADD_FOLDER: |
| 803 | case Command.EXPORT: |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 804 | return this.menuSource_ === MenuSource.TOOLBAR; |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 805 | case Command.DELETE: |
| 806 | return this.globalCanEdit_; |
| 807 | case Command.PASTE: |
| 808 | return this.globalCanEdit_ || this.isSingleBookmark_(itemIds); |
| 809 | } |
| 810 | return false; |
| 811 | }, |
| 812 | |
| 813 | /** |
| 814 | * @param {!Set<string>} itemIds |
| 815 | * @param {string} histogram |
| 816 | * @param {number} command |
| 817 | * @private |
| 818 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 819 | recordCommandHistogram_(itemIds, histogram, command) { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 820 | if (command === Command.OPEN) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 821 | command = |
| 822 | this.isFolder_(itemIds) ? Command.OPEN_FOLDER : Command.OPEN_BOOKMARK; |
| 823 | } |
| 824 | |
| 825 | this.browserProxy_.recordInHistogram(histogram, command, Command.MAX_VALUE); |
| 826 | }, |
| 827 | |
| 828 | /** |
| 829 | * Show a toast with a bookmark |title| inserted into a label, with the |
| 830 | * title ellipsised if necessary. |
| 831 | * @param {!Promise<string>} labelPromise Promise which resolves with the |
| 832 | * label for the toast. |
| 833 | * @param {string} title Bookmark title to insert. |
| 834 | * @param {boolean} canUndo If true, shows an undo button in the toast. |
| 835 | * @private |
| 836 | */ |
| 837 | showTitleToast_: async function(labelPromise, title, canUndo) { |
| 838 | const label = await labelPromise; |
| 839 | const pieces = |
| 840 | loadTimeData.getSubstitutedStringPieces(label, title).map(function(p) { |
| 841 | // Make the bookmark name collapsible. |
| 842 | p.collapsible = !!p.arg; |
| 843 | return p; |
| 844 | }); |
Esmael El-Moslimany | 544ed11 | 2019-11-27 00:28:35 | [diff] [blame] | 845 | getToastManager().querySelector('dom-if').if = canUndo; |
| 846 | getToastManager().showForStringPieces(pieces); |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 847 | }, |
| 848 | |
| 849 | /** |
| 850 | * @param {number} targetId |
| 851 | * @private |
| 852 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 853 | updateCanPaste_(targetId) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 854 | return new Promise(resolve => { |
| 855 | chrome.bookmarkManagerPrivate.canPaste(`${targetId}`, result => { |
| 856 | this.canPaste_ = result; |
| 857 | resolve(); |
| 858 | }); |
| 859 | }); |
| 860 | }, |
| 861 | |
| 862 | //////////////////////////////////////////////////////////////////////////// |
| 863 | // Event handlers: |
| 864 | |
| 865 | /** |
| 866 | * @param {Event} e |
| 867 | * @private |
| 868 | */ |
| 869 | onOpenCommandMenu_: async function(e) { |
| 870 | await this.updateCanPaste_(e.detail.source); |
| 871 | if (e.detail.targetElement) { |
| 872 | this.openCommandMenuAtElement(e.detail.targetElement, e.detail.source); |
| 873 | } else { |
| 874 | this.openCommandMenuAtPosition(e.detail.x, e.detail.y, e.detail.source); |
| 875 | } |
| 876 | this.browserProxy_.recordInHistogram( |
| 877 | 'BookmarkManager.CommandMenuOpened', e.detail.source, |
| 878 | MenuSource.NUM_VALUES); |
| 879 | }, |
| 880 | |
| 881 | /** |
| 882 | * @param {Event} e |
| 883 | * @private |
| 884 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 885 | onCommandClick_(e) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 886 | this.handle( |
| 887 | /** @type {Command} */ ( |
| 888 | Number(e.currentTarget.getAttribute('command'))), |
| 889 | assert(this.menuIds_)); |
| 890 | this.closeCommandMenu(); |
| 891 | }, |
| 892 | |
| 893 | /** |
| 894 | * @param {!Event} e |
| 895 | * @private |
| 896 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 897 | onKeydown_(e) { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 898 | const path = e.composedPath(); |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 899 | if (path[0].tagName === 'INPUT') { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 900 | return; |
| 901 | } |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 902 | if ((e.target === document.body || |
| 903 | path.some(el => el.tagName === 'BOOKMARKS-TOOLBAR')) && |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 904 | !DialogFocusManager.getInstance().hasOpenDialog()) { |
| 905 | this.handleKeyEvent(e, this.getState().selection.items); |
| 906 | } |
| 907 | }, |
| 908 | |
| 909 | /** |
| 910 | * Close the menu on mousedown so clicks can propagate to the underlying UI. |
| 911 | * This allows the user to right click the list while a context menu is |
| 912 | * showing and get another context menu. |
| 913 | * @param {Event} e |
| 914 | * @private |
| 915 | */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 916 | onMenuMousedown_(e) { |
Demetrios Papadopoulos | 5490c5e | 2019-12-12 22:24:55 | [diff] [blame] | 917 | if (e.path[0].tagName !== 'DIALOG') { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 918 | return; |
| 919 | } |
| 920 | |
| 921 | this.closeCommandMenu(); |
| 922 | }, |
| 923 | |
| 924 | /** @private */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 925 | onOpenCancelTap_() { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 926 | this.$.openDialog.get().cancel(); |
| 927 | }, |
| 928 | |
| 929 | /** @private */ |
Dan Beam | a5cfc5f | 2020-01-14 05:58:10 | [diff] [blame] | 930 | onOpenConfirmTap_() { |
rbpotter | d66215c | 2019-11-20 07:20:36 | [diff] [blame] | 931 | this.confirmOpenCallback_(); |
| 932 | this.$.openDialog.get().close(); |
| 933 | }, |
| 934 | }); |
| 935 | |
| 936 | /** @private {CommandManager} */ |
| 937 | CommandManager.instance_ = null; |
| 938 | |
| 939 | /** @return {!CommandManager} */ |
| 940 | CommandManager.getInstance = function() { |
| 941 | return assert(CommandManager.instance_); |
| 942 | }; |