OLD | NEW |
| (Empty) |
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 var ArrayDataModel = cr.ui.ArrayDataModel; | |
8 | |
9 /** | |
10 * ManageProfileOverlay class | |
11 * Encapsulated handling of the 'Manage profile...' overlay page. | |
12 * @constructor | |
13 * @class | |
14 */ | |
15 function ManageProfileOverlay() { | |
16 OptionsPage.call(this, 'manageProfile', | |
17 loadTimeData.getString('manageProfileTabTitle'), | |
18 'manage-profile-overlay'); | |
19 }; | |
20 | |
21 cr.addSingletonGetter(ManageProfileOverlay); | |
22 | |
23 ManageProfileOverlay.prototype = { | |
24 // Inherit from OptionsPage. | |
25 __proto__: OptionsPage.prototype, | |
26 | |
27 // Info about the currently managed/deleted profile. | |
28 profileInfo_: null, | |
29 | |
30 // An object containing all known profile names. | |
31 profileNames_: {}, | |
32 | |
33 // The currently selected icon in the icon grid. | |
34 iconGridSelectedURL_: null, | |
35 | |
36 /** | |
37 * Initialize the page. | |
38 */ | |
39 initializePage: function() { | |
40 // Call base class implementation to start preference initialization. | |
41 OptionsPage.prototype.initializePage.call(this); | |
42 | |
43 var self = this; | |
44 var iconGrid = $('manage-profile-icon-grid'); | |
45 var createIconGrid = $('create-profile-icon-grid'); | |
46 options.ProfilesIconGrid.decorate(iconGrid); | |
47 options.ProfilesIconGrid.decorate(createIconGrid); | |
48 iconGrid.addEventListener('change', function(e) { | |
49 self.onIconGridSelectionChanged_('manage'); | |
50 }); | |
51 createIconGrid.addEventListener('change', function(e) { | |
52 self.onIconGridSelectionChanged_('create'); | |
53 }); | |
54 | |
55 $('manage-profile-name').oninput = function(event) { | |
56 self.onNameChanged_(event, 'manage'); | |
57 }; | |
58 $('create-profile-name').oninput = function(event) { | |
59 self.onNameChanged_(event, 'create'); | |
60 }; | |
61 $('manage-profile-cancel').onclick = | |
62 $('delete-profile-cancel').onclick = | |
63 $('create-profile-cancel').onclick = function(event) { | |
64 OptionsPage.closeOverlay(); | |
65 }; | |
66 $('manage-profile-ok').onclick = function(event) { | |
67 OptionsPage.closeOverlay(); | |
68 self.submitManageChanges_(); | |
69 }; | |
70 $('delete-profile-ok').onclick = function(event) { | |
71 OptionsPage.closeOverlay(); | |
72 chrome.send('deleteProfile', [self.profileInfo_.filePath]); | |
73 }; | |
74 $('create-profile-ok').onclick = function(event) { | |
75 OptionsPage.closeOverlay(); | |
76 // Get the user's chosen name and icon, or default if they do not | |
77 // wish to customize their profile. | |
78 var name = $('create-profile-name').value; | |
79 var icon_url = createIconGrid.selectedItem; | |
80 chrome.send('createProfile', [name, icon_url]); | |
81 }; | |
82 }, | |
83 | |
84 /** @inheritDoc */ | |
85 didShowPage: function() { | |
86 chrome.send('requestDefaultProfileIcons'); | |
87 | |
88 // Just ignore the manage profile dialog on Chrome OS, they use /accounts. | |
89 if (!cr.isChromeOS && window.location.pathname == '/manageProfile') | |
90 ManageProfileOverlay.getInstance().prepareForManageDialog_(); | |
91 | |
92 $('manage-profile-name').focus(); | |
93 $('create-profile-name').focus(); | |
94 }, | |
95 | |
96 /** | |
97 * Set the profile info used in the dialog. | |
98 * @param {Object} profileInfo An object of the form: | |
99 * profileInfo = { | |
100 * name: "Profile Name", | |
101 * iconURL: "chrome://path/to/icon/image", | |
102 * filePath: "/path/to/profile/data/on/disk" | |
103 * isCurrentProfile: false, | |
104 * }; | |
105 * @param {String} mode A label that specifies the type of dialog | |
106 * box which is currently being viewed (i.e. 'create' or | |
107 * 'manage'). | |
108 * @private | |
109 */ | |
110 setProfileInfo_: function(profileInfo, mode) { | |
111 this.iconGridSelectedURL_ = profileInfo.iconURL; | |
112 this.profileInfo_ = profileInfo; | |
113 $(mode + '-profile-name').value = profileInfo.name; | |
114 $(mode + '-profile-icon-grid').selectedItem = profileInfo.iconURL; | |
115 }, | |
116 | |
117 /** | |
118 * Sets the name of the currently edited profile. | |
119 * @private | |
120 */ | |
121 setProfileName_: function(name) { | |
122 if (this.profileInfo_) | |
123 this.profileInfo_.name = name; | |
124 $('manage-profile-name').value = name; | |
125 }, | |
126 | |
127 /** | |
128 * the user will use to choose their profile icon. | |
129 * @param {Array.<string>} iconURLs An array of icon URLs. | |
130 * @private | |
131 */ | |
132 receiveDefaultProfileIcons_: function(iconGrid, iconURLs) { | |
133 $(iconGrid).dataModel = new ArrayDataModel(iconURLs); | |
134 | |
135 if (this.profileInfo_) | |
136 $(iconGrid).selectedItem = this.profileInfo_.iconURL; | |
137 | |
138 var grid = $(iconGrid); | |
139 // Recalculate the measured item size. | |
140 grid.measured_ = null; | |
141 grid.columns = 0; | |
142 grid.redraw(); | |
143 }, | |
144 | |
145 /** | |
146 * Set a dictionary of all profile names. These are used to prevent the | |
147 * user from naming two profiles the same. | |
148 * @param {Object} profileNames A dictionary of profile names. | |
149 * @private | |
150 */ | |
151 receiveProfileNames_: function(profileNames) { | |
152 this.profileNames_ = profileNames; | |
153 }, | |
154 | |
155 /** | |
156 * Display the error bubble, with |errorText| in the bubble. | |
157 * @param {string} errorText The localized string id to display as an error. | |
158 * @param {String} mode A label that specifies the type of dialog | |
159 * box which is currently being viewed (i.e. 'create' or | |
160 * 'manage'). | |
161 * @private | |
162 */ | |
163 showErrorBubble_: function(errorText, mode) { | |
164 var nameErrorEl = $(mode + '-profile-error-bubble'); | |
165 nameErrorEl.hidden = false; | |
166 nameErrorEl.textContent = loadTimeData.getString(errorText); | |
167 | |
168 $(mode + '-profile-ok').disabled = true; | |
169 }, | |
170 | |
171 /** | |
172 * Hide the error bubble. | |
173 * @param {String} mode A label that specifies the type of dialog | |
174 * box which is currently being viewed (i.e. 'create' or | |
175 * 'manage'). | |
176 * @private | |
177 */ | |
178 hideErrorBubble_: function(mode) { | |
179 $(mode + '-profile-error-bubble').hidden = true; | |
180 $(mode + '-profile-ok').disabled = false; | |
181 }, | |
182 | |
183 /** | |
184 * oninput callback for <input> field. | |
185 * @param {Event} event The event object. | |
186 * @param {String} mode A label that specifies the type of dialog | |
187 * box which is currently being viewed (i.e. 'create' or | |
188 * 'manage'). | |
189 * @private | |
190 */ | |
191 onNameChanged_: function(event, mode) { | |
192 var newName = event.target.value; | |
193 var oldName = this.profileInfo_.name; | |
194 | |
195 if (newName == oldName) { | |
196 this.hideErrorBubble_(mode); | |
197 } else if (this.profileNames_[newName] != undefined) { | |
198 this.showErrorBubble_('manageProfilesDuplicateNameError', mode); | |
199 } else { | |
200 this.hideErrorBubble_(mode); | |
201 | |
202 var nameIsValid = $(mode + '-profile-name').validity.valid; | |
203 $(mode + '-profile-ok').disabled = !nameIsValid; | |
204 } | |
205 }, | |
206 | |
207 /** | |
208 * Called when the user clicks "OK". Saves the newly changed profile info. | |
209 * @private | |
210 */ | |
211 submitManageChanges_: function() { | |
212 var name = $('manage-profile-name').value; | |
213 var iconURL = $('manage-profile-icon-grid').selectedItem; | |
214 chrome.send('setProfileNameAndIcon', | |
215 [this.profileInfo_.filePath, name, iconURL]); | |
216 }, | |
217 | |
218 /** | |
219 * Called when the selected icon in the icon grid changes. | |
220 * @param {String} mode A label that specifies the type of dialog | |
221 * box which is currently being viewed (i.e. 'create' or | |
222 * 'manage'). | |
223 * @private | |
224 */ | |
225 onIconGridSelectionChanged_: function(mode) { | |
226 var iconURL = $(mode + '-profile-icon-grid').selectedItem; | |
227 if (!iconURL || iconURL == this.iconGridSelectedURL_) | |
228 return; | |
229 this.iconGridSelectedURL_ = iconURL; | |
230 chrome.send('profileIconSelectionChanged', | |
231 [this.profileInfo_.filePath, iconURL]); | |
232 }, | |
233 | |
234 /** | |
235 * Updates the contents of the "Manage Profile" section of the dialog, | |
236 * and shows that section. | |
237 * @private | |
238 */ | |
239 prepareForManageDialog_: function() { | |
240 var profileInfo = BrowserOptions.getCurrentProfile(); | |
241 ManageProfileOverlay.setProfileInfo(profileInfo, 'manage'); | |
242 $('manage-profile-overlay-create').hidden = true; | |
243 $('manage-profile-overlay-manage').hidden = false; | |
244 $('manage-profile-overlay-delete').hidden = true; | |
245 this.hideErrorBubble_('manage'); | |
246 }, | |
247 | |
248 /** | |
249 * Display the "Manage Profile" dialog. | |
250 * @private | |
251 */ | |
252 showManageDialog_: function() { | |
253 this.prepareForManageDialog_(); | |
254 OptionsPage.navigateToPage('manageProfile'); | |
255 }, | |
256 | |
257 /** | |
258 * Display the "Delete Profile" dialog. | |
259 * @param {Object} profileInfo The profile object of the profile to delete. | |
260 * @private | |
261 */ | |
262 showDeleteDialog_: function(profileInfo) { | |
263 ManageProfileOverlay.setProfileInfo(profileInfo, 'manage'); | |
264 $('manage-profile-overlay-create').hidden = true; | |
265 $('manage-profile-overlay-manage').hidden = true; | |
266 $('manage-profile-overlay-delete').hidden = false; | |
267 $('delete-profile-message').textContent = | |
268 loadTimeData.getStringF('deleteProfileMessage', profileInfo.name); | |
269 $('delete-profile-message').style.backgroundImage = 'url("' + | |
270 profileInfo.iconURL + '")'; | |
271 | |
272 // Because this dialog isn't useful when refreshing or as part of the | |
273 // history, don't create a history entry for it when showing. | |
274 OptionsPage.showPageByName('manageProfile', false); | |
275 }, | |
276 | |
277 /** | |
278 * Display the "Create Profile" dialog. | |
279 * @param {Object} profileInfo The profile object of the profile to | |
280 * create. Upon creation, this object only needs a name and an avatar. | |
281 * @private | |
282 */ | |
283 showCreateDialog_: function(profileInfo) { | |
284 ManageProfileOverlay.setProfileInfo(profileInfo, 'create'); | |
285 $('manage-profile-overlay-create').hidden = false; | |
286 $('manage-profile-overlay-manage').hidden = true; | |
287 $('manage-profile-overlay-delete').hidden = true; | |
288 $('create-profile-instructions').textContent = | |
289 loadTimeData.getStringF('createProfileInstructions'); | |
290 ManageProfileOverlay.getInstance().hideErrorBubble_('create'); | |
291 | |
292 OptionsPage.showPageByName('manageProfile', false); | |
293 }, | |
294 | |
295 }; | |
296 | |
297 // Forward public APIs to private implementations. | |
298 [ | |
299 'receiveDefaultProfileIcons', | |
300 'receiveProfileNames', | |
301 'setProfileInfo', | |
302 'setProfileName', | |
303 'showManageDialog', | |
304 'showDeleteDialog', | |
305 'showCreateDialog', | |
306 ].forEach(function(name) { | |
307 ManageProfileOverlay[name] = function() { | |
308 var instance = ManageProfileOverlay.getInstance(); | |
309 return instance[name + '_'].apply(instance, arguments); | |
310 }; | |
311 }); | |
312 | |
313 // Export | |
314 return { | |
315 ManageProfileOverlay: ManageProfileOverlay | |
316 }; | |
317 }); | |
OLD | NEW |