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: { |
| 16 type: String, | 16 type: String, |
| 17 observer: 'updateSelectedNode_', | 17 observer: 'updateSelectedDisplay_', |
| 18 notify: true, | |
| 19 }, | |
| 20 | |
| 21 searchTerm: { | |
| 22 type: String, | |
| 23 observer: 'updateSearchDisplay_', | |
| 18 notify: true, | 24 notify: true, |
| 19 }, | 25 }, |
| 20 | 26 |
| 21 /** @type {BookmarkTreeNode} */ | 27 /** @type {BookmarkTreeNode} */ |
|
tsergeant
2017/01/12 06:45:06
Might be good to leave a comment here explaining w
angelayang
2017/01/12 07:51:24
Done.
| |
| 22 selectedNode: { | 28 displayedList: { |
| 23 type: Object, | 29 type: Array, |
| 24 notify: true, | 30 notify: true, |
| 25 readOnly: true, | 31 readOnly: true, |
| 26 }, | 32 }, |
| 27 | 33 |
| 28 idToNodeMap_: Object, | 34 idToNodeMap_: Object, |
| 29 }, | 35 }, |
| 30 | 36 |
| 31 /** @private {Object} */ | 37 /** @private {Object} */ |
| 32 documentListeners_: null, | 38 documentListeners_: null, |
| 33 | 39 |
| 34 /** @override */ | 40 /** @override */ |
| 35 attached: function() { | 41 attached: function() { |
| 36 this.documentListeners_ = { | 42 this.documentListeners_ = { |
| 37 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | 43 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
| 38 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | 44 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
| 45 'search-term-changed': this.onSearchTermChanged_.bind(this), | |
| 39 }; | 46 }; |
| 40 for (var event in this.documentListeners_) | 47 for (var event in this.documentListeners_) |
| 41 document.addEventListener(event, this.documentListeners_[event]); | 48 document.addEventListener(event, this.documentListeners_[event]); |
| 42 }, | 49 }, |
| 43 | 50 |
| 44 /** @override */ | 51 /** @override */ |
| 45 detached: function() { | 52 detached: function() { |
| 46 for (var event in this.documentListeners_) | 53 for (var event in this.documentListeners_) |
| 47 document.removeEventListener(event, this.documentListeners_[event]); | 54 document.removeEventListener(event, this.documentListeners_[event]); |
| 48 }, | 55 }, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 69 */ | 76 */ |
| 70 setupStore_: function(rootNode) { | 77 setupStore_: function(rootNode) { |
| 71 this.rootNode = rootNode; | 78 this.rootNode = rootNode; |
| 72 this.idToNodeMap_ = {}; | 79 this.idToNodeMap_ = {}; |
| 73 this.rootNode.path = 'rootNode'; | 80 this.rootNode.path = 'rootNode'; |
| 74 BookmarksStore.generatePaths(rootNode, 0); | 81 BookmarksStore.generatePaths(rootNode, 0); |
| 75 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); | 82 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
| 76 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 83 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 77 }, | 84 }, |
| 78 | 85 |
| 86 /** @private */ | |
| 87 deselectFolders_: function() { | |
|
tsergeant
2017/01/12 06:45:06
You should probably do an unlinkPaths that's the e
angelayang
2017/01/12 07:51:24
Done.
| |
| 88 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | |
| 89 this.selectedId = ''; | |
|
angelayang
2017/01/12 06:08:06
Closure doesn't like it when i set this to null, s
tsergeant
2017/01/12 06:45:06
What specifically doesn't it like? I think using n
angelayang
2017/01/12 07:51:24
Closure says it should be of type string. So i'll
| |
| 90 }, | |
| 91 | |
| 79 /** | 92 /** |
| 80 * @param {BookmarkTreeNode} folder | 93 * @param {BookmarkTreeNode} folder |
| 81 * @private | 94 * @private |
| 82 * @return {boolean} | 95 * @return {boolean} |
| 83 */ | 96 */ |
| 84 isAncestorOfSelected_: function(folder) { | 97 isAncestorOfSelected_: function(folder) { |
| 85 return this.selectedNode.path.startsWith(folder.path); | 98 if (!this.selectedId) |
| 99 return false; | |
| 100 | |
| 101 var selectedNode = this.idToNodeMap_[this.selectedId]; | |
| 102 return selectedNode.path.startsWith(folder.path); | |
| 86 }, | 103 }, |
| 87 | 104 |
| 88 /** @private */ | 105 /** @private */ |
| 89 updateSelectedNode_: function() { | 106 updateSearchDisplay_: function() { |
| 107 if (this.searchTerm == '') { | |
| 108 this.fire('selected-folder-changed', this.rootNode.children[0].id); | |
| 109 } else { | |
| 110 chrome.bookmarks.search(this.searchTerm, function(results) { | |
| 111 if (this.selectedId) | |
| 112 this.deselectFolders_(); | |
| 113 | |
| 114 this._setDisplayedList(results); | |
| 115 }.bind(this)); | |
| 116 } | |
| 117 }, | |
| 118 | |
| 119 /** @private */ | |
| 120 updateSelectedDisplay_: function() { | |
| 121 // Don't do change to selected display if ID was cleared. | |
| 122 if (!this.selectedId) | |
| 123 return; | |
| 124 | |
| 90 var selectedNode = this.idToNodeMap_[this.selectedId]; | 125 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 91 this.linkPaths('selectedNode', selectedNode.path); | 126 this.linkPaths('displayedList', selectedNode.path + '.children'); |
| 92 this._setSelectedNode(selectedNode); | 127 this._setDisplayedList(selectedNode.children); |
| 93 }, | 128 }, |
| 94 | 129 |
| 95 /** | 130 /** |
| 96 * Remove all descendants of a given node from the map. | 131 * Remove all descendants of a given node from the map. |
| 97 * @param {string} id | 132 * @param {string} id |
| 98 * @private | 133 * @private |
| 99 */ | 134 */ |
| 100 removeDescendantsFromMap_: function(id) { | 135 removeDescendantsFromMap_: function(id) { |
| 101 var node = this.idToNodeMap_[id]; | 136 var node = this.idToNodeMap_[id]; |
| 102 if (!node) | 137 if (!node) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 115 /** | 150 /** |
| 116 * Callback for when a bookmark node is removed. | 151 * Callback for when a bookmark node is removed. |
| 117 * If a folder is selected or is an ancestor of a selected folder, the parent | 152 * If a folder is selected or is an ancestor of a selected folder, the parent |
| 118 * of the removed folder will be selected. | 153 * of the removed folder will be selected. |
| 119 * @param {string} id The id of the removed bookmark node. | 154 * @param {string} id The id of the removed bookmark node. |
| 120 * @param {!{index: number, | 155 * @param {!{index: number, |
| 121 * parentId: string, | 156 * parentId: string, |
| 122 * node: BookmarkTreeNode}} removeInfo | 157 * node: BookmarkTreeNode}} removeInfo |
| 123 */ | 158 */ |
| 124 onBookmarkRemoved_: function(id, removeInfo) { | 159 onBookmarkRemoved_: function(id, removeInfo) { |
| 125 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) | 160 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) { |
| 161 // TODO (angelayang): does this ever happen? | |
|
angelayang
2017/01/12 06:08:06
Sorry i'll delete this comment. Just checking whet
tsergeant
2017/01/12 06:45:06
Try:
- Create 'folder'
- Create 'subfolder'
- Sele
angelayang
2017/01/12 07:51:24
Cool thanks! I'll definitely keep that in mind! I
| |
| 126 this.fire('selected-folder-changed', removeInfo.parentId); | 162 this.fire('selected-folder-changed', removeInfo.parentId); |
| 163 } | |
| 127 | 164 |
| 128 var parentNode = this.idToNodeMap_[removeInfo.parentId]; | 165 var parentNode = this.idToNodeMap_[removeInfo.parentId]; |
| 129 this.splice(parentNode.path + '.children', removeInfo.index, 1); | 166 this.splice(parentNode.path + '.children', removeInfo.index, 1); |
| 130 this.removeDescendantsFromMap_(id); | 167 this.removeDescendantsFromMap_(id); |
| 131 BookmarksStore.generatePaths(parentNode, removeInfo.index); | 168 BookmarksStore.generatePaths(parentNode, removeInfo.index); |
| 169 | |
| 170 // Regenerate the search list if its displayed. | |
| 171 if (this.searchTerm) | |
| 172 this.updateSearchDisplay_(); | |
| 132 }, | 173 }, |
| 133 | 174 |
| 134 /** | 175 /** |
| 176 * Initializes the nodes in the bookmarks tree as follows: | |
| 177 * - Populates |idToNodeMap_| with a mapping of all node ids to their | |
| 178 * corresponding BookmarkTreeNode. | |
| 179 * - Sets all the nodes to not selected and open by default. | |
| 180 * @param {BookmarkTreeNode} bookmarkNode | |
| 181 * @private | |
| 182 */ | |
| 183 initNodes_: function(bookmarkNode) { | |
|
tsergeant
2017/01/12 06:45:06
This function got moved by Charlotte: It now exist
angelayang
2017/01/12 07:51:24
oh thanks i didn't spot that
| |
| 184 this.idToNodeMap_[bookmarkNode.id] = bookmarkNode; | |
| 185 if (bookmarkNode.url) | |
| 186 return; | |
| 187 | |
| 188 bookmarkNode.isSelected = false; | |
| 189 bookmarkNode.isOpen = true; | |
| 190 for (var i = 0; i < bookmarkNode.children.length; i++) { | |
| 191 this.initNodes_(bookmarkNode.children[i]); | |
| 192 } | |
| 193 }, | |
| 194 | |
| 195 /** | |
| 196 * @param {Event} e | |
| 197 * @private | |
| 198 */ | |
| 199 onSearchTermChanged_: function(e) { | |
|
tsergeant
2017/01/12 06:45:06
Also, Charlotte's patch added sections to store.js
angelayang
2017/01/12 07:51:24
Done.
| |
| 200 this.searchTerm = /** @type {string} */ (e.detail); | |
| 201 }, | |
| 202 | |
| 203 /** | |
| 135 * Called when the title of a bookmark changes. | 204 * Called when the title of a bookmark changes. |
| 136 * @param {string} id The id of changed bookmark node. | 205 * @param {string} id The id of changed bookmark node. |
| 137 * @param {!Object} changeInfo | 206 * @param {!Object} changeInfo |
| 138 */ | 207 */ |
| 139 onBookmarkChanged_: function(id, changeInfo) { | 208 onBookmarkChanged_: function(id, changeInfo) { |
| 140 if (changeInfo.title) | 209 if (changeInfo.title) |
| 141 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); | 210 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); |
| 142 if (changeInfo.url) | 211 if (changeInfo.url) |
| 143 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); | 212 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); |
| 144 }, | 213 }, |
| 145 | 214 |
| 146 //////////////////////////////////////////////////////////////////////////////// | 215 //////////////////////////////////////////////////////////////////////////////// |
| 147 // bookmarks-store, bookmarks app event listeners: | 216 // bookmarks-store, bookmarks app event listeners: |
| 148 | 217 |
| 149 /** | 218 /** |
| 150 * Selects the folder specified by the event and deselects the previously | 219 * Selects the folder specified by the event and deselects the previously |
| 151 * selected folder. | 220 * selected folder. |
| 152 * @param {CustomEvent} e | 221 * @param {CustomEvent} e |
| 153 * @private | 222 * @private |
| 154 */ | 223 */ |
| 155 onSelectedFolderChanged_: function(e) { | 224 onSelectedFolderChanged_: function(e) { |
| 225 if (this.searchTerm) | |
| 226 this.searchTerm = ''; | |
| 227 | |
| 156 // Deselect the old folder if defined. | 228 // Deselect the old folder if defined. |
| 157 if (this.selectedId) | 229 if (this.selectedId) |
| 158 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | 230 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 159 | 231 |
| 160 var selectedId = /** @type {string} */ (e.detail); | 232 var selectedId = /** @type {string} */ (e.detail); |
| 161 var newFolder = this.idToNodeMap_[selectedId]; | 233 var newFolder = this.idToNodeMap_[selectedId]; |
| 162 this.set(newFolder.path + '.isSelected', true); | 234 this.set(newFolder.path + '.isSelected', true); |
| 163 this.selectedId = selectedId; | 235 this.selectedId = selectedId; |
| 164 }, | 236 }, |
| 165 | 237 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 279 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| 208 | 280 |
| 209 if (bookmarkNode.url) | 281 if (bookmarkNode.url) |
| 210 return; | 282 return; |
| 211 | 283 |
| 212 bookmarkNode.isSelected = false; | 284 bookmarkNode.isSelected = false; |
| 213 bookmarkNode.isOpen = true; | 285 bookmarkNode.isOpen = true; |
| 214 for (var i = 0; i < bookmarkNode.children.length; i++) | 286 for (var i = 0; i < bookmarkNode.children.length; i++) |
| 215 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 287 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
| 216 }; | 288 }; |
| OLD | NEW |