| 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 Polymer({ | 5 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 }, | 48 }, |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * Initializes the store with data from the bookmarks API. | 51 * Initializes the store with data from the bookmarks API. |
| 52 * Called by app on attached. | 52 * Called by app on attached. |
| 53 */ | 53 */ |
| 54 initializeStore: function() { | 54 initializeStore: function() { |
| 55 chrome.bookmarks.getTree(function(results) { | 55 chrome.bookmarks.getTree(function(results) { |
| 56 this.setupStore_(results[0]); | 56 this.setupStore_(results[0]); |
| 57 }.bind(this)); | 57 }.bind(this)); |
| 58 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); |
| 58 }, | 59 }, |
| 59 | 60 |
| 60 /** | 61 /** |
| 61 * @param {BookmarkTreeNode} rootNode | 62 * @param {BookmarkTreeNode} rootNode |
| 62 * @private | 63 * @private |
| 63 */ | 64 */ |
| 64 setupStore_: function(rootNode) { | 65 setupStore_: function(rootNode) { |
| 65 this.rootNode = rootNode; | 66 this.rootNode = rootNode; |
| 66 this.idToNodeMap_ = {}; | 67 this.idToNodeMap_ = {}; |
| 67 this.rootNode.path = 'rootNode'; | 68 this.rootNode.path = 'rootNode'; |
| 69 this.generatePaths_(rootNode, 0); |
| 68 this.initNodes_(this.rootNode); | 70 this.initNodes_(this.rootNode); |
| 69 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 71 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 70 }, | 72 }, |
| 71 | 73 |
| 72 /** | 74 /** |
| 73 * Selects the folder specified by the event and deselects the previously | 75 * Selects the folder specified by the event and deselects the previously |
| 74 * selected folder. | 76 * selected folder. |
| 75 * @param {CustomEvent} e | 77 * @param {CustomEvent} e |
| 76 * @private | 78 * @private |
| 77 */ | 79 */ |
| (...skipping 30 matching lines...) Expand all Loading... |
| 108 }, | 110 }, |
| 109 | 111 |
| 110 /** @private */ | 112 /** @private */ |
| 111 updateSelectedNode_: function() { | 113 updateSelectedNode_: function() { |
| 112 var selectedNode = this.idToNodeMap_[this.selectedId]; | 114 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 113 this.linkPaths('selectedNode', selectedNode.path); | 115 this.linkPaths('selectedNode', selectedNode.path); |
| 114 this._setSelectedNode(selectedNode); | 116 this._setSelectedNode(selectedNode); |
| 115 }, | 117 }, |
| 116 | 118 |
| 117 /** | 119 /** |
| 120 * Callback for when a bookmark node is removed. |
| 121 * If a folder is selected or is an ancestor of a selected folder, the parent |
| 122 * of the removed folder will be selected. |
| 123 * @param {string} id The id of the removed bookmark node. |
| 124 * @param {!{index: number, |
| 125 * parentId: string, |
| 126 * node: BookmarkTreeNode}} removeInfo |
| 127 */ |
| 128 onBookmarkRemoved_: function(id, removeInfo) { |
| 129 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) |
| 130 this.fire('selected-folder-changed', removeInfo.parentId); |
| 131 |
| 132 var parentNode = this.idToNodeMap_[removeInfo.parentId]; |
| 133 this.splice(parentNode.path + '.children', removeInfo.index, 1); |
| 134 this.removeDescendantsFromMap_(id); |
| 135 this.generatePaths_(parentNode, removeInfo.index); |
| 136 }, |
| 137 |
| 138 /** |
| 118 * Initializes the nodes in the bookmarks tree as follows: | 139 * Initializes the nodes in the bookmarks tree as follows: |
| 119 * - Populates |idToNodeMap_| with a mapping of all node ids to their | 140 * - Populates |idToNodeMap_| with a mapping of all node ids to their |
| 120 * corresponding BookmarkTreeNode. | 141 * 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. | 142 * - Sets all the nodes to not selected and open by default. |
| 123 * @param {BookmarkTreeNode} bookmarkNode | 143 * @param {BookmarkTreeNode} bookmarkNode |
| 124 * @private | 144 * @private |
| 125 */ | 145 */ |
| 126 initNodes_: function(bookmarkNode) { | 146 initNodes_: function(bookmarkNode) { |
| 127 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | 147 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; |
| 128 if (bookmarkNode.url) | 148 if (bookmarkNode.url) |
| 129 return; | 149 return; |
| 130 | 150 |
| 131 bookmarkNode.isSelected = false; | 151 bookmarkNode.isSelected = false; |
| 132 bookmarkNode.isOpen = true; | 152 bookmarkNode.isOpen = true; |
| 133 for (var i = 0; i < bookmarkNode.children.length; i++) { | 153 for (var i = 0; i < bookmarkNode.children.length; i++) { |
| 134 bookmarkNode.children[i].path = bookmarkNode.path + '.children.' + i; | |
| 135 this.initNodes_(bookmarkNode.children[i]); | 154 this.initNodes_(bookmarkNode.children[i]); |
| 136 } | 155 } |
| 137 }, | 156 }, |
| 157 |
| 158 /** |
| 159 * Stores the path from the store to a node inside the node. |
| 160 * @param {BookmarkTreeNode} bookmarkNode |
| 161 * @param {number} startIndex |
| 162 * @private |
| 163 */ |
| 164 generatePaths_: function(bookmarkNode, startIndex) { |
| 165 if (!bookmarkNode.children) |
| 166 return; |
| 167 |
| 168 for (var i = startIndex; i < bookmarkNode.children.length; i++) { |
| 169 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i; |
| 170 this.generatePaths_(bookmarkNode.children[i], 0); |
| 171 } |
| 172 }, |
| 173 |
| 174 /** |
| 175 * Remove all descendants of a given node from the map. |
| 176 * @param {string} id |
| 177 * @private |
| 178 */ |
| 179 removeDescendantsFromMap_: function(id) { |
| 180 var node = this.idToNodeMap_[id]; |
| 181 if (!node) |
| 182 return; |
| 183 |
| 184 if (node.children) { |
| 185 for (var i = 0; i < node.children.length; i++) |
| 186 this.removeDescendantsFromMap_(node.children[i].id); |
| 187 } |
| 188 delete this.idToNodeMap_[id]; |
| 189 } |
| 138 }); | 190 }); |
| OLD | NEW |