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 /** | 5 /** |
| 6 * @fileoverview Element which shows context menus and handles keyboard | 6 * @fileoverview Element which shows context menus and handles keyboard |
| 7 * shortcuts. | 7 * shortcuts. |
| 8 */ | 8 */ |
| 9 cr.define('bookmarks', function() { | 9 cr.define('bookmarks', function() { |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 Command.COPY, | 25 Command.COPY, |
| 26 Command.DELETE, | 26 Command.DELETE, |
| 27 // <hr> | 27 // <hr> |
| 28 Command.OPEN_NEW_TAB, | 28 Command.OPEN_NEW_TAB, |
| 29 Command.OPEN_NEW_WINDOW, | 29 Command.OPEN_NEW_WINDOW, |
| 30 Command.OPEN_INCOGNITO, | 30 Command.OPEN_INCOGNITO, |
| 31 ]; | 31 ]; |
| 32 }, | 32 }, |
| 33 }, | 33 }, |
| 34 | 34 |
| 35 /** @type {Set<string>} */ | 35 /** @private {Set<string>} */ |
| 36 menuIds_: Object, | 36 menuIds_: Object, |
| 37 | |
| 38 /** @private */ | |
| 39 canEdit_: Boolean, | |
| 37 }, | 40 }, |
| 38 | 41 |
| 39 attached: function() { | 42 attached: function() { |
| 40 assert(CommandManager.instance_ == null); | 43 assert(CommandManager.instance_ == null); |
| 41 CommandManager.instance_ = this; | 44 CommandManager.instance_ = this; |
| 42 | 45 |
| 46 this.watch('canEdit_', function(state) { | |
| 47 return state.prefs.canEdit; | |
| 48 }); | |
| 49 this.updateFromStore(); | |
| 50 | |
| 43 /** @private {function(!Event)} */ | 51 /** @private {function(!Event)} */ |
| 44 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); | 52 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); |
| 45 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); | 53 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); |
| 46 | 54 |
| 47 /** @private {function()} */ | 55 /** @private {function()} */ |
| 48 this.boundOnCommandUndo_ = function() { | 56 this.boundOnCommandUndo_ = function() { |
| 49 this.handle(Command.UNDO, new Set()); | 57 this.handle(Command.UNDO, new Set()); |
| 50 }.bind(this); | 58 }.bind(this); |
| 51 document.addEventListener('command-undo', this.boundOnCommandUndo_); | 59 document.addEventListener('command-undo', this.boundOnCommandUndo_); |
| 52 | 60 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 113 * @param {Command} command | 121 * @param {Command} command |
| 114 * @param {!Set<string>} itemIds | 122 * @param {!Set<string>} itemIds |
| 115 * @return {boolean} | 123 * @return {boolean} |
| 116 */ | 124 */ |
| 117 canExecute: function(command, itemIds) { | 125 canExecute: function(command, itemIds) { |
| 118 switch (command) { | 126 switch (command) { |
| 119 case Command.OPEN: | 127 case Command.OPEN: |
| 120 return itemIds.size > 0; | 128 return itemIds.size > 0; |
| 121 case Command.UNDO: | 129 case Command.UNDO: |
| 122 case Command.REDO: | 130 case Command.REDO: |
| 123 return true; | 131 return this.canEdit_; |
|
calamity
2017/06/09 06:47:31
Does anything bad happen if this is true?
tsergeant
2017/06/13 03:13:10
The call to chrome.bookmarkManagerPrivate.undo() w
calamity
2017/06/13 05:13:53
Acknowledged.
| |
| 124 default: | 132 default: |
| 125 return this.isCommandVisible_(command, itemIds) && | 133 return this.isCommandVisible_(command, itemIds) && |
| 126 this.isCommandEnabled_(command, itemIds); | 134 this.isCommandEnabled_(command, itemIds); |
| 127 } | 135 } |
| 128 }, | 136 }, |
| 129 | 137 |
| 130 /** | 138 /** |
| 131 * @param {Command} command | 139 * @param {Command} command |
| 132 * @param {!Set<string>} itemIds | 140 * @param {!Set<string>} itemIds |
| 133 * @return {boolean} True if the command should be visible in the context | 141 * @return {boolean} True if the command should be visible in the context |
| 134 * menu. | 142 * menu. |
| 135 */ | 143 */ |
| 136 isCommandVisible_: function(command, itemIds) { | 144 isCommandVisible_: function(command, itemIds) { |
| 137 switch (command) { | 145 switch (command) { |
| 138 case Command.EDIT: | 146 case Command.EDIT: |
| 139 return itemIds.size == 1; | 147 return itemIds.size == 1 && this.canEdit_; |
| 140 case Command.COPY: | 148 case Command.COPY: |
| 141 return itemIds.size == 1 && | 149 return this.isSingleBookmark_(itemIds); |
| 142 this.containsMatchingNode_(itemIds, function(node) { | |
| 143 return !!node.url; | |
| 144 }); | |
| 145 case Command.DELETE: | 150 case Command.DELETE: |
| 151 return itemIds.size > 0 && this.canEdit_; | |
| 146 case Command.OPEN_NEW_TAB: | 152 case Command.OPEN_NEW_TAB: |
| 147 case Command.OPEN_NEW_WINDOW: | 153 case Command.OPEN_NEW_WINDOW: |
| 148 case Command.OPEN_INCOGNITO: | 154 case Command.OPEN_INCOGNITO: |
| 149 return itemIds.size > 0; | 155 return itemIds.size > 0; |
| 150 default: | 156 default: |
| 151 return false; | 157 return false; |
| 152 } | 158 } |
| 153 }, | 159 }, |
| 154 | 160 |
| 155 /** | 161 /** |
| 156 * @param {Command} command | 162 * @param {Command} command |
| 157 * @param {!Set<string>} itemIds | 163 * @param {!Set<string>} itemIds |
| 158 * @return {boolean} True if the command should be clickable in the context | 164 * @return {boolean} True if the command should be clickable in the context |
| 159 * menu. | 165 * menu. |
| 160 */ | 166 */ |
| 161 isCommandEnabled_: function(command, itemIds) { | 167 isCommandEnabled_: function(command, itemIds) { |
| 162 switch (command) { | 168 switch (command) { |
| 169 case Command.EDIT: | |
| 170 case Command.DELETE: | |
| 171 var state = this.getState(); | |
| 172 return !this.containsMatchingNode_(itemIds, function(node) { | |
| 173 return bookmarks.util.isNodeUnmodifiable(state, node.id); | |
| 174 }); | |
| 163 case Command.OPEN_NEW_TAB: | 175 case Command.OPEN_NEW_TAB: |
| 164 case Command.OPEN_NEW_WINDOW: | 176 case Command.OPEN_NEW_WINDOW: |
| 165 return this.expandUrls_(itemIds).length > 0; | 177 return this.expandUrls_(itemIds).length > 0; |
| 166 case Command.OPEN_INCOGNITO: | 178 case Command.OPEN_INCOGNITO: |
| 167 return this.expandUrls_(itemIds).length > 0 && | 179 return this.expandUrls_(itemIds).length > 0 && |
| 168 this.getState().prefs.incognitoAvailability != | 180 this.getState().prefs.incognitoAvailability != |
| 169 IncognitoAvailability.DISABLED; | 181 IncognitoAvailability.DISABLED; |
| 170 default: | 182 default: |
| 171 return true; | 183 return true; |
| 172 } | 184 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 */ | 363 */ |
| 352 containsMatchingNode_: function(itemIds, predicate) { | 364 containsMatchingNode_: function(itemIds, predicate) { |
| 353 var nodes = this.getState().nodes; | 365 var nodes = this.getState().nodes; |
| 354 | 366 |
| 355 return Array.from(itemIds).some(function(id) { | 367 return Array.from(itemIds).some(function(id) { |
| 356 return predicate(nodes[id]); | 368 return predicate(nodes[id]); |
| 357 }); | 369 }); |
| 358 }, | 370 }, |
| 359 | 371 |
| 360 /** | 372 /** |
| 373 * @param {!Set<string>} itemIds | |
| 374 * @return {boolean} True if |itemIds| is a single bookmark (non-folder) | |
| 375 * node. | |
| 376 */ | |
| 377 isSingleBookmark_: function(itemIds) { | |
| 378 return itemIds.size == 1 && | |
| 379 this.containsMatchingNode_(itemIds, function(node) { | |
| 380 return !!node.url; | |
| 381 }); | |
| 382 }, | |
| 383 | |
| 384 /** | |
| 361 * @param {Event} e | 385 * @param {Event} e |
| 362 * @private | 386 * @private |
| 363 */ | 387 */ |
| 364 onOpenItemMenu_: function(e) { | 388 onOpenItemMenu_: function(e) { |
| 365 if (e.detail.targetElement) { | 389 if (e.detail.targetElement) { |
| 366 this.openCommandMenuAtElement(e.detail.targetElement); | 390 this.openCommandMenuAtElement(e.detail.targetElement); |
| 367 } else { | 391 } else { |
| 368 this.openCommandMenuAtPosition(e.detail.x, e.detail.y); | 392 this.openCommandMenuAtPosition(e.detail.x, e.detail.y); |
| 369 } | 393 } |
| 370 }, | 394 }, |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 } | 464 } |
| 441 | 465 |
| 442 return loadTimeData.getString(assert(label)); | 466 return loadTimeData.getString(assert(label)); |
| 443 }, | 467 }, |
| 444 | 468 |
| 445 /** | 469 /** |
| 446 * @param {Command} command | 470 * @param {Command} command |
| 447 * @return {boolean} | 471 * @return {boolean} |
| 448 * @private | 472 * @private |
| 449 */ | 473 */ |
| 450 showDividerAfter_: function(command) { | 474 showDividerAfter_: function(command, itemIds) { |
| 451 return command == Command.DELETE; | 475 return command == Command.DELETE && |
| 476 (this.canEdit_ || this.isSingleBookmark_(itemIds)); | |
| 452 }, | 477 }, |
| 453 }); | 478 }); |
| 454 | 479 |
| 455 /** @private {bookmarks.CommandManager} */ | 480 /** @private {bookmarks.CommandManager} */ |
| 456 CommandManager.instance_ = null; | 481 CommandManager.instance_ = null; |
| 457 | 482 |
| 458 /** @return {!bookmarks.CommandManager} */ | 483 /** @return {!bookmarks.CommandManager} */ |
| 459 CommandManager.getInstance = function() { | 484 CommandManager.getInstance = function() { |
| 460 return assert(CommandManager.instance_); | 485 return assert(CommandManager.instance_); |
| 461 }; | 486 }; |
| 462 | 487 |
| 463 return { | 488 return { |
| 464 CommandManager: CommandManager, | 489 CommandManager: CommandManager, |
| 465 }; | 490 }; |
| 466 }); | 491 }); |
| OLD | NEW |