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 13 matching lines...) Expand all Loading... | |
24 notify: true, | 24 notify: true, |
25 readOnly: true, | 25 readOnly: true, |
26 }, | 26 }, |
27 | 27 |
28 idToNodeMap_: Object, | 28 idToNodeMap_: Object, |
29 }, | 29 }, |
30 | 30 |
31 /** @private {Object} */ | 31 /** @private {Object} */ |
32 documentListeners_: null, | 32 documentListeners_: null, |
33 | 33 |
34 /** | |
35 * Initializes the store with data from the bookmarks API. | |
36 * Called by app on attached. | |
37 */ | |
38 initializeStore: function() { | |
39 chrome.bookmarks.getTree(function(results) { | |
40 this.setupStore_(results[0]); | |
41 }.bind(this)); | |
42 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); | |
43 }, | |
44 | |
calamity
2017/01/04 06:57:44
Move this back to where it was.
jiaxi
2017/01/04 22:44:00
Done.
| |
34 /** @override */ | 45 /** @override */ |
35 attached: function() { | 46 attached: function() { |
36 this.documentListeners_ = { | 47 this.documentListeners_ = { |
37 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | 48 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
38 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | 49 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
39 }; | 50 }; |
40 for (var event in this.documentListeners_) | 51 for (var event in this.documentListeners_) |
41 document.addEventListener(event, this.documentListeners_[event]); | 52 document.addEventListener(event, this.documentListeners_[event]); |
42 }, | 53 }, |
43 | 54 |
44 /** @override */ | 55 /** @override */ |
45 detached: function() { | 56 detached: function() { |
46 for (var event in this.documentListeners_) | 57 for (var event in this.documentListeners_) |
47 document.removeEventListener(event, this.documentListeners_[event]); | 58 document.removeEventListener(event, this.documentListeners_[event]); |
48 }, | 59 }, |
49 | 60 |
50 /** | 61 /** |
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 * @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); | |
calamity
2017/01/04 06:57:44
nit: newline
jiaxi
2017/01/04 22:44:00
Done.
| |
129 var parentNode = this.idToNodeMap_[removeInfo.parentId]; | |
130 this.splice(parentNode.path + '.children', removeInfo.index, 1); | |
131 this.removeChildrenFromMap_(id); | |
132 this.generatePaths_(parentNode, Number(removeInfo.index)); | |
133 }, | |
134 | |
135 /** | |
118 * Initializes the nodes in the bookmarks tree as follows: | 136 * Initializes the nodes in the bookmarks tree as follows: |
119 * - Populates |idToNodeMap_| with a mapping of all node ids to their | 137 * - Populates |idToNodeMap_| with a mapping of all node ids to their |
120 * corresponding BookmarkTreeNode. | 138 * 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. | 139 * - Sets all the nodes to not selected and open by default. |
123 * @param {BookmarkTreeNode} bookmarkNode | 140 * @param {BookmarkTreeNode} bookmarkNode |
124 * @private | 141 * @private |
125 */ | 142 */ |
126 initNodes_: function(bookmarkNode) { | 143 initNodes_: function(bookmarkNode) { |
127 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | 144 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; |
128 if (bookmarkNode.url) | 145 if (bookmarkNode.url) |
129 return; | 146 return; |
130 | 147 |
131 bookmarkNode.isSelected = false; | 148 bookmarkNode.isSelected = false; |
132 bookmarkNode.isOpen = true; | 149 bookmarkNode.isOpen = true; |
133 for (var i = 0; i < bookmarkNode.children.length; i++) { | 150 for (var i = 0; i < bookmarkNode.children.length; i++) { |
134 bookmarkNode.children[i].path = bookmarkNode.path + '.children.' + i; | 151 bookmarkNode.children[i].isSelected = false; |
152 bookmarkNode.isOpen = true; | |
135 this.initNodes_(bookmarkNode.children[i]); | 153 this.initNodes_(bookmarkNode.children[i]); |
136 } | 154 } |
137 }, | 155 }, |
156 | |
157 /** | |
158 * Stores the path from the store to a node inside the node. | |
159 * @param {BookmarkTreeNode} bookmarkNode | |
160 * @param {number} startIndex | |
161 * @private | |
162 */ | |
163 generatePaths_: function(bookmarkNode, startIndex) { | |
164 if (bookmarkNode.children) { | |
calamity
2017/01/04 06:57:44
Invert and early return.
jiaxi
2017/01/04 22:44:00
I think the early return is in this(https://codere
calamity
2017/01/05 07:07:08
You might as well, the next patch will just pull i
| |
165 for (var i = startIndex; i < bookmarkNode.children.length; i++) { | |
166 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i; | |
167 this.generatePaths_(bookmarkNode.children[i], 0); | |
168 } | |
169 } | |
170 }, | |
171 | |
172 /** | |
calamity
2017/01/04 06:57:44
This method deserves a comment.
jiaxi
2017/01/04 22:44:00
Done.
| |
173 * @param {string} id | |
174 * @private | |
175 */ | |
176 removeChildrenFromMap_: function(id) { | |
calamity
2017/01/04 06:57:44
Since this is all recursive children, I would pref
jiaxi
2017/01/04 22:44:00
Done.
| |
177 var node = this.idToNodeMap_[id]; | |
178 if (!node) | |
179 return; | |
180 | |
181 if (node.children) { | |
182 for (var i = 0; i < node.children.length; i++) | |
183 this.removeChildrenFromMap_(node.children[i].id); | |
184 } | |
185 delete this.idToNodeMap_[id]; | |
186 } | |
138 }); | 187 }); |
OLD | NEW |