1 // Copyright 2014 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 Page = cr.ui.pageManager.Page;
7 var PageManager = cr.ui.pageManager.PageManager;
8 var ArrayDataModel = cr.ui.ArrayDataModel;
11 * SupervisedUserImportOverlay class.
12 * Encapsulated handling of the 'Import existing supervised user' overlay
15 * @extends {cr.ui.pageManager.Page}
17 function SupervisedUserImportOverlay() {
18 var title = loadTimeData.getString('supervisedUserImportTitle');
19 Page.call(this, 'supervisedUserImport', title, 'supervised-user-import');
22 cr.addSingletonGetter(SupervisedUserImportOverlay);
24 SupervisedUserImportOverlay.prototype = {
26 __proto__: Page.prototype,
29 canShowPage: function() {
30 return !BrowserOptions.getCurrentProfile().isSupervised;
34 initializePage: function() {
35 Page.prototype.initializePage.call(this);
37 var supervisedUserList = $('supervised-user-list');
38 options.supervisedUserOptions.SupervisedUserList.decorate(
41 var avatarGrid = $('select-avatar-grid');
42 options.ProfilesIconGrid.decorate(avatarGrid);
43 var avatarIcons = loadTimeData.getValue('avatarIcons');
44 avatarGrid.dataModel = new ArrayDataModel(avatarIcons);
46 supervisedUserList.addEventListener('change', function(event) {
47 var supervisedUser = supervisedUserList.selectedItem;
48 $('supervised-user-import-ok').disabled =
49 !supervisedUser || supervisedUser.onCurrentDevice;
53 $('supervised-user-import-cancel').onclick = function(event) {
54 if (self.inProgress_) {
55 self.updateImportInProgress_(false);
57 // 'cancelCreateProfile' is handled by CreateProfileHandler.
58 chrome.send('cancelCreateProfile');
60 PageManager.closeOverlay();
63 $('supervised-user-import-ok').onclick =
64 this.showAvatarGridOrSubmit_.bind(this);
65 $('supervised-user-select-avatar-ok').onclick =
66 this.showAvatarGridOrSubmit_.bind(this);
72 didShowPage: function() {
73 // When the import link is clicked to open this overlay, it is hidden in
74 // order to trigger a cursor update. We can show the import link again
75 // now. TODO(akuegel): Remove this temporary fix when crbug/246304 is
77 $('import-existing-supervised-user-link').hidden = false;
79 options.SupervisedUserListData.requestExistingSupervisedUsers().then(
80 this.receiveExistingSupervisedUsers_, this.onSigninError_.bind(this));
81 options.SupervisedUserListData.addObserver(this);
83 this.updateImportInProgress_(false);
84 $('supervised-user-import-error-bubble').hidden = true;
85 $('supervised-user-import-ok').disabled = true;
86 this.showAppropriateElements_(/* isSelectAvatarMode */ false);
92 didClosePage: function() {
93 options.SupervisedUserListData.removeObserver(this);
97 * Shows either the supervised user import dom elements or the select avatar
99 * @param {boolean} isSelectAvatarMode True if the overlay should show the
100 * select avatar grid, and false if the overlay should show the
101 * supervised user list.
104 showAppropriateElements_: function(isSelectAvatarMode) {
106 this.pageDiv.querySelectorAll('.supervised-user-select-avatar');
107 for (var i = 0; i < avatarElements.length; i++)
108 avatarElements[i].hidden = !isSelectAvatarMode;
110 this.pageDiv.querySelectorAll('.supervised-user-import');
111 for (var i = 0; i < importElements.length; i++)
112 importElements[i].hidden = isSelectAvatarMode;
116 * Called when the user clicks the "OK" button. In case the supervised
117 * user being imported has no avatar in sync, it shows the avatar
118 * icon grid. In case the avatar grid is visible or the supervised user
119 * already has an avatar stored in sync, it proceeds with importing
120 * the supervised user.
123 showAvatarGridOrSubmit_: function() {
124 var supervisedUser = $('supervised-user-list').selectedItem;
128 $('supervised-user-import-error-bubble').hidden = true;
130 if ($('select-avatar-grid').hidden && supervisedUser.needAvatar) {
131 this.showAvatarGridHelper_();
135 var avatarUrl = supervisedUser.needAvatar ?
136 $('select-avatar-grid').selectedItem : supervisedUser.iconURL;
138 this.updateImportInProgress_(true);
140 // 'createProfile' is handled by CreateProfileHandler.
141 chrome.send('createProfile', [supervisedUser.name, avatarUrl,
142 false, true, supervisedUser.id]);
146 * Hides the 'supervised user list' and shows the avatar grid instead.
147 * It also updates the overlay text and title to instruct the user
148 * to choose an avatar for the supervised user.
151 showAvatarGridHelper_: function() {
152 this.showAppropriateElements_(/* isSelectAvatarMode */ true);
153 $('select-avatar-grid').redraw();
154 $('select-avatar-grid').selectedItem =
155 loadTimeData.getValue('avatarIcons')[0];
159 * Updates the UI according to the importing state.
160 * @param {boolean} inProgress True to indicate that
161 * importing is in progress and false otherwise.
164 updateImportInProgress_: function(inProgress) {
165 this.inProgress_ = inProgress;
166 $('supervised-user-import-ok').disabled = inProgress;
167 $('supervised-user-select-avatar-ok').disabled = inProgress;
168 $('supervised-user-list').disabled = inProgress;
169 $('select-avatar-grid').disabled = inProgress;
170 $('supervised-user-import-throbber').hidden = !inProgress;
174 * Sets the data model of the supervised user list to |supervisedUsers|.
175 * @param {Array<{id: string, name: string, iconURL: string,
176 * onCurrentDevice: boolean, needAvatar: boolean}>} supervisedUsers
177 * Array of supervised user objects.
180 receiveExistingSupervisedUsers_: function(supervisedUsers) {
181 supervisedUsers.sort(function(a, b) {
182 if (a.onCurrentDevice != b.onCurrentDevice)
183 return a.onCurrentDevice ? 1 : -1;
184 return a.name.localeCompare(b.name);
187 $('supervised-user-list').dataModel = new ArrayDataModel(supervisedUsers);
188 if (supervisedUsers.length == 0) {
189 this.onErrorInternal_(
190 loadTimeData.getString('noExistingSupervisedUsers'));
191 $('supervised-user-import-ok').disabled = true;
193 // Hide the error bubble.
194 $('supervised-user-import-error-bubble').hidden = true;
198 onSigninError_: function() {
199 $('supervised-user-list').dataModel = null;
200 this.onErrorInternal_(
201 loadTimeData.getString('supervisedUserImportSigninError'));
205 * Displays an error message if an error occurs while
206 * importing a supervised user.
207 * Called by BrowserOptions via the BrowserOptionsHandler.
208 * @param {string} error The error message to display.
211 onError_: function(error) {
212 this.onErrorInternal_(error);
213 this.updateImportInProgress_(false);
217 * Displays an error message.
218 * @param {string} error The error message to display.
221 onErrorInternal_: function(error) {
222 var errorBubble = $('supervised-user-import-error-bubble');
223 errorBubble.hidden = false;
224 errorBubble.textContent = error;
228 * Closes the overlay if importing the supervised user was successful. Also
229 * reset the cached list of supervised users in order to get an updated list
230 * when the overlay is reopened.
233 onSuccess_: function() {
234 this.updateImportInProgress_(false);
235 options.SupervisedUserListData.resetPromise();
236 PageManager.closeAllOverlays();
240 // Forward public APIs to private implementations.
241 cr.makePublic(SupervisedUserImportOverlay, [
248 SupervisedUserImportOverlay: SupervisedUserImportOverlay,