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.document_ = dialogDom.ownerDocument; | 18 this.document_ = dialogDom.ownerDocument; |
19 this.enableOnlineWallpaper_ = loadTimeData.valueExists('manifestBaseURL'); | 19 this.enableOnlineWallpaper_ = loadTimeData.valueExists('manifestBaseURL'); |
20 this.selectedCategory = null; | |
21 this.selectedItem_ = null; | 20 this.selectedItem_ = null; |
22 this.progressManager_ = new ProgressManager(); | 21 this.progressManager_ = new ProgressManager(); |
23 this.customWallpaperData_ = null; | 22 this.customWallpaperData_ = null; |
24 this.currentWallpaper_ = null; | 23 this.currentWallpaper_ = null; |
25 this.wallpaperRequest_ = null; | 24 this.wallpaperRequest_ = null; |
26 this.wallpaperDirs_ = WallpaperDirectories.getInstance(); | 25 this.wallpaperDirs_ = WallpaperDirectories.getInstance(); |
27 this.preManifestDomInit_(); | 26 this.preManifestDomInit_(); |
28 this.fetchManifest_(); | 27 this.fetchManifest_(); |
29 } | 28 } |
30 | 29 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 WallpaperUtil.saveToStorage(Constants.AccessManifestKey, manifest, false); | 174 WallpaperUtil.saveToStorage(Constants.AccessManifestKey, manifest, false); |
176 this.postManifestDomInit_(); | 175 this.postManifestDomInit_(); |
177 }; | 176 }; |
178 | 177 |
179 // Sets manifest to previously saved object if any and shows connection error. | 178 // Sets manifest to previously saved object if any and shows connection error. |
180 // Called after manifest failed to load. | 179 // Called after manifest failed to load. |
181 WallpaperManager.prototype.onLoadManifestFailed_ = function() { | 180 WallpaperManager.prototype.onLoadManifestFailed_ = function() { |
182 var accessManifestKey = Constants.AccessManifestKey; | 181 var accessManifestKey = Constants.AccessManifestKey; |
183 var self = this; | 182 var self = this; |
184 Constants.WallpaperLocalStorage.get(accessManifestKey, function(items) { | 183 Constants.WallpaperLocalStorage.get(accessManifestKey, function(items) { |
185 self.manifest_ = items[accessManifestKey] ? items[accessManifestKey] : {}; | 184 self.manifest_ = items[accessManifestKey] ? |
| 185 items[accessManifestKey] : null; |
186 self.showError_(str('connectionFailed')); | 186 self.showError_(str('connectionFailed')); |
187 self.postManifestDomInit_(); | 187 self.postManifestDomInit_(); |
188 $('wallpaper-grid').classList.add('image-picker-offline'); | 188 $('wallpaper-grid').classList.add('image-picker-offline'); |
189 }); | 189 }); |
190 }; | 190 }; |
191 | 191 |
192 /** | 192 /** |
193 * Toggle surprise me feature of wallpaper picker. It fires an storage | 193 * Toggle surprise me feature of wallpaper picker. It fires an storage |
194 * onChanged event. Event handler for that event is in event_page.js. | 194 * onChanged event. Event handler for that event is in event_page.js. |
195 * @private | 195 * @private |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 }); | 448 }); |
449 } | 449 } |
450 }; | 450 }; |
451 | 451 |
452 /** | 452 /** |
453 * Constructs the thumbnails grid. | 453 * Constructs the thumbnails grid. |
454 */ | 454 */ |
455 WallpaperManager.prototype.initThumbnailsGrid_ = function() { | 455 WallpaperManager.prototype.initThumbnailsGrid_ = function() { |
456 this.wallpaperGrid_ = $('wallpaper-grid'); | 456 this.wallpaperGrid_ = $('wallpaper-grid'); |
457 wallpapers.WallpaperThumbnailsGrid.decorate(this.wallpaperGrid_); | 457 wallpapers.WallpaperThumbnailsGrid.decorate(this.wallpaperGrid_); |
458 this.wallpaperGrid_.autoExpands = true; | |
459 | 458 |
460 this.wallpaperGrid_.addEventListener('change', this.onChange_.bind(this)); | 459 this.wallpaperGrid_.addEventListener('change', this.onChange_.bind(this)); |
461 this.wallpaperGrid_.addEventListener('dblclick', this.onClose_.bind(this)); | 460 this.wallpaperGrid_.addEventListener('dblclick', this.onClose_.bind(this)); |
462 }; | 461 }; |
463 | 462 |
464 /** | 463 /** |
465 * Handles change event dispatched by wallpaper grid. | 464 * Handles change event dispatched by wallpaper grid. |
466 */ | 465 */ |
467 WallpaperManager.prototype.onChange_ = function() { | 466 WallpaperManager.prototype.onChange_ = function() { |
468 // splice may dispatch a change event because the position of selected | 467 // splice may dispatch a change event because the position of selected |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
996 var wallpapersDataModel = new cr.ui.ArrayDataModel([]); | 995 var wallpapersDataModel = new cr.ui.ArrayDataModel([]); |
997 var selectedItem = null; | 996 var selectedItem = null; |
998 if (selectedListItem.custom) { | 997 if (selectedListItem.custom) { |
999 this.document_.body.setAttribute('custom', ''); | 998 this.document_.body.setAttribute('custom', ''); |
1000 var errorHandler = this.onFileSystemError_.bind(this); | 999 var errorHandler = this.onFileSystemError_.bind(this); |
1001 var toArray = function(list) { | 1000 var toArray = function(list) { |
1002 return Array.prototype.slice.call(list || [], 0); | 1001 return Array.prototype.slice.call(list || [], 0); |
1003 }; | 1002 }; |
1004 | 1003 |
1005 var self = this; | 1004 var self = this; |
| 1005 // Need this check for test purpose. |
| 1006 var numOnlineWallpaper = (this.enableOnlineWallpaper_ && this.manifest_) ? |
| 1007 this.manifest_.wallpaper_list.length : 0; |
1006 var processResults = function(entries) { | 1008 var processResults = function(entries) { |
1007 for (var i = 0; i < entries.length; i++) { | 1009 for (var i = 0; i < entries.length; i++) { |
1008 var entry = entries[i]; | 1010 var entry = entries[i]; |
1009 var wallpaperInfo = { | 1011 var wallpaperInfo = { |
| 1012 wallpaperId: numOnlineWallpaper + i, |
1010 baseURL: entry.name, | 1013 baseURL: entry.name, |
1011 // The layout will be replaced by the actual value saved in | 1014 // The layout will be replaced by the actual value saved in |
1012 // local storage when requested later. Layout is not important | 1015 // local storage when requested later. Layout is not important |
1013 // for constructing thumbnails grid, we use CENTER_CROPPED here | 1016 // for constructing thumbnails grid, we use CENTER_CROPPED here |
1014 // to speed up the process of constructing. So we do not need to | 1017 // to speed up the process of constructing. So we do not need to |
1015 // wait for fetching correct layout. | 1018 // wait for fetching correct layout. |
1016 layout: 'CENTER_CROPPED', | 1019 layout: 'CENTER_CROPPED', |
1017 source: Constants.WallpaperSourceEnum.Custom, | 1020 source: Constants.WallpaperSourceEnum.Custom, |
1018 availableOffline: true | 1021 availableOffline: true |
1019 }; | 1022 }; |
1020 wallpapersDataModel.push(wallpaperInfo); | 1023 wallpapersDataModel.push(wallpaperInfo); |
1021 } | 1024 } |
1022 if (loadTimeData.getBoolean('isOEMDefaultWallpaper')) { | 1025 if (loadTimeData.getBoolean('isOEMDefaultWallpaper')) { |
1023 var oemDefaultWallpaperElement = { | 1026 var oemDefaultWallpaperElement = { |
| 1027 wallpaperId: numOnlineWallpaper + entries.length, |
1024 baseURL: 'OemDefaultWallpaper', | 1028 baseURL: 'OemDefaultWallpaper', |
1025 layout: 'CENTER_CROPPED', | 1029 layout: 'CENTER_CROPPED', |
1026 source: Constants.WallpaperSourceEnum.OEM, | 1030 source: Constants.WallpaperSourceEnum.OEM, |
1027 availableOffline: true | 1031 availableOffline: true |
1028 }; | 1032 }; |
1029 wallpapersDataModel.push(oemDefaultWallpaperElement); | 1033 wallpapersDataModel.push(oemDefaultWallpaperElement); |
1030 } | 1034 } |
1031 for (var i = 0; i < wallpapersDataModel.length; i++) { | 1035 for (var i = 0; i < wallpapersDataModel.length; i++) { |
1032 if (self.currentWallpaper_ == wallpapersDataModel.item(i).baseURL) | 1036 if (self.currentWallpaper_ == wallpapersDataModel.item(i).baseURL) |
1033 selectedItem = wallpapersDataModel.item(i); | 1037 selectedItem = wallpapersDataModel.item(i); |
(...skipping 26 matching lines...) Expand all Loading... |
1060 readEntries(); | 1064 readEntries(); |
1061 } | 1065 } |
1062 }, errorHandler); | 1066 }, errorHandler); |
1063 }; | 1067 }; |
1064 readEntries(); // Start reading dirs. | 1068 readEntries(); // Start reading dirs. |
1065 }; | 1069 }; |
1066 this.wallpaperDirs_.getDirectory(Constants.WallpaperDirNameEnum.ORIGINAL, | 1070 this.wallpaperDirs_.getDirectory(Constants.WallpaperDirNameEnum.ORIGINAL, |
1067 success, errorHandler); | 1071 success, errorHandler); |
1068 } else { | 1072 } else { |
1069 this.document_.body.removeAttribute('custom'); | 1073 this.document_.body.removeAttribute('custom'); |
1070 for (var key in this.manifest_.wallpaper_list) { | 1074 // Need this check for test purpose. |
| 1075 var numOnlineWallpaper = (this.enableOnlineWallpaper_ && this.manifest_) ? |
| 1076 this.manifest_.wallpaper_list.length : 0; |
| 1077 for (var i = 0; i < numOnlineWallpaper; i++) { |
1071 if (selectedIndex == AllCategoryIndex || | 1078 if (selectedIndex == AllCategoryIndex || |
1072 this.manifest_.wallpaper_list[key].categories.indexOf( | 1079 this.manifest_.wallpaper_list[i].categories.indexOf( |
1073 selectedIndex - OnlineCategoriesOffset) != -1) { | 1080 selectedIndex - OnlineCategoriesOffset) != -1) { |
1074 var wallpaperInfo = { | 1081 var wallpaperInfo = { |
1075 baseURL: this.manifest_.wallpaper_list[key].base_url, | 1082 wallpaperId: i, |
1076 layout: this.manifest_.wallpaper_list[key].default_layout, | 1083 baseURL: this.manifest_.wallpaper_list[i].base_url, |
| 1084 layout: this.manifest_.wallpaper_list[i].default_layout, |
1077 source: Constants.WallpaperSourceEnum.Online, | 1085 source: Constants.WallpaperSourceEnum.Online, |
1078 availableOffline: false, | 1086 availableOffline: false, |
1079 author: this.manifest_.wallpaper_list[key].author, | 1087 author: this.manifest_.wallpaper_list[i].author, |
1080 authorWebsite: this.manifest_.wallpaper_list[key].author_website, | 1088 authorWebsite: this.manifest_.wallpaper_list[i].author_website, |
1081 dynamicURL: this.manifest_.wallpaper_list[key].dynamic_url | 1089 dynamicURL: this.manifest_.wallpaper_list[i].dynamic_url |
1082 }; | 1090 }; |
1083 var startIndex = wallpaperInfo.baseURL.lastIndexOf('/') + 1; | 1091 var startIndex = wallpaperInfo.baseURL.lastIndexOf('/') + 1; |
1084 var fileName = wallpaperInfo.baseURL.substring(startIndex) + | 1092 var fileName = wallpaperInfo.baseURL.substring(startIndex) + |
1085 Constants.HighResolutionSuffix; | 1093 Constants.HighResolutionSuffix; |
1086 if (this.downloadedListMap_ && | 1094 if (this.downloadedListMap_ && |
1087 this.downloadedListMap_.hasOwnProperty(encodeURI(fileName))) { | 1095 this.downloadedListMap_.hasOwnProperty(encodeURI(fileName))) { |
1088 wallpaperInfo.availableOffline = true; | 1096 wallpaperInfo.availableOffline = true; |
1089 } | 1097 } |
1090 wallpapersDataModel.push(wallpaperInfo); | 1098 wallpapersDataModel.push(wallpaperInfo); |
1091 var url = this.manifest_.wallpaper_list[key].base_url + | 1099 var url = this.manifest_.wallpaper_list[i].base_url + |
1092 Constants.HighResolutionSuffix; | 1100 Constants.HighResolutionSuffix; |
1093 if (url == this.currentWallpaper_) { | 1101 if (url == this.currentWallpaper_) { |
1094 selectedItem = wallpaperInfo; | 1102 selectedItem = wallpaperInfo; |
1095 } | 1103 } |
1096 } | 1104 } |
1097 } | 1105 } |
1098 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 1106 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
1099 if (selectedItem) { | 1107 if (selectedItem) { |
1100 this.wallpaperGrid_.selectedItem = selectedItem; | 1108 this.wallpaperGrid_.selectedItem = selectedItem; |
1101 this.wallpaperGrid_.activeItem = selectedItem; | 1109 this.wallpaperGrid_.activeItem = selectedItem; |
1102 } | 1110 } |
1103 } | 1111 } |
1104 }; | 1112 }; |
1105 | 1113 |
1106 })(); | 1114 })(); |
OLD | NEW |