| 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 /** @type {?string} */ |
| 15 selectedId: { | 16 selectedId: { |
| 16 type: String, | 17 type: String, |
| 17 observer: 'updateSelectedNode_', | 18 observer: 'updateSelectedDisplay_', |
| 18 notify: true, | 19 notify: true, |
| 19 }, | 20 }, |
| 20 | 21 |
| 21 /** @type {BookmarkTreeNode} */ | 22 searchTerm: { |
| 22 selectedNode: { | 23 type: String, |
| 23 type: Object, | 24 observer: 'updateSearchDisplay_', |
| 25 notify: true, |
| 26 }, |
| 27 |
| 28 /** |
| 29 * This updates to either the result of a search or the contents of the |
| 30 * selected folder. |
| 31 * @type {Array<BookmarkTreeNode>} |
| 32 */ |
| 33 displayedList: { |
| 34 type: Array, |
| 24 notify: true, | 35 notify: true, |
| 25 readOnly: true, | 36 readOnly: true, |
| 26 }, | 37 }, |
| 27 | 38 |
| 28 idToNodeMap_: Object, | 39 idToNodeMap_: Object, |
| 29 }, | 40 }, |
| 30 | 41 |
| 31 /** @private {Object} */ | 42 /** @private {Object} */ |
| 32 documentListeners_: null, | 43 documentListeners_: null, |
| 33 | 44 |
| 34 /** @override */ | 45 /** @override */ |
| 35 attached: function() { | 46 attached: function() { |
| 36 this.documentListeners_ = { | 47 this.documentListeners_ = { |
| 37 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), | 48 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), |
| 38 'folder-open-changed': this.onFolderOpenChanged_.bind(this), | 49 'folder-open-changed': this.onFolderOpenChanged_.bind(this), |
| 50 'search-term-changed': this.onSearchTermChanged_.bind(this), |
| 39 }; | 51 }; |
| 40 for (var event in this.documentListeners_) | 52 for (var event in this.documentListeners_) |
| 41 document.addEventListener(event, this.documentListeners_[event]); | 53 document.addEventListener(event, this.documentListeners_[event]); |
| 42 }, | 54 }, |
| 43 | 55 |
| 44 /** @override */ | 56 /** @override */ |
| 45 detached: function() { | 57 detached: function() { |
| 46 for (var event in this.documentListeners_) | 58 for (var event in this.documentListeners_) |
| 47 document.removeEventListener(event, this.documentListeners_[event]); | 59 document.removeEventListener(event, this.documentListeners_[event]); |
| 48 }, | 60 }, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 69 */ | 81 */ |
| 70 setupStore_: function(rootNode) { | 82 setupStore_: function(rootNode) { |
| 71 this.rootNode = rootNode; | 83 this.rootNode = rootNode; |
| 72 this.idToNodeMap_ = {}; | 84 this.idToNodeMap_ = {}; |
| 73 this.rootNode.path = 'rootNode'; | 85 this.rootNode.path = 'rootNode'; |
| 74 BookmarksStore.generatePaths(rootNode, 0); | 86 BookmarksStore.generatePaths(rootNode, 0); |
| 75 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); | 87 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); |
| 76 this.fire('selected-folder-changed', this.rootNode.children[0].id); | 88 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 77 }, | 89 }, |
| 78 | 90 |
| 91 /** @private */ |
| 92 deselectFolders_: function() { |
| 93 this.unlinkPaths('displayedList'); |
| 94 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 95 this.selectedId = null; |
| 96 }, |
| 97 |
| 79 /** | 98 /** |
| 80 * @param {BookmarkTreeNode} folder | 99 * @param {BookmarkTreeNode} folder |
| 81 * @private | 100 * @private |
| 82 * @return {boolean} | 101 * @return {boolean} |
| 83 */ | 102 */ |
| 84 isAncestorOfSelected_: function(folder) { | 103 isAncestorOfSelected_: function(folder) { |
| 85 return this.selectedNode.path.startsWith(folder.path); | 104 if (!this.selectedId) |
| 105 return false; |
| 106 |
| 107 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 108 return selectedNode.path.startsWith(folder.path); |
| 86 }, | 109 }, |
| 87 | 110 |
| 88 /** @private */ | 111 /** @private */ |
| 89 updateSelectedNode_: function() { | 112 updateSearchDisplay_: function() { |
| 113 if (this.searchTerm == '') { |
| 114 this.fire('selected-folder-changed', this.rootNode.children[0].id); |
| 115 } else { |
| 116 chrome.bookmarks.search(this.searchTerm, function(results) { |
| 117 if (this.selectedId) |
| 118 this.deselectFolders_(); |
| 119 |
| 120 this._setDisplayedList(results); |
| 121 }.bind(this)); |
| 122 } |
| 123 }, |
| 124 |
| 125 /** @private */ |
| 126 updateSelectedDisplay_: function() { |
| 127 // Don't change to the selected display if ID was cleared. |
| 128 if (!this.selectedId) |
| 129 return; |
| 130 |
| 90 var selectedNode = this.idToNodeMap_[this.selectedId]; | 131 var selectedNode = this.idToNodeMap_[this.selectedId]; |
| 91 this.linkPaths('selectedNode', selectedNode.path); | 132 this.linkPaths('displayedList', selectedNode.path + '.children'); |
| 92 this._setSelectedNode(selectedNode); | 133 this._setDisplayedList(selectedNode.children); |
| 93 }, | 134 }, |
| 94 | 135 |
| 95 /** | 136 /** |
| 96 * Remove all descendants of a given node from the map. | 137 * Remove all descendants of a given node from the map. |
| 97 * @param {string} id | 138 * @param {string} id |
| 98 * @private | 139 * @private |
| 99 */ | 140 */ |
| 100 removeDescendantsFromMap_: function(id) { | 141 removeDescendantsFromMap_: function(id) { |
| 101 var node = this.idToNodeMap_[id]; | 142 var node = this.idToNodeMap_[id]; |
| 102 if (!node) | 143 if (!node) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 122 * node: BookmarkTreeNode}} removeInfo | 163 * node: BookmarkTreeNode}} removeInfo |
| 123 */ | 164 */ |
| 124 onBookmarkRemoved_: function(id, removeInfo) { | 165 onBookmarkRemoved_: function(id, removeInfo) { |
| 125 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) | 166 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) |
| 126 this.fire('selected-folder-changed', removeInfo.parentId); | 167 this.fire('selected-folder-changed', removeInfo.parentId); |
| 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); |
| 189 |
| 190 if (this.searchTerm) |
| 191 this.updateSearchDisplay_(); |
| 144 }, | 192 }, |
| 145 | 193 |
| 146 //////////////////////////////////////////////////////////////////////////////// | 194 //////////////////////////////////////////////////////////////////////////////// |
| 147 // bookmarks-store, bookmarks app event listeners: | 195 // bookmarks-store, bookmarks app event listeners: |
| 148 | 196 |
| 149 /** | 197 /** |
| 198 * @param {Event} e |
| 199 * @private |
| 200 */ |
| 201 onSearchTermChanged_: function(e) { |
| 202 this.searchTerm = /** @type {string} */ (e.detail); |
| 203 }, |
| 204 |
| 205 /** |
| 150 * Selects the folder specified by the event and deselects the previously | 206 * Selects the folder specified by the event and deselects the previously |
| 151 * selected folder. | 207 * selected folder. |
| 152 * @param {CustomEvent} e | 208 * @param {CustomEvent} e |
| 153 * @private | 209 * @private |
| 154 */ | 210 */ |
| 155 onSelectedFolderChanged_: function(e) { | 211 onSelectedFolderChanged_: function(e) { |
| 212 if (this.searchTerm) |
| 213 this.searchTerm = ''; |
| 214 |
| 156 // Deselect the old folder if defined. | 215 // Deselect the old folder if defined. |
| 157 if (this.selectedId) | 216 if (this.selectedId) |
| 158 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); | 217 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); |
| 159 | 218 |
| 160 var selectedId = /** @type {string} */ (e.detail); | 219 var selectedId = /** @type {string} */ (e.detail); |
| 161 var newFolder = this.idToNodeMap_[selectedId]; | 220 var newFolder = this.idToNodeMap_[selectedId]; |
| 162 this.set(newFolder.path + '.isSelected', true); | 221 this.set(newFolder.path + '.isSelected', true); |
| 163 this.selectedId = selectedId; | 222 this.selectedId = selectedId; |
| 164 }, | 223 }, |
| 165 | 224 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 idToNodeMap[bookmarkNode.id] = bookmarkNode; | 266 idToNodeMap[bookmarkNode.id] = bookmarkNode; |
| 208 | 267 |
| 209 if (bookmarkNode.url) | 268 if (bookmarkNode.url) |
| 210 return; | 269 return; |
| 211 | 270 |
| 212 bookmarkNode.isSelected = false; | 271 bookmarkNode.isSelected = false; |
| 213 bookmarkNode.isOpen = true; | 272 bookmarkNode.isOpen = true; |
| 214 for (var i = 0; i < bookmarkNode.children.length; i++) | 273 for (var i = 0; i < bookmarkNode.children.length; i++) |
| 215 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); | 274 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); |
| 216 }; | 275 }; |
| OLD | NEW |