| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 Polymer({ |
| 6 is: 'bookmarks-store', |
| 7 |
| 8 properties: { |
| 9 /** @type {BookmarkTreeNode} */ |
| 10 rootNode: { |
| 11 type: Object, |
| 12 notify: true, |
| 13 }, |
| 14 |
| 15 selectedId: { |
| 16 type: String, |
| 17 observer: 'updateSelectedNode_', |
| 18 notify: true, |
| 19 }, |
| 20 |
| 21 /** @type {BookmarkTreeNode} */ |
| 22 selectedNode: { |
| 23 type: Object, |
| 24 notify: true, |
| 25 readOnly: true, |
| 26 }, |
| 27 |
| 28 idToNodeMap_: Object, |
| 29 }, |
| 30 |
| 31 /** @private {Object} */ |
| 32 documentListeners_: null, |
| 33 |
| 34 /** @override */ |
| 35 attached: function() { |
| 36 this.documentListeners_ = { |
| 37 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
| 38 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
| 39 }; |
| 40 for (var event in this.documentListeners_) |
| 41 document.addEventListener(event, this.documentListeners_[event]); |
| 42 }, |
| 43 |
| 44 /** @override */ |
| 45 detached: function() { |
| 46 for (var event in this.documentListeners_) |
| 47 document.removeEventListener(event, this.documentListeners_[event]); |
| 48 }, |
| 49 |
| 50 /** |
| 51 * Initializes the store with data from the bookmarks API. |
| 52 * Called by app on attached. |
| 53 */ |
| 54 initializeStore: function() { |
| 55 chrome.bookmarks.getTree(function(results) { |
| 56 this.setupStore_(results[0]); |
| 57 }.bind(this)); |
| 58 }, |
| 59 |
| 60 /** |
| 61 * @param {BookmarkTreeNode} rootNode |
| 62 * @private |
| 63 */ |
| 64 setupStore_: function(rootNode) { |
| 65 this.rootNode = rootNode; |
| 66 this.idToNodeMap_ = {}; |
| 67 this.rootNode.path = 'rootNode'; |
| 68 this.initNodes_(this.rootNode); |
| 69 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 70 }, |
| 71 |
| 72 /** |
| 73 * Selects the folder specified by the event and deselects the previously |
| 74 * selected folder. |
| 75 * @param {CustomEvent} e |
| 76 * @private |
| 77 */ |
| 78 onSelectedFolderChanged_: function(e) { |
| 79 // Deselect the old folder if defined. |
| 80 if (this.selectedId) |
| 81 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 82 |
| 83 var selectedId = /** @type {string} */ (e.detail); |
| 84 var newFolder = this.idToNodeMap_[selectedId]; |
| 85 this.set(newFolder.path + '.isSelected', true); |
| 86 this.selectedId = selectedId; |
| 87 }, |
| 88 |
| 89 /** |
| 90 * Handles events that open and close folders. |
| 91 * @param {CustomEvent} e |
| 92 * @private |
| 93 */ |
| 94 onFolderOpenChanged_: function(e) { |
| 95 var folder = this.idToNodeMap_[e.detail.id]; |
| 96 this.set(folder.path + '.isOpen', e.detail.open); |
| 97 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) |
| 98 this.fire('selected-folder-changed', folder.id); |
| 99 }, |
| 100 |
| 101 /** |
| 102 * @param {BookmarkTreeNode} folder |
| 103 * @private |
| 104 * @return {boolean} |
| 105 */ |
| 106 isAncestorOfSelected_: function(folder) { |
| 107 return this.selectedNode.path.startsWith(folder.path); |
| 108 }, |
| 109 |
| 110 /** @private */ |
| 111 updateSelectedNode_: function() { |
| 112 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 113 this.linkPaths('selectedNode', selectedNode.path); |
| 114 this._setSelectedNode(selectedNode); |
| 115 }, |
| 116 |
| 117 /** |
| 118 * Initializes the nodes in the bookmarks tree as follows: |
| 119 * - Populates |idToNodeMap_| with a mapping of all node ids to their |
| 120 * corresponding BookmarkTreeNode. |
| 121 * - Stores the path from the store to a node inside the node. |
| 122 * - Sets all the nodes to not selected and open by default. |
| 123 * @param {BookmarkTreeNode} bookmarkNode |
| 124 * @private |
| 125 */ |
| 126 initNodes_: function(bookmarkNode) { |
| 127 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; |
| 128 if (bookmarkNode.url) |
| 129 return; |
| 130 |
| 131 bookmarkNode.isSelected = false; |
| 132 bookmarkNode.isOpen = true; |
| 133 for (var i = 0; i < bookmarkNode.children.length; i++) { |
| 134 bookmarkNode.children[i].path = bookmarkNode.path + '.children.' + i; |
| 135 this.initNodes_(bookmarkNode.children[i]); |
| 136 } |
| 137 }, |
| 138 }); |
| OLD | NEW |