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

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

Issue 2613683002: [MD Bookmarks] Add Delete and Copy URL for Material Bookmarks. (Closed)
Patch Set: Created 3 years, 11 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 7a9817817b0991fe590a6f28add51eabb1c1b0b1..11bd2f761b808cff8fc3dc995c36e5bdad341736 100644
--- a/chrome/browser/resources/md_bookmarks/store.js
+++ b/chrome/browser/resources/md_bookmarks/store.js
@@ -31,6 +31,17 @@ Polymer({
/** @private {Object} */
documentListeners_: null,
+ /**
+ * Initializes the store with data from the bookmarks API.
+ * Called by app on attached.
+ */
+ initializeStore: function() {
+ chrome.bookmarks.getTree(function(results) {
+ this.setupStore_(results[0]);
+ }.bind(this));
+ chrome.bookmarks.onRemoved.addListener(this.onBookmarkRemoved_.bind(this));
+ },
+
calamity 2017/01/04 06:57:44 Move this back to where it was.
jiaxi 2017/01/04 22:44:00 Done.
/** @override */
attached: function() {
this.documentListeners_ = {
@@ -48,16 +59,6 @@ Polymer({
},
/**
- * Initializes the store with data from the bookmarks API.
- * Called by app on attached.
- */
- initializeStore: function() {
- chrome.bookmarks.getTree(function(results) {
- this.setupStore_(results[0]);
- }.bind(this));
- },
-
- /**
* @param {BookmarkTreeNode} rootNode
* @private
*/
@@ -65,6 +66,7 @@ Polymer({
this.rootNode = rootNode;
this.idToNodeMap_ = {};
this.rootNode.path = 'rootNode';
+ this.generatePaths_(rootNode, 0);
this.initNodes_(this.rootNode);
this.fire('selected-folder-changed', this.rootNode.children[0].id);
},
@@ -115,10 +117,25 @@ Polymer({
},
/**
+ * Callback for when a bookmark node is removed.
+ * If a folder is selected or is an ancestor of a selected folder, the parent
+ * of the removed folder will be selected.
+ * @param {string} id The id of the removed bookmark node.
+ * @param {!Object} removeInfo
+ */
+ onBookmarkRemoved_: function(id, removeInfo) {
+ if (this.isAncestorOfSelected_(this.idToNodeMap_[id]))
+ this.fire('selected-folder-changed', removeInfo.parentId);
calamity 2017/01/04 06:57:44 nit: newline
jiaxi 2017/01/04 22:44:00 Done.
+ var parentNode = this.idToNodeMap_[removeInfo.parentId];
+ this.splice(parentNode.path + '.children', removeInfo.index, 1);
+ this.removeChildrenFromMap_(id);
+ this.generatePaths_(parentNode, Number(removeInfo.index));
+ },
+
+ /**
* Initializes the nodes in the bookmarks tree as follows:
* - Populates |idToNodeMap_| with a mapping of all node ids to their
* corresponding BookmarkTreeNode.
- * - Stores the path from the store to a node inside the node.
* - Sets all the nodes to not selected and open by default.
* @param {BookmarkTreeNode} bookmarkNode
* @private
@@ -131,8 +148,40 @@ Polymer({
bookmarkNode.isSelected = false;
bookmarkNode.isOpen = true;
for (var i = 0; i < bookmarkNode.children.length; i++) {
- bookmarkNode.children[i].path = bookmarkNode.path + '.children.' + i;
+ bookmarkNode.children[i].isSelected = false;
+ bookmarkNode.isOpen = true;
this.initNodes_(bookmarkNode.children[i]);
}
},
+
+ /**
+ * Stores the path from the store to a node inside the node.
+ * @param {BookmarkTreeNode} bookmarkNode
+ * @param {number} startIndex
+ * @private
+ */
+ generatePaths_: function(bookmarkNode, startIndex) {
+ if (bookmarkNode.children) {
calamity 2017/01/04 06:57:44 Invert and early return.
jiaxi 2017/01/04 22:44:00 I think the early return is in this(https://codere
calamity 2017/01/05 07:07:08 You might as well, the next patch will just pull i
+ for (var i = startIndex; i < bookmarkNode.children.length; i++) {
+ bookmarkNode.children[i].path = bookmarkNode.path + '.children.#' + i;
+ this.generatePaths_(bookmarkNode.children[i], 0);
+ }
+ }
+ },
+
+ /**
calamity 2017/01/04 06:57:44 This method deserves a comment.
jiaxi 2017/01/04 22:44:00 Done.
+ * @param {string} id
+ * @private
+ */
+ removeChildrenFromMap_: function(id) {
calamity 2017/01/04 06:57:44 Since this is all recursive children, I would pref
jiaxi 2017/01/04 22:44:00 Done.
+ var node = this.idToNodeMap_[id];
+ if (!node)
+ return;
+
+ if (node.children) {
+ for (var i = 0; i < node.children.length; i++)
+ this.removeChildrenFromMap_(node.children[i].id);
+ }
+ delete this.idToNodeMap_[id];
+ }
});

Powered by Google App Engine
This is Rietveld 408576698