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