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

Unified Diff: chrome/browser/resources/md_bookmarks/store.js

Issue 2645273002: [MD Bookmarks] Modify search to retain the previously selected folder. (Closed)
Patch Set: Rebase and update test to use selectFolder. 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/md_bookmarks/store.js
diff --git a/chrome/browser/resources/md_bookmarks/store.js b/chrome/browser/resources/md_bookmarks/store.js
index eb4cb11c7a3cab9825505dd03faceb876fb8ba14..6bea4370f20fddb6a87b7e495e69a5d2b6c7a22a 100644
--- a/chrome/browser/resources/md_bookmarks/store.js
+++ b/chrome/browser/resources/md_bookmarks/store.js
@@ -12,10 +12,9 @@ var BookmarksStore = Polymer({
notify: true,
},
- /** @type {?string} */
+ /** @type {string} */
selectedId: {
type: String,
- observer: 'updateSelectedDisplay_',
notify: true,
},
@@ -41,7 +40,10 @@ var BookmarksStore = Polymer({
idToNodeMap_: Object,
/** @type {?number} */
- anchorIndex_: Number,
+ anchorIndex_: {
+ type: Number,
+ value: null,
+ },
/** @type {Set<string>} */
searchResultSet_: Object,
@@ -81,6 +83,49 @@ var BookmarksStore = Polymer({
chrome.bookmarks.onChanged.addListener(this.onBookmarkChanged_.bind(this));
},
+ /**
+ * Changes the selected folder to that of the id or else defaults to the
+ * Bookmarks Bar.
+ * @param {string} id The id of the folder to be selected.
+ */
+ selectFolder: function(id) {
+ if (!this.idToNodeMap_)
+ return;
+
+ if (!this.idToNodeMap_[id] || this.idToNodeMap_[id].url)
+ id = this.rootNode.children[0].id;
+
+ this.deselectFolders_();
+ this.selectedId = id;
+ if (this.searchTerm)
+ return;
+
+ // Update the folder node to selected.
+ this.selectedId = id;
+ var folder = this.idToNodeMap_[this.selectedId];
+ this.set(folder.path + '.isSelectedFolder', true);
+
+ // Update the displayed list to the selected folder.
+ this.linkPaths('displayedList', folder.path + '.children');
+ this._setDisplayedList(
+ /** @type {!Array<!BookmarkTreeNode>}*/ (folder.children));
+
+ // Open any closed ancestors of the selected in the sidebar.
+ if (folder.id == this.rootNode.id)
+ return;
+
+ var parent = this.idToNodeMap_[/** @type {?string} */ (folder.parentId)];
+ while (parent) {
+ if (!parent.isOpen) {
+ this.fire('folder-open-changed', {
+ id: parent.id,
+ open: true,
+ });
+ }
+ parent = this.idToNodeMap_[/** @type {?string} */ (parent.parentId)];
+ }
+ },
+
//////////////////////////////////////////////////////////////////////////////
// bookmarks-store, private:
@@ -96,18 +141,18 @@ var BookmarksStore = Polymer({
BookmarksStore.initNodes(this.rootNode, this.idToNodeMap_);
// Initialize the store's fields from the router.
- if (this.$.router.searchTerm)
- this.searchTerm = this.$.router.searchTerm;
- else
- this.fire('selected-folder-changed', this.$.router.selectedId);
+ this.selectFolder(this.$.router.selectedId);
+ this.searchTerm = this.$.router.searchTerm;
},
/** @private */
deselectFolders_: function() {
+ if (!this.idToNodeMap_[this.selectedId])
+ return;
+
this.unlinkPaths('displayedList');
this.set(
this.idToNodeMap_[this.selectedId].path + '.isSelectedFolder', false);
- this.selectedId = null;
},
/**
@@ -116,9 +161,6 @@ var BookmarksStore = Polymer({
* @return {boolean}
*/
isAncestorOfSelected_: function(folder) {
- if (!this.selectedId)
- return false;
-
var selectedNode = this.idToNodeMap_[this.selectedId];
return selectedNode.path.startsWith(folder.path);
},
@@ -129,36 +171,19 @@ var BookmarksStore = Polymer({
return;
if (!this.searchTerm) {
- this.fire('selected-folder-changed', this.rootNode.children[0].id);
+ this.selectFolder(this.selectedId);
} else {
chrome.bookmarks.search(this.searchTerm, function(results) {
this.anchorIndex_ = null;
this.clearSelectedItems_();
this.searchResultSet_ = new Set();
- if (this.selectedId)
- this.deselectFolders_();
-
+ this.deselectFolders_();
this.setupSearchResults_(results);
}.bind(this));
}
},
- /** @private */
- updateSelectedDisplay_: function() {
- // Don't change to the selected display if ID was cleared.
- if (!this.selectedId)
- return;
-
- this.clearSelectedItems_();
- this.anchorIndex_ = null;
-
- var selectedNode = this.idToNodeMap_[this.selectedId];
- this.linkPaths('displayedList', selectedNode.path + '.children');
- this._setDisplayedList(
- /** @type {Array<BookmarkTreeNode>} */ (selectedNode.children));
- },
-
/**
* Remove all descendants of a given node from the map.
* @param {string} id
@@ -176,6 +201,7 @@ var BookmarksStore = Polymer({
delete this.idToNodeMap_[id];
},
+
/**
* Remove all selected items in the list.
* @private
@@ -187,7 +213,6 @@ var BookmarksStore = Polymer({
for (var i = 0; i < this.displayedList.length; i++) {
if (!this.displayedList[i].isSelectedItem)
continue;
-
this.set('displayedList.#' + i + '.isSelectedItem', false);
}
},
@@ -290,7 +315,7 @@ var BookmarksStore = Polymer({
// Updates selectedId if the removed node is an ancestor of the current
// selected node.
if (isAncestor)
- this.fire('selected-folder-changed', removeInfo.parentId);
+ this.selectFolder(removeInfo.parentId);
// Only update the displayedList if the removed node is in the
// displayedList.
@@ -304,7 +329,7 @@ var BookmarksStore = Polymer({
this.updateSearchDisplay_();
} else {
if (!isAncestor)
- this.fire('selected-folder-changed', this.selectedId);
+ this.selectFolder(this.selectedId);
this._setDisplayedList(parentNode.children);
}
@@ -344,37 +369,10 @@ var BookmarksStore = Polymer({
* @private
*/
onSelectedFolderChanged_: function(e) {
- if (this.searchTerm)
+ if (/** @type {boolean} */ (e.detail.clearSearch))
this.searchTerm = '';
- // Deselect the old folder if defined.
- if (this.selectedId && this.idToNodeMap_[this.selectedId])
- this.set(
- this.idToNodeMap_[this.selectedId].path + '.isSelectedFolder', false);
-
- // Check if the selected id is that of a defined folder.
- var id = /** @type {string} */ (e.detail);
- if (!this.idToNodeMap_[id] || this.idToNodeMap_[id].url)
- id = this.rootNode.children[0].id;
-
- var folder = this.idToNodeMap_[id];
- this.set(folder.path + '.isSelectedFolder', true);
- this.selectedId = id;
-
- if (folder.id == this.rootNode.id)
- return;
-
- var parent = this.idToNodeMap_[/** @type {?string} */ (folder.parentId)];
- while (parent) {
- if (!parent.isOpen) {
- this.fire('folder-open-changed', {
- id: parent.id,
- open: true,
- });
- }
-
- parent = this.idToNodeMap_[/** @type {?string} */ (parent.parentId)];
- }
+ this.selectFolder(/** @type {string} */ (e.detail.id));
},
/**
@@ -386,7 +384,7 @@ var BookmarksStore = Polymer({
var folder = this.idToNodeMap_[e.detail.id];
this.set(folder.path + '.isOpen', e.detail.open);
if (!folder.isOpen && this.isAncestorOfSelected_(folder))
- this.fire('selected-folder-changed', folder.id);
+ this.selectFolder(folder.id);
},
/**
« no previous file with comments | « chrome/browser/resources/md_bookmarks/router.js ('k') | chrome/test/data/webui/md_bookmarks/sidebar_test.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698