| 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 var BookmarksStore = 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: { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 */ | 65 */ |
| 66 initializeStore: function() { | 66 initializeStore: function() { |
| 67 chrome.bookmarks.getTree(function(results) { | 67 chrome.bookmarks.getTree(function(results) { |
| 68 this.setupStore_(results[0]); | 68 this.setupStore_(results[0]); |
| 69 }.bind(this)); | 69 }.bind(this)); |
| 70 // Attach bookmarks API listeners. | 70 // Attach bookmarks API listeners. |
| 71 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); | 71 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); |
| 72 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); | 72 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); |
| 73 }, | 73 }, |
| 74 | 74 |
| 75 //////////////////////////////////////////////////////////////////////////////// | 75 ////////////////////////////////////////////////////////////////////////////// |
| 76 // bookmarks-store, private: | 76 // bookmarks-store, private: |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * @param {BookmarkTreeNode} rootNode | 79 * @param {BookmarkTreeNode} rootNode |
| 80 * @private | 80 * @private |
| 81 */ | 81 */ |
| 82 setupStore_: function(rootNode) { | 82 setupStore_: function(rootNode) { |
| 83 this.rootNode = rootNode; | 83 this.rootNode = rootNode; |
| 84 this.idToNodeMap_ = {}; | 84 this.idToNodeMap_ = {}; |
| 85 this.rootNode.path = 'rootNode'; | 85 this.rootNode.path = 'rootNode'; |
| 86 BookmarksStore.generatePaths(rootNode, 0); | 86 BookmarksStore.generatePaths(rootNode, 0); |
| 87 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); | 87 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
| 88 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 88 |
| 89 // Initialize the store's fields from the router. |
| 90 if (this.$.router.searchTerm) |
| 91 this.searchTerm = this.$.router.searchTerm; |
| 92 else |
| 93 this.fire('selected-folder-changed', this.$.router.selectedId); |
| 89 }, | 94 }, |
| 90 | 95 |
| 91 /** @private */ | 96 /** @private */ |
| 92 deselectFolders_: function() { | 97 deselectFolders_: function() { |
| 93 this.unlinkPaths('displayedList'); | 98 this.unlinkPaths('displayedList'); |
| 94 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | 99 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 95 this.selectedId = null; | 100 this.selectedId = null; |
| 96 }, | 101 }, |
| 97 | 102 |
| 98 /** | 103 /** |
| 99 * @param {BookmarkTreeNode} folder | 104 * @param {BookmarkTreeNode} folder |
| 100 * @private | 105 * @private |
| 101 * @return {boolean} | 106 * @return {boolean} |
| 102 */ | 107 */ |
| 103 isAncestorOfSelected_: function(folder) { | 108 isAncestorOfSelected_: function(folder) { |
| 104 if (!this.selectedId) | 109 if (!this.selectedId) |
| 105 return false; | 110 return false; |
| 106 | 111 |
| 107 var selectedNode = this.idToNodeMap_[this.selectedId]; | 112 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 108 return selectedNode.path.startsWith(folder.path); | 113 return selectedNode.path.startsWith(folder.path); |
| 109 }, | 114 }, |
| 110 | 115 |
| 111 /** @private */ | 116 /** @private */ |
| 112 updateSearchDisplay_: function() { | 117 updateSearchDisplay_: function() { |
| 113 if (this.searchTerm == '') { | 118 if (!this.searchTerm) { |
| 114 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 119 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 115 } else { | 120 } else { |
| 116 chrome.bookmarks.search(this.searchTerm, function(results) { | 121 chrome.bookmarks.search(this.searchTerm, function(results) { |
| 117 if (this.selectedId) | 122 if (this.selectedId) |
| 118 this.deselectFolders_(); | 123 this.deselectFolders_(); |
| 119 | 124 |
| 120 this._setDisplayedList(results); | 125 this._setDisplayedList(results); |
| 121 }.bind(this)); | 126 }.bind(this)); |
| 122 } | 127 } |
| 123 }, | 128 }, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 143 if (!node) | 148 if (!node) |
| 144 return; | 149 return; |
| 145 | 150 |
| 146 if (node.children) { | 151 if (node.children) { |
| 147 for (var i = 0; i < node.children.length; i++) | 152 for (var i = 0; i < node.children.length; i++) |
| 148 this.removeDescendantsFromMap_(node.children[i].id); | 153 this.removeDescendantsFromMap_(node.children[i].id); |
| 149 } | 154 } |
| 150 delete this.idToNodeMap_[id]; | 155 delete this.idToNodeMap_[id]; |
| 151 }, | 156 }, |
| 152 | 157 |
| 153 //////////////////////////////////////////////////////////////////////////////// | 158 ////////////////////////////////////////////////////////////////////////////// |
| 154 // bookmarks-store, bookmarks API event listeners: | 159 // bookmarks-store, bookmarks API event listeners: |
| 155 | 160 |
| 156 /** | 161 /** |
| 157 * Callback for when a bookmark node is removed. | 162 * Callback for when a bookmark node is removed. |
| 158 * If a folder is selected or is an ancestor of a selected folder, the parent | 163 * If a folder is selected or is an ancestor of a selected folder, the parent |
| 159 * of the removed folder will be selected. | 164 * of the removed folder will be selected. |
| 160 * @param {string} id The id of the removed bookmark node. | 165 * @param {string} id The id of the removed bookmark node. |
| 161 * @param {!{index: number, | 166 * @param {!{index: number, |
| 162 * parentId: string, | 167 * parentId: string, |
| 163 * node: BookmarkTreeNode}} removeInfo | 168 * node: BookmarkTreeNode}} removeInfo |
| 164 */ | 169 */ |
| (...skipping 19 matching lines...) Expand all Loading... |
| 184 onBookmarkChanged_: function(id, changeInfo) { | 189 onBookmarkChanged_: function(id, changeInfo) { |
| 185 if (changeInfo.title) | 190 if (changeInfo.title) |
| 186 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); | 191 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); |
| 187 if (changeInfo.url) | 192 if (changeInfo.url) |
| 188 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); | 193 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); |
| 189 | 194 |
| 190 if (this.searchTerm) | 195 if (this.searchTerm) |
| 191 this.updateSearchDisplay_(); | 196 this.updateSearchDisplay_(); |
| 192 }, | 197 }, |
| 193 | 198 |
| 194 //////////////////////////////////////////////////////////////////////////////// | 199 ////////////////////////////////////////////////////////////////////////////// |
| 195 // bookmarks-store, bookmarks app event listeners: | 200 // bookmarks-store, bookmarks app event listeners: |
| 196 | 201 |
| 197 /** | 202 /** |
| 198 * @param {Event} e | 203 * @param {Event} e |
| 199 * @private | 204 * @private |
| 200 */ | 205 */ |
| 201 onSearchTermChanged_: function(e) { | 206 onSearchTermChanged_: function(e) { |
| 202 this.searchTerm = /** @type {string} */ (e.detail); | 207 this.searchTerm = /** @type {string} */ (e.detail); |
| 203 }, | 208 }, |
| 204 | 209 |
| 205 /** | 210 /** |
| 206 * Selects the folder specified by the event and deselects the previously | 211 * Selects the folder specified by the event and deselects the previously |
| 207 * selected folder. | 212 * selected folder. |
| 208 * @param {CustomEvent} e | 213 * @param {CustomEvent} e |
| 209 * @private | 214 * @private |
| 210 */ | 215 */ |
| 211 onSelectedFolderChanged_: function(e) { | 216 onSelectedFolderChanged_: function(e) { |
| 212 if (this.searchTerm) | 217 if (this.searchTerm) |
| 213 this.searchTerm = ''; | 218 this.searchTerm = ''; |
| 214 | 219 |
| 215 // Deselect the old folder if defined. | 220 // Deselect the old folder if defined. |
| 216 if (this.selectedId) | 221 if (this.selectedId) |
| 217 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | 222 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 218 | 223 |
| 219 var selectedId = /** @type {string} */ (e.detail); | 224 // Check if the selected id is that of a defined folder. |
| 220 var newFolder = this.idToNodeMap_[selectedId]; | 225 var id = /** @type {string} */ (e.detail); |
| 226 if (!this.idToNodeMap_[id] || this.idToNodeMap_[id].url) |
| 227 id = this.rootNode.children[0].id; |
| 228 |
| 229 var newFolder = this.idToNodeMap_[id]; |
| 221 this.set(newFolder.path + '.isSelected', true); | 230 this.set(newFolder.path + '.isSelected', true); |
| 222 this.selectedId = selectedId; | 231 this.selectedId = id; |
| 223 }, | 232 }, |
| 224 | 233 |
| 225 /** | 234 /** |
| 226 * Handles events that open and close folders. | 235 * Handles events that open and close folders. |
| 227 * @param {CustomEvent} e | 236 * @param {CustomEvent} e |
| 228 * @private | 237 * @private |
| 229 */ | 238 */ |
| 230 onFolderOpenChanged_: function(e) { | 239 onFolderOpenChanged_: function(e) { |
| 231 var folder = this.idToNodeMap_[e.detail.id]; | 240 var folder = this.idToNodeMap_[e.detail.id]; |
| 232 this.set(folder.path + '.isOpen', e.detail.open); | 241 this.set(folder.path + '.isOpen', e.detail.open); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 275 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| 267 | 276 |
| 268 if (bookmarkNode.url) | 277 if (bookmarkNode.url) |
| 269 return; | 278 return; |
| 270 | 279 |
| 271 bookmarkNode.isSelected = false; | 280 bookmarkNode.isSelected = false; |
| 272 bookmarkNode.isOpen = true; | 281 bookmarkNode.isOpen = true; |
| 273 for (var i = 0; i < bookmarkNode.children.length; i++) | 282 for (var i = 0; i < bookmarkNode.children.length; i++) |
| 274 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 283 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
| 275 }; | 284 }; |
| OLD | NEW |