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