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.enableOnlineWallpaper_ = loadTimeData.valueExists('manifestBaseURL'); | 20 this.enableOnlineWallpaper_ = loadTimeData.valueExists('manifestBaseURL'); |
21 this.selectedCategory = null; | 21 this.selectedCategory = null; |
| 22 this.selectedItem_ = null; |
22 this.progressManager_ = new ProgressManager(); | 23 this.progressManager_ = new ProgressManager(); |
23 this.customWallpaperData_ = null; | 24 this.customWallpaperData_ = null; |
24 this.currentWallpaper_ = null; | 25 this.currentWallpaper_ = null; |
25 this.wallpaperRequest_ = null; | 26 this.wallpaperRequest_ = null; |
26 this.backgroundPage_ = null; | 27 this.backgroundPage_ = null; |
27 this.wallpaperDirs_ = WallpaperDirectories.getInstance(); | 28 this.wallpaperDirs_ = WallpaperDirectories.getInstance(); |
28 this.fetchBackgroundPage_(); | 29 this.fetchBackgroundPage_(); |
29 this.fetchManifest_(); | 30 this.fetchManifest_(); |
30 } | 31 } |
31 | 32 |
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 } | 454 } |
454 }; | 455 }; |
455 | 456 |
456 /** | 457 /** |
457 * Constructs the thumbnails grid. | 458 * Constructs the thumbnails grid. |
458 */ | 459 */ |
459 WallpaperManager.prototype.initThumbnailsGrid_ = function() { | 460 WallpaperManager.prototype.initThumbnailsGrid_ = function() { |
460 this.wallpaperGrid_ = $('wallpaper-grid'); | 461 this.wallpaperGrid_ = $('wallpaper-grid'); |
461 wallpapers.WallpaperThumbnailsGrid.decorate(this.wallpaperGrid_); | 462 wallpapers.WallpaperThumbnailsGrid.decorate(this.wallpaperGrid_); |
462 | 463 |
463 this.wallpaperGrid_.addEventListener('change', | 464 this.wallpaperGrid_.addEventListener('change', this.onChange_.bind(this)); |
464 this.onThumbnailSelectionChanged_.bind(this)); | |
465 this.wallpaperGrid_.addEventListener('dblclick', this.onClose_.bind(this)); | 465 this.wallpaperGrid_.addEventListener('dblclick', this.onClose_.bind(this)); |
466 }; | 466 }; |
467 | 467 |
468 /** | 468 /** |
| 469 * Handles change event dispatched by wallpaper grid. |
| 470 */ |
| 471 WallpaperManager.prototype.onChange_ = function() { |
| 472 // splice may dispatch a change event because the position of selected |
| 473 // element changing. But the actual selected element may not change after |
| 474 // splice. Check if the new selected element equals to the previous selected |
| 475 // element before continuing. Otherwise, wallpaper may reset to previous one |
| 476 // as described in http://crbug.com/229036. |
| 477 if (this.selectedItem_ == this.wallpaperGrid_.selectedItem) |
| 478 return; |
| 479 this.selectedItem_ = this.wallpaperGrid_.selectedItem; |
| 480 this.onSelectedItemChanged_(); |
| 481 }; |
| 482 |
| 483 /** |
469 * Closes window if no pending wallpaper request. | 484 * Closes window if no pending wallpaper request. |
470 */ | 485 */ |
471 WallpaperManager.prototype.onClose_ = function() { | 486 WallpaperManager.prototype.onClose_ = function() { |
472 if (this.wallpaperRequest_) { | 487 if (this.wallpaperRequest_) { |
473 this.wallpaperRequest_.addEventListener('loadend', function() { | 488 this.wallpaperRequest_.addEventListener('loadend', function() { |
474 // Close window on wallpaper loading finished. | 489 // Close window on wallpaper loading finished. |
475 window.close(); | 490 window.close(); |
476 }); | 491 }); |
477 } else { | 492 } else { |
478 window.close(); | 493 window.close(); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
610 break; | 625 break; |
611 default: | 626 default: |
612 msg = 'Unknown Error'; | 627 msg = 'Unknown Error'; |
613 break; | 628 break; |
614 } | 629 } |
615 console.error('Error: ' + msg); | 630 console.error('Error: ' + msg); |
616 this.showError_(str('accessFileFailure')); | 631 this.showError_(str('accessFileFailure')); |
617 }; | 632 }; |
618 | 633 |
619 /** | 634 /** |
620 * Handles click on a different thumbnail in wallpaper grid. | 635 * Handles changing of selectedItem in wallpaper manager. |
621 */ | 636 */ |
622 WallpaperManager.prototype.onThumbnailSelectionChanged_ = function() { | 637 WallpaperManager.prototype.onSelectedItemChanged_ = function() { |
623 var selectedItem = this.wallpaperGrid_.selectedItem; | 638 this.setWallpaperAttribution_(this.selectedItem_); |
624 if (selectedItem && selectedItem.source == 'ADDNEW') | 639 |
| 640 if (!this.selectedItem_ || this.selectedItem_.source == 'ADDNEW') |
625 return; | 641 return; |
626 | 642 |
627 if (selectedItem && selectedItem.baseURL && | 643 if (this.selectedItem_.baseURL && !this.wallpaperGrid_.inProgramSelection) { |
628 !this.wallpaperGrid_.inProgramSelection) { | 644 if (this.selectedItem_.source == wallpapers.WallpaperSourceEnum.Custom) { |
629 if (selectedItem.source == wallpapers.WallpaperSourceEnum.Custom) { | |
630 var items = {}; | 645 var items = {}; |
631 var key = selectedItem.baseURL; | 646 var key = this.selectedItem_.baseURL; |
632 var self = this; | 647 var self = this; |
633 this.storage_.get(key, function(items) { | 648 this.storage_.get(key, function(items) { |
634 selectedItem.layout = items[key] ? items[key] : 'CENTER_CROPPED'; | 649 self.selectedItem_.layout = |
635 self.setSelectedWallpaper_(selectedItem); | 650 items[key] ? items[key] : 'CENTER_CROPPED'; |
| 651 self.setSelectedWallpaper_(self.selectedItem_); |
636 }); | 652 }); |
637 } else { | 653 } else { |
638 this.setSelectedWallpaper_(selectedItem); | 654 this.setSelectedWallpaper_(this.selectedItem_); |
639 } | 655 } |
640 } | 656 } |
641 this.setWallpaperAttribution_(selectedItem); | |
642 }; | 657 }; |
643 | 658 |
644 /** | 659 /** |
645 * Set attributions of wallpaper with given URL. If URL is not valid, clear | 660 * Set attributions of wallpaper with given URL. If URL is not valid, clear |
646 * the attributions. | 661 * the attributions. |
647 * @param {{baseURL: string, dynamicURL: string, layout: string, | 662 * @param {{baseURL: string, dynamicURL: string, layout: string, |
648 * author: string, authorWebsite: string, availableOffline: boolean}} | 663 * author: string, authorWebsite: string, availableOffline: boolean}} |
649 * selectedItem selected wallpaper item in grid. | 664 * selectedItem selected wallpaper item in grid. |
650 * @private | 665 * @private |
651 */ | 666 */ |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 dirEntry.getFile(fileName, {create: true}, function(fileEntry) { | 774 dirEntry.getFile(fileName, {create: true}, function(fileEntry) { |
760 fileEntry.createWriter(function(fileWriter) { | 775 fileEntry.createWriter(function(fileWriter) { |
761 fileWriter.onwriteend = function(e) { | 776 fileWriter.onwriteend = function(e) { |
762 $('set-wallpaper-layout').disabled = false; | 777 $('set-wallpaper-layout').disabled = false; |
763 var wallpaperInfo = { | 778 var wallpaperInfo = { |
764 baseURL: fileName, | 779 baseURL: fileName, |
765 layout: layout, | 780 layout: layout, |
766 source: wallpapers.WallpaperSourceEnum.Custom, | 781 source: wallpapers.WallpaperSourceEnum.Custom, |
767 availableOffline: true | 782 availableOffline: true |
768 }; | 783 }; |
| 784 self.wallpaperGrid_.dataModel.splice(0, 0, wallpaperInfo); |
| 785 self.wallpaperGrid_.selectedItem = wallpaperInfo; |
| 786 self.wallpaperGrid_.activeItem = wallpaperInfo; |
769 self.currentWallpaper_ = fileName; | 787 self.currentWallpaper_ = fileName; |
770 var items = {}; | 788 var items = {}; |
771 items[self.currentWallpaper_] = layout; | 789 items[self.currentWallpaper_] = layout; |
772 self.storage_.set(items, function() {}); | 790 self.storage_.set(items, function() {}); |
773 self.wallpaperGrid_.dataModel.splice(0, 0, wallpaperInfo); | |
774 self.wallpaperGrid_.selectedItem = wallpaperInfo; | |
775 self.wallpaperGrid_.activeItem = wallpaperInfo; | |
776 }; | 791 }; |
777 | 792 |
778 fileWriter.onerror = errorHandler; | 793 fileWriter.onerror = errorHandler; |
779 | 794 |
780 var blob = new Blob([new Int8Array(thumbnail)], | 795 var blob = new Blob([new Int8Array(thumbnail)], |
781 {'type' : 'image\/jpeg'}); | 796 {'type' : 'image\/jpeg'}); |
782 fileWriter.write(blob); | 797 fileWriter.write(blob); |
783 }, errorHandler); | 798 }, errorHandler); |
784 }, errorHandler); | 799 }, errorHandler); |
785 }; | 800 }; |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1017 } | 1032 } |
1018 } | 1033 } |
1019 } | 1034 } |
1020 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 1035 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
1021 this.wallpaperGrid_.selectedItem = selectedItem; | 1036 this.wallpaperGrid_.selectedItem = selectedItem; |
1022 this.wallpaperGrid_.activeItem = selectedItem; | 1037 this.wallpaperGrid_.activeItem = selectedItem; |
1023 } | 1038 } |
1024 }; | 1039 }; |
1025 | 1040 |
1026 })(); | 1041 })(); |
OLD | NEW |