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

Side by Side Diff: resources/bookmark_manager/js/bmm/bookmarktree.js

Issue 853002: Updating the Chromium reference build for Windows. The continuous... (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/reference_builds/chrome/
Patch Set: Added the symbol files back. Created 10 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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.
4
5
6 cr.define('bmm', function() {
7 const Tree = cr.ui.Tree;
8 const TreeItem = cr.ui.TreeItem;
9
10 var treeLookup = {};
11
12 /**
13 * Creates a new tree item for a bookmark node.
14 * @param {!Object} bookmarkNode The bookmark node.
15 * @return {!cr.ui.TreeItem} The newly created tree item.
16 */
17 function createTreeItem(bookmarkNode) {
18 var id = bookmarkNode.id;
19 var ti = new TreeItem({
20 bookmarkId: id,
21 bookmarkNode: bookmarkNode,
22 // Bookmark toolbar and Other bookmarks are not draggable.
23 draggable: bookmarkNode.parentId != ROOT_ID
24 });
25 treeLookup[id] = ti;
26 updateTreeItem(ti, bookmarkNode);
27 return ti;
28 }
29
30 /**
31 * Updates an existing tree item to match a bookmark node.
32 * @param {!cr.ui.TreeItem} el The tree item to update.
33 * @param {!Object} bookmarkNode The bookmark node describing the tree item.
34 */
35 function updateTreeItem(el, bookmarkNode) {
36 el.label = bookmarkNode.title;
37 }
38
39 /**
40 * Asynchronousy adds a tree item at the correct index based on the bookmark
41 * backend.
42 *
43 * Since the bookmark tree only contains folders the index we get from certain
44 * callbacks is not very useful so we therefore have this async call which
45 * gets the children of the parent and adds the tree item at the desired
46 * index.
47 *
48 * This also exoands the parent so that newly added children are revealed.
49 *
50 * @param {!cr.ui.TreeItem} parent The parent tree item.
51 * @param {!cr.ui.TreeItem} treeItem The tree item to add.
52 * @param {Function=} f A function which gets called after the item has been
53 * added at the right index.
54 */
55 function addTreeItem(parent, treeItem, opt_f) {
56 chrome.bookmarks.getChildren(parent.bookmarkNode.id, function(children) {
57 var index = children.filter(bmm.isFolder).map(function(item) {
58 return item.id;
59 }).indexOf(treeItem.bookmarkNode.id);
60 parent.addAt(treeItem, index);
61 parent.expanded = true;
62 if (opt_f)
63 opt_f();
64 });
65 }
66
67
68 /**
69 * Creates a new bookmark list.
70 * @param {Object=} opt_propertyBag Optional properties.
71 * @constructor
72 * @extends {HTMLButtonElement}
73 */
74 var BookmarkTree = cr.ui.define('tree');
75
76 BookmarkTree.prototype = {
77 __proto__: Tree.prototype,
78
79 handleBookmarkChanged: function(id, changeInfo) {
80 var treeItem = treeLookup[id];
81 if (treeItem) {
82 treeItem.bookmarkNode.title = changeInfo.title;
83 updateTreeItem(treeItem, treeItem.bookmarkNode);
84 }
85 },
86
87 handleChildrenReordered: function(id, reorderInfo) {
88 var parentItem = treeLookup[id];
89 // The tree only contains folders.
90 var dirIds = reorderInfo.childIds.filter(function(id) {
91 return id in treeLookup;
92 }).forEach(function(id, i) {
93 parentItem.addAt(treeLookup[id], i);
94 });
95 },
96
97 handleCreated: function(id, bookmarkNode) {
98 if (bmm.isFolder(bookmarkNode)) {
99 var parentItem = treeLookup[bookmarkNode.parentId];
100 var newItem = createTreeItem(bookmarkNode);
101 addTreeItem(parentItem, newItem);
102 }
103 },
104
105 handleMoved: function(id, moveInfo) {
106 var treeItem = treeLookup[id];
107 if (treeItem) {
108 var oldParentItem = treeLookup[moveInfo.oldParentId];
109 oldParentItem.remove(treeItem);
110 var newParentItem = treeLookup[moveInfo.parentId];
111 // The tree only shows folders so the index is not the index we want. We
112 // therefore get the children need to adjust the index.
113 addTreeItem(newParentItem, treeItem);
114 }
115 },
116
117 handleRemoved: function(id, removeInfo) {
118 var parentItem = treeLookup[removeInfo.parentId];
119 var itemToRemove = treeLookup[id];
120 if (parentItem && itemToRemove) {
121 parentItem.remove(itemToRemove);
122 }
123 },
124
125 insertSubtree:function(folder) {
126 if (!bmm.isFolder(folder))
127 return;
128 var children = folder.children;
129 this.handleCreated(folder.id, folder);
130 for(var i = 0; i < children.length; i++) {
131 var child = children[i];
132 this.insertSubtree(child);
133 }
134 },
135
136 /**
137 * Returns the bookmark node with the given ID. The tree only maintains
138 * folder nodes.
139 * @param {string} id The ID of the node to find.
140 * @return {BookmarkTreeNode} The bookmark tree node or null if not found.
141 */
142 getBookmarkNodeById: function(id) {
143 var treeItem = treeLookup[id];
144 if (treeItem)
145 return treeItem.bookmarkNode;
146 return null;
147 },
148
149 /**
150 * Fetches the bookmark items and builds the tree control.
151 */
152 buildTree: function() {
153
154 /**
155 * Recursive helper function that adds all the directories to the
156 * parentTreeItem.
157 * @param {!cr.ui.Tree|!cr.ui.TreeItem} parentTreeItem The parent tree ele ment
158 * to append to.
159 * @param {!Array.<BookmarkTreeNode>} bookmarkNodes
160 * @return {boolean} Whether any directories where added.
161 */
162 function buildTreeItems(parentTreeItem, bookmarkNodes) {
163 var hasDirectories = false;
164 for (var i = 0, bookmarkNode; bookmarkNode = bookmarkNodes[i]; i++) {
165 if (bmm.isFolder(bookmarkNode)) {
166 hasDirectories = true;
167 var item = bmm.createTreeItem(bookmarkNode);
168 parentTreeItem.add(item);
169 var anyChildren = buildTreeItems(item, bookmarkNode.children);
170 item.expanded = anyChildren;
171 }
172 }
173 return hasDirectories;
174 }
175
176 var self = this;
177 chrome.bookmarks.getTree(function(root) {
178 buildTreeItems(self, root[0].children);
179 cr.dispatchSimpleEvent(self, 'load');
180 });
181 }
182 };
183
184 return {
185 BookmarkTree: BookmarkTree,
186 createTreeItem: createTreeItem,
187 treeLookup: treeLookup
188 };
189 });
OLDNEW
« no previous file with comments | « resources/bookmark_manager/js/bmm/bookmarklist.js ('k') | resources/bookmark_manager/js/bmm/treeiterator.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698