Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: chrome/browser/resources/md_bookmarks/store.js

Issue 2645273002: [MD Bookmarks] Modify search to retain the previously selected folder. (Closed)
Patch Set: Merge and move selectedId update logic into one function. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 /** @type {string} */
16 selectedId: { 16 selectedId: {
17 type: String, 17 type: String,
18 observer: 'updateSelectedDisplay_',
19 notify: true, 18 notify: true,
20 }, 19 },
21 20
22 searchTerm: { 21 searchTerm: {
23 type: String, 22 type: String,
24 value: '', 23 value: '',
25 observer: 'updateSearchDisplay_', 24 observer: 'updateSearchDisplay_',
26 notify: true, 25 notify: true,
27 }, 26 },
28 27
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 * @private 80 * @private
82 */ 81 */
83 setupStore_: function(rootNode) { 82 setupStore_: function(rootNode) {
84 this.rootNode = rootNode; 83 this.rootNode = rootNode;
85 this.idToNodeMap_ = {}; 84 this.idToNodeMap_ = {};
86 this.rootNode.path = 'rootNode'; 85 this.rootNode.path = 'rootNode';
87 BookmarksStore.generatePaths(rootNode, 0); 86 BookmarksStore.generatePaths(rootNode, 0);
88 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); 87 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_);
89 88
90 // Initialize the store's fields from the router. 89 // Initialize the store's fields from the router.
91 if (this.$.router.searchTerm) 90 this.searchTerm = this.$.router.searchTerm;
92 this.searchTerm = this.$.router.searchTerm; 91 this.updateSelectedId_(this.$.router.selectedId);
93 else
94 this.fire('selected-folder-changed', this.$.router.selectedId);
95 }, 92 },
96 93
97 /** @private */ 94 /** @private */
98 deselectFolders_: function() { 95 deselectFolders_: function() {
96 if (!this.selectedId)
97 return;
98
99 this.unlinkPaths('displayedList'); 99 this.unlinkPaths('displayedList');
100 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); 100 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false);
101 this.selectedId = null;
102 }, 101 },
103 102
104 /** 103 /**
105 * @param {BookmarkTreeNode} folder 104 * @param {BookmarkTreeNode} folder
106 * @private 105 * @private
107 * @return {boolean} 106 * @return {boolean}
108 */ 107 */
109 isAncestorOfSelected_: function(folder) { 108 isAncestorOfSelected_: function(folder) {
110 if (!this.selectedId)
111 return false;
112
113 var selectedNode = this.idToNodeMap_[this.selectedId]; 109 var selectedNode = this.idToNodeMap_[this.selectedId];
114 return selectedNode.path.startsWith(folder.path); 110 return selectedNode.path.startsWith(folder.path);
115 }, 111 },
116 112
117 /** @private */ 113 /** @private */
118 updateSearchDisplay_: function() { 114 updateSearchDisplay_: function() {
119 if (!this.rootNode) 115 if (!this.rootNode)
120 return; 116 return;
121 117
122 if (!this.searchTerm) { 118 if (!this.searchTerm) {
123 this.fire('selected-folder-changed', this.rootNode.children[0].id); 119 this.selectFolder(this.selectedId);
124 } else { 120 } else {
125 chrome.bookmarks.search(this.searchTerm, function(results) { 121 chrome.bookmarks.search(this.searchTerm, function(results) {
126 if (this.selectedId) 122 this.deselectFolders_();
127 this.deselectFolders_();
128
129 this._setDisplayedList(results); 123 this._setDisplayedList(results);
130 }.bind(this)); 124 }.bind(this));
131 } 125 }
132 }, 126 },
133 127
134 /** @private */
135 updateSelectedDisplay_: function() {
136 // Don't change to the selected display if ID was cleared.
137 if (!this.selectedId)
138 return;
139
140 var selectedNode = this.idToNodeMap_[this.selectedId];
141 this.linkPaths('displayedList', selectedNode.path + '.children');
142 this._setDisplayedList(selectedNode.children);
143 },
144
145 /** 128 /**
146 * Remove all descendants of a given node from the map. 129 * Remove all descendants of a given node from the map.
147 * @param {string} id 130 * @param {string} id
148 * @private 131 * @private
149 */ 132 */
150 removeDescendantsFromMap_: function(id) { 133 removeDescendantsFromMap_: function(id) {
151 var node = this.idToNodeMap_[id]; 134 var node = this.idToNodeMap_[id];
152 if (!node) 135 if (!node)
153 return; 136 return;
154 137
155 if (node.children) { 138 if (node.children) {
156 for (var i = 0; i < node.children.length; i++) 139 for (var i = 0; i < node.children.length; i++)
157 this.removeDescendantsFromMap_(node.children[i].id); 140 this.removeDescendantsFromMap_(node.children[i].id);
158 } 141 }
159 delete this.idToNodeMap_[id]; 142 delete this.idToNodeMap_[id];
160 }, 143 },
161 144
145 /**
146 * Update the selectedId according to the state of the store.
147 * @private
148 */
149 updateSelectedId_: function(id) {
tsergeant 2017/01/31 03:21:01 The API here is really unclear. If I want to selec
angelayang 2017/01/31 06:29:25 yeah that works much better and clearer thanks
150 if (!this.idToNodeMap_)
151 return;
152
153 if (!this.idToNodeMap_[id] || this.idToNodeMap_[id].url)
154 id = this.rootNode.children[0].id;
155
156 this.deselectFolders_();
157 if (this.searchTerm)
158 this.selectedId = id;
159 else
160 this.selectFolder(id);
161 },
162
163 /**
164 * Sets |displayedList| to the folder corresponding to the id.
165 * @param {string} id The id of the folder to be displayed.
166 */
167 selectFolder: function(id) {
168 var newFolder = this.idToNodeMap_[id];
169 this.set(newFolder.path + '.isSelected', true);
170 this.selectedId = id;
171
172 // Update the displayed list to the selected folder.
173 var selectedNode = this.idToNodeMap_[this.selectedId];
174 this.linkPaths('displayedList', selectedNode.path + '.children');
175 this._setDisplayedList(selectedNode.children);
176 },
177
162 ////////////////////////////////////////////////////////////////////////////// 178 //////////////////////////////////////////////////////////////////////////////
163 // bookmarks-store, bookmarks API event listeners: 179 // bookmarks-store, bookmarks API event listeners:
164 180
165 /** 181 /**
166 * Callback for when a bookmark node is removed. 182 * Callback for when a bookmark node is removed.
167 * If a folder is selected or is an ancestor of a selected folder, the parent 183 * If a folder is selected or is an ancestor of a selected folder, the parent
168 * of the removed folder will be selected. 184 * of the removed folder will be selected.
169 * @param {string} id The id of the removed bookmark node. 185 * @param {string} id The id of the removed bookmark node.
170 * @param {!{index: number, 186 * @param {!{index: number,
171 * parentId: string, 187 * parentId: string,
172 * node: BookmarkTreeNode}} removeInfo 188 * node: BookmarkTreeNode}} removeInfo
173 */ 189 */
174 onBookmarkRemoved_: function(id, removeInfo) { 190 onBookmarkRemoved_: function(id, removeInfo) {
175 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) 191 if (this.isAncestorOfSelected_(this.idToNodeMap_[id]))
176 this.fire('selected-folder-changed', removeInfo.parentId); 192 this.selectFolder(removeInfo.parentId);
177 193
178 var parentNode = this.idToNodeMap_[removeInfo.parentId]; 194 var parentNode = this.idToNodeMap_[removeInfo.parentId];
179 this.splice(parentNode.path + '.children', removeInfo.index, 1); 195 this.splice(parentNode.path + '.children', removeInfo.index, 1);
180 this.removeDescendantsFromMap_(id); 196 this.removeDescendantsFromMap_(id);
181 BookmarksStore.generatePaths(parentNode, removeInfo.index); 197 BookmarksStore.generatePaths(parentNode, removeInfo.index);
182 198
183 // Regenerate the search list if its displayed. 199 // Regenerate the search list if its displayed.
184 if (this.searchTerm) 200 if (this.searchTerm)
185 this.updateSearchDisplay_(); 201 this.updateSearchDisplay_();
186 }, 202 },
(...skipping 24 matching lines...) Expand all
211 this.searchTerm = /** @type {string} */ (e.detail); 227 this.searchTerm = /** @type {string} */ (e.detail);
212 }, 228 },
213 229
214 /** 230 /**
215 * Selects the folder specified by the event and deselects the previously 231 * Selects the folder specified by the event and deselects the previously
216 * selected folder. 232 * selected folder.
217 * @param {CustomEvent} e 233 * @param {CustomEvent} e
218 * @private 234 * @private
219 */ 235 */
220 onSelectedFolderChanged_: function(e) { 236 onSelectedFolderChanged_: function(e) {
221 if (this.searchTerm)
222 this.searchTerm = '';
223
224 // Deselect the old folder if defined.
225 if (this.selectedId)
226 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false);
227
228 // Check if the selected id is that of a defined folder.
229 var id = /** @type {string} */ (e.detail); 237 var id = /** @type {string} */ (e.detail);
230 if (!this.idToNodeMap_[id] || this.idToNodeMap_[id].url) 238 this.updateSelectedId_(id);
231 id = this.rootNode.children[0].id;
232
233 var newFolder = this.idToNodeMap_[id];
234 this.set(newFolder.path + '.isSelected', true);
235 this.selectedId = id;
236 }, 239 },
237 240
238 /** 241 /**
239 * Handles events that open and close folders. 242 * Handles events that open and close folders.
240 * @param {CustomEvent} e 243 * @param {CustomEvent} e
241 * @private 244 * @private
242 */ 245 */
243 onFolderOpenChanged_: function(e) { 246 onFolderOpenChanged_: function(e) {
244 var folder = this.idToNodeMap_[e.detail.id]; 247 var folder = this.idToNodeMap_[e.detail.id];
245 this.set(folder.path + '.isOpen', e.detail.open); 248 this.set(folder.path + '.isOpen', e.detail.open);
246 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) 249 if (!folder.isOpen && this.isAncestorOfSelected_(folder))
247 this.fire('selected-folder-changed', folder.id); 250 this.updateSelectedId_(folder.id);
248 }, 251 },
249 }); 252 });
250 253
251 //////////////////////////////////////////////////////////////////////////////// 254 ////////////////////////////////////////////////////////////////////////////////
252 // bookmarks-store, static methods: 255 // bookmarks-store, static methods:
253 256
254 /** 257 /**
255 * Stores the path from the store to a node inside the node. 258 * Stores the path from the store to a node inside the node.
256 * @param {BookmarkTreeNode} bookmarkNode 259 * @param {BookmarkTreeNode} bookmarkNode
257 * @param {number} startIndex 260 * @param {number} startIndex
(...skipping 21 matching lines...) Expand all
279 idToNodeMap[bookmarkNode.id] = bookmarkNode; 282 idToNodeMap[bookmarkNode.id] = bookmarkNode;
280 283
281 if (bookmarkNode.url) 284 if (bookmarkNode.url)
282 return; 285 return;
283 286
284 bookmarkNode.isSelected = false; 287 bookmarkNode.isSelected = false;
285 bookmarkNode.isOpen = true; 288 bookmarkNode.isOpen = true;
286 for (var i = 0; i < bookmarkNode.children.length; i++) 289 for (var i = 0; i < bookmarkNode.children.length; i++)
287 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); 290 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap);
288 }; 291 };
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/router.js ('k') | chrome/test/data/webui/md_bookmarks/store_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698