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

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

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

Powered by Google App Engine
This is Rietveld 408576698