OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 var BookmarksStore = Polymer({ | 5 var BookmarksStore = Polymer({ |
6 is: 'bookmarks-store', | 6 is: 'bookmarks-store', |
7 | 7 |
8 properties: { | 8 properties: { |
9 /** @type {BookmarkTreeNode} */ | 9 /** @type {BookmarkTreeNode} */ |
10 rootNode: { | 10 rootNode: { |
(...skipping 27 matching lines...) Expand all Loading... |
38 }, | 38 }, |
39 | 39 |
40 /** @type {Object<?string, !BookmarkTreeNode>} */ | 40 /** @type {Object<?string, !BookmarkTreeNode>} */ |
41 idToNodeMap_: Object, | 41 idToNodeMap_: Object, |
42 | 42 |
43 /** @type {?number} */ | 43 /** @type {?number} */ |
44 anchorIndex_: Number, | 44 anchorIndex_: Number, |
45 | 45 |
46 /** @type {Set<string>} */ | 46 /** @type {Set<string>} */ |
47 searchResultSet_: Object, | 47 searchResultSet_: Object, |
| 48 |
| 49 /** @type {Set<string>} */ |
| 50 selectedItemSet_: Object, |
48 }, | 51 }, |
49 | 52 |
50 /** @private {Object} */ | 53 /** @private {Object} */ |
51 documentListeners_: null, | 54 documentListeners_: null, |
52 | 55 |
53 /** @override */ | 56 /** @override */ |
54 attached: function() { | 57 attached: function() { |
55 this.documentListeners_ = { | 58 this.documentListeners_ = { |
56 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | 59 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
| 60 'remove-item': this.onItemRemoved_.bind(this), |
57 'search-term-changed': this.onSearchTermChanged_.bind(this), | 61 'search-term-changed': this.onSearchTermChanged_.bind(this), |
58 'select-item': this.onItemSelected_.bind(this), | 62 'select-item': this.onItemSelected_.bind(this), |
59 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | 63 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
60 }; | 64 }; |
61 for (var event in this.documentListeners_) | 65 for (var event in this.documentListeners_) |
62 document.addEventListener(event, this.documentListeners_[event]); | 66 document.addEventListener(event, this.documentListeners_[event]); |
63 }, | 67 }, |
64 | 68 |
65 /** @override */ | 69 /** @override */ |
66 detached: function() { | 70 detached: function() { |
(...skipping 18 matching lines...) Expand all Loading... |
85 // bookmarks-store, private: | 89 // bookmarks-store, private: |
86 | 90 |
87 /** | 91 /** |
88 * @param {BookmarkTreeNode} rootNode | 92 * @param {BookmarkTreeNode} rootNode |
89 * @private | 93 * @private |
90 */ | 94 */ |
91 setupStore_: function(rootNode) { | 95 setupStore_: function(rootNode) { |
92 this.rootNode = rootNode; | 96 this.rootNode = rootNode; |
93 this.idToNodeMap_ = {}; | 97 this.idToNodeMap_ = {}; |
94 this.rootNode.path = 'rootNode'; | 98 this.rootNode.path = 'rootNode'; |
| 99 this.selectedItemSet_ = new Set(); |
95 BookmarksStore.generatePaths(rootNode, 0); | 100 BookmarksStore.generatePaths(rootNode, 0); |
96 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); | 101 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
97 | 102 |
98 // Initialize the store's fields from the router. | 103 // Initialize the store's fields from the router. |
99 if (this.$.router.searchTerm) | 104 if (this.$.router.searchTerm) |
100 this.searchTerm = this.$.router.searchTerm; | 105 this.searchTerm = this.$.router.searchTerm; |
101 else | 106 else |
102 this.fire('selected-folder-changed', this.$.router.selectedId); | 107 this.fire('selected-folder-changed', this.$.router.selectedId); |
103 }, | 108 }, |
104 | 109 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 }, | 185 }, |
181 | 186 |
182 /** | 187 /** |
183 * Remove all selected items in the list. | 188 * Remove all selected items in the list. |
184 * @private | 189 * @private |
185 */ | 190 */ |
186 clearSelectedItems_: function() { | 191 clearSelectedItems_: function() { |
187 if (!this.displayedList) | 192 if (!this.displayedList) |
188 return; | 193 return; |
189 | 194 |
| 195 this.selectedItemSet_.clear(); |
| 196 |
190 for (var i = 0; i < this.displayedList.length; i++) { | 197 for (var i = 0; i < this.displayedList.length; i++) { |
191 if (!this.displayedList[i].isSelectedItem) | 198 if (!this.displayedList[i].isSelectedItem) |
192 continue; | 199 continue; |
193 | 200 |
194 this.set('displayedList.#' + i + '.isSelectedItem', false); | 201 this.set('displayedList.#' + i + '.isSelectedItem', false); |
195 } | 202 } |
196 }, | 203 }, |
197 | 204 |
198 /** | 205 /** |
199 * Return the index in the search result of an item. | 206 * Return the index in the search result of an item. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 this.anchorIndex_ = this.getIndexInList_(item); | 252 this.anchorIndex_ = this.getIndexInList_(item); |
246 startIndex = this.anchorIndex_; | 253 startIndex = this.anchorIndex_; |
247 endIndex = this.anchorIndex_; | 254 endIndex = this.anchorIndex_; |
248 } else { | 255 } else { |
249 var selectedIndex = this.getIndexInList_(item); | 256 var selectedIndex = this.getIndexInList_(item); |
250 startIndex = Math.min(this.anchorIndex_, selectedIndex); | 257 startIndex = Math.min(this.anchorIndex_, selectedIndex); |
251 endIndex = Math.max(this.anchorIndex_, selectedIndex); | 258 endIndex = Math.max(this.anchorIndex_, selectedIndex); |
252 } | 259 } |
253 for (var i = startIndex; i <= endIndex; i++) { | 260 for (var i = startIndex; i <= endIndex; i++) { |
254 this.set('displayedList.#' + i + '.isSelectedItem', true); | 261 this.set('displayedList.#' + i + '.isSelectedItem', true); |
| 262 this.selectedItemSet_.add(this.displayedList[i]); |
255 } | 263 } |
256 }, | 264 }, |
257 | 265 |
258 /** | 266 /** |
259 * Selects a single item in the displayedList. | 267 * Selects a single item in the displayedList. |
260 * @param {BookmarkTreeNode} item | 268 * @param {BookmarkTreeNode} item |
261 * @private | 269 * @private |
262 */ | 270 */ |
263 selectItem_: function(item) { | 271 selectItem_: function(item) { |
264 this.anchorIndex_ = this.getIndexInList_(item); | 272 this.anchorIndex_ = this.getIndexInList_(item); |
265 this.set('displayedList.#' + this.anchorIndex_ + '.isSelectedItem', true); | 273 this.set('displayedList.#' + this.anchorIndex_ + '.isSelectedItem', true); |
| 274 this.selectedItemSet_.add(this.displayedList[this.anchorIndex_]); |
| 275 }, |
| 276 |
| 277 /** |
| 278 * @param {BookmarkTreeNode} item |
| 279 * @private |
| 280 */ |
| 281 removeSingleItem_: function(item) { |
| 282 if (item.url) { |
| 283 chrome.bookmarks.remove(item.id, function() { |
| 284 // TODO(jiaxi): Add toast later. |
| 285 }.bind(this)); |
| 286 } else { |
| 287 chrome.bookmarks.removeTree(item.id, function() { |
| 288 // TODO(jiaxi): Add toast later. |
| 289 }.bind(this)); |
| 290 } |
266 }, | 291 }, |
267 | 292 |
268 ////////////////////////////////////////////////////////////////////////////// | 293 ////////////////////////////////////////////////////////////////////////////// |
269 // bookmarks-store, bookmarks API event listeners: | 294 // bookmarks-store, bookmarks API event listeners: |
270 | 295 |
271 /** | 296 /** |
272 * Callback for when a bookmark node is removed. | 297 * Callback for when a bookmark node is removed. |
273 * If a folder is selected or is an ancestor of a selected folder, the parent | 298 * If a folder is selected or is an ancestor of a selected folder, the parent |
274 * of the removed folder will be selected. | 299 * of the removed folder will be selected. |
275 * @param {string} id The id of the removed bookmark node. | 300 * @param {string} id The id of the removed bookmark node. |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 */ | 420 */ |
396 onItemSelected_: function(e) { | 421 onItemSelected_: function(e) { |
397 if (!e.detail.add) | 422 if (!e.detail.add) |
398 this.clearSelectedItems_(); | 423 this.clearSelectedItems_(); |
399 | 424 |
400 if (e.detail.range) | 425 if (e.detail.range) |
401 this.selectRange_(e.detail.item); | 426 this.selectRange_(e.detail.item); |
402 else | 427 else |
403 this.selectItem_(e.detail.item); | 428 this.selectItem_(e.detail.item); |
404 }, | 429 }, |
| 430 |
| 431 /** |
| 432 * @param {CustomEvent} e |
| 433 * @private |
| 434 */ |
| 435 onItemRemoved_: function(e) { |
| 436 var item = /** BookmarkTreeNode */ (e.detail); |
| 437 if (this.selectedItemSet_.size == 1 || !item.isSelectedItem) { |
| 438 this.removeSingleItem_(item); |
| 439 } else { |
| 440 for (let item of this.selectedItemSet_) |
| 441 this.removeSingleItem_(item); |
| 442 } |
| 443 }, |
405 }); | 444 }); |
406 | 445 |
407 //////////////////////////////////////////////////////////////////////////////// | 446 //////////////////////////////////////////////////////////////////////////////// |
408 // bookmarks-store, static methods: | 447 // bookmarks-store, static methods: |
409 | 448 |
410 /** | 449 /** |
411 * Stores the path from the store to a node inside the node. | 450 * Stores the path from the store to a node inside the node. |
412 * @param {BookmarkTreeNode} bookmarkNode | 451 * @param {BookmarkTreeNode} bookmarkNode |
413 * @param {number} startIndex | 452 * @param {number} startIndex |
414 */ | 453 */ |
(...skipping 21 matching lines...) Expand all Loading... |
436 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 475 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
437 | 476 |
438 if (bookmarkNode.url) | 477 if (bookmarkNode.url) |
439 return; | 478 return; |
440 | 479 |
441 bookmarkNode.isSelectedFolder = false; | 480 bookmarkNode.isSelectedFolder = false; |
442 bookmarkNode.isOpen = true; | 481 bookmarkNode.isOpen = true; |
443 for (var i = 0; i < bookmarkNode.children.length; i++) | 482 for (var i = 0; i < bookmarkNode.children.length; i++) |
444 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 483 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
445 }; | 484 }; |
OLD | NEW |