- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / chromeos / accounts_options.js
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
3 // found in the LICENSE file.
4
5 cr.define('options', function() {
6   var OptionsPage = options.OptionsPage;
7
8   /////////////////////////////////////////////////////////////////////////////
9   // AccountsOptions class:
10
11   /**
12    * Encapsulated handling of ChromeOS accounts options page.
13    * @constructor
14    */
15   function AccountsOptions(model) {
16     OptionsPage.call(this, 'accounts',
17                      loadTimeData.getString('accountsPageTabTitle'),
18                      'accountsPage');
19     // Whether to show the whitelist.
20     this.showWhitelist_ = false;
21   }
22
23   cr.addSingletonGetter(AccountsOptions);
24
25   AccountsOptions.prototype = {
26     // Inherit AccountsOptions from OptionsPage.
27     __proto__: OptionsPage.prototype,
28
29     /**
30      * Initializes AccountsOptions page.
31      */
32     initializePage: function() {
33       // Call base class implementation to starts preference initialization.
34       OptionsPage.prototype.initializePage.call(this);
35
36       // Set up accounts page.
37       var userList = $('userList');
38       userList.addEventListener('remove', this.handleRemoveUser_);
39
40       var userNameEdit = $('userNameEdit');
41       options.accounts.UserNameEdit.decorate(userNameEdit);
42       userNameEdit.addEventListener('add', this.handleAddUser_);
43
44       // If the current user is not the owner, do not show the user list.
45       // If the current user is not the owner, or the device is enterprise
46       // managed, show a warning that settings cannot be modified.
47       this.showWhitelist_ = UIAccountTweaks.currentUserIsOwner();
48       if (this.showWhitelist_) {
49         options.accounts.UserList.decorate(userList);
50       } else {
51         $('ownerOnlyWarning').hidden = false;
52         this.managed = AccountsOptions.whitelistIsManaged();
53       }
54
55       this.addEventListener('visibleChange', this.handleVisibleChange_);
56
57       $('useWhitelistCheck').addEventListener('change',
58           this.handleUseWhitelistCheckChange_.bind(this));
59
60       Preferences.getInstance().addEventListener(
61           $('useWhitelistCheck').pref,
62           this.handleUseWhitelistPrefChange_.bind(this));
63
64       $('accounts-options-overlay-confirm').onclick =
65           OptionsPage.closeOverlay.bind(OptionsPage);
66     },
67
68     /**
69      * Update user list control state.
70      * @private
71      */
72     updateControls_: function() {
73       $('userList').disabled =
74       $('userNameEdit').disabled = !this.showWhitelist_ ||
75                                    AccountsOptions.whitelistIsManaged() ||
76                                    !$('useWhitelistCheck').checked;
77     },
78
79     /**
80      * Handler for OptionsPage's visible property change event.
81      * @private
82      * @param {Event} e Property change event.
83      */
84     handleVisibleChange_: function(e) {
85       if (this.visible) {
86         this.updateControls_();
87         if (this.showWhitelist_)
88           $('userList').redraw();
89       }
90     },
91
92     /**
93      * Handler for allow guest check change.
94      * @private
95      */
96     handleUseWhitelistCheckChange_: function(e) {
97       // Whitelist existing users when guest login is being disabled.
98       if ($('useWhitelistCheck').checked) {
99         chrome.send('whitelistExistingUsers');
100       }
101
102       this.updateControls_();
103     },
104
105     /**
106      * handler for allow guest pref change.
107      * @private
108      */
109     handleUseWhitelistPrefChange_: function(e) {
110       this.updateControls_();
111     },
112
113     /**
114      * Handler for "add" event fired from userNameEdit.
115      * @private
116      * @param {Event} e Add event fired from userNameEdit.
117      */
118     handleAddUser_: function(e) {
119       chrome.send('whitelistUser', [e.user.email, e.user.name]);
120     },
121
122     /**
123      * Handler for "remove" event fired from userList.
124      * @private
125      * @param {Event} e Remove event fired from userList.
126      */
127     handleRemoveUser_: function(e) {
128       chrome.send('unwhitelistUser', [e.user.username]);
129     }
130   };
131
132
133   /**
134    * Returns whether the whitelist is managed by policy or not.
135    */
136   AccountsOptions.whitelistIsManaged = function() {
137     return loadTimeData.getBoolean('whitelist_is_managed');
138   };
139
140   /**
141    * Update account picture.
142    * @param {string} username User for which to update the image.
143    */
144   AccountsOptions.updateAccountPicture = function(username) {
145     if (this.showWhitelist_)
146       $('userList').updateAccountPicture(username);
147   };
148
149   // Export
150   return {
151     AccountsOptions: AccountsOptions
152   };
153
154 });