1 // Copyright 2013 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.
5 cr.define('options', function() {
6 var OptionsPage = options.OptionsPage;
7 var ArrayDataModel = cr.ui.ArrayDataModel;
10 * ManagedUserImportOverlay class.
11 * Encapsulated handling of the 'Import existing managed user' overlay page.
15 function ManagedUserImportOverlay() {
16 var title = loadTimeData.getString('managedUserImportTitle');
17 OptionsPage.call(this, 'managedUserImport',
18 title, 'managed-user-import');
21 cr.addSingletonGetter(ManagedUserImportOverlay);
23 ManagedUserImportOverlay.prototype = {
24 // Inherit from OptionsPage.
25 __proto__: OptionsPage.prototype,
28 canShowPage: function() {
29 return !BrowserOptions.getCurrentProfile().isManaged;
33 * Initialize the page.
35 initializePage: function() {
36 // Call base class implementation to start preference initialization.
37 OptionsPage.prototype.initializePage.call(this);
39 var managedUserList = $('managed-user-list');
40 options.managedUserOptions.ManagedUserList.decorate(managedUserList);
42 var avatarGrid = $('select-avatar-grid');
43 options.ProfilesIconGrid.decorate(avatarGrid);
44 var avatarIcons = loadTimeData.getValue('avatarIcons');
45 avatarGrid.dataModel = new ArrayDataModel(avatarIcons);
47 managedUserList.addEventListener('change', function(event) {
48 var managedUser = managedUserList.selectedItem;
52 $('managed-user-import-ok').disabled =
53 managedUserList.selectedItem.onCurrentDevice;
57 $('managed-user-import-cancel').onclick = function(event) {
58 OptionsPage.closeOverlay();
59 self.updateImportInProgress_(false);
61 // 'cancelCreateProfile' is handled by CreateProfileHandler.
62 chrome.send('cancelCreateProfile');
65 $('managed-user-import-ok').onclick =
66 this.showAvatarGridOrSubmit_.bind(this);
68 $('create-new-user-link').onclick = function(event) {
69 OptionsPage.closeOverlay();
70 OptionsPage.navigateToPage('createProfile');
77 didShowPage: function() {
78 options.ManagedUserListData.requestExistingManagedUsers().then(
79 this.receiveExistingManagedUsers_, this.onSigninError_.bind(this));
81 this.updateImportInProgress_(false);
82 $('managed-user-import-error-bubble').hidden = true;
83 $('managed-user-import-ok').disabled = true;
84 $('select-avatar-grid').hidden = true;
85 $('managed-user-list').hidden = false;
87 $('managed-user-import-ok').textContent =
88 loadTimeData.getString('managedUserImportOk');
89 $('managed-user-import-text').textContent =
90 loadTimeData.getString('managedUserImportText');
91 $('managed-user-import-title').textContent =
92 loadTimeData.getString('managedUserImportTitle');
96 * Called when the user clicks the "OK" button. In case the managed
97 * user being imported has no avatar in sync, it shows the avatar
98 * icon grid. In case the avatar grid is visible or the managed user
99 * already has an avatar stored in sync, it proceeds with importing
103 showAvatarGridOrSubmit_: function() {
104 var managedUser = $('managed-user-list').selectedItem;
108 $('managed-user-import-error-bubble').hidden = true;
110 if ($('select-avatar-grid').hidden && managedUser.needAvatar) {
111 this.showAvatarGridHelper_();
115 var avatarUrl = managedUser.needAvatar ?
116 $('select-avatar-grid').selectedItem : managedUser.iconURL;
118 this.updateImportInProgress_(true);
120 // 'createProfile' is handled by CreateProfileHandler.
121 chrome.send('createProfile', [managedUser.name, avatarUrl,
122 false, true, managedUser.id]);
123 options.ManagedUserListData.reloadExistingManagedUsers();
127 * Hides the 'managed user list' and shows the avatar grid instead.
128 * It also updates the overlay text and title to instruct the user
129 * to choose an avatar for the supervised user.
132 showAvatarGridHelper_: function() {
133 $('managed-user-list').hidden = true;
134 $('select-avatar-grid').hidden = false;
135 $('select-avatar-grid').redraw();
136 $('select-avatar-grid').selectedItem =
137 loadTimeData.getValue('avatarIcons')[0];
139 $('managed-user-import-ok').textContent =
140 loadTimeData.getString('managedUserSelectAvatarOk');
141 $('managed-user-import-text').textContent =
142 loadTimeData.getString('managedUserSelectAvatarText');
143 $('managed-user-import-title').textContent =
144 loadTimeData.getString('managedUserSelectAvatarTitle');
148 * Updates the UI according to the importing state.
149 * @param {boolean} inProgress True to indicate that
150 * importing is in progress and false otherwise.
153 updateImportInProgress_: function(inProgress) {
154 $('managed-user-import-ok').disabled = inProgress;
155 $('managed-user-list').disabled = inProgress;
156 $('select-avatar-grid').disabled = inProgress;
157 $('create-new-user-link').disabled = inProgress;
158 $('managed-user-import-throbber').hidden = !inProgress;
162 * Sets the data model of the managed user list to |managedUsers|.
163 * @param {Array.<Object>} managedUsers An array of managed user objects.
164 * Each object is of the form:
166 * id: "Managed User ID",
167 * name: "Managed User Name",
168 * iconURL: "chrome://path/to/icon/image",
169 * onCurrentDevice: true or false,
170 * needAvatar: true or false
174 receiveExistingManagedUsers_: function(managedUsers) {
175 managedUsers.sort(function(a, b) {
176 if (a.onCurrentDevice != b.onCurrentDevice)
177 return a.onCurrentDevice ? 1 : -1;
178 return a.name.localeCompare(b.name);
181 $('managed-user-list').dataModel = new ArrayDataModel(managedUsers);
182 if (managedUsers.length == 0) {
183 this.onError_(loadTimeData.getString('noExistingManagedUsers'));
184 $('managed-user-import-ok').disabled = true;
186 // Hide the error bubble.
187 $('managed-user-import-error-bubble').hidden = true;
191 onSigninError_: function() {
192 $('managed-user-list').dataModel = null;
193 this.onError_(loadTimeData.getString('managedUserImportSigninError'));
197 * Displays an error message if an error occurs while
198 * importing a managed user.
199 * Called by BrowserOptions via the BrowserOptionsHandler.
200 * @param {string} error The error message to display.
203 onError_: function(error) {
204 var errorBubble = $('managed-user-import-error-bubble');
205 errorBubble.hidden = false;
206 errorBubble.textContent = error;
207 this.updateImportInProgress_(false);
211 * Closes the overlay if importing the managed user was successful.
214 onSuccess_: function() {
215 this.updateImportInProgress_(false);
216 OptionsPage.closeOverlay();
220 // Forward public APIs to private implementations.
223 ].forEach(function(name) {
224 ManagedUserImportOverlay[name] = function() {
225 var instance = ManagedUserImportOverlay.getInstance();
226 return instance[name + '_'].apply(instance, arguments);
232 ManagedUserImportOverlay: ManagedUserImportOverlay,