OLD | NEW |
| (Empty) |
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. | |
4 | |
5 cr.define('options', function() { | |
6 var OptionsPage = options.OptionsPage; | |
7 var ArrayDataModel = cr.ui.ArrayDataModel; | |
8 | |
9 /** | |
10 * ManagedUserImportOverlay class. | |
11 * Encapsulated handling of the 'Import existing managed user' overlay page. | |
12 * @constructor | |
13 * @class | |
14 */ | |
15 function ManagedUserImportOverlay() { | |
16 var title = loadTimeData.getString('managedUserImportTitle'); | |
17 OptionsPage.call(this, 'managedUserImport', | |
18 title, 'managed-user-import'); | |
19 }; | |
20 | |
21 cr.addSingletonGetter(ManagedUserImportOverlay); | |
22 | |
23 ManagedUserImportOverlay.prototype = { | |
24 // Inherit from OptionsPage. | |
25 __proto__: OptionsPage.prototype, | |
26 | |
27 /** @override */ | |
28 canShowPage: function() { | |
29 return !BrowserOptions.getCurrentProfile().isManaged; | |
30 }, | |
31 | |
32 /** @override */ | |
33 initializePage: function() { | |
34 // Call base class implementation to start preference initialization. | |
35 OptionsPage.prototype.initializePage.call(this); | |
36 | |
37 var managedUserList = $('managed-user-list'); | |
38 options.managedUserOptions.ManagedUserList.decorate(managedUserList); | |
39 | |
40 var avatarGrid = $('select-avatar-grid'); | |
41 options.ProfilesIconGrid.decorate(avatarGrid); | |
42 var avatarIcons = loadTimeData.getValue('avatarIcons'); | |
43 avatarGrid.dataModel = new ArrayDataModel(avatarIcons); | |
44 | |
45 managedUserList.addEventListener('change', function(event) { | |
46 var managedUser = managedUserList.selectedItem; | |
47 if (!managedUser) | |
48 return; | |
49 | |
50 $('managed-user-import-ok').disabled = | |
51 managedUserList.selectedItem.onCurrentDevice; | |
52 }); | |
53 | |
54 var self = this; | |
55 $('managed-user-import-cancel').onclick = function(event) { | |
56 if (self.inProgress_) { | |
57 self.updateImportInProgress_(false); | |
58 | |
59 // 'cancelCreateProfile' is handled by CreateProfileHandler. | |
60 chrome.send('cancelCreateProfile'); | |
61 } | |
62 OptionsPage.closeOverlay(); | |
63 }; | |
64 | |
65 $('managed-user-import-ok').onclick = | |
66 this.showAvatarGridOrSubmit_.bind(this); | |
67 $('managed-user-select-avatar-ok').onclick = | |
68 this.showAvatarGridOrSubmit_.bind(this); | |
69 }, | |
70 | |
71 /** | |
72 * @override | |
73 */ | |
74 didShowPage: function() { | |
75 // When the import link is clicked to open this overlay, it is hidden in | |
76 // order to trigger a cursor update. We can show the import link again | |
77 // now. TODO(akuegel): Remove this temporary fix when crbug/246304 is | |
78 // resolved. | |
79 $('import-existing-managed-user-link').hidden = false; | |
80 | |
81 options.ManagedUserListData.requestExistingManagedUsers().then( | |
82 this.receiveExistingManagedUsers_, this.onSigninError_.bind(this)); | |
83 options.ManagedUserListData.addObserver(this); | |
84 | |
85 this.updateImportInProgress_(false); | |
86 $('managed-user-import-error-bubble').hidden = true; | |
87 $('managed-user-import-ok').disabled = true; | |
88 this.showAppropriateElements_(/* isSelectAvatarMode */ false); | |
89 }, | |
90 | |
91 /** | |
92 * @override | |
93 */ | |
94 didClosePage: function() { | |
95 options.ManagedUserListData.removeObserver(this); | |
96 }, | |
97 | |
98 /** | |
99 * Shows either the managed user import dom elements or the select avatar | |
100 * dom elements. | |
101 * @param {boolean} isSelectAvatarMode True if the overlay should show the | |
102 * select avatar grid, and false if the overlay should show the managed | |
103 * user list. | |
104 * @private | |
105 */ | |
106 showAppropriateElements_: function(isSelectAvatarMode) { | |
107 var avatarElements = | |
108 this.pageDiv.querySelectorAll('.managed-user-select-avatar'); | |
109 for (var i = 0; i < avatarElements.length; i++) | |
110 avatarElements[i].hidden = !isSelectAvatarMode; | |
111 var importElements = | |
112 this.pageDiv.querySelectorAll('.managed-user-import'); | |
113 for (var i = 0; i < importElements.length; i++) | |
114 importElements[i].hidden = isSelectAvatarMode; | |
115 }, | |
116 | |
117 /** | |
118 * Called when the user clicks the "OK" button. In case the managed | |
119 * user being imported has no avatar in sync, it shows the avatar | |
120 * icon grid. In case the avatar grid is visible or the managed user | |
121 * already has an avatar stored in sync, it proceeds with importing | |
122 * the managed user. | |
123 * @private | |
124 */ | |
125 showAvatarGridOrSubmit_: function() { | |
126 var managedUser = $('managed-user-list').selectedItem; | |
127 if (!managedUser) | |
128 return; | |
129 | |
130 $('managed-user-import-error-bubble').hidden = true; | |
131 | |
132 if ($('select-avatar-grid').hidden && managedUser.needAvatar) { | |
133 this.showAvatarGridHelper_(); | |
134 return; | |
135 } | |
136 | |
137 var avatarUrl = managedUser.needAvatar ? | |
138 $('select-avatar-grid').selectedItem : managedUser.iconURL; | |
139 | |
140 this.updateImportInProgress_(true); | |
141 | |
142 // 'createProfile' is handled by CreateProfileHandler. | |
143 chrome.send('createProfile', [managedUser.name, avatarUrl, | |
144 false, true, managedUser.id]); | |
145 }, | |
146 | |
147 /** | |
148 * Hides the 'managed user list' and shows the avatar grid instead. | |
149 * It also updates the overlay text and title to instruct the user | |
150 * to choose an avatar for the supervised user. | |
151 * @private | |
152 */ | |
153 showAvatarGridHelper_: function() { | |
154 this.showAppropriateElements_(/* isSelectAvatarMode */ true); | |
155 $('select-avatar-grid').redraw(); | |
156 $('select-avatar-grid').selectedItem = | |
157 loadTimeData.getValue('avatarIcons')[0]; | |
158 }, | |
159 | |
160 /** | |
161 * Updates the UI according to the importing state. | |
162 * @param {boolean} inProgress True to indicate that | |
163 * importing is in progress and false otherwise. | |
164 * @private | |
165 */ | |
166 updateImportInProgress_: function(inProgress) { | |
167 this.inProgress_ = inProgress; | |
168 $('managed-user-import-ok').disabled = inProgress; | |
169 $('managed-user-select-avatar-ok').disabled = inProgress; | |
170 $('managed-user-list').disabled = inProgress; | |
171 $('select-avatar-grid').disabled = inProgress; | |
172 $('managed-user-import-throbber').hidden = !inProgress; | |
173 }, | |
174 | |
175 /** | |
176 * Sets the data model of the managed user list to |managedUsers|. | |
177 * @param {Array.<Object>} managedUsers An array of managed user objects. | |
178 * Each object is of the form: | |
179 * managedUser = { | |
180 * id: "Managed User ID", | |
181 * name: "Managed User Name", | |
182 * iconURL: "chrome://path/to/icon/image", | |
183 * onCurrentDevice: true or false, | |
184 * needAvatar: true or false | |
185 * } | |
186 * @private | |
187 */ | |
188 receiveExistingManagedUsers_: function(managedUsers) { | |
189 managedUsers.sort(function(a, b) { | |
190 if (a.onCurrentDevice != b.onCurrentDevice) | |
191 return a.onCurrentDevice ? 1 : -1; | |
192 return a.name.localeCompare(b.name); | |
193 }); | |
194 | |
195 $('managed-user-list').dataModel = new ArrayDataModel(managedUsers); | |
196 if (managedUsers.length == 0) { | |
197 this.onError_(loadTimeData.getString('noExistingManagedUsers')); | |
198 $('managed-user-import-ok').disabled = true; | |
199 } else { | |
200 // Hide the error bubble. | |
201 $('managed-user-import-error-bubble').hidden = true; | |
202 } | |
203 }, | |
204 | |
205 onSigninError_: function() { | |
206 $('managed-user-list').dataModel = null; | |
207 this.onError_(loadTimeData.getString('managedUserImportSigninError')); | |
208 }, | |
209 | |
210 /** | |
211 * Displays an error message if an error occurs while | |
212 * importing a managed user. | |
213 * Called by BrowserOptions via the BrowserOptionsHandler. | |
214 * @param {string} error The error message to display. | |
215 * @private | |
216 */ | |
217 onError_: function(error) { | |
218 var errorBubble = $('managed-user-import-error-bubble'); | |
219 errorBubble.hidden = false; | |
220 errorBubble.textContent = error; | |
221 this.updateImportInProgress_(false); | |
222 }, | |
223 | |
224 /** | |
225 * Closes the overlay if importing the managed user was successful. Also | |
226 * reset the cached list of managed users in order to get an updated list | |
227 * when the overlay is reopened. | |
228 * @private | |
229 */ | |
230 onSuccess_: function() { | |
231 this.updateImportInProgress_(false); | |
232 options.ManagedUserListData.resetPromise(); | |
233 OptionsPage.closeAllOverlays(); | |
234 }, | |
235 }; | |
236 | |
237 // Forward public APIs to private implementations. | |
238 [ | |
239 'onSuccess', | |
240 ].forEach(function(name) { | |
241 ManagedUserImportOverlay[name] = function() { | |
242 var instance = ManagedUserImportOverlay.getInstance(); | |
243 return instance[name + '_'].apply(instance, arguments); | |
244 }; | |
245 }); | |
246 | |
247 // Export | |
248 return { | |
249 ManagedUserImportOverlay: ManagedUserImportOverlay, | |
250 }; | |
251 }); | |
OLD | NEW |