OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 cr.define('bmm', function() { | 6 cr.define('bmm', function() { |
7 /** | 7 /** |
8 * The id of the bookmark root. | 8 * The id of the bookmark root. |
9 * @type {string} | 9 * @type {string} |
10 * @const | 10 * @const |
11 */ | 11 */ |
12 var ROOT_ID = '0'; | 12 var ROOT_ID = '0'; |
13 | 13 |
14 /** @const */ var Tree = cr.ui.Tree; | 14 /** @const */ var Tree = cr.ui.Tree; |
15 /** @const */ var TreeItem = cr.ui.TreeItem; | 15 /** @const */ var TreeItem = cr.ui.TreeItem; |
16 | 16 |
17 var treeLookup = {}; | 17 var treeLookup = {}; |
18 | |
19 /** | |
20 * This variable is fake and used only to declare exported bmm.tree name. | |
21 * @suppress {suspiciousCode} | |
Dan Beam
2014/09/23 02:46:55
same
Vitaly Pavlenko
2014/09/23 22:20:55
Done.
| |
22 */ | |
18 var tree; | 23 var tree; |
19 | 24 |
20 // Manager for persisting the expanded state. | 25 // Manager for persisting the expanded state. |
21 var expandedManager = { | 26 var expandedManager = /** @type {EventListener} */({ |
22 /** | 27 /** |
23 * A map of the collapsed IDs. | 28 * A map of the collapsed IDs. |
24 * @type {Object} | 29 * @type {Object} |
25 */ | 30 */ |
26 map: 'bookmarkTreeState' in localStorage ? | 31 map: 'bookmarkTreeState' in window.localStorage ? |
27 JSON.parse(localStorage['bookmarkTreeState']) : {}, | 32 /** @type {Object} */(JSON.parse( |
33 window.localStorage['bookmarkTreeState']) | |
Dan Beam
2014/09/23 02:46:55
why can't we just make an alias, e.g.
var local
Vitaly Pavlenko
2014/09/23 22:20:55
Done.
| |
34 ) : {}, | |
28 | 35 |
29 /** | 36 /** |
30 * Set the collapsed state for an ID. | 37 * Set the collapsed state for an ID. |
31 * @param {string} The bookmark ID of the tree item that was expanded or | 38 * @param {string} id The bookmark ID of the tree item that was expanded or |
32 * collapsed. | 39 * collapsed. |
33 * @param {boolean} expanded Whether the tree item was expanded. | 40 * @param {boolean} expanded Whether the tree item was expanded. |
34 */ | 41 */ |
35 set: function(id, expanded) { | 42 set: function(id, expanded) { |
36 if (expanded) | 43 if (expanded) |
37 delete this.map[id]; | 44 delete this.map[id]; |
38 else | 45 else |
39 this.map[id] = 1; | 46 this.map[id] = 1; |
40 | 47 |
41 this.save(); | 48 this.save(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 timer: null, | 80 timer: null, |
74 | 81 |
75 /** | 82 /** |
76 * Saves the expanded state to the localStorage. | 83 * Saves the expanded state to the localStorage. |
77 */ | 84 */ |
78 save: function() { | 85 save: function() { |
79 clearTimeout(this.timer); | 86 clearTimeout(this.timer); |
80 var map = this.map; | 87 var map = this.map; |
81 // Save in a timeout so that we can coalesce multiple changes. | 88 // Save in a timeout so that we can coalesce multiple changes. |
82 this.timer = setTimeout(function() { | 89 this.timer = setTimeout(function() { |
83 localStorage['bookmarkTreeState'] = JSON.stringify(map); | 90 window.localStorage['bookmarkTreeState'] = JSON.stringify(map); |
84 }, 100); | 91 }, 100); |
85 } | 92 } |
86 }; | 93 }); |
87 | 94 |
88 // Clean up once per session but wait until things settle down a bit. | 95 // Clean up once per session but wait until things settle down a bit. |
89 setTimeout(expandedManager.cleanUp.bind(expandedManager), 1e4); | 96 setTimeout(expandedManager.cleanUp.bind(expandedManager), 1e4); |
90 | 97 |
91 /** | 98 /** |
92 * Creates a new tree item for a bookmark node. | 99 * Creates a new tree item for a bookmark node. |
93 * @param {!Object} bookmarkNode The bookmark node. | 100 * @param {!Object} bookmarkNode The bookmark node. |
94 * @constructor | 101 * @constructor |
95 * @extends {TreeItem} | 102 * @extends {TreeItem} |
96 */ | 103 */ |
(...skipping 27 matching lines...) Expand all Loading... | |
124 * | 131 * |
125 * Since the bookmark tree only contains folders the index we get from certain | 132 * Since the bookmark tree only contains folders the index we get from certain |
126 * callbacks is not very useful so we therefore have this async call which | 133 * callbacks is not very useful so we therefore have this async call which |
127 * gets the children of the parent and adds the tree item at the desired | 134 * gets the children of the parent and adds the tree item at the desired |
128 * index. | 135 * index. |
129 * | 136 * |
130 * This also exoands the parent so that newly added children are revealed. | 137 * This also exoands the parent so that newly added children are revealed. |
131 * | 138 * |
132 * @param {!cr.ui.TreeItem} parent The parent tree item. | 139 * @param {!cr.ui.TreeItem} parent The parent tree item. |
133 * @param {!cr.ui.TreeItem} treeItem The tree item to add. | 140 * @param {!cr.ui.TreeItem} treeItem The tree item to add. |
134 * @param {Function=} f A function which gets called after the item has been | 141 * @param {Function=} opt_f A function which gets called after the item has |
135 * added at the right index. | 142 * been added at the right index. |
136 */ | 143 */ |
137 function addTreeItem(parent, treeItem, opt_f) { | 144 function addTreeItem(parent, treeItem, opt_f) { |
138 chrome.bookmarks.getChildren(parent.bookmarkNode.id, function(children) { | 145 chrome.bookmarks.getChildren(parent.bookmarkNode.id, function(children) { |
139 var index = children.filter(bmm.isFolder).map(function(item) { | 146 var index = children.filter( |
147 /** | |
148 * @type {function (BookmarkTreeNode, number, | |
149 * Array.<(BookmarkTreeNode)>)} | |
150 */(bmm.isFolder) | |
151 ).map(function(item) { | |
Dan Beam
2014/09/23 02:46:55
use a temporary variable to keep the code pretty
Vitaly Pavlenko
2014/09/23 22:20:55
Done.
| |
140 return item.id; | 152 return item.id; |
141 }).indexOf(treeItem.bookmarkNode.id); | 153 }).indexOf(treeItem.bookmarkNode.id); |
142 parent.addAt(treeItem, index); | 154 parent.addAt(treeItem, index); |
143 parent.expanded = true; | 155 parent.expanded = true; |
144 if (opt_f) | 156 if (opt_f) |
145 opt_f(); | 157 opt_f(); |
146 }); | 158 }); |
147 } | 159 } |
148 | 160 |
149 | 161 |
150 /** | 162 /** |
151 * Creates a new bookmark list. | 163 * Creates a new bookmark list. |
152 * @param {Object=} opt_propertyBag Optional properties. | 164 * @param {Object=} opt_propertyBag Optional properties. |
153 * @constructor | 165 * @constructor |
154 * @extends {HTMLButtonElement} | 166 * @extends {cr.ui.Tree} |
155 */ | 167 */ |
156 var BookmarkTree = cr.ui.define('tree'); | 168 var BookmarkTree = cr.ui.define('tree'); |
157 | 169 |
158 BookmarkTree.prototype = { | 170 BookmarkTree.prototype = { |
159 __proto__: Tree.prototype, | 171 __proto__: Tree.prototype, |
160 | 172 |
161 decorate: function() { | 173 decorate: function() { |
162 Tree.prototype.decorate.call(this); | 174 Tree.prototype.decorate.call(this); |
163 this.addEventListener('expand', expandedManager); | 175 this.addEventListener('expand', expandedManager); |
164 this.addEventListener('collapse', expandedManager); | 176 this.addEventListener('collapse', expandedManager); |
165 | 177 |
166 bmm.tree = this; | 178 bmm.tree = this; |
167 }, | 179 }, |
168 | 180 |
169 handleBookmarkChanged: function(id, changeInfo) { | 181 handleBookmarkChanged: function(id, changeInfo) { |
170 var treeItem = treeLookup[id]; | 182 var treeItem = treeLookup[id]; |
171 if (treeItem) | 183 if (treeItem) |
172 treeItem.label = treeItem.bookmarkNode.title = changeInfo.title; | 184 treeItem.label = treeItem.bookmarkNode.title = changeInfo.title; |
173 }, | 185 }, |
174 | 186 |
187 /** | |
188 * @param {string} id | |
189 * @param {ReorderInfo} reorderInfo | |
190 */ | |
175 handleChildrenReordered: function(id, reorderInfo) { | 191 handleChildrenReordered: function(id, reorderInfo) { |
176 var parentItem = treeLookup[id]; | 192 var parentItem = treeLookup[id]; |
177 // The tree only contains folders. | 193 // The tree only contains folders. |
178 var dirIds = reorderInfo.childIds.filter(function(id) { | 194 var dirIds = reorderInfo.childIds.filter(function(id) { |
179 return id in treeLookup; | 195 return id in treeLookup; |
180 }).forEach(function(id, i) { | 196 }).forEach(function(id, i) { |
181 parentItem.addAt(treeLookup[id], i); | 197 parentItem.addAt(treeLookup[id], i); |
182 }); | 198 }); |
183 }, | 199 }, |
184 | 200 |
185 handleCreated: function(id, bookmarkNode) { | 201 handleCreated: function(id, bookmarkNode) { |
186 if (bmm.isFolder(bookmarkNode)) { | 202 if (bmm.isFolder(bookmarkNode)) { |
187 var parentItem = treeLookup[bookmarkNode.parentId]; | 203 var parentItem = treeLookup[bookmarkNode.parentId]; |
188 var newItem = new BookmarkTreeItem(bookmarkNode); | 204 var newItem = new BookmarkTreeItem(bookmarkNode); |
189 addTreeItem(parentItem, newItem); | 205 addTreeItem(parentItem, newItem); |
190 } | 206 } |
191 }, | 207 }, |
192 | 208 |
209 /** | |
210 * @param {string} id | |
211 * @param {MoveInfo} moveInfo | |
212 */ | |
193 handleMoved: function(id, moveInfo) { | 213 handleMoved: function(id, moveInfo) { |
194 var treeItem = treeLookup[id]; | 214 var treeItem = treeLookup[id]; |
195 if (treeItem) { | 215 if (treeItem) { |
196 var oldParentItem = treeLookup[moveInfo.oldParentId]; | 216 var oldParentItem = treeLookup[moveInfo.oldParentId]; |
197 oldParentItem.remove(treeItem); | 217 oldParentItem.remove(treeItem); |
198 var newParentItem = treeLookup[moveInfo.parentId]; | 218 var newParentItem = treeLookup[moveInfo.parentId]; |
199 // The tree only shows folders so the index is not the index we want. We | 219 // The tree only shows folders so the index is not the index we want. We |
200 // therefore get the children need to adjust the index. | 220 // therefore get the children need to adjust the index. |
201 addTreeItem(newParentItem, treeItem); | 221 addTreeItem(newParentItem, treeItem); |
202 } | 222 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 * @param {!Array.<BookmarkTreeNode>} bookmarkNodes A list of bookmark | 274 * @param {!Array.<BookmarkTreeNode>} bookmarkNodes A list of bookmark |
255 * nodes to be added. | 275 * nodes to be added. |
256 * @return {boolean} Whether any directories where added. | 276 * @return {boolean} Whether any directories where added. |
257 */ | 277 */ |
258 function buildTreeItems(parentTreeItem, bookmarkNodes) { | 278 function buildTreeItems(parentTreeItem, bookmarkNodes) { |
259 var hasDirectories = false; | 279 var hasDirectories = false; |
260 for (var i = 0, bookmarkNode; bookmarkNode = bookmarkNodes[i]; i++) { | 280 for (var i = 0, bookmarkNode; bookmarkNode = bookmarkNodes[i]; i++) { |
261 if (bmm.isFolder(bookmarkNode)) { | 281 if (bmm.isFolder(bookmarkNode)) { |
262 hasDirectories = true; | 282 hasDirectories = true; |
263 var item = new BookmarkTreeItem(bookmarkNode); | 283 var item = new BookmarkTreeItem(bookmarkNode); |
264 parentTreeItem.add(item); | 284 parentTreeItem.add(item); |
Dan Beam
2014/09/23 02:46:55
var children = assert(bookmarkNode.children);
var
Vitaly Pavlenko
2014/09/23 22:20:55
Done.
| |
265 var anyChildren = buildTreeItems(item, bookmarkNode.children); | 285 var anyChildren = buildTreeItems(item, |
286 assert(bookmarkNode.children)); | |
266 item.expanded = anyChildren && expandedManager.get(bookmarkNode.id); | 287 item.expanded = anyChildren && expandedManager.get(bookmarkNode.id); |
267 } | 288 } |
268 } | 289 } |
269 return hasDirectories; | 290 return hasDirectories; |
270 } | 291 } |
271 | 292 |
272 var self = this; | 293 var self = this; |
273 chrome.bookmarkManagerPrivate.getSubtree('', true, function(root) { | 294 chrome.bookmarkManagerPrivate.getSubtree('', true, function(root) { |
274 self.clear(); | 295 self.clear(); |
275 buildTreeItems(self, root[0].children); | 296 buildTreeItems(self, root[0].children); |
(...skipping 22 matching lines...) Expand all Loading... | |
298 }; | 319 }; |
299 | 320 |
300 return { | 321 return { |
301 BookmarkTree: BookmarkTree, | 322 BookmarkTree: BookmarkTree, |
302 BookmarkTreeItem: BookmarkTreeItem, | 323 BookmarkTreeItem: BookmarkTreeItem, |
303 treeLookup: treeLookup, | 324 treeLookup: treeLookup, |
304 tree: tree, | 325 tree: tree, |
305 ROOT_ID: ROOT_ID | 326 ROOT_ID: ROOT_ID |
306 }; | 327 }; |
307 }); | 328 }); |
OLD | NEW |