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 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: { |
| 11 type: Object, | 11 type: Object, |
| 12 notify: true, | 12 notify: true, |
| 13 }, | 13 }, |
| 14 | 14 |
| 15 selectedId: { | 15 selectedId: { |
|
tsergeant
2017/01/12 23:44:20
I think you'll need to put /** @type {?string} */
angelayang
2017/01/13 00:25:11
yep that works thankyou
| |
| 16 type: String, | 16 type: String, |
| 17 observer: 'updateSelectedNode_', | 17 observer: 'updateSelectedDisplay_', |
| 18 notify: true, | 18 notify: true, |
|
tsergeant
2017/01/12 23:44:21
While you're here:
selectedId is public, but neve
angelayang
2017/01/13 00:25:11
left public for URL/router
| |
| 19 }, | 19 }, |
| 20 | 20 |
| 21 /** @type {BookmarkTreeNode} */ | 21 searchTerm: { |
| 22 selectedNode: { | 22 type: String, |
| 23 type: Object, | 23 observer: 'updateSearchDisplay_', |
| 24 notify: true, | |
| 25 }, | |
| 26 | |
| 27 /** | |
| 28 * This updates to either the result of a search or the contents of the | |
| 29 * selected folder. | |
| 30 * @type {Array<BookmarkTreeNode>} | |
| 31 */ | |
| 32 displayedList: { | |
| 33 type: Array, | |
| 24 notify: true, | 34 notify: true, |
| 25 readOnly: true, | 35 readOnly: true, |
| 26 }, | 36 }, |
| 27 | 37 |
| 28 idToNodeMap_: Object, | 38 idToNodeMap_: Object, |
| 29 }, | 39 }, |
| 30 | 40 |
| 31 /** @private {Object} */ | 41 /** @private {Object} */ |
| 32 documentListeners_: null, | 42 documentListeners_: null, |
| 33 | 43 |
| 34 /** @override */ | 44 /** @override */ |
| 35 attached: function() { | 45 attached: function() { |
| 36 this.documentListeners_ = { | 46 this.documentListeners_ = { |
| 37 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | 47 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
| 38 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | 48 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
| 49 'search-term-changed': this.onSearchTermChanged_.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 }, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 69 */ | 80 */ |
| 70 setupStore_: function(rootNode) { | 81 setupStore_: function(rootNode) { |
| 71 this.rootNode = rootNode; | 82 this.rootNode = rootNode; |
| 72 this.idToNodeMap_ = {}; | 83 this.idToNodeMap_ = {}; |
| 73 this.rootNode.path = 'rootNode'; | 84 this.rootNode.path = 'rootNode'; |
| 74 BookmarksStore.generatePaths(rootNode, 0); | 85 BookmarksStore.generatePaths(rootNode, 0); |
| 75 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); | 86 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
| 76 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 87 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 77 }, | 88 }, |
| 78 | 89 |
| 90 /** @private */ | |
| 91 deselectFolders_: function() { | |
| 92 this.unlinkPaths('displayedList'); | |
| 93 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | |
| 94 this.selectedId = null; | |
|
angelayang
2017/01/12 07:51:24
This is the closure compilation error.
## foun
angelayang
2017/01/13 00:25:11
Done.
| |
| 95 }, | |
| 96 | |
| 79 /** | 97 /** |
| 80 * @param {BookmarkTreeNode} folder | 98 * @param {BookmarkTreeNode} folder |
| 81 * @private | 99 * @private |
| 82 * @return {boolean} | 100 * @return {boolean} |
| 83 */ | 101 */ |
| 84 isAncestorOfSelected_: function(folder) { | 102 isAncestorOfSelected_: function(folder) { |
| 85 return this.selectedNode.path.startsWith(folder.path); | 103 if (!this.selectedId) |
| 104 return false; | |
| 105 | |
| 106 var selectedNode = this.idToNodeMap_[this.selectedId]; | |
| 107 return selectedNode.path.startsWith(folder.path); | |
| 86 }, | 108 }, |
| 87 | 109 |
| 88 /** @private */ | 110 /** @private */ |
| 89 updateSelectedNode_: function() { | 111 updateSearchDisplay_: function() { |
| 112 if (this.searchTerm == '') { | |
| 113 this.fire('selected-folder-changed', this.rootNode.children[0].id); | |
| 114 } else { | |
| 115 chrome.bookmarks.search(this.searchTerm, function(results) { | |
| 116 if (this.selectedId) | |
| 117 this.deselectFolders_(); | |
| 118 | |
| 119 this._setDisplayedList(results); | |
| 120 }.bind(this)); | |
| 121 } | |
| 122 }, | |
| 123 | |
| 124 /** @private */ | |
| 125 updateSelectedDisplay_: function() { | |
| 126 // Don't change to the selected display if ID was cleared. | |
| 127 if (!this.selectedId) | |
| 128 return; | |
| 129 | |
| 90 var selectedNode = this.idToNodeMap_[this.selectedId]; | 130 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 91 this.linkPaths('selectedNode', selectedNode.path); | 131 this.linkPaths('displayedList', selectedNode.path + '.children'); |
| 92 this._setSelectedNode(selectedNode); | 132 this._setDisplayedList(selectedNode.children); |
| 93 }, | 133 }, |
| 94 | 134 |
| 95 /** | 135 /** |
| 96 * Remove all descendants of a given node from the map. | 136 * Remove all descendants of a given node from the map. |
| 97 * @param {string} id | 137 * @param {string} id |
| 98 * @private | 138 * @private |
| 99 */ | 139 */ |
| 100 removeDescendantsFromMap_: function(id) { | 140 removeDescendantsFromMap_: function(id) { |
| 101 var node = this.idToNodeMap_[id]; | 141 var node = this.idToNodeMap_[id]; |
| 102 if (!node) | 142 if (!node) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 115 /** | 155 /** |
| 116 * Callback for when a bookmark node is removed. | 156 * Callback for when a bookmark node is removed. |
| 117 * If a folder is selected or is an ancestor of a selected folder, the parent | 157 * If a folder is selected or is an ancestor of a selected folder, the parent |
| 118 * of the removed folder will be selected. | 158 * of the removed folder will be selected. |
| 119 * @param {string} id The id of the removed bookmark node. | 159 * @param {string} id The id of the removed bookmark node. |
| 120 * @param {!{index: number, | 160 * @param {!{index: number, |
| 121 * parentId: string, | 161 * parentId: string, |
| 122 * node: BookmarkTreeNode}} removeInfo | 162 * node: BookmarkTreeNode}} removeInfo |
| 123 */ | 163 */ |
| 124 onBookmarkRemoved_: function(id, removeInfo) { | 164 onBookmarkRemoved_: function(id, removeInfo) { |
| 125 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) | 165 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) { |
|
tsergeant
2017/01/12 23:44:21
No need to add {} here
angelayang
2017/01/13 00:25:11
Done.
| |
| 126 this.fire('selected-folder-changed', removeInfo.parentId); | 166 this.fire('selected-folder-changed', removeInfo.parentId); |
| 167 } | |
| 127 | 168 |
| 128 var parentNode = this.idToNodeMap_[removeInfo.parentId]; | 169 var parentNode = this.idToNodeMap_[removeInfo.parentId]; |
| 129 this.splice(parentNode.path + '.children', removeInfo.index, 1); | 170 this.splice(parentNode.path + '.children', removeInfo.index, 1); |
| 130 this.removeDescendantsFromMap_(id); | 171 this.removeDescendantsFromMap_(id); |
| 131 BookmarksStore.generatePaths(parentNode, removeInfo.index); | 172 BookmarksStore.generatePaths(parentNode, removeInfo.index); |
| 173 | |
| 174 // Regenerate the search list if its displayed. | |
| 175 if (this.searchTerm) | |
| 176 this.updateSearchDisplay_(); | |
| 132 }, | 177 }, |
| 133 | 178 |
| 134 /** | 179 /** |
| 135 * Called when the title of a bookmark changes. | 180 * Called when the title of a bookmark changes. |
| 136 * @param {string} id The id of changed bookmark node. | 181 * @param {string} id The id of changed bookmark node. |
| 137 * @param {!Object} changeInfo | 182 * @param {!Object} changeInfo |
| 138 */ | 183 */ |
| 139 onBookmarkChanged_: function(id, changeInfo) { | 184 onBookmarkChanged_: function(id, changeInfo) { |
| 140 if (changeInfo.title) | 185 if (changeInfo.title) |
| 141 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); | 186 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); |
| 142 if (changeInfo.url) | 187 if (changeInfo.url) |
| 143 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); | 188 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); |
|
tsergeant
2017/01/12 23:44:20
Should the search list also be refreshed here?
angelayang
2017/01/13 00:25:11
Yes it definitely should :)
| |
| 144 }, | 189 }, |
| 145 | 190 |
| 146 //////////////////////////////////////////////////////////////////////////////// | 191 //////////////////////////////////////////////////////////////////////////////// |
| 147 // bookmarks-store, bookmarks app event listeners: | 192 // bookmarks-store, bookmarks app event listeners: |
| 148 | 193 |
| 149 /** | 194 /** |
| 195 * @param {Event} e | |
| 196 * @private | |
| 197 */ | |
| 198 onSearchTermChanged_: function(e) { | |
| 199 this.searchTerm = /** @type {string} */ (e.detail); | |
| 200 }, | |
| 201 | |
| 202 /** | |
| 150 * Selects the folder specified by the event and deselects the previously | 203 * Selects the folder specified by the event and deselects the previously |
| 151 * selected folder. | 204 * selected folder. |
| 152 * @param {CustomEvent} e | 205 * @param {CustomEvent} e |
| 153 * @private | 206 * @private |
| 154 */ | 207 */ |
| 155 onSelectedFolderChanged_: function(e) { | 208 onSelectedFolderChanged_: function(e) { |
| 209 if (this.searchTerm) | |
| 210 this.searchTerm = ''; | |
| 211 | |
| 156 // Deselect the old folder if defined. | 212 // Deselect the old folder if defined. |
| 157 if (this.selectedId) | 213 if (this.selectedId) |
| 158 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | 214 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 159 | 215 |
| 160 var selectedId = /** @type {string} */ (e.detail); | 216 var selectedId = /** @type {string} */ (e.detail); |
| 161 var newFolder = this.idToNodeMap_[selectedId]; | 217 var newFolder = this.idToNodeMap_[selectedId]; |
| 162 this.set(newFolder.path + '.isSelected', true); | 218 this.set(newFolder.path + '.isSelected', true); |
| 163 this.selectedId = selectedId; | 219 this.selectedId = selectedId; |
| 164 }, | 220 }, |
| 165 | 221 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 263 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| 208 | 264 |
| 209 if (bookmarkNode.url) | 265 if (bookmarkNode.url) |
| 210 return; | 266 return; |
| 211 | 267 |
| 212 bookmarkNode.isSelected = false; | 268 bookmarkNode.isSelected = false; |
| 213 bookmarkNode.isOpen = true; | 269 bookmarkNode.isOpen = true; |
| 214 for (var i = 0; i < bookmarkNode.children.length; i++) | 270 for (var i = 0; i < bookmarkNode.children.length; i++) |
| 215 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 271 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
| 216 }; | 272 }; |
| OLD | NEW |