summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options/chromeos/change_picture_options.js
blob: 650c7808c052091f0abdde74b40c2d47a84ca234 (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
// Copyright (c) 2011 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;

  /////////////////////////////////////////////////////////////////////////////
  // ChangePictureOptions class:

  /**
   * Encapsulated handling of ChromeOS change picture options page.
   * @constructor
   */
  function ChangePictureOptions() {
    OptionsPage.call(
        this,
        'changePicture',
        localStrings.getString('changePicturePage'),
        'change-picture-page');
  }

  cr.addSingletonGetter(ChangePictureOptions);

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

    /**
     * Initializes ChangePictureOptions page.
     */
    initializePage: function() {
      // Call base class implementation to starts preference initialization.
      OptionsPage.prototype.initializePage.call(this);
      // Add "Take photo" and "Choose a file" buttons in a uniform way with
      // other buttons.
      this.addUserImage_(
          'chrome://theme/IDR_BUTTON_USER_IMAGE_TAKE_PHOTO',
          localStrings.getString('takePhoto'),
          this.handleTakePhoto_);
      this.addUserImage_(
          'chrome://theme/IDR_BUTTON_USER_IMAGE_CHOOSE_FILE',
          localStrings.getString('chooseFile'),
          this.handleChooseFile_);
      chrome.send('getAvailableImages');
    },

    /**
     * Handler for when the user clicks on "Take photo" button.
     * @private
     * @param {Event} e Click Event.
     */
    handleTakePhoto_: function(e) {
      chrome.send('takePhoto');
      OptionsPage.navigateToPage('personal');
    },

    /**
     * Handler for when the user clicks on "Choose a file" button.
     * @private
     * @param {Event} e Click Event.
     */
    handleChooseFile_: function(e) {
      chrome.send('chooseFile');
      OptionsPage.navigateToPage('personal');
    },

    /**
     * Handler for when the user clicks on any available user image.
     * @private
     * @param {Event} e Click Event.
     */
    handleImageClick_: function(e) {
      chrome.send('selectImage', [e.target.src]);
      OptionsPage.navigateToPage('personal');
    },

    /**
     * Appends new image to the end of the image list.
     * @param {string} src A url for the user image.
     * @param {string} title A tooltip for the image.
     * @param {function} clickHandler A handler for click on image.
     * @private
     */
    addUserImage_: function(src, title, clickHandler) {
      var imageElement = document.createElement('img');
      imageElement.src = src;
      if (title)
        imageElement.title = title;
      imageElement.addEventListener('click',
                                    clickHandler,
                                    false);
      var divElement = document.createElement('div');
      divElement.classList.add('list-element');
      divElement.appendChild(imageElement);
      $('images-list').appendChild(divElement);
    },

    /**
     * Inserts received images before "Choose file" button.
     * @param {List} images A list of urls to user images.
     * @private
     */
    addUserImages_: function(images) {
      for (var i = 0; i < images.length; i++) {
        var imageUrl = images[i];
        this.addUserImage_(imageUrl, "", this.handleImageClick_);
      }
    },
  };

  // Forward public APIs to private implementations.
  [
    'addUserImages',
  ].forEach(function(name) {
    ChangePictureOptions[name] = function(value) {
      ChangePictureOptions.getInstance()[name + '_'](value);
    };
  });

  // Export
  return {
    ChangePictureOptions: ChangePictureOptions
  };

});