Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: chrome/browser/resources/options2/chromeos/set_wallpaper_options.js

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

Powered by Google App Engine
This is Rietveld 408576698