Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 Polymer({ | |
| 6 is: 'bookmarks-command-manager', | |
| 7 | |
| 8 behaviors: [ | |
| 9 bookmarks.StoreClient, | |
| 10 ], | |
| 11 | |
| 12 properties: { | |
| 13 /** @type {Set<string>} */ | |
| 14 menuIds_: Object, | |
| 15 }, | |
| 16 | |
| 17 attached: function() { | |
| 18 /** @private {function(!Event)} */ | |
| 19 this.boundOnOpenItemMenu_ = this.onOpenItemMenu_.bind(this); | |
| 20 document.addEventListener('open-item-menu', this.boundOnOpenItemMenu_); | |
| 21 }, | |
| 22 | |
| 23 detached: function() { | |
| 24 document.removeEventListener('open-item-menu', this.boundOnOpenItemMenu_); | |
| 25 }, | |
| 26 | |
| 27 /** | |
| 28 * Display the command context menu at (|x|, |y|) in window co-ordinates. | |
| 29 * Commands will execute on the currently selected items. | |
| 30 * @param {number} x | |
| 31 * @param {number} y | |
| 32 */ | |
| 33 openCommandMenuAtPosition: function(x, y) { | |
| 34 this.menuIds_ = this.getState().selection.items; | |
| 35 /** @type {!CrActionMenuElement} */ (this.$.dropdown) | |
| 36 .showAtPosition({top: y, left: x}); | |
| 37 }, | |
| 38 | |
| 39 /** | |
| 40 * Display the command context menu positioned to cover the |target| | |
| 41 * element. Commands will execute on the currently selected items. | |
| 42 * @param {!Element} target | |
| 43 */ | |
| 44 openCommandMenuAtElement: function(target) { | |
| 45 this.menuIds_ = this.getState().selection.items; | |
| 46 /** @type {!CrActionMenuElement} */ (this.$.dropdown).showAt(target); | |
| 47 }, | |
| 48 | |
| 49 closeCommandMenu: function() { | |
| 50 /** @type {!CrActionMenuElement} */ (this.$.dropdown).close(); | |
| 51 }, | |
| 52 | |
| 53 //////////////////////////////////////////////////////////////////////////// | |
| 54 // Command handlers: | |
| 55 | |
| 56 /** | |
| 57 * @param {Command} command | |
| 58 * @param {!Set<string>} itemIds | |
| 59 * @return {boolean} | |
| 60 */ | |
| 61 canExecute: function(command, itemIds) { | |
| 62 switch (command) { | |
| 63 case Command.EDIT: | |
| 64 return itemIds.size == 1; | |
| 65 case Command.COPY: | |
| 66 return itemIds.size == 1 && | |
| 67 this.containsMatchingNode_(itemIds, function(node) { | |
| 68 return !!node.url; | |
|
calamity
2017/04/27 03:42:04
Wait, I should be able to copy multiple items, whi
tsergeant
2017/04/27 04:08:37
You can in the current BMM.
However, the new spec
| |
| 69 }); | |
| 70 case Command.DELETE: | |
| 71 return true; | |
| 72 default: | |
| 73 return false; | |
| 74 } | |
| 75 }, | |
| 76 | |
| 77 /** | |
| 78 * @param {Command} command | |
| 79 * @param {!Set<string>} itemIds | |
| 80 */ | |
| 81 handle: function(command, itemIds) { | |
| 82 switch (command) { | |
| 83 case Command.EDIT: | |
| 84 var id = Array.from(itemIds)[0]; | |
| 85 /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) | |
| 86 .showEditDialog(this.getState().nodes[id]); | |
| 87 break; | |
| 88 case Command.COPY: | |
| 89 var idList = Array.from(itemIds); | |
| 90 chrome.bookmarkManagerPrivate.copy(idList, function() { | |
| 91 // TODO(jiaxi): Add toast later. | |
| 92 }); | |
| 93 break; | |
| 94 case Command.DELETE: | |
| 95 // TODO(tsergeant): Filter IDs so we don't try to delete children of | |
| 96 // something else already being deleted. | |
| 97 chrome.bookmarkManagerPrivate.removeTrees( | |
| 98 Array.from(itemIds), function() { | |
| 99 // TODO(jiaxi): Add toast later. | |
| 100 }); | |
| 101 break; | |
| 102 } | |
| 103 | |
| 104 }, | |
| 105 | |
| 106 //////////////////////////////////////////////////////////////////////////// | |
| 107 // Private functions: | |
| 108 | |
| 109 /** | |
| 110 * @param {!Set<string>} itemIds | |
| 111 * @param {function(BookmarkNode):boolean} predicate | |
| 112 * @return {boolean} True if any node in |itemIds| returns true for | |
| 113 * |predicate|. | |
| 114 */ | |
| 115 containsMatchingNode_: function(itemIds, predicate) { | |
| 116 var nodes = this.getState().nodes; | |
| 117 | |
| 118 return Array.from(itemIds).some(function(id) { | |
| 119 return predicate(nodes[id]); | |
| 120 }); | |
| 121 }, | |
| 122 | |
| 123 /** | |
| 124 * @param {Event} e | |
| 125 * @private | |
| 126 */ | |
| 127 onOpenItemMenu_: function(e) { | |
| 128 if (e.detail.targetElement) { | |
| 129 this.openCommandMenuAtElement(e.detail.targetElement); | |
| 130 } else { | |
| 131 this.openCommandMenuAtPosition(e.detail.x, e.detail.y); | |
| 132 } | |
| 133 }, | |
| 134 | |
| 135 /** | |
| 136 * @param {Event} e | |
| 137 * @private | |
| 138 */ | |
| 139 onCommandClick_: function(e) { | |
| 140 this.closeCommandMenu(); | |
| 141 this.handle(e.target.getAttribute('command'), assert(this.menuIds_)); | |
| 142 }, | |
| 143 | |
| 144 /** | |
| 145 * Close the menu on mousedown so clicks can propagate to the underlying UI. | |
| 146 * This allows the user to right click the list while a context menu is | |
| 147 * showing and get another context menu. | |
| 148 * @param {Event} e | |
| 149 * @private | |
| 150 */ | |
| 151 onMenuMousedown_: function(e) { | |
| 152 if (e.path[0] != this.$.dropdown) | |
| 153 return; | |
| 154 | |
| 155 this.$.dropdown.close(); | |
| 156 }, | |
| 157 | |
| 158 /** @private */ | |
| 159 getEditActionLabel_: function() { | |
| 160 if (this.menuIds_.size > 1) | |
| 161 return; | |
| 162 | |
| 163 var id = Array.from(this.menuIds_)[0]; | |
| 164 var itemUrl = this.getState().nodes[id].url; | |
| 165 var label = itemUrl ? 'menuEdit' : 'menuRename'; | |
| 166 return loadTimeData.getString(label); | |
| 167 }, | |
| 168 }); | |
| OLD | NEW |