Chromium Code Reviews| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 searchResultSet_: Object, | 47 searchResultSet_: Object, |
| 48 }, | 48 }, |
| 49 | 49 |
| 50 /** @private {Object} */ | 50 /** @private {Object} */ |
| 51 documentListeners_: null, | 51 documentListeners_: null, |
| 52 | 52 |
| 53 /** @override */ | 53 /** @override */ |
| 54 attached: function() { | 54 attached: function() { |
| 55 this.documentListeners_ = { | 55 this.documentListeners_ = { |
| 56 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | 56 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
| 57 'node-added': this.onNodeAdded_.bind(this), | |
| 57 'search-term-changed': this.onSearchTermChanged_.bind(this), | 58 'search-term-changed': this.onSearchTermChanged_.bind(this), |
| 58 'select-item': this.onItemSelected_.bind(this), | 59 'select-item': this.onItemSelected_.bind(this), |
| 59 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | 60 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
| 60 }; | 61 }; |
| 61 for (var event in this.documentListeners_) | 62 for (var event in this.documentListeners_) |
| 62 document.addEventListener(event, this.documentListeners_[event]); | 63 document.addEventListener(event, this.documentListeners_[event]); |
| 63 }, | 64 }, |
| 64 | 65 |
| 65 /** @override */ | 66 /** @override */ |
| 66 detached: function() { | 67 detached: function() { |
| 67 for (var event in this.documentListeners_) | 68 for (var event in this.documentListeners_) |
| 68 document.removeEventListener(event, this.documentListeners_[event]); | 69 document.removeEventListener(event, this.documentListeners_[event]); |
| 69 }, | 70 }, |
| 70 | 71 |
| 71 /** | 72 /** |
| 72 * Initializes the store with data from the bookmarks API. | 73 * Initializes the store with data from the bookmarks API. |
| 73 * Called by app on attached. | 74 * Called by app on attached. |
| 74 */ | 75 */ |
| 75 initializeStore: function() { | 76 initializeStore: function() { |
| 76 chrome.bookmarks.getTree(function(results) { | 77 chrome.bookmarks.getTree(function(results) { |
| 77 this.setupStore_(results[0]); | 78 this.setupStore_(results[0]); |
| 78 }.bind(this)); | 79 }.bind(this)); |
| 79 // Attach bookmarks API listeners. | 80 // Attach bookmarks API listeners. |
| 80 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); | 81 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); |
| 81 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); | 82 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); |
| 83 chrome.bookmarks.onCreated.addListener(this.onBookmarkCreated_.bind(this)); | |
| 82 }, | 84 }, |
| 83 | 85 |
| 84 ////////////////////////////////////////////////////////////////////////////// | 86 ////////////////////////////////////////////////////////////////////////////// |
| 85 // bookmarks-store, private: | 87 // bookmarks-store, private: |
| 86 | 88 |
| 87 /** | 89 /** |
| 88 * @param {BookmarkTreeNode} rootNode | 90 * @param {BookmarkTreeNode} rootNode |
| 89 * @private | 91 * @private |
| 90 */ | 92 */ |
| 91 setupStore_: function(rootNode) { | 93 setupStore_: function(rootNode) { |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 onBookmarkChanged_: function(id, changeInfo) { | 321 onBookmarkChanged_: function(id, changeInfo) { |
| 320 if (changeInfo.title) | 322 if (changeInfo.title) |
| 321 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); | 323 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); |
| 322 if (changeInfo.url) | 324 if (changeInfo.url) |
| 323 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); | 325 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); |
| 324 | 326 |
| 325 if (this.searchTerm) | 327 if (this.searchTerm) |
| 326 this.updateSearchDisplay_(); | 328 this.updateSearchDisplay_(); |
| 327 }, | 329 }, |
| 328 | 330 |
| 331 /** | |
| 332 * Called when after a node is created. | |
| 333 * @param {string} id The id of the newly added bookmark node. | |
| 334 * @param {!BookmarkTreeNode} bookmarkNode | |
| 335 */ | |
| 336 onBookmarkCreated_: function(id, bookmarkNode) { | |
| 337 var parent = | |
| 338 this.idToNodeMap_[/** @type {?string} */ (bookmarkNode.parentId)]; | |
| 339 this.splice( | |
| 340 parent.path + '.children', /** @type {number} */ (bookmarkNode.index), | |
| 341 0, bookmarkNode); | |
| 342 BookmarksStore.generatePaths(parent, 0); | |
|
jiaxi
2017/02/09 04:09:45
The index inside the tree doesn't get updated prop
| |
| 343 BookmarksStore.initNodes(bookmarkNode, this.idToNodeMap_); | |
| 344 | |
| 345 if (this.searchTerm) | |
| 346 this.updateSearchDisplay_(); | |
| 347 }, | |
| 348 | |
| 329 ////////////////////////////////////////////////////////////////////////////// | 349 ////////////////////////////////////////////////////////////////////////////// |
| 330 // bookmarks-store, bookmarks app event listeners: | 350 // bookmarks-store, bookmarks app event listeners: |
| 331 | 351 |
| 332 /** | 352 /** |
| 333 * @param {Event} e | 353 * @param {Event} e |
| 334 * @private | 354 * @private |
| 335 */ | 355 */ |
| 336 onSearchTermChanged_: function(e) { | 356 onSearchTermChanged_: function(e) { |
| 337 this.searchTerm = /** @type {string} */ (e.detail); | 357 this.searchTerm = /** @type {string} */ (e.detail); |
| 338 }, | 358 }, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 383 * @private | 403 * @private |
| 384 */ | 404 */ |
| 385 onFolderOpenChanged_: function(e) { | 405 onFolderOpenChanged_: function(e) { |
| 386 var folder = this.idToNodeMap_[e.detail.id]; | 406 var folder = this.idToNodeMap_[e.detail.id]; |
| 387 this.set(folder.path + '.isOpen', e.detail.open); | 407 this.set(folder.path + '.isOpen', e.detail.open); |
| 388 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) | 408 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) |
| 389 this.fire('selected-folder-changed', folder.id); | 409 this.fire('selected-folder-changed', folder.id); |
| 390 }, | 410 }, |
| 391 | 411 |
| 392 /** | 412 /** |
| 413 * Appends a node to the children the selected folder. | |
| 414 * @param {CustomEvent} e | |
| 415 * @private | |
| 416 */ | |
| 417 onNodeAdded_: function(e) { | |
| 418 var info = /** @type {!Object} */ (e.detail); | |
| 419 info.parentId = this.selectedId; | |
| 420 // TODO (angelayang): handle invalid urls. | |
| 421 chrome.bookmarks.create(info); | |
| 422 }, | |
| 423 | |
| 424 /** | |
| 393 * Selects items according to keyboard behaviours. | 425 * Selects items according to keyboard behaviours. |
| 394 * @param {CustomEvent} e | 426 * @param {CustomEvent} e |
| 395 * @private | 427 * @private |
| 396 */ | 428 */ |
| 397 onItemSelected_: function(e) { | 429 onItemSelected_: function(e) { |
| 398 if (!e.detail.add) | 430 if (!e.detail.add) |
| 399 this.clearSelectedItems_(); | 431 this.clearSelectedItems_(); |
| 400 | 432 |
| 401 if (e.detail.range) | 433 if (e.detail.range) |
| 402 this.selectRange_(e.detail.item); | 434 this.selectRange_(e.detail.item); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) { | 466 BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) { |
| 435 bookmarkNode.isSelectedItem = false; | 467 bookmarkNode.isSelectedItem = false; |
| 436 if (idToNodeMap) | 468 if (idToNodeMap) |
| 437 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 469 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| 438 | 470 |
| 439 if (bookmarkNode.url) | 471 if (bookmarkNode.url) |
| 440 return; | 472 return; |
| 441 | 473 |
| 442 bookmarkNode.isSelectedFolder = false; | 474 bookmarkNode.isSelectedFolder = false; |
| 443 bookmarkNode.isOpen = true; | 475 bookmarkNode.isOpen = true; |
| 476 if (!bookmarkNode.children) | |
| 477 bookmarkNode.children = []; | |
| 478 | |
| 444 for (var i = 0; i < bookmarkNode.children.length; i++) | 479 for (var i = 0; i < bookmarkNode.children.length; i++) |
| 445 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 480 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
| 446 }; | 481 }; |
| OLD | NEW |