diff options
author | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-16 00:10:33 +0000 |
---|---|---|
committer | arv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-16 00:10:33 +0000 |
commit | 6b04ab467403c6a31b6722e84dd9e85682e339c9 (patch) | |
tree | a9f34b986e763758ceca5809427c8c1c82253b92 /chrome | |
parent | f286d5763e37dfcd181430c03d608c82cb5640ef (diff) | |
download | chromium_src-6b04ab467403c6a31b6722e84dd9e85682e339c9.zip chromium_src-6b04ab467403c6a31b6722e84dd9e85682e339c9.tar.gz chromium_src-6b04ab467403c6a31b6722e84dd9e85682e339c9.tar.bz2 |
Bookmarks: Persist expanded state of the tree items across sessions.
This fixes the bookmark manager. The state is stored using localStorage but we should probably store this in some other way so that it can be shared with Edit Bookmark dialog.
BUG=6648
TEST=Collapse a couple of tree items in the bookmark manager. The collapsed state should be persisted after reload and restart.
Review URL: http://codereview.chromium.org/3015002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@52577 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/resources/bookmark_manager/js/bmm/bookmark_tree.js | 79 | ||||
-rw-r--r-- | chrome/browser/resources/shared/js/cr/ui/tree.js | 1 |
2 files changed, 79 insertions, 1 deletions
diff --git a/chrome/browser/resources/bookmark_manager/js/bmm/bookmark_tree.js b/chrome/browser/resources/bookmark_manager/js/bmm/bookmark_tree.js index e27f1e6..453692c 100644 --- a/chrome/browser/resources/bookmark_manager/js/bmm/bookmark_tree.js +++ b/chrome/browser/resources/bookmark_manager/js/bmm/bookmark_tree.js @@ -9,6 +9,77 @@ cr.define('bmm', function() { var treeLookup = {}; + // Manager for persisting the expanded state. + var expandedManager = { + /** + * A map of the collapsed IDs. + * @type {Object} + */ + map: 'bookmarkTreeState' in localStorage ? + JSON.parse(localStorage['bookmarkTreeState']) : {}, + + /** + * Set the collapsed state for an ID. + * @param {string} The bookmark ID of the tree item that was expanded or + * collapsed. + * @param {boolean} expanded Whether the tree item was expanded. + */ + set: function(id, expanded) { + if (expanded) + delete this.map[id]; + else + this.map[id] = 1; + + this.save(); + }, + + /** + * @param {string} id The bookmark ID. + * @return {boolean} Whether the tree item should be expanded. + */ + get: function(id) { + return !(id in this.map); + }, + + /** + * Callback for the expand and collapse events from the tree. + * @param {!Event} e The collapse or expand event. + */ + handleEvent: function(e) { + this.set(e.target.bookmarkId, e.type == 'expand'); + }, + + /** + * Cleans up old bookmark IDs. + */ + cleanUp: function() { + for (var id in this.map) { + // If the id is no longer in the treeLookup the bookmark no longer + // exists. + if (!(id in treeLookup)) + delete this.map[id]; + } + this.save(); + }, + + timer: null, + + /** + * Saves the expanded state to the localStorage. + */ + save: function() { + clearTimeout(this.timer); + var map = this.map; + // Save in a timeout so that we can coalesce multiple changes. + this.timer = setTimeout(function() { + localStorage['bookmarkTreeState'] = JSON.stringify(map); + }, 100); + } + }; + + // Clean up once per session but wait until things settle down a bit. + setTimeout(cr.bind(expandedManager.cleanUp, expandedManager), 1e4); + /** * Creates a new tree item for a bookmark node. * @param {!Object} bookmarkNode The bookmark node. @@ -92,6 +163,12 @@ cr.define('bmm', function() { BookmarkTree.prototype = { __proto__: Tree.prototype, + decorate: function() { + Tree.prototype.decorate.call(this); + this.addEventListener('expand', expandedManager); + this.addEventListener('collapse', expandedManager); + }, + handleBookmarkChanged: function(id, changeInfo) { var treeItem = treeLookup[id]; if (treeItem) @@ -179,7 +256,7 @@ cr.define('bmm', function() { var item = new BookmarkTreeItem(bookmarkNode); parentTreeItem.add(item); var anyChildren = buildTreeItems(item, bookmarkNode.children); - item.expanded = anyChildren; + item.expanded = anyChildren && expandedManager.get(bookmarkNode.id); } } return hasDirectories; diff --git a/chrome/browser/resources/shared/js/cr/ui/tree.js b/chrome/browser/resources/shared/js/cr/ui/tree.js index 9e18321..1fa3352 100644 --- a/chrome/browser/resources/shared/js/cr/ui/tree.js +++ b/chrome/browser/resources/shared/js/cr/ui/tree.js @@ -442,6 +442,7 @@ cr.define('cr.ui', function() { if (b) { this.setAttribute('selected', ''); rowItem.setAttribute('selected', ''); + this.reveal(); this.labelElement.scrollIntoViewIfNeeded(false); if (tree) tree.selectedItem = this; |