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 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 {!Object} removeInfo | |
| 125 */ | |
| 126 onBookmarkRemoved_: function(id, removeInfo) { | |
| 127 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) | |
| 128 this.fire('selected-folder-changed', removeInfo.parentId); | |
| 129 | |
| 130 var parentNode = this.idToNodeMap_[removeInfo.parentId]; | |
| 131 this.splice(parentNode.path + '.children', removeInfo.index, 1); | |
| 132 this.removeDescendantsFromMap_(id); | |
| 133 this.generatePaths_(parentNode, Number(removeInfo.index)); | |
|
tsergeant
2017/01/04 23:30:36
Is this Number() call only here to satisfy Closure
jiaxi
2017/01/05 05:28:01
Done.
| |
| 134 }, | |
| 135 | |
| 136 /** | |
| 118 * Initializes the nodes in the bookmarks tree as follows: | 137 * Initializes the nodes in the bookmarks tree as follows: |
| 119 * - Populates |idToNodeMap_| with a mapping of all node ids to their | 138 * - Populates |idToNodeMap_| with a mapping of all node ids to their |
| 120 * corresponding BookmarkTreeNode. | 139 * 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. | 140 * - Sets all the nodes to not selected and open by default. |
| 123 * @param {BookmarkTreeNode} bookmarkNode | 141 * @param {BookmarkTreeNode} bookmarkNode |
| 124 * @private | 142 * @private |
| 125 */ | 143 */ |
| 126 initNodes_: function(bookmarkNode) { | 144 initNodes_: function(bookmarkNode) { |
| 127 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | 145 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; |
| 128 if (bookmarkNode.url) | 146 if (bookmarkNode.url) |
| 129 return; | 147 return; |
| 130 | 148 |
| 131 bookmarkNode.isSelected = false; | 149 bookmarkNode.isSelected = false; |
| 132 bookmarkNode.isOpen = true; | 150 bookmarkNode.isOpen = true; |
| 133 for (var i = 0; i < bookmarkNode.children.length; i++) { | 151 for (var i = 0; i < bookmarkNode.children.length; i++) { |
| 134 bookmarkNode.children[i].path = bookmarkNode.path + '.children.' + i; | 152 bookmarkNode.children[i].isSelected = false; |
| 153 bookmarkNode.isOpen = true; | |
| 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 for (var i = startIndex; i < bookmarkNode.children.length; i++) { | |
| 167 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i; | |
| 168 this.generatePaths_(bookmarkNode.children[i], 0); | |
| 169 } | |
| 170 } | |
| 171 }, | |
| 172 | |
| 173 /** | |
| 174 * Remove all descendants of a given node from the map. | |
| 175 * @param {string} id | |
| 176 * @private | |
| 177 */ | |
| 178 removeDescendantsFromMap_: function(id) { | |
| 179 var node = this.idToNodeMap_[id]; | |
| 180 if (!node) | |
| 181 return; | |
| 182 | |
| 183 if (node.children) { | |
| 184 for (var i = 0; i < node.children.length; i++) | |
| 185 this.removeDescendantsFromMap_(node.children[i].id); | |
| 186 } | |
| 187 delete this.idToNodeMap_[id]; | |
| 188 } | |
| 138 }); | 189 }); |
| OLD | NEW |