| 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 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 /** @private {Object} */ | 42 /** @private {Object} */ |
| 43 documentListeners_: null, | 43 documentListeners_: null, |
| 44 | 44 |
| 45 /** @override */ | 45 /** @override */ |
| 46 attached: function() { | 46 attached: function() { |
| 47 this.documentListeners_ = { | 47 this.documentListeners_ = { |
| 48 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | 48 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
| 49 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | 49 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
| 50 'search-term-changed': this.onSearchTermChanged_.bind(this), | 50 'search-term-changed': this.onSearchTermChanged_.bind(this), |
| 51 'url-changed': this.onUrlChanged_.bind(this), |
| 51 }; | 52 }; |
| 52 for (var event in this.documentListeners_) | 53 for (var event in this.documentListeners_) |
| 53 document.addEventListener(event, this.documentListeners_[event]); | 54 document.addEventListener(event, this.documentListeners_[event]); |
| 54 }, | 55 }, |
| 55 | 56 |
| 56 /** @override */ | 57 /** @override */ |
| 57 detached: function() { | 58 detached: function() { |
| 58 for (var event in this.documentListeners_) | 59 for (var event in this.documentListeners_) |
| 59 document.removeEventListener(event, this.documentListeners_[event]); | 60 document.removeEventListener(event, this.documentListeners_[event]); |
| 60 }, | 61 }, |
| 61 | 62 |
| 62 /** | 63 /** |
| 63 * Initializes the store with data from the bookmarks API. | 64 * Initializes the store with data from the bookmarks API. |
| 64 * Called by app on attached. | 65 * Called by app on attached. |
| 65 */ | 66 */ |
| 66 initializeStore: function() { | 67 initializeStore: function() { |
| 67 chrome.bookmarks.getTree(function(results) { | 68 chrome.bookmarks.getTree(function(results) { |
| 68 this.setupStore_(results[0]); | 69 this.setupStore_(results[0]); |
| 69 }.bind(this)); | 70 }.bind(this)); |
| 70 // Attach bookmarks API listeners. | 71 // Attach bookmarks API listeners. |
| 71 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); | 72 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); |
| 72 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); | 73 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); |
| 73 }, | 74 }, |
| 74 | 75 |
| 75 //////////////////////////////////////////////////////////////////////////////// | 76 ////////////////////////////////////////////////////////////////////////////// |
| 76 // bookmarks-store, private: | 77 // bookmarks-store, private: |
| 77 | 78 |
| 78 /** | 79 /** |
| 79 * @param {BookmarkTreeNode} rootNode | 80 * @param {BookmarkTreeNode} rootNode |
| 80 * @private | 81 * @private |
| 81 */ | 82 */ |
| 82 setupStore_: function(rootNode) { | 83 setupStore_: function(rootNode) { |
| 83 this.rootNode = rootNode; | 84 this.rootNode = rootNode; |
| 84 this.idToNodeMap_ = {}; | 85 this.idToNodeMap_ = {}; |
| 85 this.rootNode.path = 'rootNode'; | 86 this.rootNode.path = 'rootNode'; |
| 86 BookmarksStore.generatePaths(rootNode, 0); | 87 BookmarksStore.generatePaths(rootNode, 0); |
| 87 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); | 88 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
| 88 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 89 |
| 90 // Initialize the store's fields from the router. |
| 91 this.onUrlChanged_(); |
| 89 }, | 92 }, |
| 90 | 93 |
| 91 /** @private */ | 94 /** @private */ |
| 92 deselectFolders_: function() { | 95 deselectFolders_: function() { |
| 93 this.unlinkPaths('displayedList'); | 96 this.unlinkPaths('displayedList'); |
| 94 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | 97 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 95 this.selectedId = null; | 98 this.selectedId = null; |
| 96 }, | 99 }, |
| 97 | 100 |
| 98 /** | 101 /** |
| 99 * @param {BookmarkTreeNode} folder | 102 * @param {BookmarkTreeNode} folder |
| 100 * @private | 103 * @private |
| 101 * @return {boolean} | 104 * @return {boolean} |
| 102 */ | 105 */ |
| 103 isAncestorOfSelected_: function(folder) { | 106 isAncestorOfSelected_: function(folder) { |
| 104 if (!this.selectedId) | 107 if (!this.selectedId) |
| 105 return false; | 108 return false; |
| 106 | 109 |
| 107 var selectedNode = this.idToNodeMap_[this.selectedId]; | 110 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 108 return selectedNode.path.startsWith(folder.path); | 111 return selectedNode.path.startsWith(folder.path); |
| 109 }, | 112 }, |
| 110 | 113 |
| 111 /** @private */ | 114 /** @private */ |
| 112 updateSearchDisplay_: function() { | 115 updateSearchDisplay_: function() { |
| 113 if (this.searchTerm == '') { | 116 if (!this.searchTerm) { |
| 114 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 117 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 115 } else { | 118 } else { |
| 116 chrome.bookmarks.search(this.searchTerm, function(results) { | 119 chrome.bookmarks.search(this.searchTerm, function(results) { |
| 117 if (this.selectedId) | 120 if (this.selectedId) |
| 118 this.deselectFolders_(); | 121 this.deselectFolders_(); |
| 119 | 122 |
| 120 this._setDisplayedList(results); | 123 this._setDisplayedList(results); |
| 121 }.bind(this)); | 124 }.bind(this)); |
| 122 } | 125 } |
| 123 }, | 126 }, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 143 if (!node) | 146 if (!node) |
| 144 return; | 147 return; |
| 145 | 148 |
| 146 if (node.children) { | 149 if (node.children) { |
| 147 for (var i = 0; i < node.children.length; i++) | 150 for (var i = 0; i < node.children.length; i++) |
| 148 this.removeDescendantsFromMap_(node.children[i].id); | 151 this.removeDescendantsFromMap_(node.children[i].id); |
| 149 } | 152 } |
| 150 delete this.idToNodeMap_[id]; | 153 delete this.idToNodeMap_[id]; |
| 151 }, | 154 }, |
| 152 | 155 |
| 153 //////////////////////////////////////////////////////////////////////////////// | 156 ////////////////////////////////////////////////////////////////////////////// |
| 154 // bookmarks-store, bookmarks API event listeners: | 157 // bookmarks-store, bookmarks API event listeners: |
| 155 | 158 |
| 156 /** | 159 /** |
| 157 * Callback for when a bookmark node is removed. | 160 * Callback for when a bookmark node is removed. |
| 158 * If a folder is selected or is an ancestor of a selected folder, the parent | 161 * If a folder is selected or is an ancestor of a selected folder, the parent |
| 159 * of the removed folder will be selected. | 162 * of the removed folder will be selected. |
| 160 * @param {string} id The id of the removed bookmark node. | 163 * @param {string} id The id of the removed bookmark node. |
| 161 * @param {!{index: number, | 164 * @param {!{index: number, |
| 162 * parentId: string, | 165 * parentId: string, |
| 163 * node: BookmarkTreeNode}} removeInfo | 166 * node: BookmarkTreeNode}} removeInfo |
| 164 */ | 167 */ |
| (...skipping 19 matching lines...) Expand all Loading... |
| 184 onBookmarkChanged_: function(id, changeInfo) { | 187 onBookmarkChanged_: function(id, changeInfo) { |
| 185 if (changeInfo.title) | 188 if (changeInfo.title) |
| 186 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); | 189 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); |
| 187 if (changeInfo.url) | 190 if (changeInfo.url) |
| 188 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); | 191 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); |
| 189 | 192 |
| 190 if (this.searchTerm) | 193 if (this.searchTerm) |
| 191 this.updateSearchDisplay_(); | 194 this.updateSearchDisplay_(); |
| 192 }, | 195 }, |
| 193 | 196 |
| 194 //////////////////////////////////////////////////////////////////////////////// | 197 ////////////////////////////////////////////////////////////////////////////// |
| 195 // bookmarks-store, bookmarks app event listeners: | 198 // bookmarks-store, bookmarks app event listeners: |
| 196 | 199 |
| 197 /** | 200 /** |
| 198 * @param {Event} e | 201 * @param {Event} e |
| 199 * @private | 202 * @private |
| 200 */ | 203 */ |
| 201 onSearchTermChanged_: function(e) { | 204 onSearchTermChanged_: function(e) { |
| 202 this.searchTerm = /** @type {string} */ (e.detail); | 205 this.searchTerm = /** @type {string} */ (e.detail); |
| 203 }, | 206 }, |
| 204 | 207 |
| 205 /** | 208 /** |
| (...skipping 20 matching lines...) Expand all Loading... |
| 226 * Handles events that open and close folders. | 229 * Handles events that open and close folders. |
| 227 * @param {CustomEvent} e | 230 * @param {CustomEvent} e |
| 228 * @private | 231 * @private |
| 229 */ | 232 */ |
| 230 onFolderOpenChanged_: function(e) { | 233 onFolderOpenChanged_: function(e) { |
| 231 var folder = this.idToNodeMap_[e.detail.id]; | 234 var folder = this.idToNodeMap_[e.detail.id]; |
| 232 this.set(folder.path + '.isOpen', e.detail.open); | 235 this.set(folder.path + '.isOpen', e.detail.open); |
| 233 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) | 236 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) |
| 234 this.fire('selected-folder-changed', folder.id); | 237 this.fire('selected-folder-changed', folder.id); |
| 235 }, | 238 }, |
| 239 |
| 240 /** |
| 241 * Handles events from the router where the url changes. |
| 242 * @private |
| 243 */ |
| 244 onUrlChanged_: function() { |
| 245 var id = this.$.router.selectedId; |
| 246 if (this.$.router.searchTerm) { |
| 247 this.searchTerm = this.$.router.searchTerm; |
| 248 } else if (id && id in this.idToNodeMap_ && !this.idToNodeMap_[id].url) { |
| 249 this.fire('selected-folder-changed', id); |
| 250 } else { |
| 251 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 252 } |
| 253 }, |
| 236 }); | 254 }); |
| 237 | 255 |
| 238 //////////////////////////////////////////////////////////////////////////////// | 256 //////////////////////////////////////////////////////////////////////////////// |
| 239 // bookmarks-store, static methods: | 257 // bookmarks-store, static methods: |
| 240 | 258 |
| 241 /** | 259 /** |
| 242 * Stores the path from the store to a node inside the node. | 260 * Stores the path from the store to a node inside the node. |
| 243 * @param {BookmarkTreeNode} bookmarkNode | 261 * @param {BookmarkTreeNode} bookmarkNode |
| 244 * @param {number} startIndex | 262 * @param {number} startIndex |
| 245 */ | 263 */ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 266 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 284 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| 267 | 285 |
| 268 if (bookmarkNode.url) | 286 if (bookmarkNode.url) |
| 269 return; | 287 return; |
| 270 | 288 |
| 271 bookmarkNode.isSelected = false; | 289 bookmarkNode.isSelected = false; |
| 272 bookmarkNode.isOpen = true; | 290 bookmarkNode.isOpen = true; |
| 273 for (var i = 0; i < bookmarkNode.children.length; i++) | 291 for (var i = 0; i < bookmarkNode.children.length; i++) |
| 274 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 292 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
| 275 }; | 293 }; |
| OLD | NEW |