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 Polymer({ |
6 is: 'bookmarks-command-manager', | 6 is: 'bookmarks-command-manager', |
7 | 7 |
8 behaviors: [ | 8 behaviors: [ |
9 bookmarks.StoreClient, | 9 bookmarks.StoreClient, |
10 ], | 10 ], |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) | 96 /** @type {!BookmarksEditDialogElement} */ (this.$.editDialog.get()) |
97 .showEditDialog(this.getState().nodes[id]); | 97 .showEditDialog(this.getState().nodes[id]); |
98 break; | 98 break; |
99 case Command.COPY: | 99 case Command.COPY: |
100 var idList = Array.from(itemIds); | 100 var idList = Array.from(itemIds); |
101 chrome.bookmarkManagerPrivate.copy(idList, function() { | 101 chrome.bookmarkManagerPrivate.copy(idList, function() { |
102 // TODO(jiaxi): Add toast later. | 102 // TODO(jiaxi): Add toast later. |
103 }); | 103 }); |
104 break; | 104 break; |
105 case Command.DELETE: | 105 case Command.DELETE: |
106 // TODO(tsergeant): Filter IDs so we don't try to delete children of | |
107 // something else already being deleted. | |
108 chrome.bookmarkManagerPrivate.removeTrees( | 106 chrome.bookmarkManagerPrivate.removeTrees( |
109 Array.from(itemIds), function() { | 107 Array.from(this.minimizeDeletionSet_(itemIds)), function() { |
110 // TODO(jiaxi): Add toast later. | 108 // TODO(jiaxi): Add toast later. |
111 }); | 109 }); |
112 break; | 110 break; |
113 } | 111 } |
114 | |
115 }, | 112 }, |
116 | 113 |
117 //////////////////////////////////////////////////////////////////////////// | 114 //////////////////////////////////////////////////////////////////////////// |
118 // Private functions: | 115 // Private functions: |
119 | 116 |
120 /** | 117 /** |
| 118 * Minimize the set of |itemIds| by removing any node which has an ancestor |
| 119 * node already in the set. This ensures that instead of trying to delete both |
| 120 * a node and its descendant, we will only try to delete the topmost node, |
| 121 * preventing an error in the bookmarkManagerPrivate.removeTrees API call. |
| 122 * @param {!Set<string>} itemIds |
| 123 * @return {!Set<string>} |
| 124 */ |
| 125 minimizeDeletionSet_: function(itemIds) { |
| 126 var minimizedSet = new Set(); |
| 127 var nodes = this.getState().nodes; |
| 128 itemIds.forEach(function(itemId) { |
| 129 var currentId = itemId; |
| 130 while (currentId != ROOT_NODE_ID) { |
| 131 currentId = assert(nodes[currentId].parentId); |
| 132 if (itemIds.has(currentId)) |
| 133 return; |
| 134 } |
| 135 minimizedSet.add(itemId); |
| 136 }); |
| 137 return minimizedSet; |
| 138 }, |
| 139 |
| 140 /** |
121 * @param {!Set<string>} itemIds | 141 * @param {!Set<string>} itemIds |
122 * @param {function(BookmarkNode):boolean} predicate | 142 * @param {function(BookmarkNode):boolean} predicate |
123 * @return {boolean} True if any node in |itemIds| returns true for | 143 * @return {boolean} True if any node in |itemIds| returns true for |
124 * |predicate|. | 144 * |predicate|. |
125 */ | 145 */ |
126 containsMatchingNode_: function(itemIds, predicate) { | 146 containsMatchingNode_: function(itemIds, predicate) { |
127 var nodes = this.getState().nodes; | 147 var nodes = this.getState().nodes; |
128 | 148 |
129 return Array.from(itemIds).some(function(id) { | 149 return Array.from(itemIds).some(function(id) { |
130 return predicate(nodes[id]); | 150 return predicate(nodes[id]); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 getEditActionLabel_: function() { | 211 getEditActionLabel_: function() { |
192 if (this.menuIds_.size > 1) | 212 if (this.menuIds_.size > 1) |
193 return; | 213 return; |
194 | 214 |
195 var id = Array.from(this.menuIds_)[0]; | 215 var id = Array.from(this.menuIds_)[0]; |
196 var itemUrl = this.getState().nodes[id].url; | 216 var itemUrl = this.getState().nodes[id].url; |
197 var label = itemUrl ? 'menuEdit' : 'menuRename'; | 217 var label = itemUrl ? 'menuEdit' : 'menuRename'; |
198 return loadTimeData.getString(label); | 218 return loadTimeData.getString(label); |
199 }, | 219 }, |
200 }); | 220 }); |
OLD | NEW |