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;
51 $('supervised-user-import-ok').disabled =
52 supervisedUserList.selectedItem.onCurrentDevice;
56 $('supervised-user-import-cancel').onclick = function(event) {
57 if (self.inProgress_) {
58 self.updateImportInProgress_(false);
60 // 'cancelCreateProfile' is handled by CreateProfileHandler.
61 chrome.send('cancelCreateProfile');
63 PageManager.closeOverlay();
66 $('supervised-user-import-ok').onclick =
67 this.showAvatarGridOrSubmit_.bind(this);
68 $('supervised-user-select-avatar-ok').onclick =
69 this.showAvatarGridOrSubmit_.bind(this);
75 didShowPage: function() {
76 // When the import link is clicked to open this overlay, it is hidden in
77 // order to trigger a cursor update. We can show the import link again
78 // now. TODO(akuegel): Remove this temporary fix when crbug/246304 is
80 $('import-existing-supervised-user-link').hidden = false;
82 options.SupervisedUserListData.requestExistingSupervisedUsers().then(
83 this.receiveExistingSupervisedUsers_, this.onSigninError_.bind(this));
84 options.SupervisedUserListData.addObserver(this);
86 this.updateImportInProgress_(false);
87 $('supervised-user-import-error-bubble').hidden = true;
88 $('supervised-user-import-ok').disabled = true;
89 this.showAppropriateElements_(/* isSelectAvatarMode */ false);
95 didClosePage: function() {
96 options.SupervisedUserListData.removeObserver(this);
100 * Shows either the supervised user import dom elements or the select avatar
102 * @param {boolean} isSelectAvatarMode True if the overlay should show the
103 * select avatar grid, and false if the overlay should show the
104 * supervised user list.
107 showAppropriateElements_: function(isSelectAvatarMode) {
109 this.pageDiv.querySelectorAll('.supervised-user-select-avatar');
110 for (var i = 0; i < avatarElements.length; i++)
111 avatarElements[i].hidden = !isSelectAvatarMode;
113 this.pageDiv.querySelectorAll('.supervised-user-import');
114 for (var i = 0; i < importElements.length; i++)
115 importElements[i].hidden = isSelectAvatarMode;
119 * Called when the user clicks the "OK" button. In case the supervised
120 * user being imported has no avatar in sync, it shows the avatar
121 * icon grid. In case the avatar grid is visible or the supervised user
122 * already has an avatar stored in sync, it proceeds with importing
123 * the supervised user.
126 showAvatarGridOrSubmit_: function() {
127 var supervisedUser = $('supervised-user-list').selectedItem;
131 $('supervised-user-import-error-bubble').hidden = true;
133 if ($('select-avatar-grid').hidden && supervisedUser.needAvatar) {
134 this.showAvatarGridHelper_();
138 var avatarUrl = supervisedUser.needAvatar ?
139 $('select-avatar-grid').selectedItem : supervisedUser.iconURL;
141 this.updateImportInProgress_(true);
143 // 'createProfile' is handled by CreateProfileHandler.
144 chrome.send('createProfile', [supervisedUser.name, avatarUrl,
145 false, true, supervisedUser.id]);
149 * Hides the 'supervised user list' and shows the avatar grid instead.
150 * It also updates the overlay text and title to instruct the user
151 * to choose an avatar for the supervised user.
154 showAvatarGridHelper_: function() {
155 this.showAppropriateElements_(/* isSelectAvatarMode */ true);
156 $('select-avatar-grid').redraw();
157 $('select-avatar-grid').selectedItem =
158 loadTimeData.getValue('avatarIcons')[0];
162 * Updates the UI according to the importing state.
163 * @param {boolean} inProgress True to indicate that
164 * importing is in progress and false otherwise.
167 updateImportInProgress_: function(inProgress) {
168 this.inProgress_ = inProgress;
169 $('supervised-user-import-ok').disabled = inProgress;
170 $('supervised-user-select-avatar-ok').disabled = inProgress;
171 $('supervised-user-list').disabled = inProgress;
172 $('select-avatar-grid').disabled = inProgress;
173 $('supervised-user-import-throbber').hidden = !inProgress;
177 * Sets the data model of the supervised user list to |supervisedUsers|.
178 * @param {Array<{id: string, name: string, iconURL: string,
179 * onCurrentDevice: boolean, needAvatar: boolean}>} supervisedUsers
180 * Array of supervised user objects.
183 receiveExistingSupervisedUsers_: function(supervisedUsers) {
184 supervisedUsers.sort(function(a, b) {
185 if (a.onCurrentDevice != b.onCurrentDevice)
186 return a.onCurrentDevice ? 1 : -1;
187 return a.name.localeCompare(b.name);
190 $('supervised-user-list').dataModel = new ArrayDataModel(supervisedUsers);
191 if (supervisedUsers.length == 0) {
192 this.onError_(loadTimeData.getString('noExistingSupervisedUsers'));
193 $('supervised-user-import-ok').disabled = true;
195 // Hide the error bubble.
196 $('supervised-user-import-error-bubble').hidden = true;
200 onSigninError_: function() {
201 $('supervised-user-list').dataModel = null;
202 this.onError_(loadTimeData.getString('supervisedUserImportSigninError'));
206 * Displays an error message if an error occurs while
207 * importing a supervised user.
208 * Called by BrowserOptions via the BrowserOptionsHandler.
209 * @param {string} error The error message to display.
212 onError_: function(error) {
213 var errorBubble = $('supervised-user-import-error-bubble');
214 errorBubble.hidden = false;
215 errorBubble.textContent = error;
216 this.updateImportInProgress_(false);
220 * Closes the overlay if importing the supervised user was successful. Also
221 * reset the cached list of supervised users in order to get an updated list
222 * when the overlay is reopened.
225 onSuccess_: function() {
226 this.updateImportInProgress_(false);
227 options.SupervisedUserListData.resetPromise();
228 PageManager.closeAllOverlays();
232 // Forward public APIs to private implementations.
233 cr.makePublic(SupervisedUserImportOverlay, [
240 SupervisedUserImportOverlay: SupervisedUserImportOverlay,