OLD | NEW |
(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 cr.define('bmm', function() { |
| 6 const TreeIterator = bmm.TreeIterator; |
| 7 const Promise = cr.Promise; |
| 8 |
| 9 /** |
| 10 * Whether a node contains another node. |
| 11 * @param {!BookmarkTreeNode} parent |
| 12 * @param {!BookmarkTreeNode} descendant |
| 13 * @return {boolean} Whether the parent contains the descendant. |
| 14 */ |
| 15 function contains(parent, descendant) { |
| 16 if (descendant.parentId == parent.id) |
| 17 return true; |
| 18 // the bmm.treeLookup contains all folders |
| 19 var parentTreeItem = bmm.treeLookup[descendant.parentId]; |
| 20 if (!parentTreeItem || !parentTreeItem.bookmarkNode) |
| 21 return false; |
| 22 return this.contains(parent, parentTreeItem.bookmarkNode); |
| 23 } |
| 24 |
| 25 /** |
| 26 * @param {!BookmarkTreeNode} node The node to test. |
| 27 * @return {boolean} Whether a bookmark node is a folder. |
| 28 */ |
| 29 function isFolder(node) { |
| 30 return !('url' in node); |
| 31 } |
| 32 |
| 33 var loadingPromise; |
| 34 |
| 35 /** |
| 36 * Loads the entire bookmark tree and returns a {@code cr.Promise} that will |
| 37 * be fulfilled when done. This reuses multiple loads so that we never load |
| 38 * more than one tree at the same time. |
| 39 * @return {!cr.Promise} The future promise for the load. |
| 40 */ |
| 41 function loadTree() { |
| 42 var p = new Promise; |
| 43 if (!loadingPromise) { |
| 44 loadingPromise = new Promise; |
| 45 chrome.bookmarks.getTree(function(nodes) { |
| 46 loadingPromise.value = nodes[0]; |
| 47 loadingPromise = null; |
| 48 }); |
| 49 } |
| 50 loadingPromise.addListener(function(n) { |
| 51 p.value = n; |
| 52 }); |
| 53 return p; |
| 54 } |
| 55 |
| 56 /** |
| 57 * Helper function for {@code loadSubtree}. This does an in order search of |
| 58 * the tree. |
| 59 * @param {!BookmarkTreeNode} node The node to start searching at. |
| 60 * @param {string} id The ID of the node to find. |
| 61 * @return {BookmarkTreeNode} The found node or null if not found. |
| 62 */ |
| 63 function findNode(node, id) { |
| 64 var it = new TreeIterator(node); |
| 65 var n; |
| 66 while (it.moveNext()) { |
| 67 n = it.current; |
| 68 if (n.id == id) |
| 69 return n; |
| 70 } |
| 71 return null; |
| 72 } |
| 73 |
| 74 /** |
| 75 * Loads a subtree of the bookmark tree and returns a {@code cr.Promise} that |
| 76 * will be fulfilled when done. This reuses multiple loads so that we never |
| 77 * load more than one tree at the same time. (This actually loads the entire |
| 78 * tree but it will only return the relevant subtree in the value of the |
| 79 * future promise.) |
| 80 * @return {!cr.Promise} The future promise for the load. |
| 81 */ |
| 82 function loadSubtree(id) { |
| 83 var p = new Promise; |
| 84 var lp = loadTree(); |
| 85 lp.addListener(function(tree) { |
| 86 var node = findNode(tree, id); |
| 87 p.value = node || Error('Failed to load subtree ' + id); |
| 88 }); |
| 89 return p; |
| 90 } |
| 91 |
| 92 return { |
| 93 contains: contains, |
| 94 isFolder: isFolder, |
| 95 loadSubtree: loadSubtree, |
| 96 loadTree: loadTree |
| 97 }; |
| 98 }); |
OLD | NEW |