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 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 $('learn-more').href = LearnMoreURL; | 320 $('learn-more').href = LearnMoreURL; |
321 $('close-error').addEventListener('click', function() { | 321 $('close-error').addEventListener('click', function() { |
322 $('error-container').hidden = true; | 322 $('error-container').hidden = true; |
323 }); | 323 }); |
324 $('close-wallpaper-selection').addEventListener('click', function() { | 324 $('close-wallpaper-selection').addEventListener('click', function() { |
325 $('wallpaper-selection-container').hidden = true; | 325 $('wallpaper-selection-container').hidden = true; |
326 $('set-wallpaper-layout').disabled = true; | 326 $('set-wallpaper-layout').disabled = true; |
327 }); | 327 }); |
328 | 328 |
329 this.onResize_(); | 329 this.onResize_(); |
| 330 this.initContextMenuAndCommand_(); |
330 }; | 331 }; |
331 | 332 |
332 /** | 333 /** |
| 334 * One-time initialization of context menu and command. |
| 335 */ |
| 336 WallpaperManager.prototype.initContextMenuAndCommand_ = function() { |
| 337 this.wallpaperContextMenu_ = $('wallpaper-context-menu'); |
| 338 cr.ui.Menu.decorate(this.wallpaperContextMenu_); |
| 339 cr.ui.contextMenuHandler.setContextMenu(this.wallpaperGrid_, |
| 340 this.wallpaperContextMenu_); |
| 341 var commands = this.dialogDom_.querySelectorAll('command'); |
| 342 for (var i = 0; i < commands.length; i++) |
| 343 cr.ui.Command.decorate(commands[i]); |
| 344 |
| 345 var doc = this.document_; |
| 346 doc.addEventListener('command', this.onCommand_.bind(this)); |
| 347 doc.addEventListener('canExecute', this.onCommandCanExecute_.bind(this)); |
| 348 }; |
| 349 |
| 350 /** |
| 351 * Handles a command being executed. |
| 352 * @param {Event} event A command event. |
| 353 */ |
| 354 WallpaperManager.prototype.onCommand_ = function(event) { |
| 355 if (event.command.id == 'delete') { |
| 356 var wallpaperGrid = this.wallpaperGrid_; |
| 357 var selectedIndex = wallpaperGrid.selectionModel.selectedIndex; |
| 358 var item = wallpaperGrid.dataModel.item(selectedIndex); |
| 359 if (!item || item.source != wallpapers.WallpaperSourceEnum.Custom) |
| 360 return; |
| 361 this.removeCustomWallpaper(item.baseURL); |
| 362 wallpaperGrid.dataModel.splice(selectedIndex, 1); |
| 363 // Calculate the number of remaining custom wallpapers. The add new button |
| 364 // in data model needs to be excluded. |
| 365 var customWallpaperCount = wallpaperGrid.dataModel.length - 1; |
| 366 if (customWallpaperCount == 0) { |
| 367 // Active custom wallpaper is also copied in chronos data dir. It needs |
| 368 // to be deleted. |
| 369 chrome.wallpaperPrivate.resetWallpaper(); |
| 370 } else { |
| 371 selectedIndex = Math.min(selectedIndex, customWallpaperCount - 1); |
| 372 wallpaperGrid.selectionModel.selectedIndex = selectedIndex; |
| 373 } |
| 374 event.cancelBubble = true; |
| 375 } |
| 376 }; |
| 377 |
| 378 /** |
| 379 * Decides if a command can be executed on current target. |
| 380 * @param {Event} event A command event. |
| 381 */ |
| 382 WallpaperManager.prototype.onCommandCanExecute_ = function(event) { |
| 383 switch (event.command.id) { |
| 384 case 'delete': |
| 385 var wallpaperGrid = this.wallpaperGrid_; |
| 386 var selectedIndex = wallpaperGrid.selectionModel.selectedIndex; |
| 387 var item = wallpaperGrid.dataModel.item(selectedIndex); |
| 388 if (selectedIndex != this.wallpaperGrid_.dataModel.length - 1 && |
| 389 item && item.source == wallpapers.WallpaperSourceEnum.Custom) { |
| 390 event.canExecute = true; |
| 391 break; |
| 392 } |
| 393 default: |
| 394 event.canExecute = false; |
| 395 } |
| 396 }; |
| 397 |
| 398 /** |
333 * Preset to the category which contains current wallpaper. | 399 * Preset to the category which contains current wallpaper. |
334 */ | 400 */ |
335 WallpaperManager.prototype.presetCategory_ = function() { | 401 WallpaperManager.prototype.presetCategory_ = function() { |
336 this.currentWallpaper_ = str('currentWallpaper'); | 402 this.currentWallpaper_ = str('currentWallpaper'); |
337 // The currentWallpaper_ is either a url contains HightResolutionSuffix or a | 403 // The currentWallpaper_ is either a url contains HightResolutionSuffix or a |
338 // custom wallpaper file name converted from an integer value represent | 404 // custom wallpaper file name converted from an integer value represent |
339 // time (e.g., 13006377367586070). | 405 // time (e.g., 13006377367586070). |
340 if (this.currentWallpaper_ && | 406 if (this.currentWallpaper_ && |
341 this.currentWallpaper_.indexOf( | 407 this.currentWallpaper_.indexOf( |
342 this.backgroundPage_.HighResolutionSuffix) == -1) { | 408 this.backgroundPage_.HighResolutionSuffix) == -1) { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 * wallpaper, removes the second oldest one to free some space. This should | 564 * wallpaper, removes the second oldest one to free some space. This should |
499 * only be called when exceeding wallpaper quota. | 565 * only be called when exceeding wallpaper quota. |
500 */ | 566 */ |
501 WallpaperManager.prototype.removeOldestWallpaper_ = function() { | 567 WallpaperManager.prototype.removeOldestWallpaper_ = function() { |
502 // Custom wallpapers should already sorted when put to the data model. The | 568 // Custom wallpapers should already sorted when put to the data model. The |
503 // last element is the add new button, need to exclude it as well. | 569 // last element is the add new button, need to exclude it as well. |
504 var oldestIndex = this.wallpaperGrid_.dataModel.length - 2; | 570 var oldestIndex = this.wallpaperGrid_.dataModel.length - 2; |
505 var item = this.wallpaperGrid_.dataModel.item(oldestIndex); | 571 var item = this.wallpaperGrid_.dataModel.item(oldestIndex); |
506 if (!item || item.source != wallpapers.WallpaperSourceEnum.Custom) | 572 if (!item || item.source != wallpapers.WallpaperSourceEnum.Custom) |
507 return; | 573 return; |
508 console.error(item.baseURL); | |
509 if (item.baseURL == this.currentWallpaper_) | 574 if (item.baseURL == this.currentWallpaper_) |
510 item = this.wallpaperGrid_.dataModel.item(--oldestIndex); | 575 item = this.wallpaperGrid_.dataModel.item(--oldestIndex); |
511 if (item) { | 576 if (item) { |
512 this.removeCustomWallpaper(item.baseURL); | 577 this.removeCustomWallpaper(item.baseURL); |
513 this.wallpaperGrid_.dataModel.splice(oldestIndex, 1); | 578 this.wallpaperGrid_.dataModel.splice(oldestIndex, 1); |
514 } | 579 } |
515 }; | 580 }; |
516 | 581 |
517 /* | 582 /* |
518 * Shows an error message to user and log the failed reason in console. | 583 * 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... |
852 if (selectedIndex == -1) | 917 if (selectedIndex == -1) |
853 return; | 918 return; |
854 var selectedListItem = categoriesList.getListItemByIndex(selectedIndex); | 919 var selectedListItem = categoriesList.getListItemByIndex(selectedIndex); |
855 var bar = $('bar'); | 920 var bar = $('bar'); |
856 bar.style.left = selectedListItem.offsetLeft + 'px'; | 921 bar.style.left = selectedListItem.offsetLeft + 'px'; |
857 bar.style.width = selectedListItem.offsetWidth + 'px'; | 922 bar.style.width = selectedListItem.offsetWidth + 'px'; |
858 | 923 |
859 var wallpapersDataModel = new cr.ui.ArrayDataModel([]); | 924 var wallpapersDataModel = new cr.ui.ArrayDataModel([]); |
860 var selectedItem; | 925 var selectedItem; |
861 if (selectedListItem.custom) { | 926 if (selectedListItem.custom) { |
862 $('online-wallpaper-attribute').hidden = true; | 927 this.document_.body.setAttribute('custom', ''); |
863 var errorHandler = this.onFileSystemError_.bind(this); | 928 var errorHandler = this.onFileSystemError_.bind(this); |
864 var toArray = function(list) { | 929 var toArray = function(list) { |
865 return Array.prototype.slice.call(list || [], 0); | 930 return Array.prototype.slice.call(list || [], 0); |
866 } | 931 } |
867 | 932 |
868 var self = this; | 933 var self = this; |
869 var processResults = function(entries) { | 934 var processResults = function(entries) { |
870 for (var i = 0; i < entries.length; i++) { | 935 for (var i = 0; i < entries.length; i++) { |
871 var entry = entries[i]; | 936 var entry = entries[i]; |
872 var wallpaperInfo = { | 937 var wallpaperInfo = { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
909 entries = entries.concat(toArray(results)); | 974 entries = entries.concat(toArray(results)); |
910 readEntries(); | 975 readEntries(); |
911 } | 976 } |
912 }, errorHandler); | 977 }, errorHandler); |
913 }; | 978 }; |
914 readEntries(); // Start reading dirs. | 979 readEntries(); // Start reading dirs. |
915 } | 980 } |
916 this.wallpaperDirs_.getDirectory(WallpaperDirNameEnum.ORIGINAL, | 981 this.wallpaperDirs_.getDirectory(WallpaperDirNameEnum.ORIGINAL, |
917 success, errorHandler); | 982 success, errorHandler); |
918 } else { | 983 } else { |
919 $('online-wallpaper-attribute').hidden = false; | 984 this.document_.body.removeAttribute('custom'); |
920 for (var key in this.manifest_.wallpaper_list) { | 985 for (var key in this.manifest_.wallpaper_list) { |
921 if (selectedIndex == AllCategoryIndex || | 986 if (selectedIndex == AllCategoryIndex || |
922 this.manifest_.wallpaper_list[key].categories.indexOf( | 987 this.manifest_.wallpaper_list[key].categories.indexOf( |
923 selectedIndex - OnlineCategoriesOffset) != -1) { | 988 selectedIndex - OnlineCategoriesOffset) != -1) { |
924 var wallpaperInfo = { | 989 var wallpaperInfo = { |
925 baseURL: this.manifest_.wallpaper_list[key].base_url, | 990 baseURL: this.manifest_.wallpaper_list[key].base_url, |
926 layout: this.manifest_.wallpaper_list[key].default_layout, | 991 layout: this.manifest_.wallpaper_list[key].default_layout, |
927 source: wallpapers.WallpaperSourceEnum.Online, | 992 source: wallpapers.WallpaperSourceEnum.Online, |
928 availableOffline: false, | 993 availableOffline: false, |
929 author: this.manifest_.wallpaper_list[key].author, | 994 author: this.manifest_.wallpaper_list[key].author, |
(...skipping 15 matching lines...) Expand all Loading... |
945 } | 1010 } |
946 } | 1011 } |
947 } | 1012 } |
948 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 1013 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
949 this.wallpaperGrid_.selectedItem = selectedItem; | 1014 this.wallpaperGrid_.selectedItem = selectedItem; |
950 this.wallpaperGrid_.activeItem = selectedItem; | 1015 this.wallpaperGrid_.activeItem = selectedItem; |
951 } | 1016 } |
952 }; | 1017 }; |
953 | 1018 |
954 })(); | 1019 })(); |
OLD | NEW |