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 'use strict'; | 7 'use strict'; |
8 | 8 |
9 /** | 9 /** |
10 * The id of the bookmark root. | 10 * The id of the bookmark root. |
11 * @type {string} | 11 * @type {string} |
12 * @const | 12 * @const |
13 */ | 13 */ |
14 var ROOT_ID = '0'; | 14 var ROOT_ID = '0'; |
15 | 15 |
16 /** @const */ var Tree = cr.ui.Tree; | 16 /** @const */ var Tree = cr.ui.Tree; |
17 /** @const */ var TreeItem = cr.ui.TreeItem; | 17 /** @const */ var TreeItem = cr.ui.TreeItem; |
18 /** @const */ var localStorage = window.localStorage; | 18 /** @const */ var localStorage = window.localStorage; |
19 | 19 |
20 var treeLookup = {}; | 20 var treeLookup = {}; |
21 | 21 |
22 // Manager for persisting the expanded state. | 22 // Manager for persisting the expanded state. |
23 var expandedManager = /** @type {EventListener} */({ | 23 var expandedManager = /** @type {EventListener} */ ({ |
24 /** | 24 /** |
25 * A map of the collapsed IDs. | 25 * A map of the collapsed IDs. |
26 * @type {Object} | 26 * @type {Object} |
27 */ | 27 */ |
28 map: 'bookmarkTreeState' in localStorage ? | 28 map: 'bookmarkTreeState' in localStorage ? |
29 /** @type {Object} */(JSON.parse(localStorage['bookmarkTreeState'])) : | 29 /** @type {Object} */ (JSON.parse(localStorage['bookmarkTreeState'])) : |
30 {}, | 30 {}, |
31 | 31 |
32 /** | 32 /** |
33 * Set the collapsed state for an ID. | 33 * Set the collapsed state for an ID. |
34 * @param {string} id The bookmark ID of the tree item that was expanded or | 34 * @param {string} id The bookmark ID of the tree item that was expanded or |
35 * collapsed. | 35 * collapsed. |
36 * @param {boolean} expanded Whether the tree item was expanded. | 36 * @param {boolean} expanded Whether the tree item was expanded. |
37 */ | 37 */ |
38 set: function(id, expanded) { | 38 set: function(id, expanded) { |
39 if (expanded) | 39 if (expanded) |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 * @param {!cr.ui.TreeItem} parent The parent tree item. | 135 * @param {!cr.ui.TreeItem} parent The parent tree item. |
136 * @param {!cr.ui.TreeItem} treeItem The tree item to add. | 136 * @param {!cr.ui.TreeItem} treeItem The tree item to add. |
137 * @param {Function=} opt_f A function which gets called after the item has | 137 * @param {Function=} opt_f A function which gets called after the item has |
138 * been added at the right index. | 138 * been added at the right index. |
139 */ | 139 */ |
140 function addTreeItem(parent, treeItem, opt_f) { | 140 function addTreeItem(parent, treeItem, opt_f) { |
141 chrome.bookmarks.getChildren(parent.bookmarkNode.id, function(children) { | 141 chrome.bookmarks.getChildren(parent.bookmarkNode.id, function(children) { |
142 var isFolder = /** | 142 var isFolder = /** |
143 * @type {function (BookmarkTreeNode, number, | 143 * @type {function (BookmarkTreeNode, number, |
144 * Array<(BookmarkTreeNode)>)} | 144 * Array<(BookmarkTreeNode)>)} |
145 */(bmm.isFolder); | 145 */ (bmm.isFolder); |
146 var index = children.filter(isFolder).map(function(item) { | 146 var index = children.filter(isFolder) |
147 return item.id; | 147 .map(function(item) { |
148 }).indexOf(treeItem.bookmarkNode.id); | 148 return item.id; |
| 149 }) |
| 150 .indexOf(treeItem.bookmarkNode.id); |
149 parent.addAt(treeItem, index); | 151 parent.addAt(treeItem, index); |
150 parent.expanded = true; | 152 parent.expanded = true; |
151 if (opt_f) | 153 if (opt_f) |
152 opt_f(); | 154 opt_f(); |
153 }); | 155 }); |
154 } | 156 } |
155 | 157 |
156 | 158 |
157 /** | 159 /** |
158 * Creates a new bookmark list. | 160 * Creates a new bookmark list. |
(...skipping 20 matching lines...) Expand all Loading... |
179 treeItem.label = treeItem.bookmarkNode.title = changeInfo.title; | 181 treeItem.label = treeItem.bookmarkNode.title = changeInfo.title; |
180 }, | 182 }, |
181 | 183 |
182 /** | 184 /** |
183 * @param {string} id | 185 * @param {string} id |
184 * @param {ReorderInfo} reorderInfo | 186 * @param {ReorderInfo} reorderInfo |
185 */ | 187 */ |
186 handleChildrenReordered: function(id, reorderInfo) { | 188 handleChildrenReordered: function(id, reorderInfo) { |
187 var parentItem = treeLookup[id]; | 189 var parentItem = treeLookup[id]; |
188 // The tree only contains folders. | 190 // The tree only contains folders. |
189 var dirIds = reorderInfo.childIds.filter(function(id) { | 191 var dirIds = reorderInfo.childIds |
190 return id in treeLookup; | 192 .filter(function(id) { |
191 }).forEach(function(id, i) { | 193 return id in treeLookup; |
192 parentItem.addAt(treeLookup[id], i); | 194 }) |
193 }); | 195 .forEach(function(id, i) { |
| 196 parentItem.addAt(treeLookup[id], i); |
| 197 }); |
194 }, | 198 }, |
195 | 199 |
196 handleCreated: function(id, bookmarkNode) { | 200 handleCreated: function(id, bookmarkNode) { |
197 if (bmm.isFolder(bookmarkNode)) { | 201 if (bmm.isFolder(bookmarkNode)) { |
198 var parentItem = treeLookup[bookmarkNode.parentId]; | 202 var parentItem = treeLookup[bookmarkNode.parentId]; |
199 var newItem = new BookmarkTreeItem(bookmarkNode); | 203 var newItem = new BookmarkTreeItem(bookmarkNode); |
200 addTreeItem(parentItem, newItem); | 204 addTreeItem(parentItem, newItem); |
201 } | 205 } |
202 }, | 206 }, |
203 | 207 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 * @return {BookmarkTreeNode} The bookmark tree node or null if not found. | 246 * @return {BookmarkTreeNode} The bookmark tree node or null if not found. |
243 */ | 247 */ |
244 getBookmarkNodeById: function(id) { | 248 getBookmarkNodeById: function(id) { |
245 var treeItem = treeLookup[id]; | 249 var treeItem = treeLookup[id]; |
246 if (treeItem) | 250 if (treeItem) |
247 return treeItem.bookmarkNode; | 251 return treeItem.bookmarkNode; |
248 return null; | 252 return null; |
249 }, | 253 }, |
250 | 254 |
251 /** | 255 /** |
252 * Returns the selected bookmark folder node as an array. | 256 * Returns the selected bookmark folder node as an array. |
253 * @type {!Array} Array of bookmark nodes. | 257 * @type {!Array} Array of bookmark nodes. |
254 */ | 258 */ |
255 get selectedFolders() { | 259 get selectedFolders() { |
256 return this.selectedItem && this.selectedItem.bookmarkNode ? | 260 return this.selectedItem && this.selectedItem.bookmarkNode ? |
257 [this.selectedItem.bookmarkNode] : []; | 261 [this.selectedItem.bookmarkNode] : |
258 }, | 262 []; |
| 263 }, |
259 | 264 |
260 /** | 265 /** |
261 * Fetches the bookmark items and builds the tree control. | 266 * Fetches the bookmark items and builds the tree control. |
262 */ | 267 */ |
263 reload: function() { | 268 reload: function() { |
264 /** | 269 /** |
265 * Recursive helper function that adds all the directories to the | 270 * Recursive helper function that adds all the directories to the |
266 * parentTreeItem. | 271 * parentTreeItem. |
267 * @param {!cr.ui.Tree|!cr.ui.TreeItem} parentTreeItem The parent tree | 272 * @param {!cr.ui.Tree|!cr.ui.TreeItem} parentTreeItem The parent tree |
268 * element to append to. | 273 * element to append to. |
269 * @param {!Array<BookmarkTreeNode>} bookmarkNodes A list of bookmark | 274 * @param {!Array<BookmarkTreeNode>} bookmarkNodes A list of bookmark |
270 * nodes to be added. | 275 * nodes to be added. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 Tree.prototype.remove.call(this, child); | 315 Tree.prototype.remove.call(this, child); |
311 if (child.bookmarkNode) | 316 if (child.bookmarkNode) |
312 delete treeLookup[child.bookmarkNode.id]; | 317 delete treeLookup[child.bookmarkNode.id]; |
313 } | 318 } |
314 }; | 319 }; |
315 | 320 |
316 return { | 321 return { |
317 BookmarkTree: BookmarkTree, | 322 BookmarkTree: BookmarkTree, |
318 BookmarkTreeItem: BookmarkTreeItem, | 323 BookmarkTreeItem: BookmarkTreeItem, |
319 treeLookup: treeLookup, | 324 treeLookup: treeLookup, |
320 tree: /** @type {Element} */(null), // Set when decorated. | 325 tree: /** @type {Element} */ (null), // Set when decorated. |
321 ROOT_ID: ROOT_ID | 326 ROOT_ID: ROOT_ID |
322 }; | 327 }; |
323 }); | 328 }); |
OLD | NEW |