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 cr.define('ntp4', function() { | 5 cr.define('ntp4', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 var localStrings = new LocalStrings; | 8 var localStrings = new LocalStrings; |
9 | 9 |
10 /** | 10 /** |
(...skipping 13 matching lines...) Expand all Loading... |
24 el.__proto__ = Bookmark.prototype; | 24 el.__proto__ = Bookmark.prototype; |
25 el.data = data; | 25 el.data = data; |
26 el.initialize(); | 26 el.initialize(); |
27 | 27 |
28 return el; | 28 return el; |
29 } | 29 } |
30 | 30 |
31 Bookmark.prototype = { | 31 Bookmark.prototype = { |
32 __proto__: HTMLDivElement.prototype, | 32 __proto__: HTMLDivElement.prototype, |
33 | 33 |
| 34 /** |
| 35 * Initialize the bookmark object. |
| 36 */ |
34 initialize: function() { | 37 initialize: function() { |
35 var id = tileId++; | 38 var id = tileId++; |
36 this.id = 'bookmark_tile_' + id; | 39 this.id = 'bookmark_tile_' + id; |
37 | 40 |
38 var title = this.querySelector('.title'); | 41 var title = this.querySelector('.title'); |
39 title.textContent = this.data.title; | 42 title.textContent = this.data.title; |
40 | 43 |
41 if (this.data.url) { | 44 if (this.data.url) { |
42 var button = this.querySelector('.button'); | 45 var button = this.querySelector('.button'); |
43 button.href = title.href = this.data.url; | 46 button.href = title.href = this.data.url; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 var el = cr.doc.createElement('div'); | 118 var el = cr.doc.createElement('div'); |
116 el.__proto__ = BookmarkTitle.prototype; | 119 el.__proto__ = BookmarkTitle.prototype; |
117 el.initialize(data); | 120 el.initialize(data); |
118 | 121 |
119 return el; | 122 return el; |
120 } | 123 } |
121 | 124 |
122 BookmarkTitle.prototype = { | 125 BookmarkTitle.prototype = { |
123 __proto__: HTMLDivElement.prototype, | 126 __proto__: HTMLDivElement.prototype, |
124 | 127 |
| 128 /** |
| 129 * Initialize the bookmark title object. |
| 130 */ |
125 initialize: function(data) { | 131 initialize: function(data) { |
126 this.className = 'title-crumb'; | 132 this.className = 'title-crumb'; |
127 this.folderId_ = data.id; | 133 this.folderId = data.id; |
128 this.textContent = data.parentId ? data.title : | 134 this.textContent = data.parentId ? data.title : |
129 localStrings.getString('bookmarksPage'); | 135 localStrings.getString('bookmarksPage'); |
130 | 136 |
131 this.addEventListener('click', this.handleClick_); | 137 this.addEventListener('click', this.handleClick_); |
132 }, | 138 }, |
133 | 139 |
134 /** | 140 /** |
135 * Invoked when a bookmark title is clicked | 141 * Invoked when a bookmark title is clicked |
136 * @param {Event} e The click event. | 142 * @param {Event} e The click event. |
137 * @private | 143 * @private |
138 */ | 144 */ |
139 handleClick_: function(e) { | 145 handleClick_: function(e) { |
140 chrome.send('getBookmarksData', [this.folderId_]); | 146 chrome.send('getBookmarksData', [this.folderId]); |
141 }, | 147 }, |
142 }; | 148 }; |
143 | 149 |
144 var TilePage = ntp4.TilePage; | 150 var TilePage = ntp4.TilePage; |
145 | 151 |
146 var bookmarksPageGridValues = { | 152 var bookmarksPageGridValues = { |
147 // The fewest tiles we will show in a row. | 153 // The fewest tiles we will show in a row. |
148 minColCount: 3, | 154 minColCount: 3, |
149 // The most tiles we will show in a row. | 155 // The most tiles we will show in a row. |
150 maxColCount: 6, | 156 maxColCount: 6, |
(...skipping 14 matching lines...) Expand all Loading... |
165 var el = new TilePage(bookmarksPageGridValues); | 171 var el = new TilePage(bookmarksPageGridValues); |
166 el.__proto__ = BookmarksPage.prototype; | 172 el.__proto__ = BookmarksPage.prototype; |
167 el.initialize(); | 173 el.initialize(); |
168 | 174 |
169 return el; | 175 return el; |
170 } | 176 } |
171 | 177 |
172 BookmarksPage.prototype = { | 178 BookmarksPage.prototype = { |
173 __proto__: TilePage.prototype, | 179 __proto__: TilePage.prototype, |
174 | 180 |
| 181 /** |
| 182 * Initialize the bookmarks page object. |
| 183 */ |
175 initialize: function() { | 184 initialize: function() { |
176 this.classList.add('bookmarks-page'); | 185 this.classList.add('bookmarks-page'); |
177 this.insertBefore($('bookmarks-title-wrapper'), this.firstChild); | 186 this.insertBefore($('bookmarks-title-wrapper'), this.firstChild); |
178 }, | 187 }, |
179 | 188 |
180 /** | 189 /** |
181 * Build the bookmark titles bar (ie, navigation hiearchy). | 190 * Build the bookmark titles bar (ie, navigation hiearchy). |
182 * @param {Array} items The parent hiearchy of the current folder. | 191 * @param {Array} items The parent hiearchy of the current folder. |
183 * @private | 192 * @private |
184 */ | 193 */ |
(...skipping 19 matching lines...) Expand all Loading... |
204 * Build the bookmark tiles. | 213 * Build the bookmark tiles. |
205 * @param {Array} items The contents of the current folder. | 214 * @param {Array} items The contents of the current folder. |
206 * @private | 215 * @private |
207 */ | 216 */ |
208 updateBookmarkTiles_: function(items) { | 217 updateBookmarkTiles_: function(items) { |
209 this.removeAllTiles(); | 218 this.removeAllTiles(); |
210 for (var i = 0; i < items.length; i++) | 219 for (var i = 0; i < items.length; i++) |
211 this.appendTile(new Bookmark(items[i]), false); | 220 this.appendTile(new Bookmark(items[i]), false); |
212 }, | 221 }, |
213 | 222 |
| 223 /** |
| 224 * Determine whether a bookmark ID matches a folder in the current |
| 225 * hierarchy. |
| 226 * @param {string} id The bookmark ID to search for. |
| 227 * @private |
| 228 */ |
| 229 isBookmarkInParentHierarchy_: function(id) { |
| 230 var titlesWrapper = $('bookmarks-title-wrapper'); |
| 231 var titles = titlesWrapper.querySelectorAll('.title-crumb'); |
| 232 for (var i = 0; i < titles.length; i++) { |
| 233 var bookmarkTitle = titles[i]; |
| 234 if (bookmarkTitle.folderId == id) { |
| 235 return true; |
| 236 } |
| 237 } |
| 238 return false; |
| 239 }, |
| 240 |
214 /** @inheritDoc */ | 241 /** @inheritDoc */ |
215 shouldAcceptDrag: function(dataTransfer) { | 242 shouldAcceptDrag: function(dataTransfer) { |
216 return false; | 243 return false; |
217 }, | 244 }, |
218 | 245 |
219 /** | 246 /** |
| 247 * Invoked before a batch import begins. We will ignore added/changed |
| 248 * notifications while the operation is in progress. |
| 249 */ |
| 250 bookmarkImportBegan: function() { |
| 251 this.importing = true; |
| 252 }, |
| 253 |
| 254 /** |
| 255 * Invoked after a batch import finishes. We will reload the bookmarks |
| 256 * page to reflect the new state. |
| 257 */ |
| 258 bookmarkImportEnded: function() { |
| 259 this.importing = false; |
| 260 chrome.send('getBookmarksData', []); |
| 261 }, |
| 262 |
| 263 /** |
| 264 * Invoked when a node has been added. |
| 265 * @param {string} id The id of the newly created bookmark node. |
| 266 * @param {Object} bookmark The new bookmark node. |
| 267 */ |
| 268 bookmarkNodeAdded: function(id, bookmark) { |
| 269 if (this.importing) return; |
| 270 if (this.id == bookmark.parentId) |
| 271 this.addTileAt(new Bookmark(bookmark), bookmark.index, false); |
| 272 }, |
| 273 |
| 274 /** |
| 275 * Invoked when the title or url of a node changes. |
| 276 * @param {string} id The id of the changed node. |
| 277 * @param {Object} changeInfo Details of the changed node. |
| 278 */ |
| 279 bookmarkNodeChanged: function(id, changeInfo) { |
| 280 if (this.importing) return; |
| 281 |
| 282 // If the current folder or parent is being re-named, reload the page. |
| 283 // TODO(csilv): Optimize this to reload just the titles. |
| 284 if (this.isBookmarkInParentHierarchy_(id)) { |
| 285 chrome.send('getBookmarksData', [this.id]); |
| 286 return; |
| 287 } |
| 288 |
| 289 // If the target item is contained in this folder, update just that item. |
| 290 for (var i = 0; i < this.tiles.length; i++) { |
| 291 var tile = this.tiles[i]; |
| 292 var data = tile.firstChild.data; |
| 293 |
| 294 if (data.id == id) { |
| 295 data.title = changeInfo.title; |
| 296 var title = tile.querySelector('.title'); |
| 297 title.textContent = data.title; |
| 298 |
| 299 if (changeInfo.url) { |
| 300 data.url = changeInfo.url; |
| 301 var button = tile.querySelector('.button'); |
| 302 button.href = title.href = data.url; |
| 303 } |
| 304 break; |
| 305 } |
| 306 } |
| 307 }, |
| 308 |
| 309 /** |
| 310 * Invoked when the children (just direct children, not descendants) of |
| 311 * a folder have been reordered in some way, such as sorted. |
| 312 * @param {string} id The id of the reordered bookmark node. |
| 313 * @param {!Object} reorderInfo Details of the reordered bookmark node. |
| 314 */ |
| 315 bookmarkNodeChildrenReordered: function(id, reorderInfo) { |
| 316 if (this.id == id) |
| 317 chrome.send('getBookmarksData', [this.id]); |
| 318 }, |
| 319 |
| 320 /** |
| 321 * Invoked when a node has moved. |
| 322 * @param {string} id The id of the moved bookmark node. |
| 323 * @param {!Object} moveInfo Details of the moved bookmark. |
| 324 */ |
| 325 bookmarkNodeMoved: function(id, moveInfo) { |
| 326 // TODO(csilv): Optimize this by doing less than reloading the folder. |
| 327 // Reload the current page if the target item is the current folder |
| 328 // or a parent of the current folder. |
| 329 if (this.isBookmarkInParentHierarchy_(id)) { |
| 330 chrome.send('getBookmarksData', [this.id]); |
| 331 return; |
| 332 } |
| 333 |
| 334 // Reload the current page if the target item is being moved to/from the |
| 335 // current folder. |
| 336 if (this.id == moveInfo.parentId || |
| 337 this.id == moveInfo.oldParentId) { |
| 338 chrome.send('getBookmarksData', [this.id]); |
| 339 } |
| 340 }, |
| 341 |
| 342 /** |
| 343 * Invoked when a node has been removed from a folder. |
| 344 * @param {string} id The id of the removed bookmark node. |
| 345 * @param {!Object} removeInfo Details of the removed bookmark node. |
| 346 */ |
| 347 bookmarkNodeRemoved: function(id, removeInfo) { |
| 348 // If the target item is the visibile folder or a parent folder, load |
| 349 // the parent of the removed item. |
| 350 if (this.isBookmarkInParentHierarchy_(id)) { |
| 351 chrome.send('getBookmarksData', [removeInfo.parentId]); |
| 352 return; |
| 353 } |
| 354 |
| 355 // If the target item is contained in the visible folder, find the |
| 356 // matching tile and delete it. |
| 357 if (this.id == removeInfo.parentId) { |
| 358 for (var i = 0; i < this.tiles.length; i++) { |
| 359 var tile = this.tiles[i]; |
| 360 if (tile.firstChild.data.id == id) { |
| 361 this.removeTile(tile, false); |
| 362 break; |
| 363 } |
| 364 } |
| 365 } |
| 366 }, |
| 367 |
| 368 /** |
220 * Set the bookmark data that should be displayed, replacing any existing | 369 * Set the bookmark data that should be displayed, replacing any existing |
221 * data. | 370 * data. |
| 371 * @param {Object} data Data that shoudl be displayed. Contains arrays |
| 372 * 'items' and 'navigationItems'. |
222 */ | 373 */ |
223 set data(data) { | 374 set data(data) { |
| 375 this.id = data.navigationItems[0].id; |
224 this.updateBookmarkTiles_(data.items); | 376 this.updateBookmarkTiles_(data.items); |
225 this.updateBookmarkTitles_(data.navigationItems); | 377 this.updateBookmarkTitles_(data.navigationItems); |
226 }, | 378 }, |
227 }; | 379 }; |
228 | 380 |
229 /** | 381 /** |
230 * Initializes and renders the bookmark chevron canvas. This needs to be | 382 * Initializes and renders the bookmark chevron canvas. This needs to be |
231 * performed after the page has been loaded so that we have access to the | 383 * performed after the page has been loaded so that we have access to the |
232 * style sheet values. | 384 * style sheet values. |
233 */ | 385 */ |
(...skipping 24 matching lines...) Expand all Loading... |
258 }; | 410 }; |
259 | 411 |
260 return { | 412 return { |
261 BookmarksPage: BookmarksPage, | 413 BookmarksPage: BookmarksPage, |
262 initBookmarkChevron: initBookmarkChevron, | 414 initBookmarkChevron: initBookmarkChevron, |
263 setBookmarksFaviconDominantColor: setBookmarksFaviconDominantColor | 415 setBookmarksFaviconDominantColor: setBookmarksFaviconDominantColor |
264 }; | 416 }; |
265 }); | 417 }); |
266 | 418 |
267 window.addEventListener('load', ntp4.initBookmarkChevron); | 419 window.addEventListener('load', ntp4.initBookmarkChevron); |
OLD | NEW |