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

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

Issue 2670473002: [MD Bookmarks] Implement adding folders and bookmarks from toolbar menu. (Closed)
Patch Set: Add node menu and tests. 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 /** @type {Set<string>} */ 46 /** @type {Set<string>} */
47 searchResultSet_: Object, 47 searchResultSet_: Object,
48 }, 48 },
49 49
50 /** @private {Object} */ 50 /** @private {Object} */
51 documentListeners_: null, 51 documentListeners_: null,
52 52
53 /** @override */ 53 /** @override */
54 attached: function() { 54 attached: function() {
55 this.documentListeners_ = { 55 this.documentListeners_ = {
56 'add-node-saved': this.onAddNodeSaved_.bind(this),
56 'folder-open-changed': this.onFolderOpenChanged_.bind(this), 57 'folder-open-changed': this.onFolderOpenChanged_.bind(this),
57 'search-term-changed': this.onSearchTermChanged_.bind(this), 58 'search-term-changed': this.onSearchTermChanged_.bind(this),
58 'select-item': this.onItemSelected_.bind(this), 59 'select-item': this.onItemSelected_.bind(this),
59 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this), 60 'selected-folder-changed': this.onSelectedFolderChanged_.bind(this),
60 }; 61 };
61 for (var event in this.documentListeners_) 62 for (var event in this.documentListeners_)
62 document.addEventListener(event, this.documentListeners_[event]); 63 document.addEventListener(event, this.documentListeners_[event]);
63 }, 64 },
64 65
65 /** @override */ 66 /** @override */
66 detached: function() { 67 detached: function() {
67 for (var event in this.documentListeners_) 68 for (var event in this.documentListeners_)
68 document.removeEventListener(event, this.documentListeners_[event]); 69 document.removeEventListener(event, this.documentListeners_[event]);
69 }, 70 },
70 71
71 /** 72 /**
72 * Initializes the store with data from the bookmarks API. 73 * Initializes the store with data from the bookmarks API.
73 * Called by app on attached. 74 * Called by app on attached.
74 */ 75 */
75 initializeStore: function() { 76 initializeStore: function() {
76 chrome.bookmarks.getTree(function(results) { 77 chrome.bookmarks.getTree(function(results) {
77 this.setupStore_(results[0]); 78 this.setupStore_(results[0]);
78 }.bind(this)); 79 }.bind(this));
79 // Attach bookmarks API listeners. 80 // Attach bookmarks API listeners.
80 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this)); 81 chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this));
81 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this)); 82 chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this));
83 chrome.bookmarks.onCreated.addListener(this.onBookmarkCreated_.bind(this));
82 }, 84 },
83 85
84 ////////////////////////////////////////////////////////////////////////////// 86 //////////////////////////////////////////////////////////////////////////////
85 // bookmarks-store, private: 87 // bookmarks-store, private:
86 88
87 /** 89 /**
88 * @param {BookmarkTreeNode} rootNode 90 * @param {BookmarkTreeNode} rootNode
89 * @private 91 * @private
90 */ 92 */
91 setupStore_: function(rootNode) { 93 setupStore_: function(rootNode) {
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 onBookmarkChanged_: function(id, changeInfo) { 321 onBookmarkChanged_: function(id, changeInfo) {
320 if (changeInfo.title) 322 if (changeInfo.title)
321 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title); 323 this.set(this.idToNodeMap_[id].path + '.title', changeInfo.title);
322 if (changeInfo.url) 324 if (changeInfo.url)
323 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url); 325 this.set(this.idToNodeMap_[id].path + '.url', changeInfo.url);
324 326
325 if (this.searchTerm) 327 if (this.searchTerm)
326 this.updateSearchDisplay_(); 328 this.updateSearchDisplay_();
327 }, 329 },
328 330
331 /**
332 * Called when after a node is created.
333 * @param {string} id The id of the newly added bookmark node.
334 * @param {!BookmarkTreeNode} bookmarkNode
335 */
336 onBookmarkCreated_: function(id, bookmarkNode) {
337 var parent = this.idToNodeMap_[bookmarkNode.parentId || null];
angelayang 2017/02/08 07:34:03 Closure complains that bookmarkNode.parentId can b
jiaxi 2017/02/09 01:13:09 I used /** @type {?string} */ in onSelectedFolderC
angelayang 2017/02/09 03:12:24 I'll to do the same for consistency sake for now a
338 this.push(parent.path + '.children', bookmarkNode);
jiaxi 2017/02/09 01:13:09 According to my understanding, this adds the new B
jiaxi 2017/02/09 01:20:24 Take a second thought, Polymer's splice could prob
angelayang 2017/02/09 03:12:24 I think what you suggested is good, thanks
339 BookmarksStore.generatePaths(parent, parent.children.length - 1);
340 BookmarksStore.initNodes(bookmarkNode, this.idToNodeMap_);
341
342 if (this.searchTerm)
343 this.updateSearchDisplay_();
344 },
345
329 ////////////////////////////////////////////////////////////////////////////// 346 //////////////////////////////////////////////////////////////////////////////
330 // bookmarks-store, bookmarks app event listeners: 347 // bookmarks-store, bookmarks app event listeners:
331 348
332 /** 349 /**
333 * @param {Event} e 350 * @param {Event} e
334 * @private 351 * @private
335 */ 352 */
336 onSearchTermChanged_: function(e) { 353 onSearchTermChanged_: function(e) {
337 this.searchTerm = /** @type {string} */ (e.detail); 354 this.searchTerm = /** @type {string} */ (e.detail);
338 }, 355 },
(...skipping 29 matching lines...) Expand all
368 * @private 385 * @private
369 */ 386 */
370 onFolderOpenChanged_: function(e) { 387 onFolderOpenChanged_: function(e) {
371 var folder = this.idToNodeMap_[e.detail.id]; 388 var folder = this.idToNodeMap_[e.detail.id];
372 this.set(folder.path + '.isOpen', e.detail.open); 389 this.set(folder.path + '.isOpen', e.detail.open);
373 if (!folder.isOpen && this.isAncestorOfSelected_(folder)) 390 if (!folder.isOpen && this.isAncestorOfSelected_(folder))
374 this.fire('selected-folder-changed', folder.id); 391 this.fire('selected-folder-changed', folder.id);
375 }, 392 },
376 393
377 /** 394 /**
395 * Appends a node to the children the selected folder.
396 * @param {CustomEvent} e
397 * @private
398 */
399 onAddNodeSaved_: function(e) {
400 var info = /** @type {!Object} */ (e.detail);
401 info.parentId = this.selectedId;
402 // TODO (angelayang): handle invalid urls.
403 chrome.bookmarks.create(info);
404 },
405
406 /**
378 * Selects items according to keyboard behaviours. 407 * Selects items according to keyboard behaviours.
379 * @param {CustomEvent} e 408 * @param {CustomEvent} e
380 * @private 409 * @private
381 */ 410 */
382 onItemSelected_: function(e) { 411 onItemSelected_: function(e) {
383 if (!e.detail.add) 412 if (!e.detail.add)
384 this.clearSelectedItems_(); 413 this.clearSelectedItems_();
385 414
386 if (e.detail.range) 415 if (e.detail.range)
387 this.selectRange_(e.detail.item); 416 this.selectRange_(e.detail.item);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) { 448 BookmarksStore.initNodes = function(bookmarkNode, idToNodeMap) {
420 bookmarkNode.isSelectedItem = false; 449 bookmarkNode.isSelectedItem = false;
421 if (idToNodeMap) 450 if (idToNodeMap)
422 idToNodeMap[bookmarkNode.id] = bookmarkNode; 451 idToNodeMap[bookmarkNode.id] = bookmarkNode;
423 452
424 if (bookmarkNode.url) 453 if (bookmarkNode.url)
425 return; 454 return;
426 455
427 bookmarkNode.isSelectedFolder = false; 456 bookmarkNode.isSelectedFolder = false;
428 bookmarkNode.isOpen = true; 457 bookmarkNode.isOpen = true;
458 if (!bookmarkNode.children)
459 bookmarkNode.children = [];
460
429 for (var i = 0; i < bookmarkNode.children.length; i++) 461 for (var i = 0; i < bookmarkNode.children.length; i++)
430 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap); 462 BookmarksStore.initNodes(bookmarkNode.children[i], idToNodeMap);
431 }; 463 };
OLDNEW
« no previous file with comments | « chrome/browser/resources/md_bookmarks/folder_node.js ('k') | chrome/browser/resources/md_bookmarks/toolbar.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698