OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 /** | 5 /** |
6 * WallpaperManager constructor. | 6 * WallpaperManager constructor. |
7 * | 7 * |
8 * WallpaperManager objects encapsulate the functionality of the wallpaper | 8 * WallpaperManager objects encapsulate the functionality of the wallpaper |
9 * manager extension. | 9 * manager extension. |
10 * | 10 * |
11 * @constructor | 11 * @constructor |
12 * @param {HTMLElement} dialogDom The DOM node containing the prototypical | 12 * @param {HTMLElement} dialogDom The DOM node containing the prototypical |
13 * extension UI. | 13 * extension UI. |
14 */ | 14 */ |
15 | 15 |
16 function WallpaperManager(dialogDom) { | 16 function WallpaperManager(dialogDom) { |
17 this.dialogDom_ = dialogDom; | 17 this.dialogDom_ = dialogDom; |
18 this.storage_ = chrome.storage.local; | 18 this.storage_ = chrome.storage.local; |
19 this.document_ = dialogDom.ownerDocument; | 19 this.document_ = dialogDom.ownerDocument; |
20 this.selectedCategory = null; | 20 this.selectedCategory = null; |
21 this.butterBar_ = new ButterBar(this.dialogDom_); | 21 this.progressManager_ = new ProgressManager(); |
22 this.customWallpaperData_ = null; | 22 this.customWallpaperData_ = null; |
23 this.currentWallpaper_ = null; | 23 this.currentWallpaper_ = null; |
24 this.wallpaperRequest_ = null; | 24 this.wallpaperRequest_ = null; |
25 this.checkmark_ = cr.doc.createElement('div'); | |
26 this.checkmark_.classList.add('check'); | |
25 this.fetchManifest_(); | 27 this.fetchManifest_(); |
26 } | 28 } |
27 | 29 |
28 // Anonymous 'namespace'. | 30 // Anonymous 'namespace'. |
29 // TODO(bshe): Get rid of anonymous namespace. | 31 // TODO(bshe): Get rid of anonymous namespace. |
30 (function() { | 32 (function() { |
31 | 33 |
32 /** | 34 /** |
33 * Base URL of the manifest file. | 35 * Base URL of the manifest file. |
34 */ | 36 */ |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
149 this.storage_.set(items, function() {}); | 151 this.storage_.set(items, function() {}); |
150 this.initDom_(); | 152 this.initDom_(); |
151 }; | 153 }; |
152 | 154 |
153 // Sets manifest to previously saved object if any and shows connection error. | 155 // Sets manifest to previously saved object if any and shows connection error. |
154 // Called after manifest failed to load. | 156 // Called after manifest failed to load. |
155 WallpaperManager.prototype.onLoadManifestFailed_ = function() { | 157 WallpaperManager.prototype.onLoadManifestFailed_ = function() { |
156 var self = this; | 158 var self = this; |
157 this.storage_.get(AccessManifestKey, function(items) { | 159 this.storage_.get(AccessManifestKey, function(items) { |
158 self.manifest_ = items[AccessManifestKey] ? items[AccessManifestKey] : {}; | 160 self.manifest_ = items[AccessManifestKey] ? items[AccessManifestKey] : {}; |
159 self.butterBar_.showError_(str('connectionFailed'), | 161 // TODO(bshe): Add error message back once we decide how to show error |
160 {help_url: LEARN_MORE_URL}); | 162 // message in the new UI. http://crbug.com/162563 |
161 self.initDom_(); | 163 self.initDom_(); |
162 $('wallpaper-grid').classList.add('image-picker-offline'); | 164 $('wallpaper-grid').classList.add('image-picker-offline'); |
163 }); | 165 }); |
164 }; | 166 }; |
165 | 167 |
166 /** | 168 /** |
167 * One-time initialization of various DOM nodes. | 169 * One-time initialization of various DOM nodes. |
168 */ | 170 */ |
169 WallpaperManager.prototype.initDom_ = function() { | 171 WallpaperManager.prototype.initDom_ = function() { |
170 i18nTemplate.process(this.document_, loadTimeData); | 172 i18nTemplate.process(this.document_, loadTimeData); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
276 }; | 278 }; |
277 | 279 |
278 /** | 280 /** |
279 * Sets wallpaper to the corresponding wallpaper of selected thumbnail. | 281 * Sets wallpaper to the corresponding wallpaper of selected thumbnail. |
280 */ | 282 */ |
281 WallpaperManager.prototype.onThumbnailClicked_ = function() { | 283 WallpaperManager.prototype.onThumbnailClicked_ = function() { |
282 var selectedItem = this.wallpaperGrid_.selectedItem; | 284 var selectedItem = this.wallpaperGrid_.selectedItem; |
283 if (selectedItem && selectedItem.dynamicURL && | 285 if (selectedItem && selectedItem.dynamicURL && |
284 !this.wallpaperGrid_.inProgramSelection) { | 286 !this.wallpaperGrid_.inProgramSelection) { |
285 var wallpaperURL = selectedItem.baseURL + HighResolutionSuffix; | 287 var wallpaperURL = selectedItem.baseURL + HighResolutionSuffix; |
288 var selectedGridItem = this.wallpaperGrid_.getListItem(selectedItem); | |
286 var self = this; | 289 var self = this; |
287 | 290 |
288 chrome.wallpaperPrivate.setWallpaperIfExist(wallpaperURL, | 291 chrome.wallpaperPrivate.setWallpaperIfExist(wallpaperURL, |
289 selectedItem.layout, | 292 selectedItem.layout, |
290 function() { | 293 function() { |
291 if (chrome.runtime.lastError == undefined) { | 294 if (chrome.runtime.lastError == undefined) { |
292 self.currentWallpaper_ = wallpaperURL; | 295 self.currentWallpaper_ = wallpaperURL; |
293 self.setActiveThumb(selectedItem); | 296 self.setActiveThumb(selectedGridItem); |
294 return; | 297 return; |
295 } | 298 } |
296 | 299 |
297 // Falls back to request wallpaper from server. | 300 // Falls back to request wallpaper from server. |
298 if (self.wallpaperRequest_) | 301 if (self.wallpaperRequest_) |
299 self.wallpaperRequest_.abort(); | 302 self.wallpaperRequest_.abort(); |
300 | 303 |
301 self.wallpaperRequest_ = new XMLHttpRequest(); | 304 self.wallpaperRequest_ = new XMLHttpRequest(); |
302 self.wallpaperRequest_.open('GET', wallpaperURL, true); | 305 self.wallpaperRequest_.open('GET', wallpaperURL, true); |
303 self.wallpaperRequest_.responseType = 'arraybuffer'; | 306 self.wallpaperRequest_.responseType = 'arraybuffer'; |
307 self.progressManager_.reset(self.wallpaperRequest_, selectedGridItem); | |
304 self.wallpaperRequest_.send(null); | 308 self.wallpaperRequest_.send(null); |
305 self.butterBar_.setRequest(self.wallpaperRequest_); | |
306 self.wallpaperRequest_.addEventListener('load', function(e) { | 309 self.wallpaperRequest_.addEventListener('load', function(e) { |
307 if (self.wallpaperRequest_.status === 200) { | 310 if (self.wallpaperRequest_.status === 200) { |
308 var image = self.wallpaperRequest_.response; | 311 var image = self.wallpaperRequest_.response; |
309 chrome.wallpaperPrivate.setWallpaper(image, | 312 chrome.wallpaperPrivate.setWallpaper( |
310 selectedItem.layout, | 313 image, |
311 wallpaperURL, | 314 selectedItem.layout, |
312 self.onFinished_.bind(self)); | 315 wallpaperURL, |
316 self.onFinished_.bind(self, selectedGridItem)); | |
313 self.currentWallpaper_ = wallpaperURL; | 317 self.currentWallpaper_ = wallpaperURL; |
314 self.setActiveThumb(selectedItem); | |
315 } else { | 318 } else { |
316 self.butterBar_.showError_(str('downloadFailed'), | 319 // TODO(bshe): Add error message back once we decide how to show |
317 {help_url: LEARN_MORE_URL}); | 320 // error message in the new UI. http://crbug.com/162563 |
318 } | 321 } |
319 self.wallpaperRequest_ = null; | 322 self.wallpaperRequest_ = null; |
320 }); | 323 }); |
321 }); | 324 }); |
322 } | 325 } |
323 this.setWallpaperAttribution_(selectedItem); | 326 this.setWallpaperAttribution_(selectedItem); |
324 }; | 327 }; |
325 | 328 |
326 /** | 329 /** |
327 * Set attributions of wallpaper with given URL. If URL is not valid, clear | 330 * Set attributions of wallpaper with given URL. If URL is not valid, clear |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 /** | 421 /** |
419 * Handles the custom wallpaper which user selected from file manager. Called | 422 * Handles the custom wallpaper which user selected from file manager. Called |
420 * when users select a file. | 423 * when users select a file. |
421 */ | 424 */ |
422 WallpaperManager.prototype.onFileSelectorChanged_ = function() { | 425 WallpaperManager.prototype.onFileSelectorChanged_ = function() { |
423 var files = $('file-selector').files; | 426 var files = $('file-selector').files; |
424 if (files.length != 1) | 427 if (files.length != 1) |
425 console.error('More than one files are selected or no file selected'); | 428 console.error('More than one files are selected or no file selected'); |
426 var file = files[0]; | 429 var file = files[0]; |
427 if (!file.type.match('image/jpeg')) { | 430 if (!file.type.match('image/jpeg')) { |
428 this.butterBar_.showError_(str('invalidWallpaper'), | 431 // TODO(bshe): Add error message back once we decide how to show error |
429 {help_url: LEARN_MORE_URL}); | 432 // message in the new UI. http://crbug.com/162563 |
430 return; | 433 return; |
431 } | 434 } |
432 var reader = new FileReader(); | 435 var reader = new FileReader(); |
433 reader.readAsArrayBuffer(files[0]); | 436 reader.readAsArrayBuffer(files[0]); |
434 var self = this; | 437 var self = this; |
435 reader.addEventListener('error', function(e) { | 438 reader.addEventListener('error', function(e) { |
436 this.butterBar_.showError_(str('accessFileFailure'), | 439 // TODO(bshe): Add error message back once we decide how to show error |
437 {help_url: LEARN_MORE_URL}); | 440 // message in the new UI. http://crbug.com/162563 |
438 }); | 441 }); |
439 reader.addEventListener('load', function(e) { | 442 reader.addEventListener('load', function(e) { |
440 self.customWallpaperData_ = e.target.result; | 443 self.customWallpaperData_ = e.target.result; |
441 self.refreshWallpaper_(self.customWallpaperData_); | 444 self.refreshWallpaper_(self.customWallpaperData_); |
442 }); | 445 }); |
443 this.generateThumbnail_(files[0]); | 446 this.generateThumbnail_(files[0]); |
444 }; | 447 }; |
445 | 448 |
446 /** | 449 /** |
447 * Refreshes the custom wallpaper with the current selected layout. | 450 * Refreshes the custom wallpaper with the current selected layout. |
448 * @param {ArrayBuffer} customWallpaper The raw wallpaper file data. | 451 * @param {ArrayBuffer} customWallpaper The raw wallpaper file data. |
449 */ | 452 */ |
450 WallpaperManager.prototype.refreshWallpaper_ = function(customWallpaper) { | 453 WallpaperManager.prototype.refreshWallpaper_ = function(customWallpaper) { |
451 var setWallpaperLayout = $('set-wallpaper-layout'); | 454 var setWallpaperLayout = $('set-wallpaper-layout'); |
452 var layout = | 455 var layout = |
453 setWallpaperLayout.options[setWallpaperLayout.selectedIndex].value; | 456 setWallpaperLayout.options[setWallpaperLayout.selectedIndex].value; |
454 chrome.wallpaperPrivate.setCustomWallpaper(customWallpaper, | 457 chrome.wallpaperPrivate.setCustomWallpaper(customWallpaper, |
455 layout, | 458 layout, |
456 this.onFinished_.bind(this)); | 459 this.onFinished_.bind(this, |
460 null)); | |
flackr
2013/02/05 01:24:20
Remove the null, this will pass undefined instead.
bshe
2013/02/05 17:33:10
Done.
| |
457 this.currentWallpaper_ = 'CUSTOM'; | 461 this.currentWallpaper_ = 'CUSTOM'; |
458 }; | 462 }; |
459 | 463 |
460 /** | 464 /** |
461 * Sets wallpaper finished. Displays error message in butter bar if any. | 465 * Sets wallpaper finished. Displays error message in butter bar if any. |
466 * @param {WallpaperThumbnailsGridItem} selectedGridItem The wallpaper | |
flackr
2013/02/05 01:24:20
Optional parameter: @param {WallpaperThumbnailsGri
bshe
2013/02/05 17:33:10
Done.
| |
467 * thumbnail grid item. It extends from cr.ui.ListItem. | |
462 */ | 468 */ |
463 WallpaperManager.prototype.onFinished_ = function() { | 469 WallpaperManager.prototype.onFinished_ = function(selectedGridItem) { |
470 if (selectedGridItem) | |
471 this.progressManager_.hide(selectedGridItem); | |
472 | |
464 if (chrome.runtime.lastError != undefined) { | 473 if (chrome.runtime.lastError != undefined) { |
465 this.butterBar_.showError_(chrome.runtime.lastError.message, | 474 // TODO(bshe): Add error message back once we decide how to show error |
466 {help_url: LEARN_MORE_URL}); | 475 // message in the new UI. http://crbug.com/162563 |
467 } else { | 476 } else if (selectedGridItem) { |
468 this.butterBar_.hide_(); | 477 this.setActiveThumb(selectedGridItem); |
469 } | 478 } |
470 }; | 479 }; |
471 | 480 |
472 /** | 481 /** |
473 * Handles the layout setting change of custom wallpaper. | 482 * Handles the layout setting change of custom wallpaper. |
474 */ | 483 */ |
475 WallpaperManager.prototype.onWallpaperLayoutChanged_ = function() { | 484 WallpaperManager.prototype.onWallpaperLayoutChanged_ = function() { |
476 if (this.customWallpaperData_) | 485 if (this.customWallpaperData_) |
477 this.refreshWallpaper_(this.customWallpaperData_); | 486 this.refreshWallpaper_(this.customWallpaperData_); |
478 }; | 487 }; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
541 wallpapersDataModel.push(wallpaperInfo); | 550 wallpapersDataModel.push(wallpaperInfo); |
542 var url = this.manifest_.wallpaper_list[key].base_url + | 551 var url = this.manifest_.wallpaper_list[key].base_url + |
543 HighResolutionSuffix; | 552 HighResolutionSuffix; |
544 if (url == this.currentWallpaper_) { | 553 if (url == this.currentWallpaper_) { |
545 selectedItem = wallpaperInfo; | 554 selectedItem = wallpaperInfo; |
546 } | 555 } |
547 } | 556 } |
548 } | 557 } |
549 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 558 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
550 this.wallpaperGrid_.selectedItem = selectedItem; | 559 this.wallpaperGrid_.selectedItem = selectedItem; |
551 this.setActiveThumb(selectedItem); | 560 this.setActiveThumb(this.wallpaperGrid_.getListItem(selectedItem)); |
552 } | 561 } |
553 }; | 562 }; |
554 | 563 |
555 /** | 564 /** |
556 * Shows a checkmark on the active thumbnail and clears previous active one if | 565 * Shows a checkmark on the active thumbnail and clears previous active one if |
557 * any. Note if wallpaper was not set successfully, checkmark should not show | 566 * any. Note if wallpaper was not set successfully, checkmark should not show |
558 * on that thumbnail. | 567 * on that thumbnail. |
559 * @param {{baseURL: string, dynamicURL: string, layout: string, | 568 * @param {WallpaperThumbnailsGridItem} selectedGridItem The wallpaper |
560 * author: string, authorWebsite: string, availableOffline: boolean}} | 569 * thumbnail grid item. It extends from cr.ui.ListItem. |
561 * activeItem the wallpaper item to active (show checkmark). | |
562 */ | 570 */ |
563 WallpaperManager.prototype.setActiveThumb = function(activeItem) { | 571 WallpaperManager.prototype.setActiveThumb = function(selectedGridItem) { |
564 var activeThumb = $('wallpaper-grid').getListItem(activeItem); | 572 if (!selectedGridItem) |
565 if (!activeThumb) | |
566 return; | 573 return; |
567 // Clears previous checkmark. | 574 // Clears previous checkmark. |
568 var previousActiveThumb = $('wallpaper-grid').querySelector('[active]'); | 575 var previousCheckmark = $('wallpaper-grid').querySelector('.check'); |
flackr
2013/02/05 01:24:20
Seems like previousCheckmark == this.checkmark_, i
bshe
2013/02/05 17:33:10
Done.
| |
569 if (previousActiveThumb) | 576 if (previousCheckmark) |
570 previousActiveThumb.active = false; | 577 previousCheckmark.parentNode.removeChild(previousCheckmark); |
571 activeThumb.active = true; | 578 selectedGridItem.appendChild(this.checkmark_); |
572 }; | 579 }; |
573 | 580 |
574 })(); | 581 })(); |
OLD | NEW |