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)); |
| 292 | break; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 293 | case Command.DELETE: |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 294 | var idList = Array.from(this.minimizeDeletionSet_(itemIds)); |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 295 | var title = state.nodes[idList[0]].title; |
Christopher Lam | 75ca910 | 2017-07-18 02:15:18 | [diff] [blame] | 296 | var labelPromise; |
| 297 | |
| 298 | if (idList.length == 1) { |
| 299 | labelPromise = |
| 300 | Promise.resolve(loadTimeData.getString('toastItemDeleted')); |
| 301 | } else { |
| 302 | labelPromise = cr.sendWithPromise( |
| 303 | 'getPluralString', 'toastItemsDeleted', idList.length); |
| 304 | } |
| 305 | |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 306 | chrome.bookmarkManagerPrivate.removeTrees(idList, () => { |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 307 | this.showTitleToast_(labelPromise, title, true); |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 308 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 309 | break; |
calamity | 2d4b550 | 2017-05-29 03:57:58 | [diff] [blame] | 310 | case Command.UNDO: |
| 311 | chrome.bookmarkManagerPrivate.undo(); |
calamity | efe47735 | 2017-06-07 06:44:58 | [diff] [blame] | 312 | bookmarks.ToastManager.getInstance().hide(); |
calamity | 2d4b550 | 2017-05-29 03:57:58 | [diff] [blame] | 313 | break; |
| 314 | case Command.REDO: |
| 315 | chrome.bookmarkManagerPrivate.redo(); |
| 316 | break; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 317 | case Command.OPEN_NEW_TAB: |
| 318 | case Command.OPEN_NEW_WINDOW: |
| 319 | case Command.OPEN_INCOGNITO: |
| 320 | this.openUrls_(this.expandUrls_(itemIds), command); |
| 321 | break; |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 322 | case Command.OPEN: |
| 323 | var isFolder = itemIds.size == 1 && |
| 324 | this.containsMatchingNode_(itemIds, function(node) { |
| 325 | return !node.url; |
| 326 | }); |
| 327 | if (isFolder) { |
| 328 | var folderId = Array.from(itemIds)[0]; |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 329 | this.dispatch( |
| 330 | bookmarks.actions.selectFolder(folderId, state.nodes)); |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 331 | } else { |
| 332 | this.openUrls_(this.expandUrls_(itemIds), command); |
| 333 | } |
| 334 | break; |
tsergeant | 679159f | 2017-06-16 06:58:41 | [diff] [blame] | 335 | case Command.SELECT_ALL: |
| 336 | var displayedIds = bookmarks.util.getDisplayedList(state); |
| 337 | this.dispatch(bookmarks.actions.selectAll(displayedIds, state)); |
| 338 | break; |
| 339 | case Command.DESELECT_ALL: |
| 340 | this.dispatch(bookmarks.actions.deselectItems()); |
| 341 | break; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 342 | case Command.CUT: |
| 343 | chrome.bookmarkManagerPrivate.cut(Array.from(itemIds)); |
| 344 | break; |
| 345 | case Command.PASTE: |
| 346 | var selectedFolder = state.selectedFolder; |
| 347 | var selectedItems = state.selection.items; |
| 348 | chrome.bookmarkManagerPrivate.paste( |
| 349 | selectedFolder, Array.from(selectedItems)); |
| 350 | break; |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 351 | default: |
| 352 | assert(false); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 353 | } |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 354 | |
| 355 | bookmarks.util.recordEnumHistogram( |
| 356 | 'BookmarkManager.CommandExecuted', command, Command.MAX_VALUE); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 357 | }, |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 358 | |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 359 | /** |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 360 | * @param {!Event} e |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 361 | * @param {!Set<string>} itemIds |
| 362 | * @return {boolean} True if the event was handled, triggering a keyboard |
| 363 | * shortcut. |
| 364 | */ |
| 365 | handleKeyEvent: function(e, itemIds) { |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 366 | for (var commandTuple of this.shortcuts_) { |
| 367 | var command = /** @type {Command} */ (commandTuple[0]); |
| 368 | var shortcut = |
| 369 | /** @type {cr.ui.KeyboardShortcutList} */ (commandTuple[1]); |
| 370 | if (shortcut.matchesEvent(e) && this.canExecute(command, itemIds)) { |
| 371 | this.handle(command, itemIds); |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 372 | |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 373 | bookmarks.util.recordEnumHistogram( |
| 374 | 'BookmarkManager.CommandExecutedFromKeyboard', command, |
| 375 | Command.MAX_VALUE); |
tsergeant | 6c3a6df | 2017-06-06 23:53:02 | [diff] [blame] | 376 | e.stopPropagation(); |
| 377 | e.preventDefault(); |
| 378 | return true; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | return false; |
| 383 | }, |
| 384 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 385 | //////////////////////////////////////////////////////////////////////////// |
| 386 | // Private functions: |
| 387 | |
| 388 | /** |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 389 | * Register a keyboard shortcut for a command. |
| 390 | * @param {Command} command Command that the shortcut will trigger. |
| 391 | * @param {string} shortcut Keyboard shortcut, using the syntax of |
| 392 | * cr/ui/command.js. |
| 393 | * @param {string=} macShortcut If set, enables a replacement shortcut for |
| 394 | * Mac. |
| 395 | */ |
| 396 | addShortcut_: function(command, shortcut, macShortcut) { |
| 397 | var shortcut = (cr.isMac && macShortcut) ? macShortcut : shortcut; |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 398 | this.shortcuts_.set(command, new cr.ui.KeyboardShortcutList(shortcut)); |
tsergeant | 0292e51a | 2017-06-16 03:44:35 | [diff] [blame] | 399 | }, |
| 400 | |
| 401 | /** |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 402 | * Minimize the set of |itemIds| by removing any node which has an ancestor |
| 403 | * node already in the set. This ensures that instead of trying to delete |
| 404 | * both a node and its descendant, we will only try to delete the topmost |
| 405 | * node, preventing an error in the bookmarkManagerPrivate.removeTrees API |
| 406 | * call. |
| 407 | * @param {!Set<string>} itemIds |
| 408 | * @return {!Set<string>} |
| 409 | */ |
| 410 | minimizeDeletionSet_: function(itemIds) { |
| 411 | var minimizedSet = new Set(); |
| 412 | var nodes = this.getState().nodes; |
| 413 | itemIds.forEach(function(itemId) { |
| 414 | var currentId = itemId; |
| 415 | while (currentId != ROOT_NODE_ID) { |
| 416 | currentId = assert(nodes[currentId].parentId); |
| 417 | if (itemIds.has(currentId)) |
| 418 | return; |
| 419 | } |
| 420 | minimizedSet.add(itemId); |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 421 | }); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 422 | return minimizedSet; |
| 423 | }, |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 424 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 425 | /** |
tsergeant | 7fb9e13f | 2017-06-26 06:35:19 | [diff] [blame] | 426 | * Open the given |urls| in response to a |command|. May show a confirmation |
| 427 | * dialog before opening large numbers of URLs. |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 428 | * @param {!Array<string>} urls |
| 429 | * @param {Command} command |
| 430 | * @private |
| 431 | */ |
| 432 | openUrls_: function(urls, command) { |
| 433 | assert( |
tsergeant | 6c5ad90a | 2017-05-19 14:12:34 | [diff] [blame] | 434 | command == Command.OPEN || command == Command.OPEN_NEW_TAB || |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 435 | command == Command.OPEN_NEW_WINDOW || |
| 436 | command == Command.OPEN_INCOGNITO); |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 437 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 438 | if (urls.length == 0) |
tsergeant | fad224f | 2017-05-05 04:42:09 | [diff] [blame] | 439 | return; |
tsergeant | fad224f | 2017-05-05 04:42:09 | [diff] [blame] | 440 | |
tsergeant | 7fb9e13f | 2017-06-26 06:35:19 | [diff] [blame] | 441 | var openUrlsCallback = function() { |
| 442 | var incognito = command == Command.OPEN_INCOGNITO; |
| 443 | if (command == Command.OPEN_NEW_WINDOW || incognito) { |
| 444 | chrome.windows.create({url: urls, incognito: incognito}); |
| 445 | } else { |
| 446 | if (command == Command.OPEN) |
| 447 | chrome.tabs.create({url: urls.shift(), active: true}); |
| 448 | urls.forEach(function(url) { |
| 449 | chrome.tabs.create({url: url, active: false}); |
| 450 | }); |
| 451 | } |
| 452 | }; |
| 453 | |
| 454 | if (urls.length <= OPEN_CONFIRMATION_LIMIT) { |
| 455 | openUrlsCallback(); |
| 456 | return; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 457 | } |
tsergeant | 7fb9e13f | 2017-06-26 06:35:19 | [diff] [blame] | 458 | |
| 459 | this.confirmOpenCallback_ = openUrlsCallback; |
| 460 | var dialog = this.$.openDialog.get(); |
| 461 | dialog.querySelector('.body').textContent = |
| 462 | loadTimeData.getStringF('openDialogBody', urls.length); |
calamity | 57b2e8fd | 2017-06-29 18:46:58 | [diff] [blame] | 463 | |
| 464 | bookmarks.DialogFocusManager.getInstance().showDialog( |
| 465 | this.$.openDialog.get()); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 466 | }, |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 467 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 468 | /** |
| 469 | * Returns all URLs in the given set of nodes and their immediate children. |
| 470 | * Note that these will be ordered by insertion order into the |itemIds| |
| 471 | * set, and that it is possible to duplicate a URL by passing in both the |
| 472 | * parent ID and child ID. |
| 473 | * @param {!Set<string>} itemIds |
| 474 | * @return {!Array<string>} |
| 475 | * @private |
| 476 | */ |
| 477 | expandUrls_: function(itemIds) { |
| 478 | var urls = []; |
| 479 | var nodes = this.getState().nodes; |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 480 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 481 | itemIds.forEach(function(id) { |
| 482 | var node = nodes[id]; |
| 483 | if (node.url) { |
| 484 | urls.push(node.url); |
| 485 | } else { |
| 486 | node.children.forEach(function(childId) { |
| 487 | var childNode = nodes[childId]; |
| 488 | if (childNode.url) |
| 489 | urls.push(childNode.url); |
| 490 | }); |
| 491 | } |
| 492 | }); |
tsergeant | 13a46646 | 2017-05-15 01:21:03 | [diff] [blame] | 493 | |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 494 | return urls; |
| 495 | }, |
| 496 | |
| 497 | /** |
| 498 | * @param {!Set<string>} itemIds |
| 499 | * @param {function(BookmarkNode):boolean} predicate |
| 500 | * @return {boolean} True if any node in |itemIds| returns true for |
| 501 | * |predicate|. |
| 502 | */ |
| 503 | containsMatchingNode_: function(itemIds, predicate) { |
| 504 | var nodes = this.getState().nodes; |
| 505 | |
| 506 | return Array.from(itemIds).some(function(id) { |
| 507 | return predicate(nodes[id]); |
| 508 | }); |
| 509 | }, |
| 510 | |
| 511 | /** |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 512 | * @param {!Set<string>} itemIds |
| 513 | * @return {boolean} True if |itemIds| is a single bookmark (non-folder) |
| 514 | * node. |
| 515 | */ |
| 516 | isSingleBookmark_: function(itemIds) { |
| 517 | return itemIds.size == 1 && |
| 518 | this.containsMatchingNode_(itemIds, function(node) { |
| 519 | return !!node.url; |
| 520 | }); |
| 521 | }, |
| 522 | |
| 523 | /** |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 524 | * @param {Command} command |
| 525 | * @return {string} |
| 526 | * @private |
| 527 | */ |
| 528 | getCommandLabel_: function(command) { |
| 529 | var multipleNodes = this.menuIds_.size > 1 || |
| 530 | this.containsMatchingNode_(this.menuIds_, function(node) { |
| 531 | return !node.url; |
| 532 | }); |
| 533 | var label; |
| 534 | switch (command) { |
| 535 | case Command.EDIT: |
tsergeant | 4707d17 | 2017-06-05 05:47:02 | [diff] [blame] | 536 | if (this.menuIds_.size != 1) |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 537 | return ''; |
| 538 | |
| 539 | var id = Array.from(this.menuIds_)[0]; |
| 540 | var itemUrl = this.getState().nodes[id].url; |
| 541 | label = itemUrl ? 'menuEdit' : 'menuRename'; |
| 542 | break; |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 543 | case Command.COPY_URL: |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 544 | label = 'menuCopyURL'; |
| 545 | break; |
| 546 | case Command.DELETE: |
| 547 | label = 'menuDelete'; |
| 548 | break; |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 549 | case Command.SHOW_IN_FOLDER: |
| 550 | label = 'menuShowInFolder'; |
| 551 | break; |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 552 | case Command.OPEN_NEW_TAB: |
| 553 | label = multipleNodes ? 'menuOpenAllNewTab' : 'menuOpenNewTab'; |
| 554 | break; |
| 555 | case Command.OPEN_NEW_WINDOW: |
| 556 | label = multipleNodes ? 'menuOpenAllNewWindow' : 'menuOpenNewWindow'; |
| 557 | break; |
| 558 | case Command.OPEN_INCOGNITO: |
| 559 | label = multipleNodes ? 'menuOpenAllIncognito' : 'menuOpenIncognito'; |
| 560 | break; |
| 561 | } |
| 562 | |
| 563 | return loadTimeData.getString(assert(label)); |
| 564 | }, |
| 565 | |
| 566 | /** |
| 567 | * @param {Command} command |
calamity | 64e2012a | 2017-06-21 09:57:14 | [diff] [blame] | 568 | * @return {string} |
| 569 | * @private |
| 570 | */ |
| 571 | getCommandSublabel_: function(command) { |
| 572 | var multipleNodes = this.menuIds_.size > 1 || |
| 573 | this.containsMatchingNode_(this.menuIds_, function(node) { |
| 574 | return !node.url; |
| 575 | }); |
| 576 | switch (command) { |
| 577 | case Command.OPEN_NEW_TAB: |
| 578 | var urls = this.expandUrls_(this.menuIds_); |
| 579 | return multipleNodes && urls.length > 0 ? String(urls.length) : ''; |
| 580 | default: |
| 581 | return ''; |
| 582 | } |
| 583 | }, |
| 584 | |
| 585 | /** @private */ |
| 586 | onMenuIdsChanged_: function() { |
| 587 | if (!this.menuIds_) |
| 588 | return; |
| 589 | |
dpapad | 325bf2f1 | 2017-07-26 18:47:34 | [diff] [blame] | 590 | this.hasAnySublabel_ = this.menuCommands_.some( |
| 591 | (command) => this.getCommandSublabel_(command) != ''); |
calamity | 64e2012a | 2017-06-21 09:57:14 | [diff] [blame] | 592 | }, |
| 593 | |
| 594 | /** |
| 595 | * @param {Command} command |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 596 | * @return {boolean} |
| 597 | * @private |
| 598 | */ |
tsergeant | 2437f99 | 2017-06-13 23:54:29 | [diff] [blame] | 599 | showDividerAfter_: function(command, itemIds) { |
| 600 | return command == Command.DELETE && |
| 601 | (this.globalCanEdit_ || this.isSingleBookmark_(itemIds)); |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 602 | }, |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 603 | |
| 604 | /** |
| 605 | * Show a toast with a bookmark |title| inserted into a label, with the |
| 606 | * title ellipsised if necessary. |
| 607 | * @param {!Promise<string>} labelPromise Promise which resolves with the |
| 608 | * label for the toast. |
| 609 | * @param {string} title Bookmark title to insert. |
| 610 | * @param {boolean} canUndo If true, shows an undo button in the toast. |
| 611 | * @private |
| 612 | */ |
| 613 | showTitleToast_: function(labelPromise, title, canUndo) { |
| 614 | labelPromise.then(function(label) { |
| 615 | var pieces = loadTimeData.getSubstitutedStringPieces(label, title) |
| 616 | .map(function(p) { |
| 617 | // Make the bookmark name collapsible. |
| 618 | p.collapsible = !!p.arg; |
| 619 | return p; |
| 620 | }); |
| 621 | |
| 622 | bookmarks.ToastManager.getInstance().showForStringPieces( |
| 623 | pieces, canUndo); |
| 624 | }); |
| 625 | }, |
| 626 | |
| 627 | //////////////////////////////////////////////////////////////////////////// |
| 628 | // Event handlers: |
| 629 | |
| 630 | /** |
| 631 | * @param {Event} e |
| 632 | * @private |
| 633 | */ |
| 634 | onOpenItemMenu_: function(e) { |
| 635 | if (e.detail.targetElement) { |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 636 | this.openCommandMenuAtElement(e.detail.targetElement, e.detail.source); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 637 | } else { |
tsergeant | d16c95a | 2017-07-14 04:49:43 | [diff] [blame] | 638 | this.openCommandMenuAtPosition(e.detail.x, e.detail.y, e.detail.source); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 639 | } |
| 640 | }, |
| 641 | |
| 642 | /** |
| 643 | * @param {Event} e |
| 644 | * @private |
| 645 | */ |
| 646 | onCommandClick_: function(e) { |
| 647 | this.handle( |
Tim Sergeant | a2233c81 | 2017-07-26 03:02:47 | [diff] [blame] | 648 | /** @type {Command} */ ( |
| 649 | Number(e.currentTarget.getAttribute('command'))), |
| 650 | assert(this.menuIds_)); |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 651 | this.closeCommandMenu(); |
| 652 | }, |
| 653 | |
| 654 | /** |
| 655 | * @param {!Event} e |
| 656 | * @private |
| 657 | */ |
| 658 | onKeydown_: function(e) { |
| 659 | var selection = this.getState().selection.items; |
Tim Sergeant | 109279fe | 2017-07-20 03:07:17 | [diff] [blame] | 660 | if (e.target == document.body && |
| 661 | !bookmarks.DialogFocusManager.getInstance().hasOpenDialog()) { |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 662 | this.handleKeyEvent(e, selection); |
Tim Sergeant | 109279fe | 2017-07-20 03:07:17 | [diff] [blame] | 663 | } |
tsergeant | b7253e9 | 2017-07-04 02:59:22 | [diff] [blame] | 664 | }, |
| 665 | |
| 666 | /** |
| 667 | * Close the menu on mousedown so clicks can propagate to the underlying UI. |
| 668 | * This allows the user to right click the list while a context menu is |
| 669 | * showing and get another context menu. |
| 670 | * @param {Event} e |
| 671 | * @private |
| 672 | */ |
| 673 | onMenuMousedown_: function(e) { |
| 674 | if (e.path[0] != this.$.dropdown.getIfExists()) |
| 675 | return; |
| 676 | |
| 677 | this.closeCommandMenu(); |
| 678 | }, |
| 679 | |
| 680 | /** @private */ |
| 681 | onOpenCancelTap_: function() { |
| 682 | this.$.openDialog.get().cancel(); |
| 683 | }, |
| 684 | |
| 685 | /** @private */ |
| 686 | onOpenConfirmTap_: function() { |
| 687 | this.confirmOpenCallback_(); |
| 688 | this.$.openDialog.get().close(); |
| 689 | }, |
tsergeant | 2db3626 | 2017-05-15 02:47:53 | [diff] [blame] | 690 | }); |
| 691 | |
| 692 | /** @private {bookmarks.CommandManager} */ |
| 693 | CommandManager.instance_ = null; |
| 694 | |
| 695 | /** @return {!bookmarks.CommandManager} */ |
| 696 | CommandManager.getInstance = function() { |
| 697 | return assert(CommandManager.instance_); |
| 698 | }; |
| 699 | |
| 700 | return { |
| 701 | CommandManager: CommandManager, |
| 702 | }; |
tsergeant | 7736518 | 2017-05-05 04:02:33 | [diff] [blame] | 703 | }); |