OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 const localStrings = new LocalStrings(); | |
10 | |
11 /** | |
12 * ProfilesManageOverlay class | |
13 * Encapsulated handling of the 'Manage profile...' overlay page. | |
James Hawkins
2011/07/19 20:22:09
@constructor
| |
14 * @class | |
15 */ | |
16 function ProfilesManageOverlay() { | |
17 OptionsPage.call(this, | |
18 'profilesManage', | |
19 templateData.profilesManageOverlayTabTitle, | |
20 'profiles-manage-overlay'); | |
21 }; | |
22 | |
23 cr.addSingletonGetter(ProfilesManageOverlay); | |
24 | |
25 ProfilesManageOverlay.prototype = { | |
26 // Inherit from OptionsPage. | |
27 __proto__: OptionsPage.prototype, | |
28 | |
29 // Info about the currently managed/deleted profile. | |
30 profileInfo_: null, | |
31 | |
32 // The list of all known profile names. | |
33 profileNames_: [], | |
34 | |
35 /** | |
36 * Initialize the page. | |
37 */ | |
38 initializePage: function() { | |
39 // Call base class implementation to start preference initialization. | |
40 OptionsPage.prototype.initializePage.call(this); | |
41 | |
42 var self = this; | |
43 var iconList = $('profiles-manage-icon-list'); | |
44 options.ProfilesIconList.decorate(iconList); | |
45 | |
46 $('profiles-manage-name').oninput = this.onNameChanged_.bind(this); | |
47 $('profiles-manage-cancel').onclick = | |
48 $('profiles-delete-cancel').onclick = function(event) { | |
49 OptionsPage.closeOverlay(); | |
50 }; | |
51 $('profiles-manage-ok').onclick = function(event) { | |
52 OptionsPage.closeOverlay(); | |
53 self.submitManageChanges_(); | |
54 }; | |
55 $('profiles-delete-ok').onclick = function(event) { | |
56 OptionsPage.closeOverlay(); | |
57 chrome.send('deleteProfile', [self.profileInfo_.filePath]); | |
58 }; | |
59 }, | |
60 | |
61 /** @inheritDoc */ | |
62 didShowPage: function() { | |
63 $('profiles-manage-icon-list').redraw(); | |
64 $('profiles-manage-name').focus(); | |
65 }, | |
66 | |
67 /** | |
68 * Set the profile info used in the dialog. | |
69 * @param {Object} profileInfo An object like this: | |
70 * profileInfo = { | |
71 * name: "Profile Name", | |
72 * iconURL: "chrome://path/to/icon/image", | |
73 * filePath: "/path/to/profile/data/on/disk" | |
74 * }; | |
75 * @private | |
76 */ | |
77 setProfileInfo_: function(profileInfo) { | |
78 this.profileInfo_ = profileInfo; | |
79 $('profiles-manage-name').value = profileInfo.name; | |
80 $('profiles-manage-icon-list').selectedItem = profileInfo.iconURL; | |
81 }, | |
82 | |
83 /** | |
84 * Called by the C++ handler to give icon URLs to javascript. These are the | |
85 * icons that can be chosen by the user. | |
86 * @param {Array.<string>} iconURLs An array of icon URLs. | |
87 * @private | |
88 */ | |
89 receiveProfileIcons_: function(iconURLs) { | |
90 $('profiles-manage-icon-list').dataModel = new ArrayDataModel(iconURLs); | |
91 }, | |
92 | |
93 /** | |
94 * Called by the C++ handler to give profile names to javascript. These are | |
95 * used to prevent the user from naming two profiles the same. | |
96 * @param {Object} profileNames A dictionary of profile names. | |
97 * @private | |
98 */ | |
99 receiveProfileNames_: function(profileNames) { | |
100 this.profileNames_ = profileNames; | |
101 }, | |
102 | |
103 /** | |
104 * Determine whether |name| is valid; i.e. not equal to any other profile | |
105 * name. | |
106 * @param {string} name The profile name to validate. | |
107 * @return true if the name is not equal to any other profile name. | |
108 * @private | |
109 */ | |
110 isNameValid_: function(name) { | |
111 // if the name hasn't changed, assume it is valid. | |
112 if (name == this.profileInfo_.name) | |
113 return true; | |
114 | |
115 return this.profileNames_[name] == undefined; | |
116 }, | |
117 | |
118 /** | |
119 * Update the UI elements accordingly if the profile name is valid/invalid. | |
120 * @param {boolean} isValid True if the UI should be updated as if the name | |
121 * was valid. | |
James Hawkins
2011/07/19 20:22:09
s/was/were/
| |
122 * @private | |
123 */ | |
124 setNameIsValid_: function(isValid) { | |
125 var dupeNameErrorEl = $('profiles-manage-duplicate-name-error'); | |
126 if (isValid) | |
127 dupeNameErrorEl.classList.add('hiding'); | |
128 else | |
129 dupeNameErrorEl.classList.remove('hiding'); | |
130 | |
131 $('profiles-manage-ok').disabled = !isValid; | |
132 }, | |
133 | |
134 /** | |
135 * oninput callback for <input> field. | |
136 * @param event The event object | |
137 * @private | |
138 */ | |
139 onNameChanged_: function(event) { | |
140 this.setNameIsValid_(this.isNameValid_(event.target.value)); | |
141 }, | |
142 | |
143 /** | |
144 * Called when the user clicks "OK". Saves the newly changed profile info. | |
145 * @private | |
146 */ | |
147 submitManageChanges_: function() { | |
148 var name = $('profiles-manage-name').value; | |
149 var iconURL = $('profiles-manage-icon-list').selectedItem; | |
150 chrome.send('setProfileNameAndIcon', | |
151 [this.profileInfo_.filePath, name, iconURL]); | |
152 }, | |
153 }; | |
154 | |
155 // Forward public APIs to private implementations. | |
156 [ | |
157 'setProfileInfo', | |
158 'receiveProfileIcons', | |
159 'receiveProfileNames', | |
160 ].forEach(function(name) { | |
161 ProfilesManageOverlay[name] = function(value) { | |
162 ProfilesManageOverlay.getInstance()[name + '_'](value); | |
163 }; | |
164 }); | |
165 | |
166 /** | |
167 * Display the "Manage Profile" dialog. | |
168 * @param {Object} profileInfo The profile object of the profile to manage. | |
169 * @public | |
170 */ | |
171 ProfilesManageOverlay.show = function(profileInfo) { | |
James Hawkins
2011/07/19 20:22:09
Move to a private implementation and forward.
| |
172 ProfilesManageOverlay.setProfileInfo(profileInfo); | |
173 $('profiles-manage-overlay-manage').hidden = false; | |
174 $('profiles-manage-overlay-delete').hidden = true; | |
175 ProfilesManageOverlay.getInstance().setNameIsValid_(true); | |
176 | |
177 // Intentionally don't show the URL in the location bar as we don't want | |
178 // people trying to navigate here by hand. | |
179 OptionsPage.showPageByName('profilesManage', false); | |
180 }; | |
181 | |
182 /** | |
183 * Display the "Delete Profile" dialog. | |
184 * @param {Object} profileInfo The profile object of the profile to delete. | |
185 */ | |
186 ProfilesManageOverlay.showDeleteDialog = function(profileInfo) { | |
187 ProfilesManageOverlay.setProfileInfo(profileInfo); | |
188 $('profiles-manage-overlay-manage').hidden = true; | |
189 $('profiles-manage-overlay-delete').hidden = false; | |
190 $('profiles-delete-message').textContent = | |
191 localStrings.getStringF('profilesDeleteMessage', profileInfo.name); | |
192 | |
193 // Intentionally don't show the URL in the location bar as we don't want | |
194 // people trying to navigate here by hand. | |
195 OptionsPage.showPageByName('profilesManage', false); | |
196 }; | |
197 | |
198 // Export | |
199 return { | |
200 ProfilesManageOverlay: ProfilesManageOverlay | |
201 }; | |
202 }); | |
OLD | NEW |