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() { |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 10 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 11 | var CommandManager = Polymer({ |
| 12 | is: 'bookmarks-command-manager', |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 13 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 14 | behaviors: [ |
| 15 | bookmarks.StoreClient, |
| 16 | ], |
| 17 | |
| 18 | properties: { |
| 19 | /** @private {!Array<Command>} */ |
| 20 | menuCommands_: { |
| 21 | type: Array, |
| 22 | value: function() { |
| 23 | return [ |
| 24 | Command.EDIT, |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 25 | Command.COPY_URL, |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 26 | Command.SHOW_IN_FOLDER, |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 27 | Command.DELETE, |
| 28 | // <hr> |
| 29 | Command.OPEN_NEW_TAB, |
| 30 | Command.OPEN_NEW_WINDOW, |
| 31 | Command.OPEN_INCOGNITO, |
| 32 | ]; |
| 33 | }, |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 34 | }, |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 35 | |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 36 | /** @private {Set<string>} */ |
calamity | 64e2012a | 2017-06-21 09:57:14 | [diff] [blame] | 37 | menuIds_: { |
| 38 | type: Object, |
| 39 | observer: 'onMenuIdsChanged_', |
| 40 | }, |
| 41 | |
| 42 | /** @private */ |
| 43 | hasAnySublabel_: { |
| 44 | type: Boolean, |
| 45 | reflectToAttribute: true, |
| 46 | }, |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 47 | |
| 48 | /** @private */ |
| 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 | |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 59 | this.watch('globalCanEdit_', function(state) { |
| 60 | return state.prefs.canEdit; |
| 61 | }); |
| 62 | this.updateFromStore(); |
| 63 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 64 | /** @private {function(!Event)} */ |
| 65 | this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); |
| 66 | document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); |
tsergeant | fad224f | 2017-05-05 04:42:09 | [diff] [blame] | 67 | |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 68 | /** @private {function()} */ |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 69 | this.boundOnCommandUndo_ = () => { |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 70 | this.handle(Command.UNDO, new Set()); |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 71 | }; |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 72 | document.addEventListener('command-undo', this.boundOnCommandUndo_); |
| 73 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 74 | /** @private {function(!Event)} */ |
| 75 | this.boundOnKeydown_ = this.onKeydown_.bind(this); |
| 76 | document.addEventListener('keydown', this.boundOnKeydown_); |
tsergeant | fad224f | 2017-05-05 04:42:09 | [diff] [blame] | 77 | |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 78 | /** |
| 79 | * Indicates where the context menu was opened from. Will be NONE if |
| 80 | * menu is not open, indicating that commands are from keyboard shortcuts |
| 81 | * or elsewhere in the UI. |
| 82 | * @private {MenuSource} |
| 83 | */ |
| 84 | this.menuSource_ = MenuSource.NONE; |
| 85 | |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 86 | /** @private {!Map<Command, cr.ui.KeyboardShortcutList>} */ |
| 87 | this.shortcuts_ = new Map(); |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 88 | |
| 89 | this.addShortcut_(Command.EDIT, 'F2', 'Enter'); |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 90 | this.addShortcut_(Command.DELETE, 'Delete', 'Delete Backspace'); |
| 91 | |
Tim Sergeant | 40d2d2c | 2017-07-20 01:37:06 | [diff] [blame] | 92 | this.addShortcut_(Command.OPEN, 'Enter', 'Meta|o'); |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 93 | this.addShortcut_(Command.OPEN_NEW_TAB, 'Ctrl|Enter', 'Meta|Enter'); |
| 94 | this.addShortcut_(Command.OPEN_NEW_WINDOW, 'Shift|Enter'); |
| 95 | |
| 96 | this.addShortcut_(Command.UNDO, 'Ctrl|z', 'Meta|z'); |
| 97 | this.addShortcut_(Command.REDO, 'Ctrl|y Ctrl|Shift|Z', 'Meta|Shift|Z'); |
tsergeant | 679159f | 2017-06-16 06:58:41 | [diff] [blame] | 98 | |
| 99 | this.addShortcut_(Command.SELECT_ALL, 'Ctrl|a', 'Meta|a'); |
| 100 | this.addShortcut_(Command.DESELECT_ALL, 'Escape'); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 101 | |
| 102 | this.addShortcut_(Command.CUT, 'Ctrl|x', 'Meta|x'); |
| 103 | this.addShortcut_(Command.COPY, 'Ctrl|c', 'Meta|c'); |
| 104 | this.addShortcut_(Command.PASTE, 'Ctrl|v', 'Meta|v'); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 105 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 106 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 107 | detached: function() { |
| 108 | CommandManager.instance_ = null; |
| 109 | document.removeEventListener('open-item-menu', this.boundOnOpenItemMenu_); |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 110 | document.removeEventListener('command-undo', this.boundOnCommandUndo_); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 111 | document.removeEventListener('keydown', this.boundOnKeydown_); |
| 112 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 113 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 114 | /** |
| 115 | * Display the command context menu at (|x|, |y|) in window co-ordinates. |
tsergeant | a274e041 | 2017-06-16 05:22:28 | [diff] [blame] | 116 | * Commands will execute on |items| if given, or on the currently selected |
| 117 | * items. |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 118 | * @param {number} x |
| 119 | * @param {number} y |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 120 | * @param {MenuSource} source |
tsergeant | a274e041 | 2017-06-16 05:22:28 | [diff] [blame] | 121 | * @param {Set<string>=} items |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 122 | */ |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 123 | openCommandMenuAtPosition: function(x, y, source, items) { |
| 124 | this.menuSource_ = source; |
tsergeant | a274e041 | 2017-06-16 05:22:28 | [diff] [blame] | 125 | this.menuIds_ = items || this.getState().selection.items; |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 126 | |
tsergeant | 9b9aa15f | 2017-06-22 03:22:27 | [diff] [blame] | 127 | var dropdown = |
| 128 | /** @type {!CrActionMenuElement} */ (this.$.dropdown.get()); |
| 129 | // Ensure that the menu is fully rendered before trying to position it. |
| 130 | Polymer.dom.flush(); |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 131 | bookmarks.DialogFocusManager.getInstance().showDialog( |
| 132 | dropdown, function() { |
| 133 | dropdown.showAtPosition({top: y, left: x}); |
| 134 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 135 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 136 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 137 | /** |
| 138 | * Display the command context menu positioned to cover the |target| |
| 139 | * element. Commands will execute on the currently selected items. |
| 140 | * @param {!Element} target |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 141 | * @param {MenuSource} source |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 142 | */ |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 143 | openCommandMenuAtElement: function(target, source) { |
| 144 | this.menuSource_ = source; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 145 | this.menuIds_ = this.getState().selection.items; |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 146 | |
tsergeant | 9b9aa15f | 2017-06-22 03:22:27 | [diff] [blame] | 147 | var dropdown = |
| 148 | /** @type {!CrActionMenuElement} */ (this.$.dropdown.get()); |
| 149 | // Ensure that the menu is fully rendered before trying to position it. |
| 150 | Polymer.dom.flush(); |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 151 | bookmarks.DialogFocusManager.getInstance().showDialog( |
| 152 | dropdown, function() { |
| 153 | dropdown.showAt(target); |
| 154 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 155 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 156 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 157 | closeCommandMenu: function() { |
tsergeant | 4707d17 | 2017-06-05 05:47:02 | [diff] [blame] | 158 | this.menuIds_ = new Set(); |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 159 | this.menuSource_ = MenuSource.NONE; |
tsergeant | 9b9aa15f | 2017-06-22 03:22:27 | [diff] [blame] | 160 | /** @type {!CrActionMenuElement} */ (this.$.dropdown.get()).close(); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 161 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 162 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 163 | //////////////////////////////////////////////////////////////////////////// |
| 164 | // Command handlers: |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 165 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 166 | /** |
| 167 | * Determine if the |command| can be executed with the given |itemIds|. |
| 168 | * Commands which appear in the context menu should be implemented |
| 169 | * separately using `isCommandVisible_` and `isCommandEnabled_`. |
| 170 | * @param {Command} command |
| 171 | * @param {!Set<string>} itemIds |
| 172 | * @return {boolean} |
| 173 | */ |
| 174 | canExecute: function(command, itemIds) { |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 175 | var state = this.getState(); |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 176 | switch (command) { |
| 177 | case Command.OPEN: |
| 178 | return itemIds.size > 0; |
calamity | 2d4b550 | 2017-05-29 03:57:58 | [diff] [blame] | 179 | case Command.UNDO: |
| 180 | case Command.REDO: |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 181 | return this.globalCanEdit_; |
tsergeant | 679159f | 2017-06-16 06:58:41 | [diff] [blame] | 182 | case Command.SELECT_ALL: |
| 183 | case Command.DESELECT_ALL: |
| 184 | return true; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 185 | case Command.COPY: |
| 186 | return itemIds.size > 0; |
| 187 | case Command.CUT: |
| 188 | return itemIds.size > 0 && |
| 189 | !this.containsMatchingNode_(itemIds, function(node) { |
| 190 | return !bookmarks.util.canEditNode(state, node.id); |
| 191 | }); |
| 192 | case Command.PASTE: |
| 193 | return state.search.term == '' && |
| 194 | bookmarks.util.canReorderChildren(state, state.selectedFolder); |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 195 | default: |
| 196 | return this.isCommandVisible_(command, itemIds) && |
| 197 | this.isCommandEnabled_(command, itemIds); |
| 198 | } |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 199 | }, |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 200 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 201 | /** |
| 202 | * @param {Command} command |
| 203 | * @param {!Set<string>} itemIds |
| 204 | * @return {boolean} True if the command should be visible in the context |
| 205 | * menu. |
| 206 | */ |
| 207 | isCommandVisible_: function(command, itemIds) { |
| 208 | switch (command) { |
| 209 | case Command.EDIT: |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 210 | return itemIds.size == 1 && this.globalCanEdit_; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 211 | case Command.COPY_URL: |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 212 | return this.isSingleBookmark_(itemIds); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 213 | case Command.DELETE: |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 214 | return itemIds.size > 0 && this.globalCanEdit_; |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 215 | case Command.SHOW_IN_FOLDER: |
| 216 | return this.menuSource_ == MenuSource.LIST && itemIds.size == 1 && |
| 217 | this.getState().search.term != '' && |
| 218 | !this.containsMatchingNode_(itemIds, function(node) { |
| 219 | return !node.parentId || node.parentId == ROOT_NODE_ID; |
| 220 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 221 | case Command.OPEN_NEW_TAB: |
| 222 | case Command.OPEN_NEW_WINDOW: |
| 223 | case Command.OPEN_INCOGNITO: |
| 224 | return itemIds.size > 0; |
| 225 | default: |
| 226 | return false; |
tsergeant | f1ffc89 | 2017-05-05 07:43:43 | [diff] [blame] | 227 | } |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 228 | }, |
tsergeant | f1ffc89 | 2017-05-05 07:43:43 | [diff] [blame] | 229 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 230 | /** |
| 231 | * @param {Command} command |
| 232 | * @param {!Set<string>} itemIds |
| 233 | * @return {boolean} True if the command should be clickable in the context |
| 234 | * menu. |
| 235 | */ |
| 236 | isCommandEnabled_: function(command, itemIds) { |
| 237 | switch (command) { |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 238 | case Command.EDIT: |
| 239 | case Command.DELETE: |
| 240 | var state = this.getState(); |
| 241 | return !this.containsMatchingNode_(itemIds, function(node) { |
| 242 | return !bookmarks.util.canEditNode(state, node.id); |
| 243 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 244 | case Command.OPEN_NEW_TAB: |
| 245 | case Command.OPEN_NEW_WINDOW: |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 246 | return this.expandUrls_(itemIds).length > 0; |
tsergeant | 4707d17 | 2017-06-05 05:47:02 | [diff] [blame] | 247 | case Command.OPEN_INCOGNITO: |
| 248 | return this.expandUrls_(itemIds).length > 0 && |
| 249 | this.getState().prefs.incognitoAvailability != |
| 250 | IncognitoAvailability.DISABLED; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 251 | default: |
| 252 | return true; |
| 253 | } |
| 254 | }, |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 255 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 256 | /** |
| 257 | * @param {Command} command |
| 258 | * @param {!Set<string>} itemIds |
| 259 | */ |
| 260 | handle: function(command, itemIds) { |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 261 | var state = this.getState(); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 262 | switch (command) { |
| 263 | case Command.EDIT: |
| 264 | var id = Array.from(itemIds)[0]; |
| 265 | /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 266 | .showEditDialog(state.nodes[id]); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 267 | break; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 268 | case Command.COPY_URL: |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 269 | case Command.COPY: |
| 270 | var idList = Array.from(itemIds); |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 271 | chrome.bookmarkManagerPrivate.copy(idList, () => { |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 272 | var labelPromise; |
| 273 | if (command == Command.COPY_URL) { |
| 274 | labelPromise = |
| 275 | Promise.resolve(loadTimeData.getString('toastUrlCopied')); |
Christopher Lam | 75ca910 | 2017-07-18 02:15:18 | [diff] [blame] | 276 | } else if (idList.length == 1) { |
| 277 | labelPromise = |
| 278 | Promise.resolve(loadTimeData.getString('toastItemCopied')); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 279 | } else { |
| 280 | labelPromise = cr.sendWithPromise( |
| 281 | 'getPluralString', 'toastItemsCopied', idList.length); |
| 282 | } |
| 283 | |
| 284 | this.showTitleToast_( |
| 285 | labelPromise, state.nodes[idList[0]].title, false); |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 286 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 287 | break; |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 288 | case Command.SHOW_IN_FOLDER: |
| 289 | var id = Array.from(itemIds)[0]; |
| 290 | this.dispatch(bookmarks.actions.selectFolder( |
| 291 | assert(state.nodes[id].parentId), state.nodes)); |
tsergeant | f42530c | 2017-07-28 02:44:57 | [diff] [blame^] | 292 | bookmarks.DialogFocusManager.getInstance().clearFocus(); |
| 293 | this.fire('highlight-items', [id]); |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 294 | break; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 295 | case Command.DELETE: |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 296 | var idList = Array.from(this.minimizeDeletionSet_(itemIds)); |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 297 | var title = state.nodes[idList[0]].title; |
Christopher Lam | 75ca910 | 2017-07-18 02:15:18 | [diff] [blame] | 298 | var labelPromise; |
| 299 | |
| 300 | if (idList.length == 1) { |
| 301 | labelPromise = |
| 302 | Promise.resolve(loadTimeData.getString('toastItemDeleted')); |
| 303 | } else { |
| 304 | labelPromise = cr.sendWithPromise( |
| 305 | 'getPluralString', 'toastItemsDeleted', idList.length); |
| 306 | } |
| 307 | |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 308 | chrome.bookmarkManagerPrivate.removeTrees(idList, () => { |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 309 | this.showTitleToast_(labelPromise, title, true); |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 310 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 311 | break; |
calamity | 2d4b550 | 2017-05-29 03:57:58 | [diff] [blame] | 312 | case Command.UNDO: |
| 313 | chrome.bookmarkManagerPrivate.undo(); |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 314 | bookmarks.ToastManager.getInstance().hide(); |
calamity | 2d4b550 | 2017-05-29 03:57:58 | [diff] [blame] | 315 | break; |
| 316 | case Command.REDO: |
| 317 | chrome.bookmarkManagerPrivate.redo(); |
| 318 | break; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 319 | case Command.OPEN_NEW_TAB: |
| 320 | case Command.OPEN_NEW_WINDOW: |
| 321 | case Command.OPEN_INCOGNITO: |
| 322 | this.openUrls_(this.expandUrls_(itemIds), command); |
| 323 | break; |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 324 | case Command.OPEN: |
| 325 | var isFolder = itemIds.size == 1 && |
| 326 | this.containsMatchingNode_(itemIds, function(node) { |
| 327 | return !node.url; |
| 328 | }); |
| 329 | if (isFolder) { |
| 330 | var folderId = Array.from(itemIds)[0]; |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 331 | this.dispatch( |
| 332 | bookmarks.actions.selectFolder(folderId, state.nodes)); |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 333 | } else { |
| 334 | this.openUrls_(this.expandUrls_(itemIds), command); |
| 335 | } |
| 336 | break; |
tsergeant | 679159f | 2017-06-16 06:58:41 | [diff] [blame] | 337 | case Command.SELECT_ALL: |
| 338 | var displayedIds = bookmarks.util.getDisplayedList(state); |
| 339 | this.dispatch(bookmarks.actions.selectAll(displayedIds, state)); |
| 340 | break; |
| 341 | case Command.DESELECT_ALL: |
| 342 | this.dispatch(bookmarks.actions.deselectItems()); |
| 343 | break; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 344 | case Command.CUT: |
| 345 | chrome.bookmarkManagerPrivate.cut(Array.from(itemIds)); |
| 346 | break; |
| 347 | case Command.PASTE: |
| 348 | var selectedFolder = state.selectedFolder; |
| 349 | var selectedItems = state.selection.items; |
tsergeant | f42530c | 2017-07-28 02:44:57 | [diff] [blame^] | 350 | bookmarks.ApiListener.trackUpdatedItems(); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 351 | chrome.bookmarkManagerPrivate.paste( |
tsergeant | f42530c | 2017-07-28 02:44:57 | [diff] [blame^] | 352 | selectedFolder, Array.from(selectedItems), |
| 353 | bookmarks.ApiListener.highlightUpdatedItems); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 354 | break; |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 355 | default: |
| 356 | assert(false); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 357 | } |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 358 | |
| 359 | bookmarks.util.recordEnumHistogram( |
| 360 | 'BookmarkManager.CommandExecuted', command, Command.MAX_VALUE); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 361 | }, |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 362 | |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 363 | /** |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 364 | * @param {!Event} e |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 365 | * @param {!Set<string>} itemIds |
| 366 | * @return {boolean} True if the event was handled, triggering a keyboard |
| 367 | * shortcut. |
| 368 | */ |
| 369 | handleKeyEvent: function(e, itemIds) { |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 370 | for (var commandTuple of this.shortcuts_) { |
| 371 | var command = /** @type {Command} */ (commandTuple[0]); |
| 372 | var shortcut = |
| 373 | /** @type {cr.ui.KeyboardShortcutList} */ (commandTuple[1]); |
| 374 | if (shortcut.matchesEvent(e) && this.canExecute(command, itemIds)) { |
| 375 | this.handle(command, itemIds); |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 376 | |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 377 | bookmarks.util.recordEnumHistogram( |
| 378 | 'BookmarkManager.CommandExecutedFromKeyboard', command, |
| 379 | Command.MAX_VALUE); |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 380 | e.stopPropagation(); |
| 381 | e.preventDefault(); |
| 382 | return true; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return false; |
| 387 | }, |
| 388 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 389 | //////////////////////////////////////////////////////////////////////////// |
| 390 | // Private functions: |
| 391 | |
| 392 | /** |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 393 | * Register a keyboard shortcut for a command. |
| 394 | * @param {Command} command Command that the shortcut will trigger. |
| 395 | * @param {string} shortcut Keyboard shortcut, using the syntax of |
| 396 | * cr/ui/command.js. |
| 397 | * @param {string=} macShortcut If set, enables a replacement shortcut for |
| 398 | * Mac. |
| 399 | */ |
| 400 | addShortcut_: function(command, shortcut, macShortcut) { |
| 401 | var shortcut = (cr.isMac && macShortcut) ? macShortcut : shortcut; |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 402 | this.shortcuts_.set(command, new cr.ui.KeyboardShortcutList(shortcut)); |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 403 | }, |
| 404 | |
| 405 | /** |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 406 | * Minimize the set of |itemIds| by removing any node which has an ancestor |
| 407 | * node already in the set. This ensures that instead of trying to delete |
| 408 | * both a node and its descendant, we will only try to delete the topmost |
| 409 | * node, preventing an error in the bookmarkManagerPrivate.removeTrees API |
| 410 | * call. |
| 411 | * @param {!Set<string>} itemIds |
| 412 | * @return {!Set<string>} |
| 413 | */ |
| 414 | minimizeDeletionSet_: function(itemIds) { |
| 415 | var minimizedSet = new Set(); |
| 416 | var nodes = this.getState().nodes; |
| 417 | itemIds.forEach(function(itemId) { |
| 418 | var currentId = itemId; |
| 419 | while (currentId != ROOT_NODE_ID) { |
| 420 | currentId = assert(nodes[currentId].parentId); |
| 421 | if (itemIds.has(currentId)) |
| 422 | return; |
| 423 | } |
| 424 | minimizedSet.add(itemId); |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 425 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 426 | return minimizedSet; |
| 427 | }, |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 428 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 429 | /** |
tsergeant | 7fb9e13f | 2017-06-26 06:35:19 | [diff] [blame] | 430 | * Open the given |urls| in response to a |command|. May show a confirmation |
| 431 | * dialog before opening large numbers of URLs. |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 432 | * @param {!Array<string>} urls |
| 433 | * @param {Command} command |
| 434 | * @private |
| 435 | */ |
| 436 | openUrls_: function(urls, command) { |
| 437 | assert( |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 438 | command == Command.OPEN || command == Command.OPEN_NEW_TAB || |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 439 | command == Command.OPEN_NEW_WINDOW || |
| 440 | command == Command.OPEN_INCOGNITO); |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 441 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 442 | if (urls.length == 0) |
tsergeant | fad224f | 2017-05-05 04:42:09 | [diff] [blame] | 443 | return; |
tsergeant | fad224f | 2017-05-05 04:42:09 | [diff] [blame] | 444 | |
tsergeant | 7fb9e13f | 2017-06-26 06:35:19 | [diff] [blame] | 445 | var openUrlsCallback = function() { |
| 446 | var incognito = command == Command.OPEN_INCOGNITO; |
| 447 | if (command == Command.OPEN_NEW_WINDOW || incognito) { |
| 448 | chrome.windows.create({url: urls, incognito: incognito}); |
| 449 | } else { |
| 450 | if (command == Command.OPEN) |
| 451 | chrome.tabs.create({url: urls.shift(), active: true}); |
| 452 | urls.forEach(function(url) { |
| 453 | chrome.tabs.create({url: url, active: false}); |
| 454 | }); |
| 455 | } |
| 456 | }; |
| 457 | |
| 458 | if (urls.length <= OPEN_CONFIRMATION_LIMIT) { |
| 459 | openUrlsCallback(); |
| 460 | return; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 461 | } |
tsergeant | 7fb9e13f | 2017-06-26 06:35:19 | [diff] [blame] | 462 | |
| 463 | this.confirmOpenCallback_ = openUrlsCallback; |
| 464 | var dialog = this.$.openDialog.get(); |
| 465 | dialog.querySelector('.body').textContent = |
| 466 | loadTimeData.getStringF('openDialogBody', urls.length); |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 467 | |
| 468 | bookmarks.DialogFocusManager.getInstance().showDialog( |
| 469 | this.$.openDialog.get()); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 470 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 471 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 472 | /** |
| 473 | * Returns all URLs in the given set of nodes and their immediate children. |
| 474 | * Note that these will be ordered by insertion order into the |itemIds| |
| 475 | * set, and that it is possible to duplicate a URL by passing in both the |
| 476 | * parent ID and child ID. |
| 477 | * @param {!Set<string>} itemIds |
| 478 | * @return {!Array<string>} |
| 479 | * @private |
| 480 | */ |
| 481 | expandUrls_: function(itemIds) { |
| 482 | var urls = []; |
| 483 | var nodes = this.getState().nodes; |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 484 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 485 | itemIds.forEach(function(id) { |
| 486 | var node = nodes[id]; |
| 487 | if (node.url) { |
| 488 | urls.push(node.url); |
| 489 | } else { |
| 490 | node.children.forEach(function(childId) { |
| 491 | var childNode = nodes[childId]; |
| 492 | if (childNode.url) |
| 493 | urls.push(childNode.url); |
| 494 | }); |
| 495 | } |
| 496 | }); |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 497 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 498 | return urls; |
| 499 | }, |
| 500 | |
| 501 | /** |
| 502 | * @param {!Set<string>} itemIds |
| 503 | * @param {function(BookmarkNode):boolean} predicate |
| 504 | * @return {boolean} True if any node in |itemIds| returns true for |
| 505 | * |predicate|. |
| 506 | */ |
| 507 | containsMatchingNode_: function(itemIds, predicate) { |
| 508 | var nodes = this.getState().nodes; |
| 509 | |
| 510 | return Array.from(itemIds).some(function(id) { |
| 511 | return predicate(nodes[id]); |
| 512 | }); |
| 513 | }, |
| 514 | |
| 515 | /** |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 516 | * @param {!Set<string>} itemIds |
| 517 | * @return {boolean} True if |itemIds| is a single bookmark (non-folder) |
| 518 | * node. |
| 519 | */ |
| 520 | isSingleBookmark_: function(itemIds) { |
| 521 | return itemIds.size == 1 && |
| 522 | this.containsMatchingNode_(itemIds, function(node) { |
| 523 | return !!node.url; |
| 524 | }); |
| 525 | }, |
| 526 | |
| 527 | /** |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 528 | * @param {Command} command |
| 529 | * @return {string} |
| 530 | * @private |
| 531 | */ |
| 532 | getCommandLabel_: function(command) { |
| 533 | var multipleNodes = this.menuIds_.size > 1 || |
| 534 | this.containsMatchingNode_(this.menuIds_, function(node) { |
| 535 | return !node.url; |
| 536 | }); |
| 537 | var label; |
| 538 | switch (command) { |
| 539 | case Command.EDIT: |
tsergeant | 4707d17 | 2017-06-05 05:47:02 | [diff] [blame] | 540 | if (this.menuIds_.size != 1) |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 541 | return ''; |
| 542 | |
| 543 | var id = Array.from(this.menuIds_)[0]; |
| 544 | var itemUrl = this.getState().nodes[id].url; |
| 545 | label = itemUrl ? 'menuEdit' : 'menuRename'; |
| 546 | break; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 547 | case Command.COPY_URL: |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 548 | label = 'menuCopyURL'; |
| 549 | break; |
| 550 | case Command.DELETE: |
| 551 | label = 'menuDelete'; |
| 552 | break; |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 553 | case Command.SHOW_IN_FOLDER: |
| 554 | label = 'menuShowInFolder'; |
| 555 | break; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 556 | case Command.OPEN_NEW_TAB: |
| 557 | label = multipleNodes ? 'menuOpenAllNewTab' : 'menuOpenNewTab'; |
| 558 | break; |
| 559 | case Command.OPEN_NEW_WINDOW: |
| 560 | label = multipleNodes ? 'menuOpenAllNewWindow' : 'menuOpenNewWindow'; |
| 561 | break; |
| 562 | case Command.OPEN_INCOGNITO: |
| 563 | label = multipleNodes ? 'menuOpenAllIncognito' : 'menuOpenIncognito'; |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | return loadTimeData.getString(assert(label)); |
| 568 | }, |
| 569 | |
| 570 | /** |
| 571 | * @param {Command} command |
calamity | 64e2012a | 2017-06-21 09:57:14 | [diff] [blame] | 572 | * @return {string} |
| 573 | * @private |
| 574 | */ |
| 575 | getCommandSublabel_: function(command) { |
| 576 | var multipleNodes = this.menuIds_.size > 1 || |
| 577 | this.containsMatchingNode_(this.menuIds_, function(node) { |
| 578 | return !node.url; |
| 579 | }); |
| 580 | switch (command) { |
| 581 | case Command.OPEN_NEW_TAB: |
| 582 | var urls = this.expandUrls_(this.menuIds_); |
| 583 | return multipleNodes && urls.length > 0 ? String(urls.length) : ''; |
| 584 | default: |
| 585 | return ''; |
| 586 | } |
| 587 | }, |
| 588 | |
| 589 | /** @private */ |
| 590 | onMenuIdsChanged_: function() { |
| 591 | if (!this.menuIds_) |
| 592 | return; |
| 593 | |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 594 | this.hasAnySublabel_ = this.menuCommands_.some( |
| 595 | (command) => this.getCommandSublabel_(command) != ''); |
calamity | 64e2012a | 2017-06-21 09:57:14 | [diff] [blame] | 596 | }, |
| 597 | |
| 598 | /** |
| 599 | * @param {Command} command |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 600 | * @return {boolean} |
| 601 | * @private |
| 602 | */ |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 603 | showDividerAfter_: function(command, itemIds) { |
| 604 | return command == Command.DELETE && |
| 605 | (this.globalCanEdit_ || this.isSingleBookmark_(itemIds)); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 606 | }, |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 607 | |
| 608 | /** |
| 609 | * Show a toast with a bookmark |title| inserted into a label, with the |
| 610 | * title ellipsised if necessary. |
| 611 | * @param {!Promise<string>} labelPromise Promise which resolves with the |
| 612 | * label for the toast. |
| 613 | * @param {string} title Bookmark title to insert. |
| 614 | * @param {boolean} canUndo If true, shows an undo button in the toast. |
| 615 | * @private |
| 616 | */ |
| 617 | showTitleToast_: function(labelPromise, title, canUndo) { |
| 618 | labelPromise.then(function(label) { |
| 619 | var pieces = loadTimeData.getSubstitutedStringPieces(label, title) |
| 620 | .map(function(p) { |
| 621 | // Make the bookmark name collapsible. |
| 622 | p.collapsible = !!p.arg; |
| 623 | return p; |
| 624 | }); |
| 625 | |
| 626 | bookmarks.ToastManager.getInstance().showForStringPieces( |
| 627 | pieces, canUndo); |
| 628 | }); |
| 629 | }, |
| 630 | |
| 631 | //////////////////////////////////////////////////////////////////////////// |
| 632 | // Event handlers: |
| 633 | |
| 634 | /** |
| 635 | * @param {Event} e |
| 636 | * @private |
| 637 | */ |
| 638 | onOpenItemMenu_: function(e) { |
| 639 | if (e.detail.targetElement) { |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 640 | this.openCommandMenuAtElement(e.detail.targetElement, e.detail.source); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 641 | } else { |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 642 | this.openCommandMenuAtPosition(e.detail.x, e.detail.y, e.detail.source); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 643 | } |
| 644 | }, |
| 645 | |
| 646 | /** |
| 647 | * @param {Event} e |
| 648 | * @private |
| 649 | */ |
| 650 | onCommandClick_: function(e) { |
| 651 | this.handle( |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 652 | /** @type {Command} */ ( |
| 653 | Number(e.currentTarget.getAttribute('command'))), |
| 654 | assert(this.menuIds_)); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 655 | this.closeCommandMenu(); |
| 656 | }, |
| 657 | |
| 658 | /** |
| 659 | * @param {!Event} e |
| 660 | * @private |
| 661 | */ |
| 662 | onKeydown_: function(e) { |
| 663 | var selection = this.getState().selection.items; |
Tim Sergeant | 109279fe | 2017-07-20 03:07:17 | [diff] [blame] | 664 | if (e.target == document.body && |
| 665 | !bookmarks.DialogFocusManager.getInstance().hasOpenDialog()) { |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 666 | this.handleKeyEvent(e, selection); |
Tim Sergeant | 109279fe | 2017-07-20 03:07:17 | [diff] [blame] | 667 | } |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 668 | }, |
| 669 | |
| 670 | /** |
| 671 | * Close the menu on mousedown so clicks can propagate to the underlying UI. |
| 672 | * This allows the user to right click the list while a context menu is |
| 673 | * showing and get another context menu. |
| 674 | * @param {Event} e |
| 675 | * @private |
| 676 | */ |
| 677 | onMenuMousedown_: function(e) { |
| 678 | if (e.path[0] != this.$.dropdown.getIfExists()) |
| 679 | return; |
| 680 | |
| 681 | this.closeCommandMenu(); |
| 682 | }, |
| 683 | |
| 684 | /** @private */ |
| 685 | onOpenCancelTap_: function() { |
| 686 | this.$.openDialog.get().cancel(); |
| 687 | }, |
| 688 | |
| 689 | /** @private */ |
| 690 | onOpenConfirmTap_: function() { |
| 691 | this.confirmOpenCallback_(); |
| 692 | this.$.openDialog.get().close(); |
| 693 | }, |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 694 | }); |
| 695 | |
| 696 | /** @private {bookmarks.CommandManager} */ |
| 697 | CommandManager.instance_ = null; |
| 698 | |
| 699 | /** @return {!bookmarks.CommandManager} */ |
| 700 | CommandManager.getInstance = function() { |
| 701 | return assert(CommandManager.instance_); |
| 702 | }; |
| 703 | |
| 704 | return { |
| 705 | CommandManager: CommandManager, |
| 706 | }; |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 707 | }); |