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, item; item = items[i]; i++) { | |
mazda
2011/11/09 04:49:43
for (var i = 0; i < items.length; ++i) {
yoshiki
2011/11/09 15:06:39
Done.
| |
75 var bookmarkNode = item.bookmarkNode; | |
76 delete bmm.treeLookup[bookmarkNode.id]; | |
77 } | |
78 delete bmm.treeLookup[id]; | |
79 } | |
80 }, | |
81 | |
82 /** | |
83 * Updates the underlying bookmark node for the tree items and list items by | |
84 * querying the bookmark backend. | |
85 * @param {string} id The id of the node to update the children for. | |
86 * @param {Function=} opt_f A funciton to call when done. | |
87 */ | |
88 updateChildren: function(id, opt_f) { | |
89 function updateItem(bookmarkNode) { | |
90 var treeItem = bmm.treeLookup[bookmarkNode.id]; | |
91 if (treeItem) { | |
92 treeItem.bookmarkNode = bookmarkNode; | |
93 } | |
94 } | |
95 | |
96 chrome.bookmarks.getChildren(id, function(children) { | |
97 if (children) | |
98 children.forEach(updateItem); | |
99 | |
100 if (opt_f) | |
101 opt_f(children); | |
102 }); | |
103 } | |
104 }; | |
105 | |
106 /** | |
107 * Called when the title of a bookmark changes. | |
108 * @param {string} id | |
109 * @param {!Object} changeInfo | |
110 */ | |
111 function handleBookmarkChanged(id, changeInfo) { | |
112 if (bmm.tree) | |
113 bmm.tree.handleBookmarkChanged(id, changeInfo); | |
114 if (bmm.list) | |
115 bmm.list.handleBookmarkChanged(id, changeInfo); | |
116 } | |
117 | |
118 /** | |
119 * Callback for when the user reorders by title. | |
120 * @param {string} id The id of the bookmark folder that was reordered. | |
121 * @param {!Object} reorderInfo The information about how the items where | |
122 * reordered. | |
123 */ | |
124 function handleChildrenReordered(id, reorderInfo) { | |
125 if (bmm.tree) | |
126 bmm.tree.handleChildrenReordered(id, changeInfo); | |
127 if (bmm.list) | |
128 bmm.list.handleChildrenReordered(id, changeInfo); | |
129 bookmarkCache.updateChildren(id); | |
130 } | |
131 | |
132 /** | |
133 * Callback for when a bookmark node is created. | |
134 * @param {string} id The id of the newly created bookmark node. | |
135 * @param {!Object} bookmarkNode The new bookmark node. | |
136 */ | |
137 function handleCreated(id, bookmarkNode) { | |
138 // console.info('handleCreated', id, bookmarkNode); | |
mazda
2011/11/09 04:49:43
Please delete this.
yoshiki
2011/11/09 15:06:39
Done.
| |
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 function handleMoved(id, moveInfo) { | |
mazda
2011/11/09 04:49:43
Please add comment.
yoshiki
2011/11/09 15:06:39
Done.
| |
147 // console.info('handleMoved', id, moveInfo); | |
mazda
2011/11/09 04:49:43
Please delete this.
yoshiki
2011/11/09 15:06:39
Done.
| |
148 if (bmm.list) | |
149 bmm.list.handleMoved(id, moveInfo); | |
150 if (bmm.tree) | |
151 bmm.tree.handleMoved(id, moveInfo); | |
152 | |
153 bookmarkCache.updateChildren(moveInfo.parentId); | |
154 if (moveInfo.parentId != moveInfo.oldParentId) | |
155 bookmarkCache.updateChildren(moveInfo.oldParentId); | |
156 } | |
157 | |
158 function handleRemoved(id, removeInfo) { | |
mazda
2011/11/09 04:49:43
Please add comment.
yoshiki
2011/11/09 15:06:39
Done.
| |
159 // console.info('handleRemoved', id, removeInfo); | |
mazda
2011/11/09 04:49:43
Please delete this.
yoshiki
2011/11/09 15:06:39
Done.
| |
160 if (bmm.list) | |
161 bmm.list.handleRemoved(id, removeInfo); | |
162 if (bmm.tree) | |
163 bmm.tree.handleRemoved(id, removeInfo); | |
164 | |
165 bookmarkCache.updateChildren(removeInfo.parentId); | |
166 bookmarkCache.remove(id); | |
167 } | |
168 | |
169 function handleImportBegan() { | |
mazda
2011/11/09 04:49:43
Please add comment.
yoshiki
2011/11/09 15:06:39
Done.
| |
170 chrome.bookmarks.onCreated.removeListener(handleCreated); | |
171 chrome.bookmarks.onChanged.removeListener(handleBookmarkChanged); | |
172 } | |
173 | |
174 function handleImportEnded() { | |
mazda
2011/11/09 04:49:43
Please add comment.
yoshiki
2011/11/09 15:06:39
Done.
| |
175 // When importing is done we reload the tree and the list. | |
176 | |
177 function f() { | |
178 bmm.tree.removeEventListener('load', f); | |
179 | |
180 chrome.bookmarks.onCreated.addListener(handleCreated); | |
181 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged); | |
182 | |
183 if (!bmm.list) | |
184 return; | |
185 | |
186 if (bmm.list.selectImportedFolder) { | |
187 var otherBookmarks = bmm.tree.items[1].items; | |
188 var importedFolder = otherBookmarks[otherBookmarks.length - 1]; | |
189 navigateTo(importedFolder.bookmarkId) | |
190 bmm.list.selectImportedFolder = false | |
191 } else { | |
192 bmm.list.reload(); | |
193 } | |
194 } | |
195 | |
196 if (bmm.tree) { | |
197 bmm.treeaddEventListener('load', f); | |
198 bmm.tree.reload(); | |
199 } | |
200 } | |
201 | |
202 /** | |
203 * Adds the listeners for the bookmark model change events. | |
204 */ | |
205 function addBookmarkModelListeners() { | |
206 chrome.bookmarks.onChanged.addListener(handleBookmarkChanged); | |
207 chrome.bookmarks.onChildrenReordered.addListener(handleChildrenReordered); | |
208 chrome.bookmarks.onCreated.addListener(handleCreated); | |
209 chrome.bookmarks.onMoved.addListener(handleMoved); | |
210 chrome.bookmarks.onRemoved.addListener(handleRemoved); | |
211 chrome.bookmarks.onImportBegan.addListener(handleImportBegan); | |
212 chrome.bookmarks.onImportEnded.addListener(handleImportEnded); | |
213 }; | |
214 | |
66 return { | 215 return { |
67 contains: contains, | 216 contains: contains, |
68 isFolder: isFolder, | 217 isFolder: isFolder, |
69 loadSubtree: loadSubtree, | 218 loadSubtree: loadSubtree, |
70 loadTree: loadTree | 219 loadTree: loadTree, |
220 addBookmarkModelListeners: addBookmarkModelListeners | |
71 }; | 221 }; |
222 | |
mazda
2011/11/09 04:49:43
Unnecessary empty line.
yoshiki
2011/11/09 15:06:39
Done.
| |
72 }); | 223 }); |
OLD | NEW |