1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5cr.define('options', function() {
6
7  var OptionsPage = options.OptionsPage;
8
9  /////////////////////////////////////////////////////////////////////////////
10  // AccountsOptions class:
11
12  /**
13   * Encapsulated handling of ChromeOS accounts options page.
14   * @constructor
15   */
16  function AccountsOptions(model) {
17    OptionsPage.call(this, 'accounts', templateData.accountsPageTabTitle,
18                     'accountsPage');
19  }
20
21  cr.addSingletonGetter(AccountsOptions);
22
23  AccountsOptions.prototype = {
24    // Inherit AccountsOptions from OptionsPage.
25    __proto__: OptionsPage.prototype,
26
27    /**
28     * Initializes AccountsOptions page.
29     */
30    initializePage: function() {
31      // Call base class implementation to starts preference initialization.
32      OptionsPage.prototype.initializePage.call(this);
33
34      // Set up accounts page.
35      var userList = $('userList');
36
37      var userNameEdit = $('userNameEdit');
38      options.accounts.UserNameEdit.decorate(userNameEdit);
39      userNameEdit.addEventListener('add', this.handleAddUser_);
40
41      // If the current user is not the owner, show some warning,
42      // and do not show the user list.
43      if (AccountsOptions.currentUserIsOwner()) {
44        if (!AccountsOptions.whitelistIsManaged()) {
45          options.accounts.UserList.decorate(userList);
46        }
47      } else {
48        $('ownerOnlyWarning').classList.remove('hidden');
49      }
50
51      this.addEventListener('visibleChange', this.handleVisibleChange_);
52
53      $('useWhitelistCheck').addEventListener('change',
54          this.handleUseWhitelistCheckChange_.bind(this));
55
56      Preferences.getInstance().addEventListener(
57          $('useWhitelistCheck').pref,
58          this.handleUseWhitelistPrefChange_.bind(this));
59    },
60
61    /**
62     * Update user list control state.
63     * @private
64     */
65    updateControls_: function() {
66      $('userList').disabled =
67      $('userNameEdit').disabled = !AccountsOptions.currentUserIsOwner() ||
68                                   AccountsOptions.whitelistIsManaged() ||
69                                   !$('useWhitelistCheck').checked;
70    },
71
72    /**
73     * Handler for OptionsPage's visible property change event.
74     * @private
75     * @param {Event} e Property change event.
76     */
77    handleVisibleChange_: function(e) {
78      if (this.visible) {
79        // fetchUserPictures calls back AccountsOptions.setUserPictures and
80        // triggers redraw.
81        chrome.send('fetchUserPictures', []);
82
83        this.updateControls_();
84      }
85    },
86
87    /**
88     * Handler for allow guest check change.
89     * @private
90     */
91    handleUseWhitelistCheckChange_: function(e) {
92      // Whitelist existing users when guest login is being disabled.
93      if ($('useWhitelistCheck').checked) {
94        chrome.send('whitelistExistingUsers', []);
95      }
96
97      this.updateControls_();
98    },
99
100    /**
101     * handler for allow guest pref change.
102     * @private
103     */
104    handleUseWhitelistPrefChange_: function(e) {
105      this.updateControls_();
106    },
107
108    /**
109     * Handler for "add" event fired from userNameEdit.
110     * @private
111     * @param {Event} e Add event fired from userNameEdit.
112     */
113    handleAddUser_: function(e) {
114      AccountsOptions.addUsers([e.user]);
115    }
116  };
117
118  /**
119   * Returns whether the current user is owner or not.
120   */
121  AccountsOptions.currentUserIsOwner = function() {
122    return localStrings.getString('current_user_is_owner') == 'true';
123  };
124
125  /**
126   * Returns whether the whitelist is managed by policy or not.
127   */
128  AccountsOptions.whitelistIsManaged = function() {
129    return localStrings.getString('whitelist_is_managed') == 'true';
130  };
131
132  /**
133   * Updates user picture cache in UserList.
134   */
135  AccountsOptions.setUserPictures = function(cache) {
136    $('userList').setUserPictures(cache);
137  };
138
139  /**
140   * Adds given users to userList.
141   */
142  AccountsOptions.addUsers = function(users) {
143    var userList = $('userList');
144    for (var i = 0; i < users.length; ++i) {
145      userList.addUser(users[i]);
146    }
147  };
148
149  // Export
150  return {
151    AccountsOptions: AccountsOptions
152  };
153
154});
155