| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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.selectedCategory = null; | 19 this.selectedCategory = null; |
| 20 this.currentButter_ = null; | 20 this.currentButter_ = null; |
| 21 this.customWallpaperData_ = null; |
| 21 this.fetchManifest_(); | 22 this.fetchManifest_(); |
| 22 this.initDom_(); | 23 this.initDom_(); |
| 23 } | 24 } |
| 24 | 25 |
| 25 // Anonymous 'namespace'. | 26 // Anonymous 'namespace'. |
| 26 // TODO(bshe): Get rid of anonymous namespace. | 27 // TODO(bshe): Get rid of anonymous namespace. |
| 27 (function() { | 28 (function() { |
| 28 | 29 |
| 29 /** | 30 /** |
| 30 * Base URL of the manifest file. | 31 * Base URL of the manifest file. |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 * One-time initialization of various DOM nodes. | 105 * One-time initialization of various DOM nodes. |
| 105 */ | 106 */ |
| 106 WallpaperManager.prototype.initDom_ = function() { | 107 WallpaperManager.prototype.initDom_ = function() { |
| 107 i18nTemplate.process(this.document_, loadTimeData); | 108 i18nTemplate.process(this.document_, loadTimeData); |
| 108 this.initCategoriesList_(); | 109 this.initCategoriesList_(); |
| 109 this.initThumbnailsGrid_(); | 110 this.initThumbnailsGrid_(); |
| 110 | 111 |
| 111 // Selects the first category in the list as default. | 112 // Selects the first category in the list as default. |
| 112 this.categoriesList_.selectionModel.selectedIndex = 0; | 113 this.categoriesList_.selectionModel.selectedIndex = 0; |
| 113 | 114 |
| 114 this.dialogDom_.querySelector('#file-selector').addEventListener( | 115 $('file-selector').addEventListener( |
| 115 'change', this.onFileSelectorChanged_.bind(this)); | 116 'change', this.onFileSelectorChanged_.bind(this)); |
| 116 | 117 |
| 118 $('set-wallpaper-layout').addEventListener( |
| 119 'change', this.onWallpaperLayoutChanged_.bind(this)); |
| 120 |
| 117 this.dialogDom_.ownerDocument.defaultView.addEventListener( | 121 this.dialogDom_.ownerDocument.defaultView.addEventListener( |
| 118 'resize', this.onResize_.bind(this)); | 122 'resize', this.onResize_.bind(this)); |
| 119 | 123 |
| 120 this.onResize_(); | 124 this.onResize_(); |
| 121 }; | 125 }; |
| 122 | 126 |
| 123 /** | 127 /** |
| 124 * Constructs the thumbnails grid. | 128 * Constructs the thumbnails grid. |
| 125 */ | 129 */ |
| 126 WallpaperManager.prototype.initThumbnailsGrid_ = function() { | 130 WallpaperManager.prototype.initThumbnailsGrid_ = function() { |
| 127 this.wallpaperGrid_ = this.dialogDom_.querySelector('#wallpaper-grid'); | 131 this.wallpaperGrid_ = $('wallpaper-grid'); |
| 128 wallpapers.WallpaperThumbnailsGrid.decorate(this.wallpaperGrid_); | 132 wallpapers.WallpaperThumbnailsGrid.decorate(this.wallpaperGrid_); |
| 129 | 133 |
| 130 this.wallpaperGrid_.addEventListener('change', | 134 this.wallpaperGrid_.addEventListener('change', |
| 131 this.onThumbnailClicked_.bind(this)); | 135 this.onThumbnailClicked_.bind(this)); |
| 132 this.wallpaperGrid_.addEventListener('activate', | 136 this.wallpaperGrid_.addEventListener('activate', |
| 133 function() { window.close() }); | 137 function() { window.close() }); |
| 134 }; | 138 }; |
| 135 | 139 |
| 136 /** | 140 /** |
| 137 * Sets wallpaper to the corresponding wallpaper of selected thumbnail. | 141 * Sets wallpaper to the corresponding wallpaper of selected thumbnail. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 149 if (this.wallpaperRequest_) | 153 if (this.wallpaperRequest_) |
| 150 this.wallpaperRequest_.abort(); | 154 this.wallpaperRequest_.abort(); |
| 151 // TODO(bshe): XMLHttpRequest may be cancelled by window.close(). We should | 155 // TODO(bshe): XMLHttpRequest may be cancelled by window.close(). We should |
| 152 // either wait for response before closing window or use an extension | 156 // either wait for response before closing window or use an extension |
| 153 // background page to get response afte window closed. | 157 // background page to get response afte window closed. |
| 154 this.wallpaperRequest_ = new XMLHttpRequest(); | 158 this.wallpaperRequest_ = new XMLHttpRequest(); |
| 155 this.wallpaperRequest_.open('GET', wallpaperURL, true); | 159 this.wallpaperRequest_.open('GET', wallpaperURL, true); |
| 156 this.wallpaperRequest_.responseType = 'arraybuffer'; | 160 this.wallpaperRequest_.responseType = 'arraybuffer'; |
| 157 this.wallpaperRequest_.send(null); | 161 this.wallpaperRequest_.send(null); |
| 158 var self = this; | 162 var self = this; |
| 159 this.wallpaperRequest_.onload = function(e) { | 163 this.wallpaperRequest_.addEventListener('load', function(e) { |
| 160 //TODO(bshe): Add error handling code. | 164 //TODO(bshe): Add error handling code. |
| 161 if (self.wallpaperRequest_.status === 200) { | 165 if (self.wallpaperRequest_.status === 200) { |
| 162 var image = self.wallpaperRequest_.response; | 166 var image = self.wallpaperRequest_.response; |
| 163 chrome.wallpaperPrivate.setWallpaper(image, | 167 chrome.wallpaperPrivate.setWallpaper(image, |
| 164 selectedItem.layout, | 168 selectedItem.layout, |
| 165 wallpaperURL); | 169 wallpaperURL); |
| 166 self.wallpaperRequest_ = null; | 170 self.wallpaperRequest_ = null; |
| 167 } | 171 } |
| 168 }; | 172 }); |
| 169 this.setWallpaperAttribution_(selectedItem); | 173 this.setWallpaperAttribution_(selectedItem); |
| 170 }; | 174 }; |
| 171 | 175 |
| 172 /** | 176 /** |
| 173 * Set attributions of wallpaper with given URL. If URL is not valid, clear | 177 * Set attributions of wallpaper with given URL. If URL is not valid, clear |
| 174 * the attributions. | 178 * the attributions. |
| 175 * @param {{baseURL: string, dynamicURL: string, layout: string, | 179 * @param {{baseURL: string, dynamicURL: string, layout: string, |
| 176 * author: string, authorWebsite: string}} | 180 * author: string, authorWebsite: string}} |
| 177 * selectedItem selected wallpaper item in grid. | 181 * selectedItem selected wallpaper item in grid. |
| 178 * @private | 182 * @private |
| 179 */ | 183 */ |
| 180 WallpaperManager.prototype.setWallpaperAttribution_ = function(selectedItem) { | 184 WallpaperManager.prototype.setWallpaperAttribution_ = function(selectedItem) { |
| 181 if (selectedItem) { | 185 if (selectedItem) { |
| 182 this.dialogDom_.querySelector('#author-name').textContent = | 186 $('author-name').textContent = selectedItem.author; |
| 183 selectedItem.author; | 187 $('author-website').textContent = $('author-website').href = |
| 184 this.dialogDom_.querySelector('#author-website').textContent = | |
| 185 this.dialogDom_.querySelector('#author-website').href = | |
| 186 selectedItem.authorWebsite; | 188 selectedItem.authorWebsite; |
| 187 return; | 189 return; |
| 188 } | 190 } |
| 189 this.dialogDom_.querySelector('#author-name').textContent = ''; | 191 $('author-name').textContent = ''; |
| 190 this.dialogDom_.querySelector('#author-website').textContent = | 192 $('author-website').textContent = $('author-website').href = ''; |
| 191 this.dialogDom_.querySelector('#author-website').href = ''; | |
| 192 }; | 193 }; |
| 193 | 194 |
| 194 /** | 195 /** |
| 195 * Resize thumbnails grid and categories list to fit the new window size. | 196 * Resize thumbnails grid and categories list to fit the new window size. |
| 196 */ | 197 */ |
| 197 WallpaperManager.prototype.onResize_ = function() { | 198 WallpaperManager.prototype.onResize_ = function() { |
| 198 this.wallpaperGrid_.redraw(); | 199 this.wallpaperGrid_.redraw(); |
| 199 this.categoriesList_.redraw(); | 200 this.categoriesList_.redraw(); |
| 200 }; | 201 }; |
| 201 | 202 |
| 202 /** | 203 /** |
| 203 * Constructs the categories list. | 204 * Constructs the categories list. |
| 204 */ | 205 */ |
| 205 WallpaperManager.prototype.initCategoriesList_ = function() { | 206 WallpaperManager.prototype.initCategoriesList_ = function() { |
| 206 this.categoriesList_ = this.dialogDom_.querySelector('#categories-list'); | 207 this.categoriesList_ = $('categories-list'); |
| 207 cr.ui.List.decorate(this.categoriesList_); | 208 cr.ui.List.decorate(this.categoriesList_); |
| 208 | 209 |
| 209 var self = this; | 210 var self = this; |
| 210 this.categoriesList_.itemConstructor = function(entry) { | 211 this.categoriesList_.itemConstructor = function(entry) { |
| 211 return self.renderCategory_(entry); | 212 return self.renderCategory_(entry); |
| 212 }; | 213 }; |
| 213 | 214 |
| 214 this.categoriesList_.selectionModel = new cr.ui.ListSingleSelectionModel(); | 215 this.categoriesList_.selectionModel = new cr.ui.ListSingleSelectionModel(); |
| 215 this.categoriesList_.selectionModel.addEventListener( | 216 this.categoriesList_.selectionModel.addEventListener( |
| 216 'change', this.onCategoriesChange_.bind(this)); | 217 'change', this.onCategoriesChange_.bind(this)); |
| 217 | 218 |
| 218 var categoriesDataModel = new cr.ui.ArrayDataModel([]); | 219 var categoriesDataModel = new cr.ui.ArrayDataModel([]); |
| 219 for (var key in this.manifest_.categories) { | 220 for (var key in this.manifest_.categories) { |
| 220 categoriesDataModel.push(this.manifest_.categories[key]); | 221 categoriesDataModel.push(this.manifest_.categories[key]); |
| 221 } | 222 } |
| 222 //TODO(bshe): Add custom wallpaper category once it is ready. | 223 // Adds custom category as last category. |
| 224 categoriesDataModel.push(str('customCategoryLabel')); |
| 223 this.categoriesList_.dataModel = categoriesDataModel; | 225 this.categoriesList_.dataModel = categoriesDataModel; |
| 224 }; | 226 }; |
| 225 | 227 |
| 226 /** | 228 /** |
| 227 * Constructs the element in categories list. | 229 * Constructs the element in categories list. |
| 228 * @param {string} entry Text content of a category. | 230 * @param {string} entry Text content of a category. |
| 229 */ | 231 */ |
| 230 WallpaperManager.prototype.renderCategory_ = function(entry) { | 232 WallpaperManager.prototype.renderCategory_ = function(entry) { |
| 231 var li = this.document_.createElement('li'); | 233 var li = this.document_.createElement('li'); |
| 232 cr.defineProperty(li, 'custom', cr.PropertyKind.BOOL_ATTR); | 234 cr.defineProperty(li, 'custom', cr.PropertyKind.BOOL_ATTR); |
| 233 li.custom = (entry == str('customCategoryLabel')); | 235 li.custom = (entry == str('customCategoryLabel')); |
| 234 cr.defineProperty(li, 'lead', cr.PropertyKind.BOOL_ATTR); | 236 cr.defineProperty(li, 'lead', cr.PropertyKind.BOOL_ATTR); |
| 235 cr.defineProperty(li, 'selected', cr.PropertyKind.BOOL_ATTR); | 237 cr.defineProperty(li, 'selected', cr.PropertyKind.BOOL_ATTR); |
| 236 var div = this.document_.createElement('div'); | 238 var div = this.document_.createElement('div'); |
| 237 div.textContent = entry; | 239 div.textContent = entry; |
| 238 li.appendChild(div); | 240 li.appendChild(div); |
| 239 return li; | 241 return li; |
| 240 }; | 242 }; |
| 241 | 243 |
| 242 /** | 244 /** |
| 243 * Handles the custom wallpaper which user selected from file manager. Called | 245 * Handles the custom wallpaper which user selected from file manager. Called |
| 244 * when users select a file. | 246 * when users select a file. |
| 245 */ | 247 */ |
| 246 WallpaperManager.prototype.onFileSelectorChanged_ = function() { | 248 WallpaperManager.prototype.onFileSelectorChanged_ = function() { |
| 247 var files = this.dialogDom_.querySelector('#file-selector').files; | 249 var files = $('file-selector').files; |
| 248 if (files.length != 1) | 250 if (files.length != 1) |
| 249 console.error('More than one files are selected or no file selected'); | 251 console.error('More than one files are selected or no file selected'); |
| 252 var reader = new FileReader(); |
| 253 reader.readAsArrayBuffer(files[0]); |
| 254 var self = this; |
| 255 // TODO(bshe): Handle file error. |
| 256 reader.addEventListener('load', function(e) { |
| 257 self.customWallpaperData_ = e.target.result; |
| 258 self.refreshWallpaper_(self.customWallpaperData_); |
| 259 }); |
| 250 this.generateThumbnail_(files[0]); | 260 this.generateThumbnail_(files[0]); |
| 251 }; | 261 }; |
| 252 | 262 |
| 253 /** | 263 /** |
| 264 * Refreshes the custom wallpaper with the current selected layout. |
| 265 * @param {ArrayBuffer} customWallpaper The raw wallpaper file data. |
| 266 */ |
| 267 WallpaperManager.prototype.refreshWallpaper_ = function(customWallpaper) { |
| 268 var setWallpaperLayout = $('set-wallpaper-layout'); |
| 269 var layout = |
| 270 setWallpaperLayout.options[setWallpaperLayout.selectedIndex].value; |
| 271 chrome.wallpaperPrivate.setCustomWallpaper(customWallpaper, |
| 272 layout); |
| 273 }; |
| 274 |
| 275 /** |
| 276 * Handles the layout setting change of custom wallpaper. |
| 277 */ |
| 278 WallpaperManager.prototype.onWallpaperLayoutChanged_ = function() { |
| 279 if (this.customWallpaperData_) |
| 280 this.refreshWallpaper_(this.customWallpaperData_); |
| 281 }; |
| 282 |
| 283 /** |
| 254 * Generates a thumbnail of user selected image file. | 284 * Generates a thumbnail of user selected image file. |
| 255 * @param {Object} file The file user selected from file manager. | 285 * @param {Object} file The file user selected from file manager. |
| 256 */ | 286 */ |
| 257 WallpaperManager.prototype.generateThumbnail_ = function(file) { | 287 WallpaperManager.prototype.generateThumbnail_ = function(file) { |
| 258 var img = this.dialogDom_.querySelector('#preview'); | 288 var img = $('preview'); |
| 259 img.file = file; | 289 img.file = file; |
| 260 var reader = new FileReader(); | 290 var reader = new FileReader(); |
| 261 reader.onload = function(e) { | 291 reader.addEventListener('load', function(e) { |
| 262 img.src = e.target.result; | 292 img.src = e.target.result; |
| 263 }; | 293 }); |
| 264 reader.readAsDataURL(file); | 294 reader.readAsDataURL(file); |
| 265 }; | 295 }; |
| 266 | 296 |
| 267 /** | 297 /** |
| 268 * Toggle visibility of custom container and category container. | 298 * Toggle visibility of custom container and category container. |
| 269 * @param {boolean} show True if display custom container and hide category | 299 * @param {boolean} showCustom True if display custom container and hide |
| 270 * container. | 300 * category container. |
| 271 */ | 301 */ |
| 272 WallpaperManager.prototype.showCustomContainer_ = function(show) { | 302 WallpaperManager.prototype.showCustomContainer_ = function(showCustom) { |
| 273 if (show) { | 303 $('category-container').hidden = showCustom; |
| 274 this.dialogDom_.querySelector('#category-container').hidden = true; | 304 $('custom-container').hidden = !showCustom; |
| 275 this.dialogDom_.querySelector('#custom-container').hidden = false; | |
| 276 } else { | |
| 277 this.dialogDom_.querySelector('#category-container').hidden = false; | |
| 278 this.dialogDom_.querySelector('#custom-container').hidden = true; | |
| 279 } | |
| 280 }; | 305 }; |
| 281 | 306 |
| 282 /** | 307 /** |
| 283 * Handles user clicking on a different category. | 308 * Handles user clicking on a different category. |
| 284 */ | 309 */ |
| 285 WallpaperManager.prototype.onCategoriesChange_ = function() { | 310 WallpaperManager.prototype.onCategoriesChange_ = function() { |
| 286 var categoriesList = this.categoriesList_; | 311 var categoriesList = this.categoriesList_; |
| 287 var selectedIndex = categoriesList.selectionModel.selectedIndex; | 312 var selectedIndex = categoriesList.selectionModel.selectedIndex; |
| 288 if (selectedIndex == -1) | 313 if (selectedIndex == -1) |
| 289 return; | 314 return; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 305 authorWebsite: this.manifest_.wallpaper_list[key].author_website | 330 authorWebsite: this.manifest_.wallpaper_list[key].author_website |
| 306 }; | 331 }; |
| 307 wallpapersDataModel.push(wallpaperInfo); | 332 wallpapersDataModel.push(wallpaperInfo); |
| 308 } | 333 } |
| 309 } | 334 } |
| 310 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 335 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
| 311 } | 336 } |
| 312 }; | 337 }; |
| 313 | 338 |
| 314 })(); | 339 })(); |
| OLD | NEW |