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 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: { |
11 type: Object, | 11 type: Object, |
12 notify: true, | 12 notify: true, |
13 }, | 13 }, |
14 | 14 |
15 selectedId: { | 15 selectedId: { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 }, | 59 }, |
60 | 60 |
61 /** | 61 /** |
62 * @param {BookmarkTreeNode} rootNode | 62 * @param {BookmarkTreeNode} rootNode |
63 * @private | 63 * @private |
64 */ | 64 */ |
65 setupStore_: function(rootNode) { | 65 setupStore_: function(rootNode) { |
66 this.rootNode = rootNode; | 66 this.rootNode = rootNode; |
67 this.idToNodeMap_ = {}; | 67 this.idToNodeMap_ = {}; |
68 this.rootNode.path = 'rootNode'; | 68 this.rootNode.path = 'rootNode'; |
69 this.generatePaths_(rootNode, 0); | 69 BookmarksStore.generatePaths(rootNode, 0); |
70 this.initNodes_(this.rootNode); | 70 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
71 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 71 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
72 }, | 72 }, |
73 | 73 |
74 /** | 74 /** |
75 * Selects the folder specified by the event and deselects the previously | 75 * Selects the folder specified by the event and deselects the previously |
76 * selected folder. | 76 * selected folder. |
77 * @param {CustomEvent} e | 77 * @param {CustomEvent} e |
78 * @private | 78 * @private |
79 */ | 79 */ |
80 onSelectedFolderChanged_: function(e) { | 80 onSelectedFolderChanged_: function(e) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 * parentId: string, | 125 * parentId: string, |
126 * node: BookmarkTreeNode}} removeInfo | 126 * node: BookmarkTreeNode}} removeInfo |
127 */ | 127 */ |
128 onBookmarkRemoved_: function(id, removeInfo) { | 128 onBookmarkRemoved_: function(id, removeInfo) { |
129 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) | 129 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) |
130 this.fire('selected-folder-changed', removeInfo.parentId); | 130 this.fire('selected-folder-changed', removeInfo.parentId); |
131 | 131 |
132 var parentNode = this.idToNodeMap_[removeInfo.parentId]; | 132 var parentNode = this.idToNodeMap_[removeInfo.parentId]; |
133 this.splice(parentNode.path + '.children', removeInfo.index, 1); | 133 this.splice(parentNode.path + '.children', removeInfo.index, 1); |
134 this.removeDescendantsFromMap_(id); | 134 this.removeDescendantsFromMap_(id); |
135 this.generatePaths_(parentNode, removeInfo.index); | 135 BookmarksStore.generatePaths(parentNode, removeInfo.index); |
136 }, | 136 }, |
137 | 137 |
138 /** | 138 /** |
139 * Initializes the nodes in the bookmarks tree as follows: | |
140 * - Populates |idToNodeMap_| with a mapping of all node ids to their | |
141 * corresponding BookmarkTreeNode. | |
142 * - Sets all the nodes to not selected and open by default. | |
143 * @param {BookmarkTreeNode} bookmarkNode | |
144 * @private | |
145 */ | |
146 initNodes_: function(bookmarkNode) { | |
147 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | |
148 if (bookmarkNode.url) | |
149 return; | |
150 | |
151 bookmarkNode.isSelected = false; | |
152 bookmarkNode.isOpen = true; | |
153 for (var i = 0; i < bookmarkNode.children.length; i++) { | |
154 this.initNodes_(bookmarkNode.children[i]); | |
155 } | |
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. | 139 * Remove all descendants of a given node from the map. |
176 * @param {string} id | 140 * @param {string} id |
177 * @private | 141 * @private |
178 */ | 142 */ |
179 removeDescendantsFromMap_: function(id) { | 143 removeDescendantsFromMap_: function(id) { |
180 var node = this.idToNodeMap_[id]; | 144 var node = this.idToNodeMap_[id]; |
181 if (!node) | 145 if (!node) |
182 return; | 146 return; |
183 | 147 |
184 if (node.children) { | 148 if (node.children) { |
185 for (var i = 0; i < node.children.length; i++) | 149 for (var i = 0; i < node.children.length; i++) |
186 this.removeDescendantsFromMap_(node.children[i].id); | 150 this.removeDescendantsFromMap_(node.children[i].id); |
187 } | 151 } |
188 delete this.idToNodeMap_[id]; | 152 delete this.idToNodeMap_[id]; |
153 }, | |
154 }); | |
155 | |
156 /** | |
157 * Stores the path from the store to a node inside the node. | |
158 * @param {BookmarkTreeNode} bookmarkNode | |
159 * @param {number} startIndex | |
160 * @private | |
tsergeant
2017/01/08 23:39:29
These aren't private anymore.
jiaxi
2017/01/09 00:04:11
Done.
| |
161 */ | |
162 BookmarksStore.generatePaths = function(bookmarkNode, startIndex) { | |
163 if (!bookmarkNode.children) | |
164 return; | |
165 | |
166 for (var i = startIndex; i < bookmarkNode.children.length; i++) { | |
167 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i; | |
168 BookmarksStore.generatePaths(bookmarkNode.children[i], 0); | |
189 } | 169 } |
190 }); | 170 }; |
171 | |
172 /** | |
173 * Initializes the nodes in the bookmarks tree as follows: | |
174 * - Populates |idToNodeMap_| with a mapping of all node ids to their | |
175 * corresponding BookmarkTreeNode. | |
176 * - Sets all the nodes to not selected and open by default. | |
177 * @param {BookmarkTreeNode} bookmarkNode | |
178 * @param {Object=} idToNodeMap | |
179 * @private | |
180 */ | |
181 BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) { | |
182 if (idToNodeMap) | |
183 idToNodeMap[bookmarkNode.id] = bookmarkNode; | |
184 | |
185 if (bookmarkNode.url) | |
186 return; | |
187 | |
188 bookmarkNode.isSelected = false; | |
189 bookmarkNode.isOpen = true; | |
190 for (var i = 0; i < bookmarkNode.children.length; i++) | |
191 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | |
192 }; | |
OLD | NEW |