Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 Polymer({ | 5 /** |
| 6 is: 'bookmarks-command-manager', | 6 * @fileoverview Element which shows context menus and handles keyboard |
| 7 | 7 * shortcuts. |
| 8 behaviors: [ | 8 */ |
| 9 bookmarks.StoreClient, | 9 cr.define('bookmarks', function() { |
| 10 ], | 10 |
| 11 | 11 var CommandManager = Polymer({ |
| 12 properties: { | 12 is: 'bookmarks-command-manager', |
| 13 /** @private {!Array<Command>} */ | 13 |
| 14 menuCommands_: { | 14 behaviors: [ |
| 15 type: Array, | 15 bookmarks.StoreClient, |
| 16 value: function() { | 16 ], |
| 17 return [ | 17 |
| 18 Command.EDIT, | 18 properties: { |
| 19 Command.COPY, | 19 /** @private {!Array<Command>} */ |
| 20 Command.DELETE, | 20 menuCommands_: { |
| 21 // <hr> | 21 type: Array, |
| 22 Command.OPEN_NEW_TAB, | 22 value: function() { |
| 23 Command.OPEN_NEW_WINDOW, | 23 return [ |
| 24 Command.OPEN_INCOGNITO, | 24 Command.EDIT, |
| 25 ]; | 25 Command.COPY, |
| 26 Command.DELETE, | |
| 27 // <hr> | |
| 28 Command.OPEN_NEW_TAB, | |
| 29 Command.OPEN_NEW_WINDOW, | |
| 30 Command.OPEN_INCOGNITO, | |
| 31 ]; | |
| 32 }, | |
| 26 }, | 33 }, |
| 27 }, | 34 |
| 28 | 35 /** @type {Set<string>} */ |
| 29 /** @type {Set<string>} */ | 36 menuIds_: Object, |
| 30 menuIds_: Object, | 37 }, |
| 31 }, | 38 |
| 32 | 39 attached: function() { |
| 33 attached: function() { | 40 assert(CommandManager.instance_ == null); |
| 34 /** @private {function(!Event)} */ | 41 CommandManager.instance_ = this; |
|
tsergeant
2017/05/11 05:02:24
The only changes are here in attached(), below in
calamity
2017/05/12 03:24:57
Acknowledged.
| |
| 35 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); | 42 |
| 36 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); | 43 /** @private {function(!Event)} */ |
| 37 | 44 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); |
| 38 /** @private {function(!Event)} */ | 45 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); |
| 39 this.boundOnKeydown_ = this.onKeydown_.bind(this); | 46 |
| 40 document.addEventListener('keydown', this.boundOnKeydown_); | 47 /** @private {function(!Event)} */ |
| 41 | 48 this.boundOnKeydown_ = this.onKeydown_.bind(this); |
| 42 /** @private {Object<Command, string>} */ | 49 document.addEventListener('keydown', this.boundOnKeydown_); |
| 43 this.shortcuts_ = {}; | 50 |
| 44 this.shortcuts_[Command.EDIT] = cr.isMac ? 'enter' : 'f2'; | 51 /** @private {Object<Command, string>} */ |
| 45 this.shortcuts_[Command.COPY] = cr.isMac ? 'meta+c' : 'ctrl+c'; | 52 this.shortcuts_ = {}; |
| 46 this.shortcuts_[Command.DELETE] = cr.isMac ? 'delete backspace' : 'delete'; | 53 this.shortcuts_[Command.EDIT] = cr.isMac ? 'enter' : 'f2'; |
| 47 this.shortcuts_[Command.OPEN_NEW_TAB] = | 54 this.shortcuts_[Command.COPY] = cr.isMac ? 'meta+c' : 'ctrl+c'; |
| 48 cr.isMac ? 'meta+enter' : 'ctrl+enter'; | 55 this.shortcuts_[Command.DELETE] = |
| 49 this.shortcuts_[Command.OPEN_NEW_WINDOW] = 'shift+enter'; | 56 cr.isMac ? 'delete backspace' : 'delete'; |
| 50 }, | 57 this.shortcuts_[Command.OPEN_NEW_TAB] = |
| 51 | 58 cr.isMac ? 'meta+enter' : 'ctrl+enter'; |
| 52 detached: function() { | 59 this.shortcuts_[Command.OPEN_NEW_WINDOW] = 'shift+enter'; |
| 53 document.removeEventListener('open-item-menu', this.boundOnOpenItemMenu_); | 60 }, |
| 54 document.removeEventListener('keydown', this.boundOnKeydown_); | 61 |
| 55 }, | 62 detached: function() { |
| 56 | 63 CommandManager.instance_ = null; |
| 57 /** | 64 document.removeEventListener('open-item-menu', this.boundOnOpenItemMenu_); |
| 58 * Display the command context menu at (|x|, |y|) in window co-ordinates. | 65 document.removeEventListener('keydown', this.boundOnKeydown_); |
| 59 * Commands will execute on the currently selected items. | 66 }, |
| 60 * @param {number} x | 67 |
| 61 * @param {number} y | 68 /** |
| 62 */ | 69 * Display the command context menu at (|x|, |y|) in window co-ordinates. |
| 63 openCommandMenuAtPosition: function(x, y) { | 70 * Commands will execute on the currently selected items. |
| 64 this.menuIds_ = this.getState().selection.items; | 71 * @param {number} x |
| 65 /** @type {!CrActionMenuElement} */ (this.$.dropdown) | 72 * @param {number} y |
| 66 .showAtPosition({top: y, left: x}); | 73 */ |
| 67 }, | 74 openCommandMenuAtPosition: function(x, y) { |
| 68 | 75 this.menuIds_ = this.getState().selection.items; |
| 69 /** | 76 /** @type {!CrActionMenuElement} */ (this.$.dropdown) |
| 70 * Display the command context menu positioned to cover the |target| | 77 .showAtPosition({top: y, left: x}); |
| 71 * element. Commands will execute on the currently selected items. | 78 }, |
| 72 * @param {!Element} target | 79 |
| 73 */ | 80 /** |
| 74 openCommandMenuAtElement: function(target) { | 81 * Display the command context menu positioned to cover the |target| |
| 75 this.menuIds_ = this.getState().selection.items; | 82 * element. Commands will execute on the currently selected items. |
| 76 /** @type {!CrActionMenuElement} */ (this.$.dropdown).showAt(target); | 83 * @param {!Element} target |
| 77 }, | 84 */ |
| 78 | 85 openCommandMenuAtElement: function(target) { |
| 79 closeCommandMenu: function() { | 86 this.menuIds_ = this.getState().selection.items; |
| 80 /** @type {!CrActionMenuElement} */ (this.$.dropdown).close(); | 87 /** @type {!CrActionMenuElement} */ (this.$.dropdown).showAt(target); |
| 81 }, | 88 }, |
| 82 | 89 |
| 83 //////////////////////////////////////////////////////////////////////////// | 90 closeCommandMenu: function() { |
| 84 // Command handlers: | 91 /** @type {!CrActionMenuElement} */ (this.$.dropdown).close(); |
| 85 | 92 }, |
| 86 /** | 93 |
| 87 * Determine if the |command| can be executed with the given |itemIds|. | 94 //////////////////////////////////////////////////////////////////////////// |
| 88 * Commands which appear in the context menu should be implemented separately | 95 // Command handlers: |
| 89 * using `isCommandVisible_` and `isCommandEnabled_`. | 96 |
| 90 * @param {Command} command | 97 /** |
| 91 * @param {!Set<string>} itemIds | 98 * Determine if the |command| can be executed with the given |itemIds|. |
| 92 * @return {boolean} | 99 * Commands which appear in the context menu should be implemented |
| 93 */ | 100 * separately using `isCommandVisible_` and `isCommandEnabled_`. |
| 94 canExecute: function(command, itemIds) { | 101 * @param {Command} command |
| 95 return this.isCommandVisible_(command, itemIds) && | 102 * @param {!Set<string>} itemIds |
| 96 this.isCommandEnabled_(command, itemIds); | 103 * @return {boolean} |
| 97 }, | 104 */ |
| 98 | 105 canExecute: function(command, itemIds) { |
| 99 /** | 106 return this.isCommandVisible_(command, itemIds) && |
| 100 * @param {Command} command | 107 this.isCommandEnabled_(command, itemIds); |
| 101 * @param {!Set<string>} itemIds | 108 }, |
| 102 * @return {boolean} True if the command should be visible in the context | 109 |
| 103 * menu. | 110 /** |
| 104 */ | 111 * @param {Command} command |
| 105 isCommandVisible_: function(command, itemIds) { | 112 * @param {!Set<string>} itemIds |
| 106 switch (command) { | 113 * @return {boolean} True if the command should be visible in the context |
| 107 case Command.EDIT: | 114 * menu. |
| 108 return itemIds.size == 1; | 115 */ |
| 109 case Command.COPY: | 116 isCommandVisible_: function(command, itemIds) { |
| 110 return itemIds.size == 1 && | 117 switch (command) { |
| 111 this.containsMatchingNode_(itemIds, function(node) { | 118 case Command.EDIT: |
| 112 return !!node.url; | 119 return itemIds.size == 1; |
| 113 }); | 120 case Command.COPY: |
| 114 case Command.DELETE: | 121 return itemIds.size == 1 && |
| 115 case Command.OPEN_NEW_TAB: | 122 this.containsMatchingNode_(itemIds, function(node) { |
| 116 case Command.OPEN_NEW_WINDOW: | 123 return !!node.url; |
| 117 case Command.OPEN_INCOGNITO: | 124 }); |
| 118 return itemIds.size > 0; | 125 case Command.DELETE: |
| 119 default: | 126 case Command.OPEN_NEW_TAB: |
| 120 return false; | 127 case Command.OPEN_NEW_WINDOW: |
| 121 } | 128 case Command.OPEN_INCOGNITO: |
| 122 }, | 129 return itemIds.size > 0; |
| 123 | 130 default: |
| 124 /** | 131 return false; |
| 125 * @param {Command} command | 132 } |
| 126 * @param {!Set<string>} itemIds | 133 }, |
| 127 * @return {boolean} True if the command should be clickable in the context | 134 |
| 128 * menu. | 135 /** |
| 129 */ | 136 * @param {Command} command |
| 130 isCommandEnabled_: function(command, itemIds) { | 137 * @param {!Set<string>} itemIds |
| 131 switch (command) { | 138 * @return {boolean} True if the command should be clickable in the context |
| 132 case Command.OPEN_NEW_TAB: | 139 * menu. |
| 133 case Command.OPEN_NEW_WINDOW: | 140 */ |
| 134 case Command.OPEN_INCOGNITO: | 141 isCommandEnabled_: function(command, itemIds) { |
| 135 return this.expandUrls_(itemIds).length > 0; | 142 switch (command) { |
| 136 default: | 143 case Command.OPEN_NEW_TAB: |
| 137 return true; | 144 case Command.OPEN_NEW_WINDOW: |
| 138 } | 145 case Command.OPEN_INCOGNITO: |
| 139 }, | 146 return this.expandUrls_(itemIds).length > 0; |
| 140 | 147 default: |
| 141 /** | 148 return true; |
| 142 * @param {Command} command | 149 } |
| 143 * @param {!Set<string>} itemIds | 150 }, |
| 144 */ | 151 |
| 145 handle: function(command, itemIds) { | 152 /** |
| 146 switch (command) { | 153 * @param {Command} command |
| 147 case Command.EDIT: | 154 * @param {!Set<string>} itemIds |
| 148 var id = Array.from(itemIds)[0]; | 155 */ |
| 149 /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) | 156 handle: function(command, itemIds) { |
| 150 .showEditDialog(this.getState().nodes[id]); | 157 switch (command) { |
| 151 break; | 158 case Command.EDIT: |
| 152 case Command.COPY: | 159 var id = Array.from(itemIds)[0]; |
| 153 var idList = Array.from(itemIds); | 160 /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) |
| 154 chrome.bookmarkManagerPrivate.copy(idList, function() { | 161 .showEditDialog(this.getState().nodes[id]); |
| 155 // TODO(jiaxi): Add toast later. | 162 break; |
| 163 case Command.COPY: | |
| 164 var idList = Array.from(itemIds); | |
| 165 chrome.bookmarkManagerPrivate.copy(idList, function() { | |
| 166 // TODO(jiaxi): Add toast later. | |
| 167 }); | |
| 168 break; | |
| 169 case Command.DELETE: | |
| 170 chrome.bookmarkManagerPrivate.removeTrees( | |
| 171 Array.from(this.minimizeDeletionSet_(itemIds)), function() { | |
| 172 // TODO(jiaxi): Add toast later. | |
| 173 }); | |
| 174 break; | |
| 175 case Command.OPEN_NEW_TAB: | |
| 176 case Command.OPEN_NEW_WINDOW: | |
| 177 case Command.OPEN_INCOGNITO: | |
| 178 this.openUrls_(this.expandUrls_(itemIds), command); | |
| 179 break; | |
| 180 } | |
| 181 }, | |
| 182 | |
| 183 //////////////////////////////////////////////////////////////////////////// | |
| 184 // Private functions: | |
| 185 | |
| 186 /** | |
| 187 * Minimize the set of |itemIds| by removing any node which has an ancestor | |
| 188 * node already in the set. This ensures that instead of trying to delete | |
| 189 * both a node and its descendant, we will only try to delete the topmost | |
| 190 * node, preventing an error in the bookmarkManagerPrivate.removeTrees API | |
| 191 * call. | |
| 192 * @param {!Set<string>} itemIds | |
| 193 * @return {!Set<string>} | |
| 194 */ | |
| 195 minimizeDeletionSet_: function(itemIds) { | |
| 196 var minimizedSet = new Set(); | |
| 197 var nodes = this.getState().nodes; | |
| 198 itemIds.forEach(function(itemId) { | |
| 199 var currentId = itemId; | |
| 200 while (currentId != ROOT_NODE_ID) { | |
| 201 currentId = assert(nodes[currentId].parentId); | |
| 202 if (itemIds.has(currentId)) | |
| 203 return; | |
| 204 } | |
| 205 minimizedSet.add(itemId); | |
| 206 }); | |
| 207 return minimizedSet; | |
| 208 }, | |
| 209 | |
| 210 /** | |
| 211 * @param {!Array<string>} urls | |
| 212 * @param {Command} command | |
| 213 * @private | |
| 214 */ | |
| 215 openUrls_: function(urls, command) { | |
| 216 assert( | |
| 217 command == Command.OPEN_NEW_TAB || | |
| 218 command == Command.OPEN_NEW_WINDOW || | |
| 219 command == Command.OPEN_INCOGNITO); | |
| 220 | |
| 221 if (urls.length == 0) | |
| 222 return; | |
| 223 | |
| 224 var incognito = command == Command.OPEN_INCOGNITO; | |
| 225 if (command == Command.OPEN_NEW_WINDOW || incognito) { | |
| 226 chrome.windows.create({url: urls, incognito: incognito}); | |
| 227 } else { | |
| 228 urls.forEach(function(url) { | |
| 229 chrome.tabs.create({url: url, active: false}); | |
| 156 }); | 230 }); |
| 157 break; | 231 } |
| 158 case Command.DELETE: | 232 }, |
| 159 chrome.bookmarkManagerPrivate.removeTrees( | 233 |
| 160 Array.from(this.minimizeDeletionSet_(itemIds)), function() { | 234 /** |
| 161 // TODO(jiaxi): Add toast later. | 235 * Returns all URLs in the given set of nodes and their immediate children. |
| 162 }); | 236 * Note that these will be ordered by insertion order into the |itemIds| |
| 163 break; | 237 * set. |
| 164 case Command.OPEN_NEW_TAB: | 238 * @param {!Set<string>} itemIds |
| 165 case Command.OPEN_NEW_WINDOW: | 239 * @return {!Array<string>} |
| 166 case Command.OPEN_INCOGNITO: | 240 * @private |
| 167 this.openUrls_(this.expandUrls_(itemIds), command); | 241 */ |
| 168 break; | 242 expandUrls_: function(itemIds) { |
| 169 } | 243 var urls = []; |
| 170 }, | 244 var nodes = this.getState().nodes; |
| 171 | 245 |
| 172 //////////////////////////////////////////////////////////////////////////// | 246 itemIds.forEach(function(id) { |
| 173 // Private functions: | 247 var node = nodes[id]; |
| 174 | 248 if (node.url) { |
| 175 /** | 249 urls.push(node.url); |
| 176 * Minimize the set of |itemIds| by removing any node which has an ancestor | 250 } else { |
| 177 * node already in the set. This ensures that instead of trying to delete both | 251 node.children.forEach(function(childId) { |
| 178 * a node and its descendant, we will only try to delete the topmost node, | 252 var childNode = nodes[childId]; |
| 179 * preventing an error in the bookmarkManagerPrivate.removeTrees API call. | 253 if (childNode.url) |
| 180 * @param {!Set<string>} itemIds | 254 urls.push(childNode.url); |
| 181 * @return {!Set<string>} | 255 }); |
| 182 */ | 256 } |
| 183 minimizeDeletionSet_: function(itemIds) { | 257 }); |
| 184 var minimizedSet = new Set(); | 258 |
| 185 var nodes = this.getState().nodes; | 259 return urls; |
| 186 itemIds.forEach(function(itemId) { | 260 }, |
| 187 var currentId = itemId; | 261 |
| 188 while (currentId != ROOT_NODE_ID) { | 262 /** |
| 189 currentId = assert(nodes[currentId].parentId); | 263 * @param {!Set<string>} itemIds |
| 190 if (itemIds.has(currentId)) | 264 * @param {function(BookmarkNode):boolean} predicate |
| 265 * @return {boolean} True if any node in |itemIds| returns true for | |
| 266 * |predicate|. | |
| 267 */ | |
| 268 containsMatchingNode_: function(itemIds, predicate) { | |
| 269 var nodes = this.getState().nodes; | |
| 270 | |
| 271 return Array.from(itemIds).some(function(id) { | |
| 272 return predicate(nodes[id]); | |
| 273 }); | |
| 274 }, | |
| 275 | |
| 276 /** | |
| 277 * @param {Event} e | |
| 278 * @private | |
| 279 */ | |
| 280 onOpenItemMenu_: function(e) { | |
| 281 if (e.detail.targetElement) { | |
| 282 this.openCommandMenuAtElement(e.detail.targetElement); | |
| 283 } else { | |
| 284 this.openCommandMenuAtPosition(e.detail.x, e.detail.y); | |
| 285 } | |
| 286 }, | |
| 287 | |
| 288 /** | |
| 289 * @param {Event} e | |
| 290 * @private | |
| 291 */ | |
| 292 onCommandClick_: function(e) { | |
| 293 this.closeCommandMenu(); | |
| 294 this.handle(e.target.getAttribute('command'), assert(this.menuIds_)); | |
| 295 }, | |
| 296 | |
| 297 /** | |
| 298 * @param {!Event} e | |
| 299 * @private | |
| 300 */ | |
| 301 onKeydown_: function(e) { | |
| 302 var selection = this.getState().selection.items; | |
| 303 // TODO(tsergeant): Prevent keyboard shortcuts when a dialog is open or | |
| 304 // text field is focused. | |
| 305 for (var commandName in this.shortcuts_) { | |
| 306 var shortcut = this.shortcuts_[commandName]; | |
| 307 if (Polymer.IronA11yKeysBehavior.keyboardEventMatchesKeys( | |
| 308 e, shortcut) && | |
| 309 this.canExecute(commandName, selection)) { | |
| 310 this.handle(commandName, selection); | |
| 311 | |
| 312 e.stopPropagation(); | |
| 313 e.preventDefault(); | |
| 191 return; | 314 return; |
| 192 } | 315 } |
| 193 minimizedSet.add(itemId); | 316 } |
| 194 }); | 317 }, |
| 195 return minimizedSet; | 318 |
| 196 }, | 319 /** |
| 197 | 320 * Close the menu on mousedown so clicks can propagate to the underlying UI. |
| 198 /** | 321 * This allows the user to right click the list while a context menu is |
| 199 * @param {!Array<string>} urls | 322 * showing and get another context menu. |
| 200 * @param {Command} command | 323 * @param {Event} e |
| 201 * @private | 324 * @private |
| 202 */ | 325 */ |
| 203 openUrls_: function(urls, command) { | 326 onMenuMousedown_: function(e) { |
| 204 assert( | 327 if (e.path[0] != this.$.dropdown) |
| 205 command == Command.OPEN_NEW_TAB || command == Command.OPEN_NEW_WINDOW || | |
| 206 command == Command.OPEN_INCOGNITO); | |
| 207 | |
| 208 if (urls.length == 0) | |
| 209 return; | |
| 210 | |
| 211 var incognito = command == Command.OPEN_INCOGNITO; | |
| 212 if (command == Command.OPEN_NEW_WINDOW || incognito) { | |
| 213 chrome.windows.create({url: urls, incognito: incognito}); | |
| 214 } else { | |
| 215 urls.forEach(function(url) { | |
| 216 chrome.tabs.create({url: url, active: false}); | |
| 217 }); | |
| 218 } | |
| 219 }, | |
| 220 | |
| 221 /** | |
| 222 * Returns all URLs in the given set of nodes and their immediate children. | |
| 223 * Note that these will be ordered by insertion order into the |itemIds| set. | |
| 224 * @param {!Set<string>} itemIds | |
| 225 * @return {!Array<string>} | |
| 226 * @private | |
| 227 */ | |
| 228 expandUrls_: function(itemIds) { | |
| 229 var urls = []; | |
| 230 var nodes = this.getState().nodes; | |
| 231 | |
| 232 itemIds.forEach(function(id) { | |
| 233 var node = nodes[id]; | |
| 234 if (node.url) { | |
| 235 urls.push(node.url); | |
| 236 } else { | |
| 237 node.children.forEach(function(childId) { | |
| 238 var childNode = nodes[childId]; | |
| 239 if (childNode.url) | |
| 240 urls.push(childNode.url); | |
| 241 }); | |
| 242 } | |
| 243 }); | |
| 244 | |
| 245 return urls; | |
| 246 }, | |
| 247 | |
| 248 /** | |
| 249 * @param {!Set<string>} itemIds | |
| 250 * @param {function(BookmarkNode):boolean} predicate | |
| 251 * @return {boolean} True if any node in |itemIds| returns true for | |
| 252 * |predicate|. | |
| 253 */ | |
| 254 containsMatchingNode_: function(itemIds, predicate) { | |
| 255 var nodes = this.getState().nodes; | |
| 256 | |
| 257 return Array.from(itemIds).some(function(id) { | |
| 258 return predicate(nodes[id]); | |
| 259 }); | |
| 260 }, | |
| 261 | |
| 262 /** | |
| 263 * @param {Event} e | |
| 264 * @private | |
| 265 */ | |
| 266 onOpenItemMenu_: function(e) { | |
| 267 if (e.detail.targetElement) { | |
| 268 this.openCommandMenuAtElement(e.detail.targetElement); | |
| 269 } else { | |
| 270 this.openCommandMenuAtPosition(e.detail.x, e.detail.y); | |
| 271 } | |
| 272 }, | |
| 273 | |
| 274 /** | |
| 275 * @param {Event} e | |
| 276 * @private | |
| 277 */ | |
| 278 onCommandClick_: function(e) { | |
| 279 this.closeCommandMenu(); | |
| 280 this.handle(e.target.getAttribute('command'), assert(this.menuIds_)); | |
| 281 }, | |
| 282 | |
| 283 /** | |
| 284 * @param {!Event} e | |
| 285 * @private | |
| 286 */ | |
| 287 onKeydown_: function(e) { | |
| 288 var selection = this.getState().selection.items; | |
| 289 // TODO(tsergeant): Prevent keyboard shortcuts when a dialog is open or text | |
| 290 // field is focused. | |
| 291 for (var commandName in this.shortcuts_) { | |
| 292 var shortcut = this.shortcuts_[commandName]; | |
| 293 if (Polymer.IronA11yKeysBehavior.keyboardEventMatchesKeys(e, shortcut) && | |
| 294 this.canExecute(commandName, selection)) { | |
| 295 this.handle(commandName, selection); | |
| 296 | |
| 297 e.stopPropagation(); | |
| 298 e.preventDefault(); | |
| 299 return; | 328 return; |
| 300 } | 329 |
| 301 } | 330 this.$.dropdown.close(); |
| 302 }, | 331 }, |
| 303 | 332 |
| 304 /** | 333 /** |
| 305 * Close the menu on mousedown so clicks can propagate to the underlying UI. | 334 * @param {Command} command |
| 306 * This allows the user to right click the list while a context menu is | 335 * @return {string} |
| 307 * showing and get another context menu. | 336 * @private |
| 308 * @param {Event} e | 337 */ |
| 309 * @private | 338 getCommandLabel_: function(command) { |
| 310 */ | 339 var multipleNodes = this.menuIds_.size > 1 || |
| 311 onMenuMousedown_: function(e) { | 340 this.containsMatchingNode_(this.menuIds_, function(node) { |
| 312 if (e.path[0] != this.$.dropdown) | 341 return !node.url; |
| 313 return; | 342 }); |
| 314 | 343 var label; |
| 315 this.$.dropdown.close(); | 344 switch (command) { |
| 316 }, | 345 case Command.EDIT: |
| 317 | 346 if (this.menuIds_.size > 1) |
| 318 /** | 347 return ''; |
| 319 * @param {Command} command | 348 |
| 320 * @return {string} | 349 var id = Array.from(this.menuIds_)[0]; |
| 321 * @private | 350 var itemUrl = this.getState().nodes[id].url; |
| 322 */ | 351 label = itemUrl ? 'menuEdit' : 'menuRename'; |
| 323 getCommandLabel_: function(command) { | 352 break; |
| 324 var multipleNodes = this.menuIds_.size > 1 || | 353 case Command.COPY: |
| 325 this.containsMatchingNode_(this.menuIds_, function(node) { | 354 label = 'menuCopyURL'; |
| 326 return !node.url; | 355 break; |
| 327 }); | 356 case Command.DELETE: |
| 328 var label; | 357 label = 'menuDelete'; |
| 329 switch (command) { | 358 break; |
| 330 case Command.EDIT: | 359 case Command.OPEN_NEW_TAB: |
| 331 if (this.menuIds_.size > 1) | 360 label = multipleNodes ? 'menuOpenAllNewTab' : 'menuOpenNewTab'; |
| 332 return ''; | 361 break; |
| 333 | 362 case Command.OPEN_NEW_WINDOW: |
| 334 var id = Array.from(this.menuIds_)[0]; | 363 label = multipleNodes ? 'menuOpenAllNewWindow' : 'menuOpenNewWindow'; |
| 335 var itemUrl = this.getState().nodes[id].url; | 364 break; |
| 336 label = itemUrl ? 'menuEdit' : 'menuRename'; | 365 case Command.OPEN_INCOGNITO: |
| 337 break; | 366 label = multipleNodes ? 'menuOpenAllIncognito' : 'menuOpenIncognito'; |
| 338 case Command.COPY: | 367 break; |
| 339 label = 'menuCopyURL'; | 368 } |
| 340 break; | 369 |
| 341 case Command.DELETE: | 370 return loadTimeData.getString(assert(label)); |
| 342 label = 'menuDelete'; | 371 }, |
| 343 break; | 372 |
| 344 case Command.OPEN_NEW_TAB: | 373 /** |
| 345 label = multipleNodes ? 'menuOpenAllNewTab' : 'menuOpenNewTab'; | 374 * @param {Command} command |
| 346 break; | 375 * @return {boolean} |
| 347 case Command.OPEN_NEW_WINDOW: | 376 * @private |
| 348 label = multipleNodes ? 'menuOpenAllNewWindow' : 'menuOpenNewWindow'; | 377 */ |
| 349 break; | 378 showDividerAfter_: function(command) { |
| 350 case Command.OPEN_INCOGNITO: | 379 return command == Command.DELETE; |
| 351 label = multipleNodes ? 'menuOpenAllIncognito' : 'menuOpenIncognito'; | 380 }, |
| 352 break; | 381 }); |
| 353 } | 382 |
| 354 | 383 /** @private {bookmarks.CommandManager} */ |
| 355 return loadTimeData.getString(assert(label)); | 384 CommandManager.instance_ = null; |
| 356 }, | 385 |
| 357 | 386 /** @return {!bookmarks.CommandManager} */ |
| 358 /** | 387 CommandManager.getInstance = function() { |
| 359 * @param {Command} command | 388 return assert(CommandManager.instance_); |
| 360 * @return {boolean} | 389 }; |
| 361 * @private | 390 |
| 362 */ | 391 return { |
| 363 showDividerAfter_: function(command) { | 392 CommandManager: CommandManager, |
| 364 return command == Command.DELETE; | 393 }; |
| 365 }, | |
| 366 }); | 394 }); |
| OLD | NEW |