OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 cr.define('bmm', function() { | 5 cr.define('bmm', function() { |
6 const Promise = cr.Promise; | 6 const Promise = cr.Promise; |
7 | 7 |
8 /** | 8 /** |
9 * Whether a node contains another node. | 9 * Whether a node contains another node. |
10 * @param {!BookmarkTreeNode} parent | 10 * @param {!BookmarkTreeNode} parent |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 /** | 56 /** |
57 * Loads the entire bookmark tree and returns a {@code cr.Promise} that will | 57 * Loads the entire bookmark tree and returns a {@code cr.Promise} that will |
58 * be fulfilled when done. This reuses multiple loads so that we do not load | 58 * be fulfilled when done. This reuses multiple loads so that we do not load |
59 * the same tree more than once at the same time. | 59 * the same tree more than once at the same time. |
60 * @return {!cr.Promise} The future promise for the load. | 60 * @return {!cr.Promise} The future promise for the load. |
61 */ | 61 */ |
62 function loadTree() { | 62 function loadTree() { |
63 return loadSubtree(''); | 63 return loadSubtree(''); |
64 } | 64 } |
65 | 65 |
| 66 var bookmarkCache = { |
| 67 /** |
| 68 * Removes the cached item from both the list and tree lookups. |
| 69 */ |
| 70 remove: function(id) { |
| 71 var treeItem = bmm.treeLookup[id]; |
| 72 if (treeItem) { |
| 73 var items = treeItem.items; // is an HTMLCollection |
| 74 for (var i = 0; i < items.length; ++i) { |
| 75 var item = items[i]; |
| 76 var bookmarkNode = item.bookmarkNode; |
| 77 delete bmm.treeLookup[bookmarkNode.id]; |
| 78 } |
| 79 delete bmm.treeLookup[id]; |
| 80 } |
| 81 }, |
| 82 |
| 83 /** |
| 84 * Updates the underlying bookmark node for the tree items and list items by |
| 85 * querying the bookmark backend. |
| 86 * @param {string} id The id of the node to update the children for. |
| 87 * @param {Function=} opt_f A funciton to call when done. |
| 88 */ |
| 89 updateChildren: function(id, opt_f) { |
| 90 function updateItem(bookmarkNode) { |
| 91 var treeItem = bmm.treeLookup[bookmarkNode.id]; |
| 92 if (treeItem) { |
| 93 treeItem.bookmarkNode = bookmarkNode; |
| 94 } |
| 95 } |
| 96 |
| 97 chrome.bookmarks.getChildren(id, function(children) { |
| 98 if (children) |
| 99 children.forEach(updateItem); |
| 100 |
| 101 if (opt_f) |
| 102 opt_f(children); |
| 103 }); |
| 104 } |
| 105 }; |
| 106 |
| 107 /** |
| 108 * Called when the title of a bookmark changes. |
| 109 * @param {string} id |
| 110 * @param {!Object} changeInfo |
| 111 */ |
| 112 function handleBookmarkChanged(id, changeInfo) { |
| 113 if (bmm.tree) |
| 114 bmm.tree.handleBookmarkChanged(id, changeInfo); |
| 115 if (bmm.list) |
| 116 bmm.list.handleBookmarkChanged(id, changeInfo); |
| 117 } |
| 118 |
| 119 /** |
| 120 * Callback for when the user reorders by title. |
| 121 * @param {string} id The id of the bookmark folder that was reordered. |
| 122 * @param {!Object} reorderInfo The information about how the items where |
| 123 * reordered. |
| 124 */ |
| 125 function handleChildrenReordered(id, reorderInfo) { |
| 126 if (bmm.tree) |
| 127 bmm.tree.handleChildrenReordered(id, changeInfo); |
| 128 if (bmm.list) |
| 129 bmm.list.handleChildrenReordered(id, changeInfo); |
| 130 bookmarkCache.updateChildren(id); |
| 131 } |
| 132 |
| 133 /** |
| 134 * Callback for when a bookmark node is created. |
| 135 * @param {string} id The id of the newly created bookmark node. |
| 136 * @param {!Object} bookmarkNode The new bookmark node. |
| 137 */ |
| 138 function handleCreated(id, bookmarkNode) { |
| 139 if (bmm.list) |
| 140 bmm.list.handleCreated(id, bookmarkNode); |
| 141 if (bmm.tree) |
| 142 bmm.tree.handleCreated(id, bookmarkNode); |
| 143 bookmarkCache.updateChildren(bookmarkNode.parentId); |
| 144 } |
| 145 |
| 146 /** |
| 147 * Callback for when a bookmark node is moved. |
| 148 * @param {string} id The id of the moved bookmark node. |
| 149 * @param {!Object} moveInfo The information about move. |
| 150 */ |
| 151 function handleMoved(id, moveInfo) { |
| 152 if (bmm.list) |
| 153 bmm.list.handleMoved(id, moveInfo); |
| 154 if (bmm.tree) |
| 155 bmm.tree.handleMoved(id, moveInfo); |
| 156 |
| 157 bookmarkCache.updateChildren(moveInfo.parentId); |
| 158 if (moveInfo.parentId != moveInfo.oldParentId) |
| 159 bookmarkCache.updateChildren(moveInfo.oldParentId); |
| 160 } |
| 161 |
| 162 /** |
| 163 * Callback for when a bookmark node is removed. |
| 164 * @param {string} id The id of the removed bookmark node. |
| 165 * @param {!Object} bookmarkNode The information about removed. |
| 166 */ |
| 167 function handleRemoved(id, removeInfo) { |
| 168 if (bmm.list) |
| 169 bmm.list.handleRemoved(id, removeInfo); |
| 170 if (bmm.tree) |
| 171 bmm.tree.handleRemoved(id, removeInfo); |
| 172 |
| 173 bookmarkCache.updateChildren(removeInfo.parentId); |
| 174 bookmarkCache.remove(id); |
| 175 } |
| 176 |
| 177 /** |
| 178 * Callback for when importing bookmark is started. |
| 179 */ |
| 180 function handleImportBegan() { |
| 181 chrome.bookmarks.onCreated.removeListener(handleCreated); |
| 182 chrome.bookmarks.onChanged.removeListener(handleBookmarkChanged); |
| 183 } |
| 184 |
| 185 /** |
| 186 * Callback for when importing bookmark node is finished. |
| 187 */ |
| 188 function handleImportEnded() { |
| 189 // When importing is done we reload the tree and the list. |
| 190 |
| 191 function f() { |
| 192 bmm.tree.removeEventListener('load', f); |
| 193 |
| 194 chrome.bookmarks.onCreated.addListener(handleCreated); |
| 195 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged); |
| 196 |
| 197 if (!bmm.list) |
| 198 return; |
| 199 |
| 200 if (bmm.list.selectImportedFolder) { |
| 201 var otherBookmarks = bmm.tree.items[1].items; |
| 202 var importedFolder = otherBookmarks[otherBookmarks.length - 1]; |
| 203 navigateTo(importedFolder.bookmarkId) |
| 204 bmm.list.selectImportedFolder = false |
| 205 } else { |
| 206 bmm.list.reload(); |
| 207 } |
| 208 } |
| 209 |
| 210 if (bmm.tree) { |
| 211 bmm.treeaddEventListener('load', f); |
| 212 bmm.tree.reload(); |
| 213 } |
| 214 } |
| 215 |
| 216 /** |
| 217 * Adds the listeners for the bookmark model change events. |
| 218 */ |
| 219 function addBookmarkModelListeners() { |
| 220 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged); |
| 221 chrome.bookmarks.onChildrenReordered.addListener(handleChildrenReordered); |
| 222 chrome.bookmarks.onCreated.addListener(handleCreated); |
| 223 chrome.bookmarks.onMoved.addListener(handleMoved); |
| 224 chrome.bookmarks.onRemoved.addListener(handleRemoved); |
| 225 chrome.bookmarks.onImportBegan.addListener(handleImportBegan); |
| 226 chrome.bookmarks.onImportEnded.addListener(handleImportEnded); |
| 227 }; |
| 228 |
66 return { | 229 return { |
67 contains: contains, | 230 contains: contains, |
68 isFolder: isFolder, | 231 isFolder: isFolder, |
69 loadSubtree: loadSubtree, | 232 loadSubtree: loadSubtree, |
70 loadTree: loadTree | 233 loadTree: loadTree, |
| 234 addBookmarkModelListeners: addBookmarkModelListeners |
71 }; | 235 }; |
72 }); | 236 }); |
OLD | NEW |