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