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 // Attach bookmarks API listeners. | |
| 58 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); | 59 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); |
| 60 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); | |
| 59 }, | 61 }, |
| 60 | 62 |
| 63 //////////////////////////////////////////////////////////////////////////////// | |
| 64 // bookmarks-store, private: | |
|
tsergeant
2017/01/09 00:21:05
👍 I like this lots
| |
| 65 | |
| 61 /** | 66 /** |
| 62 * @param {BookmarkTreeNode} rootNode | 67 * @param {BookmarkTreeNode} rootNode |
| 63 * @private | 68 * @private |
| 64 */ | 69 */ |
| 65 setupStore_: function(rootNode) { | 70 setupStore_: function(rootNode) { |
| 66 this.rootNode = rootNode; | 71 this.rootNode = rootNode; |
| 67 this.idToNodeMap_ = {}; | 72 this.idToNodeMap_ = {}; |
| 68 this.rootNode.path = 'rootNode'; | 73 this.rootNode.path = 'rootNode'; |
| 69 this.generatePaths_(rootNode, 0); | 74 this.generatePaths_(rootNode, 0); |
| 70 this.initNodes_(this.rootNode); | 75 this.initNodes_(this.rootNode); |
| 71 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 76 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 72 }, | 77 }, |
| 73 | 78 |
| 74 /** | 79 /** |
| 75 * Selects the folder specified by the event and deselects the previously | 80 * Initializes the nodes in the bookmarks tree as follows: |
| 76 * selected folder. | 81 * - Populates |idToNodeMap_| with a mapping of all node ids to their |
| 77 * @param {CustomEvent} e | 82 * corresponding BookmarkTreeNode. |
| 83 * - Sets all the nodes to not selected and open by default. | |
| 84 * @param {BookmarkTreeNode} bookmarkNode | |
| 78 * @private | 85 * @private |
| 79 */ | 86 */ |
| 80 onSelectedFolderChanged_: function(e) { | 87 initNodes_: function(bookmarkNode) { |
| 81 // Deselect the old folder if defined. | 88 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; |
| 82 if (this.selectedId) | 89 if (bookmarkNode.url) |
| 83 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | 90 return; |
| 84 | 91 |
| 85 var selectedId = /** @type {string} */ (e.detail); | 92 bookmarkNode.isSelected = false; |
| 86 var newFolder = this.idToNodeMap_[selectedId]; | 93 bookmarkNode.isOpen = true; |
| 87 this.set(newFolder.path + '.isSelected', true); | 94 for (var i = 0; i < bookmarkNode.children.length; i++) { |
| 88 this.selectedId = selectedId; | 95 this.initNodes_(bookmarkNode.children[i]); |
| 96 } | |
| 89 }, | 97 }, |
| 90 | 98 |
| 91 /** | 99 /** |
| 92 * Handles events that open and close folders. | |
| 93 * @param {CustomEvent} e | |
| 94 * @private | |
| 95 */ | |
| 96 onFolderOpenChanged_: function(e) { | |
| 97 var folder = this.idToNodeMap_[e.detail.id]; | |
| 98 this.set(folder.path + '.isOpen', e.detail.open); | |
| 99 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) | |
| 100 this.fire('selected-folder-changed', folder.id); | |
| 101 }, | |
| 102 | |
| 103 /** | |
| 104 * @param {BookmarkTreeNode} folder | 100 * @param {BookmarkTreeNode} folder |
| 105 * @private | 101 * @private |
| 106 * @return {boolean} | 102 * @return {boolean} |
| 107 */ | 103 */ |
| 108 isAncestorOfSelected_: function(folder) { | 104 isAncestorOfSelected_: function(folder) { |
| 109 return this.selectedNode.path.startsWith(folder.path); | 105 return this.selectedNode.path.startsWith(folder.path); |
| 110 }, | 106 }, |
| 111 | 107 |
| 112 /** @private */ | 108 /** @private */ |
| 113 updateSelectedNode_: function() { | 109 updateSelectedNode_: function() { |
| 114 var selectedNode = this.idToNodeMap_[this.selectedId]; | 110 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 115 this.linkPaths('selectedNode', selectedNode.path); | 111 this.linkPaths('selectedNode', selectedNode.path); |
| 116 this._setSelectedNode(selectedNode); | 112 this._setSelectedNode(selectedNode); |
| 117 }, | 113 }, |
| 118 | 114 |
| 119 /** | 115 /** |
| 116 * Stores the path from the store to a node inside the node. | |
| 117 * @param {BookmarkTreeNode} bookmarkNode | |
| 118 * @param {number} startIndex | |
| 119 * @private | |
| 120 */ | |
| 121 generatePaths_: function(bookmarkNode, startIndex) { | |
| 122 if (!bookmarkNode.children) | |
| 123 return; | |
| 124 | |
| 125 for (var i = startIndex; i < bookmarkNode.children.length; i++) { | |
| 126 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i; | |
| 127 this.generatePaths_(bookmarkNode.children[i], 0); | |
| 128 } | |
| 129 }, | |
| 130 | |
| 131 /** | |
| 132 * Remove all descendants of a given node from the map. | |
| 133 * @param {string} id | |
| 134 * @private | |
| 135 */ | |
| 136 removeDescendantsFromMap_: function(id) { | |
| 137 var node = this.idToNodeMap_[id]; | |
| 138 if (!node) | |
| 139 return; | |
| 140 | |
| 141 if (node.children) { | |
| 142 for (var i = 0; i < node.children.length; i++) | |
| 143 this.removeDescendantsFromMap_(node.children[i].id); | |
| 144 } | |
| 145 delete this.idToNodeMap_[id]; | |
| 146 }, | |
| 147 | |
| 148 //////////////////////////////////////////////////////////////////////////////// | |
| 149 // bookmarks-store, bookmarks API event listeners: | |
| 150 | |
|
tsergeant
2017/01/09 00:21:04
Just one blank line here
jiaxi
2017/01/09 02:17:46
Done.
| |
| 151 | |
| 152 /** | |
| 120 * Callback for when a bookmark node is removed. | 153 * Callback for when a bookmark node is removed. |
| 121 * If a folder is selected or is an ancestor of a selected folder, the parent | 154 * If a folder is selected or is an ancestor of a selected folder, the parent |
| 122 * of the removed folder will be selected. | 155 * of the removed folder will be selected. |
| 123 * @param {string} id The id of the removed bookmark node. | 156 * @param {string} id The id of the removed bookmark node. |
| 124 * @param {!{index: number, | 157 * @param {!{index: number, |
| 125 * parentId: string, | 158 * parentId: string, |
| 126 * node: BookmarkTreeNode}} removeInfo | 159 * node: BookmarkTreeNode}} removeInfo |
| 127 */ | 160 */ |
| 128 onBookmarkRemoved_: function(id, removeInfo) { | 161 onBookmarkRemoved_: function(id, removeInfo) { |
| 129 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) | 162 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) |
| 130 this.fire('selected-folder-changed', removeInfo.parentId); | 163 this.fire('selected-folder-changed', removeInfo.parentId); |
| 131 | 164 |
| 132 var parentNode = this.idToNodeMap_[removeInfo.parentId]; | 165 var parentNode = this.idToNodeMap_[removeInfo.parentId]; |
| 133 this.splice(parentNode.path + '.children', removeInfo.index, 1); | 166 this.splice(parentNode.path + '.children', removeInfo.index, 1); |
| 134 this.removeDescendantsFromMap_(id); | 167 this.removeDescendantsFromMap_(id); |
| 135 this.generatePaths_(parentNode, removeInfo.index); | 168 this.generatePaths_(parentNode, removeInfo.index); |
| 136 }, | 169 }, |
| 137 | 170 |
| 138 /** | 171 /** |
| 139 * Initializes the nodes in the bookmarks tree as follows: | 172 * Called when the title of a bookmark changes. |
| 140 * - Populates |idToNodeMap_| with a mapping of all node ids to their | 173 * @param {string} id The id of changed bookmark node. |
| 141 * corresponding BookmarkTreeNode. | 174 * @param {!Object} changeInfo |
| 142 * - Sets all the nodes to not selected and open by default. | 175 */ |
| 143 * @param {BookmarkTreeNode} bookmarkNode | 176 onBookmarkChanged_: function(id, changeInfo) { |
| 177 if (changeInfo.title) | |
| 178 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); | |
| 179 if (changeInfo.url) | |
| 180 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); | |
| 181 }, | |
| 182 | |
| 183 //////////////////////////////////////////////////////////////////////////////// | |
| 184 // bookmarks-store, bookmarks app event listeners: | |
| 185 | |
| 186 /** | |
| 187 * Selects the folder specified by the event and deselects the previously | |
| 188 * selected folder. | |
| 189 * @param {CustomEvent} e | |
| 144 * @private | 190 * @private |
| 145 */ | 191 */ |
| 146 initNodes_: function(bookmarkNode) { | 192 onSelectedFolderChanged_: function(e) { |
| 147 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | 193 // Deselect the old folder if defined. |
| 148 if (bookmarkNode.url) | 194 if (this.selectedId) |
| 149 return; | 195 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 150 | 196 |
| 151 bookmarkNode.isSelected = false; | 197 var selectedId = /** @type {string} */ (e.detail); |
| 152 bookmarkNode.isOpen = true; | 198 var newFolder = this.idToNodeMap_[selectedId]; |
| 153 for (var i = 0; i < bookmarkNode.children.length; i++) { | 199 this.set(newFolder.path + '.isSelected', true); |
| 154 this.initNodes_(bookmarkNode.children[i]); | 200 this.selectedId = selectedId; |
| 155 } | |
| 156 }, | 201 }, |
| 157 | 202 |
| 158 /** | 203 /** |
| 159 * Stores the path from the store to a node inside the node. | 204 * Handles events that open and close folders. |
| 160 * @param {BookmarkTreeNode} bookmarkNode | 205 * @param {CustomEvent} e |
| 161 * @param {number} startIndex | |
| 162 * @private | 206 * @private |
| 163 */ | 207 */ |
| 164 generatePaths_: function(bookmarkNode, startIndex) { | 208 onFolderOpenChanged_: function(e) { |
| 165 if (!bookmarkNode.children) | 209 var folder = this.idToNodeMap_[e.detail.id]; |
| 166 return; | 210 this.set(folder.path + '.isOpen', e.detail.open); |
| 167 | 211 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) |
| 168 for (var i = startIndex; i < bookmarkNode.children.length; i++) { | 212 this.fire('selected-folder-changed', folder.id); |
| 169 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i; | |
| 170 this.generatePaths_(bookmarkNode.children[i], 0); | |
| 171 } | |
| 172 }, | 213 }, |
| 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 } | |
| 190 }); | 214 }); |
| OLD | NEW |