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 | |
8 /** | |
9 * ImportDataOverlay class | |
10 * Encapsulated handling of the 'Import Data' overlay page. | |
11 * @class | |
12 */ | |
13 function ImportDataOverlay() { | |
14 OptionsPage.call(this, | |
15 'importData', | |
16 loadTimeData.getString('importDataOverlayTabTitle'), | |
17 'import-data-overlay'); | |
18 } | |
19 | |
20 cr.addSingletonGetter(ImportDataOverlay); | |
21 | |
22 ImportDataOverlay.prototype = { | |
23 // Inherit from OptionsPage. | |
24 __proto__: OptionsPage.prototype, | |
25 | |
26 /** | |
27 * Initialize the page. | |
28 */ | |
29 initializePage: function() { | |
30 // Call base class implementation to start preference initialization. | |
31 OptionsPage.prototype.initializePage.call(this); | |
32 | |
33 var self = this; | |
34 var checkboxes = | |
35 document.querySelectorAll('#import-checkboxes input[type=checkbox]'); | |
36 for (var i = 0; i < checkboxes.length; i++) { | |
37 checkboxes[i].onchange = function() { | |
38 self.validateCommitButton_(); | |
39 }; | |
40 } | |
41 | |
42 $('import-browsers').onchange = function() { | |
43 self.updateCheckboxes_(); | |
44 self.validateCommitButton_(); | |
45 }; | |
46 | |
47 $('import-data-commit').onclick = function() { | |
48 chrome.send('importData', [ | |
49 String($('import-browsers').selectedIndex), | |
50 String($('import-history').checked), | |
51 String($('import-favorites').checked), | |
52 String($('import-passwords').checked), | |
53 String($('import-search').checked)]); | |
54 }; | |
55 | |
56 $('import-data-cancel').onclick = function() { | |
57 ImportDataOverlay.dismiss(); | |
58 }; | |
59 | |
60 $('import-data-show-bookmarks-bar').onchange = function() { | |
61 // Note: The callback 'toggleShowBookmarksBar' is handled within the | |
62 // browser options handler -- rather than the import data handler -- | |
63 // as the implementation is shared by several clients. | |
64 chrome.send('toggleShowBookmarksBar'); | |
65 } | |
66 | |
67 $('import-data-confirm').onclick = function() { | |
68 ImportDataOverlay.dismiss(); | |
69 }; | |
70 | |
71 // Form controls are disabled until the profile list has been loaded. | |
72 self.setControlsSensitive_(false); | |
73 }, | |
74 | |
75 /** | |
76 * Set enabled and checked state of the commit button. | |
77 * @private | |
78 */ | |
79 validateCommitButton_: function() { | |
80 var somethingToImport = | |
81 $('import-history').checked || $('import-favorites').checked || | |
82 $('import-passwords').checked || $('import-search').checked; | |
83 $('import-data-commit').disabled = !somethingToImport; | |
84 }, | |
85 | |
86 /** | |
87 * Sets the sensitivity of all the checkboxes and the commit button. | |
88 * @private | |
89 */ | |
90 setControlsSensitive_: function(sensitive) { | |
91 var checkboxes = | |
92 document.querySelectorAll('#import-checkboxes input[type=checkbox]'); | |
93 for (var i = 0; i < checkboxes.length; i++) | |
94 this.setUpCheckboxState_(checkboxes[i], sensitive); | |
95 $('import-data-commit').disabled = !sensitive; | |
96 }, | |
97 | |
98 /** | |
99 * Set enabled and checked states a checkbox element. | |
100 * @param {Object} checkbox A checkbox element. | |
101 * @param {boolean} enabled The enabled state of the chekbox. | |
102 * @private | |
103 */ | |
104 setUpCheckboxState_: function(checkbox, enabled) { | |
105 checkbox.setDisabled('noProfileData', !enabled); | |
106 }, | |
107 | |
108 /** | |
109 * Update the enabled and checked states of all checkboxes. | |
110 * @private | |
111 */ | |
112 updateCheckboxes_: function() { | |
113 var index = $('import-browsers').selectedIndex; | |
114 var browserProfile; | |
115 if (this.browserProfiles.length > index) | |
116 browserProfile = this.browserProfiles[index]; | |
117 var importOptions = ['history', 'favorites', 'passwords', 'search']; | |
118 for (var i = 0; i < importOptions.length; i++) { | |
119 var checkbox = $('import-' + importOptions[i]); | |
120 var enable = browserProfile && browserProfile[importOptions[i]]; | |
121 this.setUpCheckboxState_(checkbox, enable); | |
122 } | |
123 }, | |
124 | |
125 /** | |
126 * Update the supported browsers popup with given entries. | |
127 * @param {array} browsers List of supported browsers name. | |
128 * @private | |
129 */ | |
130 updateSupportedBrowsers_: function(browsers) { | |
131 this.browserProfiles = browsers; | |
132 var browserSelect = $('import-browsers'); | |
133 browserSelect.remove(0); // Remove the 'Loading...' option. | |
134 browserSelect.textContent = ''; | |
135 var browserCount = browsers.length; | |
136 | |
137 if (browserCount == 0) { | |
138 var option = new Option(loadTimeData.getString('noProfileFound'), 0); | |
139 browserSelect.appendChild(option); | |
140 | |
141 this.setControlsSensitive_(false); | |
142 } else { | |
143 this.setControlsSensitive_(true); | |
144 for (var i = 0; i < browserCount; i++) { | |
145 var browser = browsers[i]; | |
146 var option = new Option(browser['name'], browser['index']); | |
147 browserSelect.appendChild(option); | |
148 } | |
149 | |
150 this.updateCheckboxes_(); | |
151 this.validateCommitButton_(); | |
152 } | |
153 }, | |
154 | |
155 /** | |
156 * Clear import prefs set when user checks/unchecks a checkbox so that each | |
157 * checkbox goes back to the default "checked" state (or alternatively, to | |
158 * the state set by a recommended policy). | |
159 * @private | |
160 */ | |
161 clearUserPrefs_: function() { | |
162 var importPrefs = ['import_history', | |
163 'import_bookmarks', | |
164 'import_saved_passwords', | |
165 'import_search_engine']; | |
166 for (var i = 0; i < importPrefs.length; i++) | |
167 Preferences.clearPref(importPrefs[i], undefined); | |
168 }, | |
169 | |
170 /** | |
171 * Update the dialog layout to reflect success state. | |
172 * @param {boolean} success If true, show success dialog elements. | |
173 * @private | |
174 */ | |
175 updateSuccessState_: function(success) { | |
176 var sections = document.querySelectorAll('.import-data-configure'); | |
177 for (var i = 0; i < sections.length; i++) | |
178 sections[i].hidden = success; | |
179 | |
180 sections = document.querySelectorAll('.import-data-success'); | |
181 for (var i = 0; i < sections.length; i++) | |
182 sections[i].hidden = !success; | |
183 }, | |
184 }; | |
185 | |
186 ImportDataOverlay.clearUserPrefs = function() { | |
187 ImportDataOverlay.getInstance().clearUserPrefs_(); | |
188 }; | |
189 | |
190 /** | |
191 * Update the supported browsers popup with given entries. | |
192 * @param {array} list of supported browsers name. | |
193 */ | |
194 ImportDataOverlay.updateSupportedBrowsers = function(browsers) { | |
195 ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers); | |
196 }; | |
197 | |
198 /** | |
199 * Update the UI to reflect whether an import operation is in progress. | |
200 * @param {boolean} state True if an import operation is in progress. | |
201 */ | |
202 ImportDataOverlay.setImportingState = function(state) { | |
203 var checkboxes = | |
204 document.querySelectorAll('#import-checkboxes input[type=checkbox]'); | |
205 for (var i = 0; i < checkboxes.length; i++) | |
206 checkboxes[i].setDisabled('Importing', state); | |
207 if (!state) | |
208 ImportDataOverlay.getInstance().updateCheckboxes_(); | |
209 $('import-browsers').disabled = state; | |
210 $('import-throbber').style.visibility = state ? 'visible' : 'hidden'; | |
211 ImportDataOverlay.getInstance().validateCommitButton_(); | |
212 }; | |
213 | |
214 /** | |
215 * Remove the import overlay from display. | |
216 */ | |
217 ImportDataOverlay.dismiss = function() { | |
218 ImportDataOverlay.clearUserPrefs(); | |
219 OptionsPage.closeOverlay(); | |
220 }; | |
221 | |
222 /** | |
223 * Show a message confirming the success of the import operation. | |
224 */ | |
225 ImportDataOverlay.confirmSuccess = function() { | |
226 var showBookmarksMessage = $('import-favorites').checked; | |
227 ImportDataOverlay.setImportingState(false); | |
228 $('import-find-your-bookmarks').hidden = !showBookmarksMessage; | |
229 ImportDataOverlay.getInstance().updateSuccessState_(true); | |
230 }; | |
231 | |
232 /** | |
233 * Show the import data overlay. | |
234 */ | |
235 ImportDataOverlay.show = function() { | |
236 // Make sure that any previous import success message is hidden, and | |
237 // we're showing the UI to import further data. | |
238 ImportDataOverlay.getInstance().updateSuccessState_(false); | |
239 | |
240 OptionsPage.navigateToPage('importData'); | |
241 }; | |
242 | |
243 // Export | |
244 return { | |
245 ImportDataOverlay: ImportDataOverlay | |
246 }; | |
247 }); | |
OLD | NEW |