summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options2/chromeos/set_wallpaper_options.js
blob: 8e9791bd3376768cf76bbe006f5bdde1ecda4943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('options', function() {

  var OptionsPage = options.OptionsPage;
  var UserImagesGrid = options.UserImagesGrid;

  /** @const */ var CUSTOM_WALLPAPER_PREFIX =
      'chrome://wallpaper-thumb/custom_';

  /////////////////////////////////////////////////////////////////////////////
  // SetWallpaperOptions class:

  /**
   * Encapsulated handling of ChromeOS set wallpaper options page.
   * @constructor
   */
  function SetWallpaperOptions() {
    OptionsPage.call(
        this,
        'setWallpaper',
        loadTimeData.getString('setWallpaper'),
        'set-wallpaper-page');
  }

  cr.addSingletonGetter(SetWallpaperOptions);

  SetWallpaperOptions.prototype = {
    // Inherit SetWallpaperOptions from OptionsPage.
    __proto__: options.OptionsPage.prototype,

    /**
     * Initializes SetWallpaperOptions page.
     */
    initializePage: function() {
      // Call base class implementation to start preferences initialization.
      OptionsPage.prototype.initializePage.call(this);

      var wallpaperGrid = $('wallpaper-grid');
      UserImagesGrid.decorate(wallpaperGrid);

      wallpaperGrid.addEventListener('change',
                                     this.handleImageSelected_.bind(this));
      wallpaperGrid.addEventListener('dblclick',
                                     this.handleImageDblClick_.bind(this));
      wallpaperGrid.addEventListener('activate',
                                     function() { OptionsPage.closeOverlay() });

      $('set-wallpaper-layout').addEventListener('change',
          this.handleLayoutChange_);
      $('set-custom-wallpaper').onclick = this.handleChooseFile_;
      $('use-random-wallpaper').onclick = this.handleCheckboxClick_.bind(this);
      $('set-wallpaper-overlay-confirm').onclick = function() {
        OptionsPage.closeOverlay();
      };

      // @type {Array.<author: string, url: string, website: string>}
      this.wallpapers_ = [];

      // @type {Object} Old user custom wallpaper thumbnail.
      this.oldImage_ = null;

      chrome.send('onSetWallpaperPageInitialized');
    },

    /**
     * Called right after the page has been shown to user.
     */
    didShowPage: function() {
      $('wallpaper-grid').updateAndFocus();
      // A quick hack to fix issue 118472. This is a general problem of list
      // control and options overlay.
      // TODO(bshe): Remove this hack when we fixed the general problem which
      // tracked in issue 118829.
      $('wallpaper-grid').redraw();
      chrome.send('onSetWallpaperPageShown');
    },

    /**
     * Called right before the page is hidden.
     */
    willHidePage: function() {
      var wallpaperGrid = $('wallpaper-grid');
      wallpaperGrid.blur();
      if (this.oldImage_) {
        wallpaperGrid.removeItem(this.oldImage_);
        this.oldImage_ = null;
      }
      $('set-wallpaper-layout').innerText = '';
    },

    /**
     * Set attributions of wallpaper with given URL.
     * @param {string} url URL of the selected wallpaper.
     * @private
     */
    setWallpaperAttribution_: function(url) {
      for (var i = 0; i < this.wallpapers_.length; i++) {
        if (this.wallpapers_[i].url == url) {
          $('author-name').textContent = this.wallpapers_[i].author;
          $('author-website').textContent = this.wallpapers_[i].website;
          return;
        }
      }
      $('author-name').textContent = '';
      $('author-website').textContent = '';
    },

    /**
     * Populates the drop down box for custom wallpaper layouts.
     * param {string} layouts Available wallpaper layouts.
     * param {number} selectedLayout The value of selected/default layout.
     * @private
     */
    populateWallpaperLayouts_: function(layouts, selectedLayout) {
      var wallpaperLayout = $('set-wallpaper-layout');
      var selectedIndex = -1;
      for (var i = 0; i < layouts.length; i++) {
        var option = new Option(layouts[i]['name'], layouts[i]['index']);
        if (selectedLayout == option.value)
          selectedIndex = i;
        wallpaperLayout.appendChild(option);
      }
      if (selectedIndex >= 0)
        wallpaperLayout.selectedIndex = selectedIndex;
    },

    /**
     * Handles "Custom..." button activation.
     * @private
     */
    handleChooseFile_: function() {
      chrome.send('chooseWallpaper');
    },

    /**
     * Handle the wallpaper layout setting change.
     * @private
     */
    handleLayoutChange_: function() {
      var setWallpaperLayout = $('set-wallpaper-layout');
      var layout = setWallpaperLayout.options[
          setWallpaperLayout.selectedIndex].value;
      chrome.send('changeWallpaperLayout', [layout]);
    },

    /**
     * Handles image selection change.
     * @private
     */
    handleImageSelected_: function() {
      var wallpaperGrid = $('wallpaper-grid');
      var url = wallpaperGrid.selectedItemUrl;
      if (url &&
          !wallpaperGrid.inProgramSelection) {
        if (url.indexOf(CUSTOM_WALLPAPER_PREFIX) == 0) {
          // User custom wallpaper is selected
          this.isCustom = true;
          // When users select the custom wallpaper thumbnail from picker UI,
          // use the saved layout value and redraw the wallpaper.
          this.handleLayoutChange_();
        } else {
          this.isCustom = false;
          chrome.send('selectDefaultWallpaper', [url]);
        }
        this.setWallpaperAttribution_(url);
      }
    },

    /**
     * Handles double click on the image grid.
     * @param {Event} e Double click Event.
     */
    handleImageDblClick_: function(e) {
      var wallpaperGrid = $('wallpaper-grid');
      if (wallpaperGrid.disabled)
        return;
      // Close page unless the click target is the grid itself.
      if (e.target instanceof HTMLImageElement)
        OptionsPage.closeOverlay();
    },

    /**
     * Handles click on the "I'm feeling lucky" checkbox.
     * @private
     */
    handleCheckboxClick_: function() {
      var wallpaperGrid = $('wallpaper-grid');
      if ($('use-random-wallpaper').checked) {
        wallpaperGrid.disabled = true;
        $('attribution-label').hidden = false;
        chrome.send('selectRandomWallpaper');
        wallpaperGrid.classList.add('grayout');
        $('set-wallpaper-layout').hidden = true;
      } else {
        wallpaperGrid.disabled = false;
        wallpaperGrid.classList.remove('grayout');
        // Set the wallpaper type to User::DEFAULT.
        this.handleImageSelected_();
      }
    },

    /**
     * Selects corresponding wallpaper thumbnail with the given URL and toggle
     * the "I'm feeling lucky" checkbox.
     * @param {string} url URL of the wallpaper thumbnail to select.
     * @param {boolean} isRandom True if user checked "I'm feeling lucky"
     * checkbox.
     * @private
     */
    setSelectedImage_: function(url, isRandom) {
      var wallpaperGrid = $('wallpaper-grid');
      wallpaperGrid.selectedItemUrl = url;
      this.setWallpaperAttribution_(url);
      if (isRandom) {
        // Do not call chrome.send('selectRandomWallpaper'), it is not
        // neccessary to generate a new random index here.
        $('use-random-wallpaper').checked = true;
        wallpaperGrid.disabled = true;
        wallpaperGrid.classList.add('grayout');
      }
    },

    /**
     * Appends default images to the image grid. Should only be called once.
     * @param {Array.<{author: string, url: string, website: string}>}
     * wallpapers An array of wallpaper objects.
     * @private
     */
    setDefaultImages_: function(wallpapers) {
      var wallpaperGrid = $('wallpaper-grid');
      // TODO(bshe): Ideally we should save author and website with the actual
      // image (URL) and not use index related storage. This way this data is
      // stored in one place rather than depending on the index to be
      // consistent.
      for (var i = 0, wallpaper; wallpaper = wallpapers[i]; i++) {
        this.wallpapers_.push(wallpaper);
        wallpaperGrid.addItem(wallpaper.url);
      }
    },

    /**
     * Display layout drop down box and disable random mode if enabled. Called
     * when user select a valid file from file system.
     */
    didSelectFile_: function() {
      $('set-wallpaper-layout').hidden = false;
      var wallpaperGrid = $('wallpaper-grid');
      if ($('use-random-wallpaper').checked) {
        $('use-random-wallpaper').checked = false;
        wallpaperGrid.disabled = false;
        wallpaperGrid.classList.remove('grayout');
      }
    },

    /**
     * Returns url of current user's custom wallpaper thumbnail.
     * @private
     */
    currentWallpaperImageUrl_: function() {
      return CUSTOM_WALLPAPER_PREFIX + BrowserOptions.getLoggedInUsername() +
          '?id=' + (new Date()).getTime();
    },

    /**
     * Updates the visibility of attribution-label and set-wallpaper-layout.
     * @param {boolean} isCustom True if users select custom wallpaper.
     */
    set isCustom(isCustom) {
      if (isCustom) {
        // Clear attributions for custom wallpaper.
        $('attribution-label').hidden = true;
        // Enable the layout drop down box when custom wallpaper is selected.
        $('set-wallpaper-layout').hidden = false;
      } else {
        $('attribution-label').hidden = false;
        $('set-wallpaper-layout').hidden = true;
      }
    },

    /**
     * Adds or updates custom user wallpaper thumbnail from file.
     * @private
     */
    setCustomImage_: function() {
      var wallpaperGrid = $('wallpaper-grid');
      var url = this.currentWallpaperImageUrl_();
      if (this.oldImage_) {
        this.oldImage_ = wallpaperGrid.updateItem(this.oldImage_, url);
      } else {
        // Insert to the end of wallpaper list.
        var pos = wallpaperGrid.length;
        this.oldImage_ = wallpaperGrid.addItem(url, undefined, undefined, pos);
      }

      this.isCustom = true;
      this.setWallpaperAttribution_('');
      wallpaperGrid.selectedItem = this.oldImage_;
     },
  };

  // Forward public APIs to private implementations.
  [
    'setDefaultImages',
    'setSelectedImage',
    'populateWallpaperLayouts',
    'didSelectFile',
    'setCustomImage'
  ].forEach(function(name) {
    SetWallpaperOptions[name] = function() {
      var instance = SetWallpaperOptions.getInstance();
      return instance[name + '_'].apply(instance, arguments);
    };
  });

  // Export
  return {
    SetWallpaperOptions: SetWallpaperOptions
  };

});