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 * |
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
245 $('learn-more').href = LearnMoreURL; | 245 $('learn-more').href = LearnMoreURL; |
246 $('close-error').addEventListener('click', function() { | 246 $('close-error').addEventListener('click', function() { |
247 $('error-container').hidden = true; | 247 $('error-container').hidden = true; |
248 }); | 248 }); |
249 $('close-wallpaper-selection').addEventListener('click', function() { | 249 $('close-wallpaper-selection').addEventListener('click', function() { |
250 $('wallpaper-selection-container').hidden = true; | 250 $('wallpaper-selection-container').hidden = true; |
251 $('set-wallpaper-layout').disabled = true; | 251 $('set-wallpaper-layout').disabled = true; |
252 }); | 252 }); |
253 | 253 |
254 this.onResize_(); | 254 this.onResize_(); |
255 this.initContextMenuAndCommand_(); | |
255 }; | 256 }; |
256 | 257 |
257 /** | 258 /** |
259 * One-time initialization of context menu and command. | |
260 */ | |
261 WallpaperManager.prototype.initContextMenuAndCommand_ = function() { | |
262 this.wallpaperContextMenu_ = $('wallpaper-context-menu'); | |
263 cr.ui.Menu.decorate(this.wallpaperContextMenu_); | |
264 cr.ui.contextMenuHandler.setContextMenu(this.wallpaperGrid_, | |
265 this.wallpaperContextMenu_); | |
266 var commands = this.dialogDom_.querySelectorAll('command'); | |
267 for (var i = 0; i < commands.length; i++) | |
268 cr.ui.Command.decorate(commands[i]); | |
269 | |
270 var doc = this.document_; | |
271 doc.addEventListener('command', this.onCommand_.bind(this)); | |
272 doc.addEventListener('canExecute', this.onCommandCanExecute_.bind(this)); | |
273 }; | |
274 | |
275 /** | |
276 * Handles a command being executed. | |
277 * @param {Event} event A command event. | |
278 */ | |
279 WallpaperManager.prototype.onCommand_ = function(event) { | |
280 if (event.command.id == 'delete') { | |
281 var wallpaperGrid = this.wallpaperGrid_; | |
282 var selectedIndex = wallpaperGrid.selectionModel.selectedIndex; | |
283 var item = wallpaperGrid.dataModel.item(selectedIndex); | |
284 if (!item || item.source != wallpapers.WallpaperSourceEnum.Custom) | |
285 return; | |
286 this.removeCustomWallpaper(item.baseURL); | |
287 wallpaperGrid.dataModel.splice(selectedIndex, 1); | |
288 // The number of remaining custom wallpapers. The add new button in data | |
flackr
2013/03/21 21:51:19
s/The number of/Calculate the number of
The secon
bshe
2013/03/22 15:11:57
Done.
| |
289 // model needs to be excluded. | |
290 var number = wallpaperGrid.dataModel.length - 1; | |
flackr
2013/03/21 21:51:19
s/number/customWallpaperCount
bshe
2013/03/22 15:11:57
Done.
| |
291 if (number == 0) { | |
292 // Active custom wallpaper is also copied in chronos data dir. It needs | |
293 // to be deleted. | |
294 chrome.wallpaperPrivate.resetWallpaper(); | |
295 } else { | |
296 selectedIndex = Math.min(selectedIndex, number - 1); | |
297 wallpaperGrid.selectionModel.selectedIndex = selectedIndex; | |
298 } | |
299 event.cancelBubble = true; | |
300 } | |
301 }; | |
302 | |
303 /** | |
304 * Decides if a command can be executed on current target. | |
305 * @param {Event} event A command event. | |
306 */ | |
307 WallpaperManager.prototype.onCommandCanExecute_ = function(event) { | |
308 if (event.command.id == 'delete') { | |
309 var wallpaperGrid = this.wallpaperGrid_; | |
310 var selectedIndex = wallpaperGrid.selectionModel.selectedIndex; | |
311 if (selectedIndex != -1 && | |
312 selectedIndex != this.wallpaperGrid_.dataModel.length - 1) { | |
313 event.canExecute = true; | |
314 return; | |
315 } | |
flackr
2013/03/21 21:51:19
No check for if this is a custom wallpaper or not?
bshe
2013/03/22 15:11:57
Good call. I have hide the context menu for online
| |
316 } | |
317 event.canExecute = false; | |
flackr
2013/03/21 21:51:19
Nit: Usually the pattern followed is to have multi
bshe
2013/03/22 15:11:57
I was thinking to use this function if we ever wan
| |
318 }; | |
319 | |
320 /** | |
258 * Preset to the category which contains current wallpaper. | 321 * Preset to the category which contains current wallpaper. |
259 */ | 322 */ |
260 WallpaperManager.prototype.presetCategory_ = function() { | 323 WallpaperManager.prototype.presetCategory_ = function() { |
261 this.currentWallpaper_ = str('currentWallpaper'); | 324 this.currentWallpaper_ = str('currentWallpaper'); |
262 // The currentWallpaper_ is either a url contains HightResolutionSuffix or a | 325 // The currentWallpaper_ is either a url contains HightResolutionSuffix or a |
263 // custom wallpaper file name converted from an integer value represent | 326 // custom wallpaper file name converted from an integer value represent |
264 // time (e.g., 13006377367586070). | 327 // time (e.g., 13006377367586070). |
265 if (this.currentWallpaper_ && | 328 if (this.currentWallpaper_ && |
266 this.currentWallpaper_.indexOf(HighResolutionSuffix) == -1) { | 329 this.currentWallpaper_.indexOf(HighResolutionSuffix) == -1) { |
267 // Custom is the last one in the categories list. | 330 // Custom is the last one in the categories list. |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
421 * wallpaper, removes the second oldest one to free some space. This should | 484 * wallpaper, removes the second oldest one to free some space. This should |
422 * only be called when exceeding wallpaper quota. | 485 * only be called when exceeding wallpaper quota. |
423 */ | 486 */ |
424 WallpaperManager.prototype.removeOldestWallpaper_ = function() { | 487 WallpaperManager.prototype.removeOldestWallpaper_ = function() { |
425 // Custom wallpapers should already sorted when put to the data model. The | 488 // Custom wallpapers should already sorted when put to the data model. The |
426 // last element is the add new button, need to exclude it as well. | 489 // last element is the add new button, need to exclude it as well. |
427 var oldestIndex = this.wallpaperGrid_.dataModel.length - 2; | 490 var oldestIndex = this.wallpaperGrid_.dataModel.length - 2; |
428 var item = this.wallpaperGrid_.dataModel.item(oldestIndex); | 491 var item = this.wallpaperGrid_.dataModel.item(oldestIndex); |
429 if (!item || item.source != wallpapers.WallpaperSourceEnum.Custom) | 492 if (!item || item.source != wallpapers.WallpaperSourceEnum.Custom) |
430 return; | 493 return; |
431 console.error(item.baseURL); | |
432 if (item.baseURL == this.currentWallpaper_) | 494 if (item.baseURL == this.currentWallpaper_) |
433 item = this.wallpaperGrid_.dataModel.item(--oldestIndex); | 495 item = this.wallpaperGrid_.dataModel.item(--oldestIndex); |
434 if (item) { | 496 if (item) { |
435 this.removeCustomWallpaper(item.baseURL); | 497 this.removeCustomWallpaper(item.baseURL); |
436 this.wallpaperGrid_.dataModel.splice(oldestIndex, 1); | 498 this.wallpaperGrid_.dataModel.splice(oldestIndex, 1); |
437 } | 499 } |
438 }; | 500 }; |
439 | 501 |
440 /* | 502 /* |
441 * Shows an error message to user and log the failed reason in console. | 503 * Shows an error message to user and log the failed reason in console. |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
775 if (selectedIndex == -1) | 837 if (selectedIndex == -1) |
776 return; | 838 return; |
777 var selectedListItem = categoriesList.getListItemByIndex(selectedIndex); | 839 var selectedListItem = categoriesList.getListItemByIndex(selectedIndex); |
778 var bar = $('bar'); | 840 var bar = $('bar'); |
779 bar.style.left = selectedListItem.offsetLeft + 'px'; | 841 bar.style.left = selectedListItem.offsetLeft + 'px'; |
780 bar.style.width = selectedListItem.offsetWidth + 'px'; | 842 bar.style.width = selectedListItem.offsetWidth + 'px'; |
781 | 843 |
782 var wallpapersDataModel = new cr.ui.ArrayDataModel([]); | 844 var wallpapersDataModel = new cr.ui.ArrayDataModel([]); |
783 var selectedItem; | 845 var selectedItem; |
784 if (selectedListItem.custom) { | 846 if (selectedListItem.custom) { |
785 $('online-wallpaper-attribute').hidden = true; | 847 this.document_.body.setAttribute('custom', ''); |
786 var errorHandler = this.onFileSystemError_.bind(this); | 848 var errorHandler = this.onFileSystemError_.bind(this); |
787 var toArray = function(list) { | 849 var toArray = function(list) { |
788 return Array.prototype.slice.call(list || [], 0); | 850 return Array.prototype.slice.call(list || [], 0); |
789 } | 851 } |
790 | 852 |
791 var self = this; | 853 var self = this; |
792 var processResults = function(entries) { | 854 var processResults = function(entries) { |
793 for (var i = 0; i < entries.length; i++) { | 855 for (var i = 0; i < entries.length; i++) { |
794 var entry = entries[i]; | 856 var entry = entries[i]; |
795 var wallpaperInfo = { | 857 var wallpaperInfo = { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
832 entries = entries.concat(toArray(results)); | 894 entries = entries.concat(toArray(results)); |
833 readEntries(); | 895 readEntries(); |
834 } | 896 } |
835 }, errorHandler); | 897 }, errorHandler); |
836 }; | 898 }; |
837 readEntries(); // Start reading dirs. | 899 readEntries(); // Start reading dirs. |
838 } | 900 } |
839 this.wallpaperDirs_.getDirectory(WallpaperDirNameEnum.ORIGINAL, | 901 this.wallpaperDirs_.getDirectory(WallpaperDirNameEnum.ORIGINAL, |
840 success, errorHandler); | 902 success, errorHandler); |
841 } else { | 903 } else { |
842 $('online-wallpaper-attribute').hidden = false; | 904 this.document_.body.removeAttribute('custom'); |
843 for (var key in this.manifest_.wallpaper_list) { | 905 for (var key in this.manifest_.wallpaper_list) { |
844 if (selectedIndex == AllCategoryIndex || | 906 if (selectedIndex == AllCategoryIndex || |
845 this.manifest_.wallpaper_list[key].categories.indexOf( | 907 this.manifest_.wallpaper_list[key].categories.indexOf( |
846 selectedIndex - OnlineCategoriesOffset) != -1) { | 908 selectedIndex - OnlineCategoriesOffset) != -1) { |
847 var wallpaperInfo = { | 909 var wallpaperInfo = { |
848 baseURL: this.manifest_.wallpaper_list[key].base_url, | 910 baseURL: this.manifest_.wallpaper_list[key].base_url, |
849 layout: this.manifest_.wallpaper_list[key].default_layout, | 911 layout: this.manifest_.wallpaper_list[key].default_layout, |
850 source: wallpapers.WallpaperSourceEnum.Online, | 912 source: wallpapers.WallpaperSourceEnum.Online, |
851 availableOffline: false, | 913 availableOffline: false, |
852 author: this.manifest_.wallpaper_list[key].author, | 914 author: this.manifest_.wallpaper_list[key].author, |
(...skipping 15 matching lines...) Expand all Loading... | |
868 } | 930 } |
869 } | 931 } |
870 } | 932 } |
871 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 933 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
872 this.wallpaperGrid_.selectedItem = selectedItem; | 934 this.wallpaperGrid_.selectedItem = selectedItem; |
873 this.wallpaperGrid_.activeItem = selectedItem; | 935 this.wallpaperGrid_.activeItem = selectedItem; |
874 } | 936 } |
875 }; | 937 }; |
876 | 938 |
877 })(); | 939 })(); |
OLD | NEW |