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 cr.define('wallpapers', function() { | 5 cr.define('wallpapers', function() { |
6 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; | 6 /** @const */ var ArrayDataModel = cr.ui.ArrayDataModel; |
7 /** @const */ var Grid = cr.ui.Grid; | 7 /** @const */ var Grid = cr.ui.Grid; |
8 /** @const */ var GridItem = cr.ui.GridItem; | 8 /** @const */ var GridItem = cr.ui.GridItem; |
9 /** @const */ var GridSelectionController = cr.ui.GridSelectionController; | 9 /** @const */ var GridSelectionController = cr.ui.GridSelectionController; |
10 /** @const */ var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; | 10 /** @const */ var ListSingleSelectionModel = cr.ui.ListSingleSelectionModel; |
11 /** @const */ var ThumbnailSuffix = '_thumbnail.png'; | 11 /** @const */ var ThumbnailSuffix = '_thumbnail.png'; |
12 | 12 |
13 /** | 13 /** |
| 14 * Wallpaper sources enum. |
| 15 */ |
| 16 /** #const */ var WallpaperSourceEnum = { |
| 17 Online: 'ONLINE', |
| 18 Custom: 'CUSTOM', |
| 19 AddNew: 'ADDNEW' |
| 20 }; |
| 21 |
| 22 /** |
14 * Creates a new wallpaper thumbnails grid item. | 23 * Creates a new wallpaper thumbnails grid item. |
15 * @param {{baseURL: string, dynamicURL: string, layout: string, | 24 * @param {{baseURL: string, layout: string, source: string, |
16 * author: string, authorWebsite: string, availableOffline: boolean}} | 25 * availableOffline: boolean, opt_dynamicURL: string, |
17 * wallpaperInfo Wallpaper baseURL, dynamicURL, layout, author and | 26 * opt_author: string, opt_authorWebsite: string}} |
18 * author website. | 27 * wallpaperInfo Wallpaper data item in WallpaperThumbnailsGrid's data |
| 28 * model. |
19 * @constructor | 29 * @constructor |
20 * @extends {cr.ui.GridItem} | 30 * @extends {cr.ui.GridItem} |
21 */ | 31 */ |
22 function WallpaperThumbnailsGridItem(wallpaperInfo) { | 32 function WallpaperThumbnailsGridItem(wallpaperInfo) { |
23 var el = new GridItem(wallpaperInfo); | 33 var el = new GridItem(wallpaperInfo); |
24 el.__proto__ = WallpaperThumbnailsGridItem.prototype; | 34 el.__proto__ = WallpaperThumbnailsGridItem.prototype; |
25 return el; | 35 return el; |
26 } | 36 } |
27 | 37 |
28 WallpaperThumbnailsGridItem.prototype = { | 38 WallpaperThumbnailsGridItem.prototype = { |
29 __proto__: GridItem.prototype, | 39 __proto__: GridItem.prototype, |
30 | 40 |
31 /** @override */ | 41 /** @override */ |
32 decorate: function() { | 42 decorate: function() { |
33 GridItem.prototype.decorate.call(this); | 43 GridItem.prototype.decorate.call(this); |
34 // Removes garbage created by GridItem. | 44 // Removes garbage created by GridItem. |
35 this.innerText = ''; | 45 this.innerText = ''; |
36 var imageEl = cr.doc.createElement('img'); | 46 var imageEl = cr.doc.createElement('img'); |
37 imageEl.classList.add('thumbnail'); | 47 imageEl.classList.add('thumbnail'); |
38 cr.defineProperty(imageEl, 'offline', cr.PropertyKind.BOOL_ATTR); | 48 cr.defineProperty(imageEl, 'offline', cr.PropertyKind.BOOL_ATTR); |
39 imageEl.offline = this.dataItem.availableOffline; | 49 imageEl.offline = this.dataItem.availableOffline; |
40 this.appendChild(imageEl); | 50 this.appendChild(imageEl); |
| 51 var self = this; |
41 | 52 |
42 var self = this; | 53 switch (this.dataItem.source) { |
43 chrome.wallpaperPrivate.getThumbnail(this.dataItem.baseURL, 'ONLINE', | 54 case WallpaperSourceEnum.AddNew: |
44 function(data) { | 55 this.id = 'add-new'; |
45 if (data) { | 56 this.addEventListener('click', function(e) { |
46 var blob = new Blob([new Int8Array(data)], {'type' : 'image\/png'}); | 57 $('wallpaper-selection-container').hidden = false; |
47 imageEl.src = window.URL.createObjectURL(blob); | |
48 imageEl.addEventListener('load', function(e) { | |
49 window.URL.revokeObjectURL(this.src); | |
50 }); | 58 }); |
51 } else { | 59 break; |
52 var xhr = new XMLHttpRequest(); | 60 case WallpaperSourceEnum.Custom: |
53 xhr.open('GET', self.dataItem.baseURL + ThumbnailSuffix, true); | 61 var errorHandler = function(e) { |
54 xhr.responseType = 'arraybuffer'; | 62 console.error('Can not access file system.'); |
55 xhr.send(null); | 63 }; |
56 xhr.addEventListener('load', function(e) { | 64 var wallpaperDirectories = WallpaperDirectories.getInstance(); |
57 if (xhr.status === 200) { | 65 var getThumbnail = function(fileName) { |
58 chrome.wallpaperPrivate.saveThumbnail(self.dataItem.baseURL, | 66 var setURL = function(fileEntry) { |
59 xhr.response); | 67 imageEl.src = fileEntry.toURL(); |
60 var blob = new Blob([new Int8Array(xhr.response)], | 68 }; |
61 {'type' : 'image\/png'}); | 69 var fallback = function() { |
| 70 wallpaperDirectories.getDirectory(WallpaperDirNameEnum.ORIGINAL, |
| 71 function(dirEntry) { |
| 72 dirEntry.getFile(fileName, {create: false}, setURL, |
| 73 errorHandler); |
| 74 }, errorHandler); |
| 75 }; |
| 76 var success = function(dirEntry) { |
| 77 dirEntry.getFile(fileName, {create: false}, setURL, fallback); |
| 78 }; |
| 79 wallpaperDirectories.getDirectory(WallpaperDirNameEnum.THUMBNAIL, |
| 80 success, |
| 81 errorHandler); |
| 82 } |
| 83 getThumbnail(self.dataItem.baseURL); |
| 84 break; |
| 85 case WallpaperSourceEnum.Online: |
| 86 chrome.wallpaperPrivate.getThumbnail(this.dataItem.baseURL, |
| 87 this.dataItem.source, |
| 88 function(data) { |
| 89 if (data) { |
| 90 var blob = new Blob([new Int8Array(data)], |
| 91 {'type': 'image\/png'}); |
62 imageEl.src = window.URL.createObjectURL(blob); | 92 imageEl.src = window.URL.createObjectURL(blob); |
63 // TODO(bshe): We currently use empty div to reserve space for | |
64 // thumbnail. Use a placeholder like "loading" image may better. | |
65 imageEl.addEventListener('load', function(e) { | 93 imageEl.addEventListener('load', function(e) { |
66 window.URL.revokeObjectURL(this.src); | 94 window.URL.revokeObjectURL(this.src); |
67 }); | 95 }); |
| 96 } else if (self.dataItem.source == WallpaperSourceEnum.Online) { |
| 97 var xhr = new XMLHttpRequest(); |
| 98 xhr.open('GET', self.dataItem.baseURL + ThumbnailSuffix, true); |
| 99 xhr.responseType = 'arraybuffer'; |
| 100 xhr.send(null); |
| 101 xhr.addEventListener('load', function(e) { |
| 102 if (xhr.status === 200) { |
| 103 chrome.wallpaperPrivate.saveThumbnail(self.dataItem.baseURL, |
| 104 xhr.response); |
| 105 var blob = new Blob([new Int8Array(xhr.response)], |
| 106 {'type' : 'image\/png'}); |
| 107 imageEl.src = window.URL.createObjectURL(blob); |
| 108 // TODO(bshe): We currently use empty div to reserve space for |
| 109 // thumbnail. Use a placeholder like "loading" image may |
| 110 // better. |
| 111 imageEl.addEventListener('load', function(e) { |
| 112 window.URL.revokeObjectURL(this.src); |
| 113 }); |
| 114 } |
| 115 }); |
68 } | 116 } |
69 }); | 117 }); |
70 } | 118 break; |
71 }); | 119 default: |
| 120 console.error('Unsupported image source.'); |
| 121 } |
72 }, | 122 }, |
73 }; | 123 }; |
74 | 124 |
75 /** | 125 /** |
76 * Creates a selection controller that wraps selection on grid ends | 126 * Creates a selection controller that wraps selection on grid ends |
77 * and translates Enter presses into 'activate' events. | 127 * and translates Enter presses into 'activate' events. |
78 * @param {cr.ui.ListSelectionModel} selectionModel The selection model to | 128 * @param {cr.ui.ListSelectionModel} selectionModel The selection model to |
79 * interact with. | 129 * interact with. |
80 * @param {cr.ui.Grid} grid The grid to interact with. | 130 * @param {cr.ui.Grid} grid The grid to interact with. |
81 * @constructor | 131 * @constructor |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 */ | 283 */ |
234 redraw: function() { | 284 redraw: function() { |
235 Grid.prototype.redraw.call(this); | 285 Grid.prototype.redraw.call(this); |
236 // The active thumbnail maybe deleted in the above redraw(). Sets it again | 286 // The active thumbnail maybe deleted in the above redraw(). Sets it again |
237 // to make sure checkmark shows correctly. | 287 // to make sure checkmark shows correctly. |
238 this.updateActiveThumb_(); | 288 this.updateActiveThumb_(); |
239 } | 289 } |
240 }; | 290 }; |
241 | 291 |
242 return { | 292 return { |
| 293 WallpaperSourceEnum: WallpaperSourceEnum, |
243 WallpaperThumbnailsGrid: WallpaperThumbnailsGrid | 294 WallpaperThumbnailsGrid: WallpaperThumbnailsGrid |
244 }; | 295 }; |
245 }); | 296 }); |
OLD | NEW |