1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr.define('bmm', function() {
6 var Promise = cr.Promise;
9 * Whether a node contains another node.
10 * TODO(yosin): Once JavaScript style guide is updated and linter follows
11 * that, we'll remove useless documentations for |parent| and |descendant|.
12 * TODO(yosin): bmm.contains() should be method of BookmarkTreeNode.
13 * @param {!BookmarkTreeNode} parent .
14 * @param {!BookmarkTreeNode} descendant .
15 * @return {boolean} Whether the parent contains the descendant.
17 function contains(parent, descendant) {
18 if (descendant.parentId == parent.id)
20 // the bmm.treeLookup contains all folders
21 var parentTreeItem = bmm.treeLookup[descendant.parentId];
22 if (!parentTreeItem || !parentTreeItem.bookmarkNode)
24 return this.contains(parent, parentTreeItem.bookmarkNode);
28 * @param {!BookmarkTreeNode} node The node to test.
29 * @return {boolean} Whether a bookmark node is a folder.
31 function isFolder(node) {
32 return !('url' in node);
35 var loadingPromises = {};
38 * Loads a subtree of the bookmark tree and returns a {@code cr.Promise} that
39 * will be fulfilled when done. This reuses multiple loads so that we do not
40 * load the same subtree more than once at the same time.
41 * @return {!cr.Promise} The future promise for the load.
43 function loadSubtree(id) {
45 if (!(id in loadingPromises)) {
46 loadingPromises[id] = new Promise;
47 loadingPromises[id].addListener(function(n) {
50 chrome.bookmarkManagerPrivate.getSubtree(id, false, function(nodes) {
51 loadingPromises[id].value = nodes && nodes[0];
52 delete loadingPromises[id];
55 loadingPromises[id].addListener(function(n) {
63 * Loads the entire bookmark tree and returns a {@code cr.Promise} that will
64 * be fulfilled when done. This reuses multiple loads so that we do not load
65 * the same tree more than once at the same time.
66 * @return {!cr.Promise} The future promise for the load.
69 return loadSubtree('');
74 * Removes the cached item from both the list and tree lookups.
76 remove: function(id) {
77 var treeItem = bmm.treeLookup[id];
79 var items = treeItem.items; // is an HTMLCollection
80 for (var i = 0; i < items.length; ++i) {
82 var bookmarkNode = item.bookmarkNode;
83 delete bmm.treeLookup[bookmarkNode.id];
85 delete bmm.treeLookup[id];
90 * Updates the underlying bookmark node for the tree items and list items by
91 * querying the bookmark backend.
92 * @param {string} id The id of the node to update the children for.
93 * @param {Function=} opt_f A funciton to call when done.
95 updateChildren: function(id, opt_f) {
96 function updateItem(bookmarkNode) {
97 var treeItem = bmm.treeLookup[bookmarkNode.id];
99 treeItem.bookmarkNode = bookmarkNode;
103 chrome.bookmarks.getChildren(id, function(children) {
105 children.forEach(updateItem);
114 * Called when the title of a bookmark changes.
115 * @param {string} id The id of changed bookmark node.
116 * @param {!Object} changeInfo The information about how the node changed.
118 function handleBookmarkChanged(id, changeInfo) {
120 bmm.tree.handleBookmarkChanged(id, changeInfo);
122 bmm.list.handleBookmarkChanged(id, changeInfo);
126 * Callback for when the user reorders by title.
127 * @param {string} id The id of the bookmark folder that was reordered.
128 * @param {!Object} reorderInfo The information about how the items where
131 function handleChildrenReordered(id, reorderInfo) {
133 bmm.tree.handleChildrenReordered(id, reorderInfo);
135 bmm.list.handleChildrenReordered(id, reorderInfo);
136 bookmarkCache.updateChildren(id);
140 * Callback for when a bookmark node is created.
141 * @param {string} id The id of the newly created bookmark node.
142 * @param {!Object} bookmarkNode The new bookmark node.
144 function handleCreated(id, bookmarkNode) {
146 bmm.list.handleCreated(id, bookmarkNode);
148 bmm.tree.handleCreated(id, bookmarkNode);
149 bookmarkCache.updateChildren(bookmarkNode.parentId);
153 * Callback for when a bookmark node is moved.
154 * @param {string} id The id of the moved bookmark node.
155 * @param {!Object} moveInfo The information about move.
157 function handleMoved(id, moveInfo) {
159 bmm.list.handleMoved(id, moveInfo);
161 bmm.tree.handleMoved(id, moveInfo);
163 bookmarkCache.updateChildren(moveInfo.parentId);
164 if (moveInfo.parentId != moveInfo.oldParentId)
165 bookmarkCache.updateChildren(moveInfo.oldParentId);
169 * Callback for when a bookmark node is removed.
170 * @param {string} id The id of the removed bookmark node.
171 * @param {!Object} bookmarkNode The information about removed.
173 function handleRemoved(id, removeInfo) {
175 bmm.list.handleRemoved(id, removeInfo);
177 bmm.tree.handleRemoved(id, removeInfo);
179 bookmarkCache.updateChildren(removeInfo.parentId);
180 bookmarkCache.remove(id);
184 * Callback for when all bookmark nodes have been deleted.
186 function handleRemoveAll() {
187 // Reload the list and the tree.
195 * Callback for when importing bookmark is started.
197 function handleImportBegan() {
198 chrome.bookmarks.onCreated.removeListener(handleCreated);
199 chrome.bookmarks.onChanged.removeListener(handleBookmarkChanged);
203 * Callback for when importing bookmark node is finished.
205 function handleImportEnded() {
206 // When importing is done we reload the tree and the list.
209 bmm.tree.removeEventListener('load', f);
211 chrome.bookmarks.onCreated.addListener(handleCreated);
212 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged);
217 // TODO(estade): this should navigate to the newly imported folder, which
218 // may be the bookmark bar if there were no previous bookmarks.
223 bmm.tree.addEventListener('load', f);
229 * Adds the listeners for the bookmark model change events.
231 function addBookmarkModelListeners() {
232 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged);
233 chrome.bookmarks.onChildrenReordered.addListener(handleChildrenReordered);
234 chrome.bookmarks.onCreated.addListener(handleCreated);
235 chrome.bookmarks.onMoved.addListener(handleMoved);
236 chrome.bookmarks.onRemoved.addListener(handleRemoved);
237 chrome.bookmarks.onImportBegan.addListener(handleImportBegan);
238 chrome.bookmarks.onImportEnded.addListener(handleImportEnded);
244 loadSubtree: loadSubtree,
246 addBookmarkModelListeners: addBookmarkModelListeners