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 const BookmarkList = bmm.BookmarkList; | 5 const BookmarkList = bmm.BookmarkList; |
6 const BookmarkTree = bmm.BookmarkTree; | 6 const BookmarkTree = bmm.BookmarkTree; |
7 const ListItem = cr.ui.ListItem; | 7 const ListItem = cr.ui.ListItem; |
8 const TreeItem = cr.ui.TreeItem; | 8 const TreeItem = cr.ui.TreeItem; |
9 const LinkKind = cr.LinkKind; | 9 const LinkKind = cr.LinkKind; |
10 const Command = cr.ui.Command; | 10 const Command = cr.ui.Command; |
(...skipping 27 matching lines...) Expand all Loading... |
38 recentTreeItem.label = localStrings.getString('recent'); | 38 recentTreeItem.label = localStrings.getString('recent'); |
39 searchTreeItem.label = localStrings.getString('search'); | 39 searchTreeItem.label = localStrings.getString('search'); |
40 }); | 40 }); |
41 | 41 |
42 /** | 42 /** |
43 * The id of the bookmark root. | 43 * The id of the bookmark root. |
44 * @type {number} | 44 * @type {number} |
45 */ | 45 */ |
46 const ROOT_ID = '0'; | 46 const ROOT_ID = '0'; |
47 | 47 |
48 var bookmarkCache = { | |
49 /** | |
50 * Removes the cached item from both the list and tree lookups. | |
51 */ | |
52 remove: function(id) { | |
53 var treeItem = bmm.treeLookup[id]; | |
54 if (treeItem) { | |
55 var items = treeItem.items; // is an HTMLCollection | |
56 for (var i = 0, item; item = items[i]; i++) { | |
57 var bookmarkNode = item.bookmarkNode; | |
58 delete bmm.treeLookup[bookmarkNode.id]; | |
59 } | |
60 delete bmm.treeLookup[id]; | |
61 } | |
62 }, | |
63 | |
64 /** | |
65 * Updates the underlying bookmark node for the tree items and list items by | |
66 * querying the bookmark backend. | |
67 * @param {string} id The id of the node to update the children for. | |
68 * @param {Function=} opt_f A funciton to call when done. | |
69 */ | |
70 updateChildren: function(id, opt_f) { | |
71 function updateItem(bookmarkNode) { | |
72 var treeItem = bmm.treeLookup[bookmarkNode.id]; | |
73 if (treeItem) { | |
74 treeItem.bookmarkNode = bookmarkNode; | |
75 } | |
76 } | |
77 | |
78 chrome.bookmarks.getChildren(id, function(children) { | |
79 if (children) | |
80 children.forEach(updateItem); | |
81 | |
82 if (opt_f) | |
83 opt_f(children); | |
84 }); | |
85 } | |
86 }; | |
87 | |
88 var splitter = document.querySelector('.main > .splitter'); | 48 var splitter = document.querySelector('.main > .splitter'); |
89 cr.ui.Splitter.decorate(splitter); | 49 cr.ui.Splitter.decorate(splitter); |
90 | 50 |
91 // The splitter persists the size of the left component in the local store. | 51 // The splitter persists the size of the left component in the local store. |
92 if ('treeWidth' in localStorage) | 52 if ('treeWidth' in localStorage) |
93 splitter.previousElementSibling.style.width = localStorage['treeWidth']; | 53 splitter.previousElementSibling.style.width = localStorage['treeWidth']; |
94 splitter.addEventListener('resize', function(e) { | 54 splitter.addEventListener('resize', function(e) { |
95 localStorage['treeWidth'] = splitter.previousElementSibling.style.width; | 55 localStorage['treeWidth'] = splitter.previousElementSibling.style.width; |
96 }); | 56 }); |
97 | 57 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 } | 249 } |
290 | 250 |
291 // Handle the logo button UI. | 251 // Handle the logo button UI. |
292 // When the user clicks the button we should navigate "home" and focus the list. | 252 // When the user clicks the button we should navigate "home" and focus the list. |
293 document.querySelector('button.logo').onclick = function(e) { | 253 document.querySelector('button.logo').onclick = function(e) { |
294 setSearch(''); | 254 setSearch(''); |
295 $('list').focus(); | 255 $('list').focus(); |
296 }; | 256 }; |
297 | 257 |
298 /** | 258 /** |
299 * Called when the title of a bookmark changes. | |
300 * @param {string} id | |
301 * @param {!Object} changeInfo | |
302 */ | |
303 function handleBookmarkChanged(id, changeInfo) { | |
304 list.handleBookmarkChanged(id, changeInfo); | |
305 tree.handleBookmarkChanged(id, changeInfo); | |
306 } | |
307 | |
308 /** | |
309 * Callback for when the user reorders by title. | |
310 * @param {string} id The id of the bookmark folder that was reordered. | |
311 * @param {!Object} reorderInfo The information about how the items where | |
312 * reordered. | |
313 */ | |
314 function handleChildrenReordered(id, reorderInfo) { | |
315 list.handleChildrenReordered(id, reorderInfo); | |
316 tree.handleChildrenReordered(id, reorderInfo); | |
317 bookmarkCache.updateChildren(id); | |
318 } | |
319 | |
320 /** | |
321 * Callback for when a bookmark node is created. | |
322 * @param {string} id The id of the newly created bookmark node. | |
323 * @param {!Object} bookmarkNode The new bookmark node. | |
324 */ | |
325 function handleCreated(id, bookmarkNode) { | |
326 list.handleCreated(id, bookmarkNode); | |
327 tree.handleCreated(id, bookmarkNode); | |
328 bookmarkCache.updateChildren(bookmarkNode.parentId); | |
329 } | |
330 | |
331 function handleMoved(id, moveInfo) { | |
332 list.handleMoved(id, moveInfo); | |
333 tree.handleMoved(id, moveInfo); | |
334 | |
335 bookmarkCache.updateChildren(moveInfo.parentId); | |
336 if (moveInfo.parentId != moveInfo.oldParentId) | |
337 bookmarkCache.updateChildren(moveInfo.oldParentId); | |
338 } | |
339 | |
340 function handleRemoved(id, removeInfo) { | |
341 list.handleRemoved(id, removeInfo); | |
342 tree.handleRemoved(id, removeInfo); | |
343 | |
344 bookmarkCache.updateChildren(removeInfo.parentId); | |
345 bookmarkCache.remove(id); | |
346 } | |
347 | |
348 function handleImportBegan() { | |
349 chrome.bookmarks.onCreated.removeListener(handleCreated); | |
350 chrome.bookmarks.onChanged.removeListener(handleBookmarkChanged); | |
351 } | |
352 | |
353 function handleImportEnded() { | |
354 // When importing is done we reload the tree and the list. | |
355 | |
356 function f() { | |
357 tree.removeEventListener('load', f); | |
358 | |
359 chrome.bookmarks.onCreated.addListener(handleCreated); | |
360 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged); | |
361 | |
362 if (list.selectImportedFolder) { | |
363 var otherBookmarks = tree.items[1].items; | |
364 var importedFolder = otherBookmarks[otherBookmarks.length - 1]; | |
365 navigateTo(importedFolder.bookmarkId) | |
366 list.selectImportedFolder = false | |
367 } else { | |
368 list.reload(); | |
369 } | |
370 } | |
371 | |
372 tree.addEventListener('load', f); | |
373 tree.reload(); | |
374 } | |
375 | |
376 /** | |
377 * Adds the listeners for the bookmark model change events. | |
378 */ | |
379 function addBookmarkModelListeners() { | |
380 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged); | |
381 chrome.bookmarks.onChildrenReordered.addListener(handleChildrenReordered); | |
382 chrome.bookmarks.onCreated.addListener(handleCreated); | |
383 chrome.bookmarks.onMoved.addListener(handleMoved); | |
384 chrome.bookmarks.onRemoved.addListener(handleRemoved); | |
385 chrome.bookmarks.onImportBegan.addListener(handleImportBegan); | |
386 chrome.bookmarks.onImportEnded.addListener(handleImportEnded); | |
387 } | |
388 | |
389 /** | |
390 * This returns the user visible path to the folder where the bookmark is | 259 * This returns the user visible path to the folder where the bookmark is |
391 * located. | 260 * located. |
392 * @param {number} parentId The ID of the parent folder. | 261 * @param {number} parentId The ID of the parent folder. |
393 * @return {string} The path to the the bookmark, | 262 * @return {string} The path to the the bookmark, |
394 */ | 263 */ |
395 function getFolder(parentId) { | 264 function getFolder(parentId) { |
396 var parentNode = tree.getBookmarkNodeById(parentId); | 265 var parentNode = tree.getBookmarkNodeById(parentId); |
397 if (parentNode) { | 266 if (parentNode) { |
398 var s = parentNode.title; | 267 var s = parentNode.title; |
399 if (parentNode.parentId != ROOT_ID) { | 268 if (parentNode.parentId != ROOT_ID) { |
400 return getFolder(parentNode.parentId) + '/' + s; | 269 return getFolder(parentNode.parentId) + '/' + s; |
401 } | 270 } |
402 return s; | 271 return s; |
403 } | 272 } |
404 } | 273 } |
405 | 274 |
406 tree.addEventListener('load', function(e) { | 275 tree.addEventListener('load', function(e) { |
407 // Add hard coded tree items. | 276 // Add hard coded tree items. |
408 tree.add(recentTreeItem); | 277 tree.add(recentTreeItem); |
409 processHash(); | 278 processHash(); |
410 }); | 279 }); |
411 | 280 |
412 tree.reload(); | 281 tree.reload(); |
413 addBookmarkModelListeners(); | 282 bmm.addBookmarkModelListeners(); |
414 | 283 |
415 var dnd = { | 284 var dnd = { |
416 dragData: null, | 285 dragData: null, |
417 | 286 |
418 getBookmarkElement: function(el) { | 287 getBookmarkElement: function(el) { |
419 while (el && !el.bookmarkNode) { | 288 while (el && !el.bookmarkNode) { |
420 el = el.parentNode; | 289 el = el.parentNode; |
421 } | 290 } |
422 return el; | 291 return el; |
423 }, | 292 }, |
(...skipping 1234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1658 document.addEventListener('copy', handle('copy-command')); | 1527 document.addEventListener('copy', handle('copy-command')); |
1659 document.addEventListener('cut', handle('cut-command')); | 1528 document.addEventListener('cut', handle('cut-command')); |
1660 | 1529 |
1661 var pasteHandler = handle('paste-command'); | 1530 var pasteHandler = handle('paste-command'); |
1662 document.addEventListener('paste', function(e) { | 1531 document.addEventListener('paste', function(e) { |
1663 // Paste is a bit special since we need to do an async call to see if we can | 1532 // Paste is a bit special since we need to do an async call to see if we can |
1664 // paste because the paste command might not be up to date. | 1533 // paste because the paste command might not be up to date. |
1665 updatePasteCommand(pasteHandler); | 1534 updatePasteCommand(pasteHandler); |
1666 }); | 1535 }); |
1667 })(); | 1536 })(); |
OLD | NEW |