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

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

Issue 2639453002: [MD Bookmarks] Add Select for Bookmarks. (Closed)
Patch Set: clean up 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: {
(...skipping 19 matching lines...) Expand all
30 * This updates to either the result of a search or the contents of the 30 * This updates to either the result of a search or the contents of the
31 * selected folder. 31 * selected folder.
32 * @type {Array<BookmarkTreeNode>} 32 * @type {Array<BookmarkTreeNode>}
33 */ 33 */
34 displayedList: { 34 displayedList: {
35 type: Array, 35 type: Array,
36 notify: true, 36 notify: true,
37 readOnly: true, 37 readOnly: true,
38 }, 38 },
39 39
40 /** @type {Object<string, !BookmarkTreeNode>} */
40 idToNodeMap_: Object, 41 idToNodeMap_: Object,
42
43 anchorIndex_: Number,
44
45 searchResultSet_: Object,
41 }, 46 },
42 47
43 /** @private {Object} */ 48 /** @private {Object} */
44 documentListeners_: null, 49 documentListeners_: null,
45 50
46 /** @override */ 51 /** @override */
47 attached: function() { 52 attached: function() {
48 this.documentListeners_ = { 53 this.documentListeners_ = {
49 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this),
50 'folder-open-changed': this.onFolderOpenChanged_.bind(this), 54 'folder-open-changed': this.onFolderOpenChanged_.bind(this),
51 'search-term-changed': this.onSearchTermChanged_.bind(this), 55 'search-term-changed': this.onSearchTermChanged_.bind(this),
56 'select-item': this.onItemSelected_.bind(this),
57 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this),
52 }; 58 };
53 for (var event in this.documentListeners_) 59 for (var event in this.documentListeners_)
54 document.addEventListener(event, this.documentListeners_[event]); 60 document.addEventListener(event, this.documentListeners_[event]);
55 }, 61 },
56 62
57 /** @override */ 63 /** @override */
58 detached: function() { 64 detached: function() {
59 for (var event in this.documentListeners_) 65 for (var event in this.documentListeners_)
60 document.removeEventListener(event, this.documentListeners_[event]); 66 document.removeEventListener(event, this.documentListeners_[event]);
61 }, 67 },
62 68
63 /** 69 /**
64 * Initializes the store with data from the bookmarks API. 70 * Initializes the store with data from the bookmarks API.
65 * Called by app on attached. 71 * Called by app on attached.
66 */ 72 */
67 initializeStore: function() { 73 initializeStore: function() {
68 chrome.bookmarks.getTree(function(results) { 74 chrome.bookmarks.getTree(function(results) {
69 this.setupStore_(results[0]); 75 this.setupStore_(results[0]);
70 }.bind(this)); 76 }.bind(this));
71 // Attach bookmarks API listeners. 77 // Attach bookmarks API listeners.
72 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); 78 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this));
73 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); 79 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this));
74 }, 80 },
75 81
76 ////////////////////////////////////////////////////////////////////////////// 82 ////////////////////////////////////////////////////////////////////////////////
77 // bookmarks-store, private: 83 // bookmarks-store, private:
78 84
79 /** 85 /**
80 * @param {BookmarkTreeNode} rootNode 86 * @param {BookmarkTreeNode} rootNode
81 * @private 87 * @private
82 */ 88 */
83 setupStore_: function(rootNode) { 89 setupStore_: function(rootNode) {
84 this.rootNode = rootNode; 90 this.rootNode = rootNode;
85 this.idToNodeMap_ = {}; 91 this.idToNodeMap_ = {};
86 this.rootNode.path = 'rootNode'; 92 this.rootNode.path = 'rootNode';
87 BookmarksStore.generatePaths(rootNode, 0); 93 BookmarksStore.generatePaths(rootNode, 0);
88 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_); 94 BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_);
89 95
90 // Initialize the store's fields from the router. 96 // Initialize the store's fields from the router.
91 if (this.$.router.searchTerm) 97 if (this.$.router.searchTerm)
92 this.searchTerm = this.$.router.searchTerm; 98 this.searchTerm = this.$.router.searchTerm;
93 else 99 else
94 this.fire('selected-folder-changed', this.$.router.selectedId); 100 this.fire('selected-folder-changed', this.$.router.selectedId);
95 }, 101 },
96 102
97 /** @private */ 103 /** @private */
98 deselectFolders_: function() { 104 deselectFolders_: function() {
99 this.unlinkPaths('displayedList'); 105 this.unlinkPaths('displayedList');
100 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); 106 this.set(
107 this.idToNodeMap_[this.selectedId].path + '.isSelectedFolder', false);
101 this.selectedId = null; 108 this.selectedId = null;
102 }, 109 },
103 110
104 /** 111 /**
105 * @param {BookmarkTreeNode} folder 112 * @param {BookmarkTreeNode} folder
106 * @private 113 * @private
107 * @return {boolean} 114 * @return {boolean}
108 */ 115 */
109 isAncestorOfSelected_: function(folder) { 116 isAncestorOfSelected_: function(folder) {
110 if (!this.selectedId) 117 if (!this.selectedId)
111 return false; 118 return false;
112 119
113 var selectedNode = this.idToNodeMap_[this.selectedId]; 120 var selectedNode = this.idToNodeMap_[this.selectedId];
114 return selectedNode.path.startsWith(folder.path); 121 return selectedNode.path.startsWith(folder.path);
115 }, 122 },
116 123
117 /** @private */ 124 /**
118 updateSearchDisplay_: function() { 125 * When being called as an observer, |searchObserver| is the search term. When
126 * being called elsewhere, |searchObserver| is undefined. This helps the
127 * function with doing observer specific actions and can be reused in other
128 * functions to update |displayedList|.
129 * @param {?string} searchObserver
130 * @private
131 */
132 updateSearchDisplay_: function(searchObserver) {
119 if (!this.rootNode) 133 if (!this.rootNode)
120 return; 134 return;
121 135
122 if (!this.searchTerm) { 136 if (!this.searchTerm) {
123 this.fire('selected-folder-changed', this.rootNode.children[0].id); 137 this.fire('selected-folder-changed', this.rootNode.children[0].id);
124 } else { 138 } else {
125 chrome.bookmarks.search(this.searchTerm, function(results) { 139 chrome.bookmarks.search(this.searchTerm, function(results) {
140 if (searchObserver)
141 this.anchorIndex_ = undefined;
142 this.clearSelectedItems_();
143 this.searchResultSet_ = new Set();
144
126 if (this.selectedId) 145 if (this.selectedId)
127 this.deselectFolders_(); 146 this.deselectFolders_();
128 147
129 this._setDisplayedList(results); 148 this.setupSearchResults_(results);
149
150 this.set(
151 'displayedList.#' + this.anchorIndex_ + '.isSelectedItem', true);
130 }.bind(this)); 152 }.bind(this));
131 } 153 }
132 }, 154 },
133 155
134 /** @private */ 156 /** @private */
135 updateSelectedDisplay_: function() { 157 updateSelectedDisplay_: function() {
136 // Don't change to the selected display if ID was cleared. 158 // Don't change to the selected display if ID was cleared.
137 if (!this.selectedId) 159 if (!this.selectedId)
138 return; 160 return;
139 161
162 this.clearSelectedItems_();
163 this.anchorIndex_ = undefined;
164
140 var selectedNode = this.idToNodeMap_[this.selectedId]; 165 var selectedNode = this.idToNodeMap_[this.selectedId];
141 this.linkPaths('displayedList', selectedNode.path + '.children'); 166 this.linkPaths('displayedList', selectedNode.path + '.children');
142 this._setDisplayedList(selectedNode.children); 167 this._setDisplayedList(selectedNode.children);
143 }, 168 },
144 169
145 /** 170 /**
146 * Remove all descendants of a given node from the map. 171 * Remove all descendants of a given node from the map.
147 * @param {string} id 172 * @param {string} id
148 * @private 173 * @private
149 */ 174 */
150 removeDescendantsFromMap_: function(id) { 175 removeDescendantsFromMap_: function(id) {
151 var node = this.idToNodeMap_[id]; 176 var node = this.idToNodeMap_[id];
152 if (!node) 177 if (!node)
153 return; 178 return;
154 179
155 if (node.children) { 180 if (node.children) {
156 for (var i = 0; i < node.children.length; i++) 181 for (var i = 0; i < node.children.length; i++)
157 this.removeDescendantsFromMap_(node.children[i].id); 182 this.removeDescendantsFromMap_(node.children[i].id);
158 } 183 }
159 delete this.idToNodeMap_[id]; 184 delete this.idToNodeMap_[id];
160 }, 185 },
161 186
162 ////////////////////////////////////////////////////////////////////////////// 187 /**
163 // bookmarks-store, bookmarks API event listeners: 188 * Remove all selected items in the list.
189 * @private
190 */
191 clearSelectedItems_: function() {
192 if (!this.displayedList)
193 return;
194
195 for (var i = 0; i < this.displayedList.length; i++) {
196 if (!this.displayedList[i].isSelectedItem)
197 continue;
198
199 this.set('displayedList.#' + i + '.isSelectedItem', false);
200 }
201 },
202
203 /**
204 * Return the index in the search result of an item.
205 * @param {BookmarkTreeNode} item
206 * @return {number}
207 * @private
208 */
209 getIndexInList_: function(item) {
210 return this.searchTerm ? item.searchResultIndex : item.index;
211 },
212
213 /**
214 * @param {BookmarkTreeNode} item
215 * @return {boolean}
216 * @private
217 */
218 isInDisplayedList_: function(id) {
219 return this.searchTerm ? this.searchResultSet_.has(id) :
220 this.idToNodeMap_[id].parentId == this.selectedId;
221 },
222
223 /**
224 * Initializes the search results returned by the API as follows:
225 * - Populates |searchResultSet_| with a mapping of all result ids to
226 * their corresponding result.
227 * - Sets up the |searchResultIndex|.
228 * @param {Array<BookmarkTreeNode>} item
229 * @private
230 */
231 setupSearchResults_: function(results) {
232 for (var i = 0; i < results.length; i++) {
233 results[i].searchResultIndex = i;
234 results[i].isSelectedItem = false;
235 this.searchResultSet_.add(results[i].id);
236 }
237
238 this._setDisplayedList(results);
239 },
240
241 /**
242 * Select multiple items based on |anchorIndex_| and the selected
243 * item. If |anchorIndex_| is not set, single select the item.
244 * @param {BookmarkTreeNode} item
245 * @private
246 */
247 selectRange_: function(item) {
248 var startIndex, endIndex;
249 if (this.anchorIndex_ == undefined) {
250 this.anchorIndex_ = this.getIndexInList_(item);
251 startIndex = this.anchorIndex_;
252 endIndex = this.anchorIndex_;
253 } else {
254 var selectedIndex = this.getIndexInList_(item);
255 startIndex = Math.min(this.anchorIndex_, selectedIndex);
256 endIndex = Math.max(this.anchorIndex_, selectedIndex);
257 }
258 for (var i = startIndex; i <= endIndex; i++)
259 this.set('displayedList.#' + i + '.isSelectedItem', true);
260 },
261
262 /**
263 * Selects a single item in the displayedList.
264 * @param {BookmarkTreeNode} item
265 * @private
266 */
267 selectItem_: function(item) {
268 this.anchorIndex_ = this.getIndexInList_(item);
269 this.set('displayedList.#' + this.anchorIndex_ + '.isSelectedItem', true);
270 },
271
272 ////////////////////////////////////////////////////////////////////////////////
273 // bookmarks-store, bookmarks API event listeners:
164 274
165 /** 275 /**
166 * Callback for when a bookmark node is removed. 276 * Callback for when a bookmark node is removed.
167 * If a folder is selected or is an ancestor of a selected folder, the parent 277 * If a folder is selected or is an ancestor of a selected folder, the parent
168 * of the removed folder will be selected. 278 * of the removed folder will be selected.
169 * @param {string} id The id of the removed bookmark node. 279 * @param {string} id The id of the removed bookmark node.
170 * @param {!{index: number, 280 * @param {!{index: number,
171 * parentId: string, 281 * parentId: string,
172 * node: BookmarkTreeNode}} removeInfo 282 * node: BookmarkTreeNode}} removeInfo
173 */ 283 */
174 onBookmarkRemoved_: function(id, removeInfo) { 284 onBookmarkRemoved_: function(id, removeInfo) {
175 if (this.isAncestorOfSelected_(this.idToNodeMap_[id])) { 285 chrome.bookmarks.getSubTree(removeInfo.parentId, function(parentNodes) {
176 this.fire('selected-folder-changed', removeInfo.parentId); 286 var parentNode = parentNodes[0];
177 } 287 var isAncestor = this.isAncestorOfSelected_(this.idToNodeMap_[id]);
288 var isInDisplayedList = this.isInDisplayedList_(id);
calamity 2017/01/31 05:05:15 Maybe call this 'wasInDisplayedList' to indicate t
jiaxi 2017/02/01 03:19:25 Done.
tsergeant 2017/02/02 00:16:02 It makes sense to call the var `wasInDisplayedList
jiaxi 2017/02/02 04:11:55 Done.
178 289
179 var parentNode = this.idToNodeMap_[removeInfo.parentId]; 290 // Polymer doesn't update the indexes after a deletion. Manually updates
180 this.splice(parentNode.path + '.children', removeInfo.index, 1); 291 // the indexes.
calamity 2017/01/31 05:05:15 What does indexes mean? How about 'Refresh the par
jiaxi 2017/02/01 03:19:25 Done.
181 this.removeDescendantsFromMap_(id); 292 this.removeDescendantsFromMap_(id);
182 BookmarksStore.generatePaths(parentNode, removeInfo.index); 293 parentNode.path = this.idToNodeMap_[parentNode.id].path;
294 BookmarksStore.generatePaths(parentNode, 0);
295 BookmarksStore.initNodes(parentNode, this.idToNodeMap_);
296 this.set(parentNode.path, parentNode);
183 297
184 // Regenerate the search list if its displayed. 298 // Updates selectedId if the removed node is an ancestor of the current
185 if (this.searchTerm) 299 // selected node.
186 this.updateSearchDisplay_(); 300 if (isAncestor)
301 this.fire('selected-folder-changed', removeInfo.parentId);
302
303 // Only update the displayedList if the removed node is in the
304 // displayedList.
305 if (!isInDisplayedList)
306 return;
307
308 if (this.anchorIndex_ == this.displayedList.length - 1)
309 this.anchorIndex_--;
calamity 2017/01/31 05:05:15 I'm pretty happy for everything to just be deselec
jiaxi 2017/02/01 03:19:25 Done.
310
311 if (this.searchTerm) {
calamity 2017/01/31 05:05:15 Comment above here: // Update the currently displa
jiaxi 2017/02/01 03:19:25 Done.
312 this.updateSearchDisplay_();
313 } else {
314 if (!isAncestor)
315 this.fire('selected-folder-changed', this.selectedId);
316
317 this._setDisplayedList(parentNode.children);
318
319 this.set(
320 'displayedList.#' + this.anchorIndex_ + '.isSelectedItem', true);
321 }
322 }.bind(this));
187 }, 323 },
188 324
189 /** 325 /**
190 * Called when the title of a bookmark changes. 326 * Called when the title of a bookmark changes.
191 * @param {string} id The id of changed bookmark node. 327 * @param {string} id The id of changed bookmark node.
192 * @param {!Object} changeInfo 328 * @param {!Object} changeInfo
193 */ 329 */
194 onBookmarkChanged_: function(id, changeInfo) { 330 onBookmarkChanged_: function(id, changeInfo) {
195 if (changeInfo.title) 331 if (changeInfo.title)
196 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); 332 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title);
197 if (changeInfo.url) 333 if (changeInfo.url)
198 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); 334 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url);
199 335
200 if (this.searchTerm) 336 if (this.searchTerm)
201 this.updateSearchDisplay_(); 337 this.updateSearchDisplay_();
202 }, 338 },
203 339
204 ////////////////////////////////////////////////////////////////////////////// 340 ////////////////////////////////////////////////////////////////////////////////
205 // bookmarks-store, bookmarks app event listeners: 341 // bookmarks-store, bookmarks app event listeners:
206 342
207 /** 343 /**
208 * @param {Event} e 344 * @param {Event} e
209 * @private 345 * @private
210 */ 346 */
211 onSearchTermChanged_: function(e) { 347 onSearchTermChanged_: function(e) {
212 this.searchTerm = /** @type {string} */ (e.detail); 348 this.searchTerm = /** @type {string} */ (e.detail);
213 }, 349 },
214 350
215 /** 351 /**
216 * Selects the folder specified by the event and deselects the previously 352 * Selects the folder specified by the event and deselects the previously
217 * selected folder. 353 * selected folder.
218 * @param {CustomEvent} e 354 * @param {CustomEvent} e
219 * @private 355 * @private
220 */ 356 */
221 onSelectedFolderChanged_: function(e) { 357 onSelectedFolderChanged_: function(e) {
222 if (this.searchTerm) 358 if (this.searchTerm)
223 this.searchTerm = ''; 359 this.searchTerm = '';
224 360
225 // Deselect the old folder if defined. 361 // Deselect the old folder if defined.
226 if (this.selectedId) 362 if (this.selectedId && this.idToNodeMap_[this.selectedId])
227 this.set(this.idToNodeMap_[this.selectedId].path + '.isSelected', false); 363 this.set(
364 this.idToNodeMap_[this.selectedId].path + '.isSelectedFolder', false);
228 365
229 // Check if the selected id is that of a defined folder. 366 // Check if the selected id is that of a defined folder.
230 var id = /** @type {string} */ (e.detail); 367 var id = /** @type {string} */ (e.detail);
231 if (!this.idToNodeMap_[id] || this.idToNodeMap_[id].url) 368 if (!this.idToNodeMap_[id] || this.idToNodeMap_[id].url)
232 id = this.rootNode.children[0].id; 369 id = this.rootNode.children[0].id;
233 370
234 var newFolder = this.idToNodeMap_[id]; 371 var newFolder = this.idToNodeMap_[id];
235 this.set(newFolder.path + '.isSelected', true); 372 this.set(newFolder.path + '.isSelectedFolder', true);
236 this.selectedId = id; 373 this.selectedId = id;
237 }, 374 },
238 375
239 /** 376 /**
240 * Handles events that open and close folders. 377 * Handles events that open and close folders.
241 * @param {CustomEvent} e 378 * @param {CustomEvent} e
242 * @private 379 * @private
243 */ 380 */
244 onFolderOpenChanged_: function(e) { 381 onFolderOpenChanged_: function(e) {
245 var folder = this.idToNodeMap_[e.detail.id]; 382 var folder = this.idToNodeMap_[e.detail.id];
246 this.set(folder.path + '.isOpen', e.detail.open); 383 this.set(folder.path + '.isOpen', e.detail.open);
247 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) 384 if (!folder.isOpen && this.isAncestorOfSelected_(folder))
248 this.fire('selected-folder-changed', folder.id); 385 this.fire('selected-folder-changed', folder.id);
249 }, 386 },
387
388 /**
389 * Selects items according to keyboard behaviours.
390 * @param {CustomEvent} e
391 * @private
392 */
393 onItemSelected_: function(e) {
394 if (!e.detail.add)
395 this.clearSelectedItems_();
396
397 if (e.detail.range)
398 this.selectRange_(e.detail.item);
399 else
400 this.selectItem_(e.detail.item);
401 },
250 }); 402 });
251 403
252 //////////////////////////////////////////////////////////////////////////////// 404 ////////////////////////////////////////////////////////////////////////////////
253 // bookmarks-store, static methods: 405 // bookmarks-store, static methods:
254 406
255 /** 407 /**
256 * Stores the path from the store to a node inside the node. 408 * Stores the path from the store to a node inside the node.
257 * @param {BookmarkTreeNode} bookmarkNode 409 * @param {BookmarkTreeNode} bookmarkNode
258 * @param {number} startIndex 410 * @param {number} startIndex
259 */ 411 */
260 BookmarksStore.generatePaths = function(bookmarkNode, startIndex) { 412 BookmarksStore.generatePaths = function(bookmarkNode, startIndex) {
261 if (!bookmarkNode.children) 413 if (!bookmarkNode.children)
262 return; 414 return;
263 415
264 for (var i = startIndex; i < bookmarkNode.children.length; i++) { 416 for (var i = startIndex; i < bookmarkNode.children.length; i++) {
265 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i; 417 bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i;
266 BookmarksStore.generatePaths(bookmarkNode.children[i], 0); 418 BookmarksStore.generatePaths(bookmarkNode.children[i], 0);
267 } 419 }
268 }; 420 };
269 421
270 /** 422 /**
271 * Initializes the nodes in the bookmarks tree as follows: 423 * Initializes the nodes in the bookmarks tree as follows:
272 * - Populates |idToNodeMap_| with a mapping of all node ids to their 424 * - Populates |idToNodeMap_| with a mapping of all node ids to their
273 * corresponding BookmarkTreeNode. 425 * corresponding BookmarkTreeNode.
274 * - Sets all the nodes to not selected and open by default. 426 * - Sets all the nodes to not selected and open by default.
275 * @param {BookmarkTreeNode} bookmarkNode 427 * @param {BookmarkTreeNode} bookmarkNode
276 * @param {Object=} idToNodeMap 428 * @param {Object=} idToNodeMap
277 */ 429 */
278 BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) { 430 BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) {
431 bookmarkNode.isSelectedItem = false;
279 if (idToNodeMap) 432 if (idToNodeMap)
280 idToNodeMap[bookmarkNode.id] = bookmarkNode; 433 idToNodeMap[bookmarkNode.id] = bookmarkNode;
281 434
282 if (bookmarkNode.url) 435 if (bookmarkNode.url)
283 return; 436 return;
284 437
285 bookmarkNode.isSelected = false; 438 bookmarkNode.isSelectedFolder = false;
286 bookmarkNode.isOpen = true; 439 bookmarkNode.isOpen = true;
287 for (var i = 0; i < bookmarkNode.children.length; i++) 440 for (var i = 0; i < bookmarkNode.children.length; i++)
288 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); 441 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap);
289 }; 442 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698