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 cr.define('options', function() { | 5 cr.define('options', function() { |
6 | 6 |
7 var OptionsPage = options.OptionsPage; | 7 var OptionsPage = options.OptionsPage; |
8 var UserImagesGrid = options.UserImagesGrid; | 8 var UserImagesGrid = options.UserImagesGrid; |
9 | 9 |
| 10 /** @const */ var CUSTOM_WALLPAPER_PREFIX = 'chrome://wallpaper/custom_'; |
| 11 |
10 ///////////////////////////////////////////////////////////////////////////// | 12 ///////////////////////////////////////////////////////////////////////////// |
11 // SetWallpaperOptions class: | 13 // SetWallpaperOptions class: |
12 | 14 |
13 /** | 15 /** |
14 * Encapsulated handling of ChromeOS set wallpaper options page. | 16 * Encapsulated handling of ChromeOS set wallpaper options page. |
15 * @constructor | 17 * @constructor |
16 */ | 18 */ |
17 function SetWallpaperOptions() { | 19 function SetWallpaperOptions() { |
18 OptionsPage.call( | 20 OptionsPage.call( |
19 this, | 21 this, |
(...skipping 18 matching lines...) Expand all Loading... |
38 var wallpaperGrid = $('wallpaper-grid'); | 40 var wallpaperGrid = $('wallpaper-grid'); |
39 UserImagesGrid.decorate(wallpaperGrid); | 41 UserImagesGrid.decorate(wallpaperGrid); |
40 | 42 |
41 wallpaperGrid.addEventListener('change', | 43 wallpaperGrid.addEventListener('change', |
42 this.handleImageSelected_.bind(this)); | 44 this.handleImageSelected_.bind(this)); |
43 wallpaperGrid.addEventListener('dblclick', | 45 wallpaperGrid.addEventListener('dblclick', |
44 this.handleImageDblClick_.bind(this)); | 46 this.handleImageDblClick_.bind(this)); |
45 wallpaperGrid.addEventListener('activate', | 47 wallpaperGrid.addEventListener('activate', |
46 function() { OptionsPage.closeOverlay() }); | 48 function() { OptionsPage.closeOverlay() }); |
47 | 49 |
| 50 $('set-custom-wallpaper').addEventListener('click', |
| 51 this.handleChooseFile_); |
| 52 $('set-wallpaper-layout').addEventListener('change', |
| 53 this.handleLayoutChange_); |
48 $('use-random-wallpaper').onclick = this.handleCheckboxClick_.bind(this); | 54 $('use-random-wallpaper').onclick = this.handleCheckboxClick_.bind(this); |
49 $('set-wallpaper-overlay-confirm').onclick = function() { | 55 $('set-wallpaper-overlay-confirm').onclick = function() { |
50 OptionsPage.closeOverlay(); | 56 OptionsPage.closeOverlay(); |
51 }; | 57 }; |
52 | 58 |
53 // @type {Array.<author: string, url: string, website: string>} | 59 // @type {Array.<author: string, url: string, website: string>} |
54 this.wallpapers_ = []; | 60 this.wallpapers_ = []; |
55 | 61 |
| 62 // @type {Object} Old user custom wallpaper thumbnail. |
| 63 this.oldImage_ = null; |
| 64 |
56 chrome.send('onSetWallpaperPageInitialized'); | 65 chrome.send('onSetWallpaperPageInitialized'); |
57 }, | 66 }, |
58 | 67 |
59 /** | 68 /** |
60 * Called right after the page has been shown to user. | 69 * Called right after the page has been shown to user. |
61 */ | 70 */ |
62 didShowPage: function() { | 71 didShowPage: function() { |
63 $('wallpaper-grid').updateAndFocus(); | 72 $('wallpaper-grid').updateAndFocus(); |
64 // A quick hack to fix issue 118472. This is a general problem of list | 73 // A quick hack to fix issue 118472. This is a general problem of list |
65 // control and options overlay. | 74 // control and options overlay. |
66 // TODO(bshe): Remove this hack when we fixed the general problem which | 75 // TODO(bshe): Remove this hack when we fixed the general problem which |
67 // tracked in issue 118829. | 76 // tracked in issue 118829. |
68 $('wallpaper-grid').redraw(); | 77 $('wallpaper-grid').redraw(); |
69 chrome.send('onSetWallpaperPageShown'); | 78 chrome.send('onSetWallpaperPageShown'); |
70 }, | 79 }, |
71 | 80 |
72 /** | 81 /** |
73 * Called right before the page is hidden. | 82 * Called right before the page is hidden. |
74 */ | 83 */ |
75 willHidePage: function() { | 84 willHidePage: function() { |
76 var wallpaperGrid = $('wallpaper-grid'); | 85 var wallpaperGrid = $('wallpaper-grid'); |
77 wallpaperGrid.blur(); | 86 wallpaperGrid.blur(); |
| 87 if (this.oldImage_) { |
| 88 wallpaperGrid.removeItem(this.oldImage_); |
| 89 this.oldImage_ = null; |
| 90 } |
| 91 $('set-wallpaper-layout').innerText = ''; |
78 }, | 92 }, |
79 | 93 |
80 /** | 94 /** |
81 * Set attributions of wallpaper with given URL. | 95 * Set attributions of wallpaper with given URL. |
82 * @param {string} url URL of the selected wallpaper. | 96 * @param {string} url URL of the selected wallpaper. |
83 * @private | 97 * @private |
84 */ | 98 */ |
85 setWallpaperAttribution_: function(url) { | 99 setWallpaperAttribution_: function(url) { |
86 for (var i = 0; i < this.wallpapers_.length; i++) { | 100 for (var i = 0; i < this.wallpapers_.length; i++) { |
87 if (this.wallpapers_[i].url == url) { | 101 if (this.wallpapers_[i].url == url) { |
88 $('author-name').textContent = this.wallpapers_[i].author; | 102 $('author-name').textContent = this.wallpapers_[i].author; |
89 $('author-website').textContent = this.wallpapers_[i].website; | 103 $('author-website').textContent = this.wallpapers_[i].website; |
90 return; | 104 return; |
91 } | 105 } |
92 } | 106 } |
93 $('author-name').textContent = ''; | 107 $('author-name').textContent = ''; |
94 $('author-website').textContent = ''; | 108 $('author-website').textContent = ''; |
95 }, | 109 }, |
96 | 110 |
97 /** | 111 /** |
| 112 * Populates the drop down box for custom wallpaper layouts. |
| 113 * param {string} layouts Available wallpaper layouts. |
| 114 * param {number} selectedLayout The value of selected/default layout. |
| 115 * @private |
| 116 */ |
| 117 populateWallpaperLayouts_: function(layouts, selectedLayout) { |
| 118 var wallpaperLayout = $('set-wallpaper-layout'); |
| 119 var selectedIndex = -1; |
| 120 for (var i = 0; i < layouts.length; i++) { |
| 121 var option = new Option(layouts[i]['name'], layouts[i]['index']); |
| 122 if (selectedLayout == option.value) |
| 123 selectedIndex = i; |
| 124 wallpaperLayout.appendChild(option); |
| 125 } |
| 126 if (selectedIndex >= 0) |
| 127 wallpaperLayout.selectedIndex = selectedIndex; |
| 128 }, |
| 129 |
| 130 /** |
| 131 * Handles "Custom..." button activation. |
| 132 * @private |
| 133 */ |
| 134 handleChooseFile_: function() { |
| 135 chrome.send('chooseWallpaper'); |
| 136 }, |
| 137 |
| 138 /** |
| 139 * Handle the wallpaper layout setting change. |
| 140 * @private |
| 141 */ |
| 142 handleLayoutChange_: function() { |
| 143 var setWallpaperLayout = $('set-wallpaper-layout'); |
| 144 var layout = setWallpaperLayout.options[ |
| 145 setWallpaperLayout.selectedIndex].value; |
| 146 chrome.send('changeWallpaperLayout', [layout]); |
| 147 }, |
| 148 |
| 149 /** |
98 * Handles image selection change. | 150 * Handles image selection change. |
99 * @private | 151 * @private |
100 */ | 152 */ |
101 handleImageSelected_: function() { | 153 handleImageSelected_: function() { |
102 var wallpaperGrid = $('wallpaper-grid'); | 154 var wallpaperGrid = $('wallpaper-grid'); |
103 var url = wallpaperGrid.selectedItemUrl; | 155 var url = wallpaperGrid.selectedItemUrl; |
104 if (url && | 156 if (url && |
105 !wallpaperGrid.inProgramSelection) { | 157 !wallpaperGrid.inProgramSelection) { |
| 158 if (url.indexOf(CUSTOM_WALLPAPER_PREFIX) == 0) { |
| 159 // User custom wallpaper is selected |
| 160 this.isCustom = true; |
| 161 // When users select the custom wallpaper thumbnail from picker UI, |
| 162 // use the saved layout value and redraw the wallpaper. |
| 163 this.handleLayoutChange_(); |
| 164 } else { |
| 165 this.isCustom = false; |
| 166 chrome.send('selectDefaultWallpaper', [url]); |
| 167 } |
106 this.setWallpaperAttribution_(url); | 168 this.setWallpaperAttribution_(url); |
107 chrome.send('selectDefaultWallpaper', [url]); | |
108 } | 169 } |
109 }, | 170 }, |
110 | 171 |
111 /** | 172 /** |
112 * Handles double click on the image grid. | 173 * Handles double click on the image grid. |
113 * @param {Event} e Double click Event. | 174 * @param {Event} e Double click Event. |
114 */ | 175 */ |
115 handleImageDblClick_: function(e) { | 176 handleImageDblClick_: function(e) { |
116 var wallpaperGrid = $('wallpaper-grid'); | 177 var wallpaperGrid = $('wallpaper-grid'); |
117 if (wallpaperGrid.disabled) | 178 if (wallpaperGrid.disabled) |
118 return; | 179 return; |
119 // Close page unless the click target is the grid itself. | 180 // Close page unless the click target is the grid itself. |
120 if (e.target instanceof HTMLImageElement) | 181 if (e.target instanceof HTMLImageElement) |
121 OptionsPage.closeOverlay(); | 182 OptionsPage.closeOverlay(); |
122 }, | 183 }, |
123 | 184 |
124 /** | 185 /** |
125 * Handles click on the "I'm feeling lucky" checkbox. | 186 * Handles click on the "I'm feeling lucky" checkbox. |
126 * @private | 187 * @private |
127 */ | 188 */ |
128 handleCheckboxClick_: function() { | 189 handleCheckboxClick_: function() { |
129 var wallpaperGrid = $('wallpaper-grid'); | 190 var wallpaperGrid = $('wallpaper-grid'); |
130 if ($('use-random-wallpaper').checked) { | 191 if ($('use-random-wallpaper').checked) { |
131 wallpaperGrid.disabled = true; | 192 wallpaperGrid.disabled = true; |
| 193 $('attribution-label').hidden = false; |
132 chrome.send('selectRandomWallpaper'); | 194 chrome.send('selectRandomWallpaper'); |
133 wallpaperGrid.classList.add('grayout'); | 195 wallpaperGrid.classList.add('grayout'); |
| 196 $('set-wallpaper-layout').hidden = true; |
134 } else { | 197 } else { |
135 wallpaperGrid.disabled = false; | 198 wallpaperGrid.disabled = false; |
136 wallpaperGrid.classList.remove('grayout'); | 199 wallpaperGrid.classList.remove('grayout'); |
137 // Set the wallpaper type to User::DEFAULT. | 200 // Set the wallpaper type to User::DEFAULT. |
138 this.handleImageSelected_(); | 201 this.handleImageSelected_(); |
139 } | 202 } |
140 }, | 203 }, |
141 | 204 |
142 /** | 205 /** |
143 * Selects corresponding wallpaper thumbnail with the given URL and toggle | 206 * Selects corresponding wallpaper thumbnail with the given URL and toggle |
(...skipping 27 matching lines...) Expand all Loading... |
171 // TODO(bshe): Ideally we should save author and website with the actual | 234 // TODO(bshe): Ideally we should save author and website with the actual |
172 // image (URL) and not use index related storage. This way this data is | 235 // image (URL) and not use index related storage. This way this data is |
173 // stored in one place rather than depending on the index to be | 236 // stored in one place rather than depending on the index to be |
174 // consistent. | 237 // consistent. |
175 for (var i = 0, wallpaper; wallpaper = wallpapers[i]; i++) { | 238 for (var i = 0, wallpaper; wallpaper = wallpapers[i]; i++) { |
176 this.wallpapers_.push(wallpaper); | 239 this.wallpapers_.push(wallpaper); |
177 wallpaperGrid.addItem(wallpaper.url); | 240 wallpaperGrid.addItem(wallpaper.url); |
178 } | 241 } |
179 }, | 242 }, |
180 | 243 |
| 244 /** |
| 245 * Display layout drop down box and disable random mode if enabled. Called |
| 246 * when user select a valid file from file system. |
| 247 */ |
| 248 didSelectFile_: function() { |
| 249 $('set-wallpaper-layout').hidden = false; |
| 250 var wallpaperGrid = $('wallpaper-grid'); |
| 251 if ($('use-random-wallpaper').checked) { |
| 252 $('use-random-wallpaper').checked = false; |
| 253 wallpaperGrid.disabled = false; |
| 254 wallpaperGrid.classList.remove('grayout'); |
| 255 } |
| 256 }, |
| 257 |
| 258 /** |
| 259 * Returns url of current user's custom wallpaper thumbnail. |
| 260 * @private |
| 261 */ |
| 262 currentWallpaperImageUrl_: function() { |
| 263 return CUSTOM_WALLPAPER_PREFIX + BrowserOptions.getLoggedInUsername() + |
| 264 '?id=' + (new Date()).getTime(); |
| 265 }, |
| 266 |
| 267 /** |
| 268 * Updates the visibility of attribution-label and set-wallpaper-layout. |
| 269 * @param {boolean} isCustom True if users select custom wallpaper. |
| 270 */ |
| 271 set isCustom(isCustom) { |
| 272 if (isCustom) { |
| 273 // Clear attributions for custom wallpaper. |
| 274 $('attribution-label').hidden = true; |
| 275 // Enable the layout drop down box when custom wallpaper is selected. |
| 276 $('set-wallpaper-layout').hidden = false; |
| 277 } else { |
| 278 $('attribution-label').hidden = false; |
| 279 $('set-wallpaper-layout').hidden = true; |
| 280 } |
| 281 }, |
| 282 |
| 283 /** |
| 284 * Adds or updates custom user wallpaper thumbnail from file. |
| 285 * @private |
| 286 */ |
| 287 setCustomImage_: function() { |
| 288 var wallpaperGrid = $('wallpaper-grid'); |
| 289 var url = this.currentWallpaperImageUrl_(); |
| 290 if (this.oldImage_) { |
| 291 this.oldImage_ = wallpaperGrid.updateItem(this.oldImage_, url); |
| 292 } else { |
| 293 // Insert to the end of wallpaper list. |
| 294 var pos = wallpaperGrid.length; |
| 295 this.oldImage_ = wallpaperGrid.addItem(url, undefined, undefined, pos); |
| 296 } |
| 297 |
| 298 this.isCustom = true; |
| 299 this.setWallpaperAttribution_(''); |
| 300 wallpaperGrid.selectedItem = this.oldImage_; |
| 301 }, |
181 }; | 302 }; |
182 | 303 |
183 // Forward public APIs to private implementations. | 304 // Forward public APIs to private implementations. |
184 [ | 305 [ |
185 'setDefaultImages', | 306 'setDefaultImages', |
186 'setSelectedImage' | 307 'setSelectedImage', |
| 308 'populateWallpaperLayouts', |
| 309 'didSelectFile', |
| 310 'setCustomImage' |
187 ].forEach(function(name) { | 311 ].forEach(function(name) { |
188 SetWallpaperOptions[name] = function() { | 312 SetWallpaperOptions[name] = function() { |
189 var instance = SetWallpaperOptions.getInstance(); | 313 var instance = SetWallpaperOptions.getInstance(); |
190 return instance[name + '_'].apply(instance, arguments); | 314 return instance[name + '_'].apply(instance, arguments); |
191 }; | 315 }; |
192 }); | 316 }); |
193 | 317 |
194 // Export | 318 // Export |
195 return { | 319 return { |
196 SetWallpaperOptions: SetWallpaperOptions | 320 SetWallpaperOptions: SetWallpaperOptions |
197 }; | 321 }; |
198 | 322 |
199 }); | 323 }); |
200 | 324 |
OLD | NEW |