blob: 8841b737036c55afbb5e36c71e0ef211678e5f7f [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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
5cr.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.
Ben Murdochbb1529c2013-08-08 10:24:53 +010072 self.setAllControlsEnabled_(false);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000073 },
74
75 /**
Ben Murdochbb1529c2013-08-08 10:24:53 +010076 * Sets the enabled and checked state of the commit button.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000077 * @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 /**
Ben Murdochbb1529c2013-08-08 10:24:53 +010087 * Sets the enabled state of all the checkboxes and the commit button.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000088 * @private
89 */
Ben Murdochbb1529c2013-08-08 10:24:53 +010090 setAllControlsEnabled_: function(enabled) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091 var checkboxes =
92 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
93 for (var i = 0; i < checkboxes.length; i++)
Ben Murdochbb1529c2013-08-08 10:24:53 +010094 this.setUpCheckboxState_(checkboxes[i], enabled);
95 $('import-data-commit').disabled = !enabled;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000096 },
97
98 /**
Ben Murdochbb1529c2013-08-08 10:24:53 +010099 * Sets the enabled and checked states of a checkbox element.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000100 * @param {Object} checkbox A checkbox element.
Ben Murdochbb1529c2013-08-08 10:24:53 +0100101 * @param {boolean} enabled The enabled state of the checkbox. If false,
102 * the checkbox is disabled and unchecked. If true, the checkbox is enabled
103 * and checked.
104 * @param {boolean} visible The visible state of the checkbox.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000105 * @private
106 */
107 setUpCheckboxState_: function(checkbox, enabled) {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100108 checkbox.setDisabled('noProfileData', !enabled);
109 checkbox.checked = enabled;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000110 },
111
112 /**
113 * Update the enabled and checked states of all checkboxes.
114 * @private
115 */
116 updateCheckboxes_: function() {
117 var index = $('import-browsers').selectedIndex;
118 var browserProfile;
119 if (this.browserProfiles.length > index)
120 browserProfile = this.browserProfiles[index];
121 var importOptions = ['history', 'favorites', 'passwords', 'search'];
122 for (var i = 0; i < importOptions.length; i++) {
123 var checkbox = $('import-' + importOptions[i]);
124 var enable = browserProfile && browserProfile[importOptions[i]];
125 this.setUpCheckboxState_(checkbox, enable);
Ben Murdochbb1529c2013-08-08 10:24:53 +0100126 var checkboxWithLabel = $('import-' + importOptions[i] + '-with-label');
127 checkboxWithLabel.style.display = enable ? '' : 'none';
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000128 }
129 },
130
131 /**
132 * Update the supported browsers popup with given entries.
133 * @param {array} browsers List of supported browsers name.
134 * @private
135 */
136 updateSupportedBrowsers_: function(browsers) {
137 this.browserProfiles = browsers;
138 var browserSelect = $('import-browsers');
139 browserSelect.remove(0); // Remove the 'Loading...' option.
140 browserSelect.textContent = '';
141 var browserCount = browsers.length;
142
143 if (browserCount == 0) {
144 var option = new Option(loadTimeData.getString('noProfileFound'), 0);
145 browserSelect.appendChild(option);
146
Ben Murdochbb1529c2013-08-08 10:24:53 +0100147 this.setAllControlsEnabled_(false);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000148 } else {
Ben Murdochbb1529c2013-08-08 10:24:53 +0100149 this.setAllControlsEnabled_(true);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000150 for (var i = 0; i < browserCount; i++) {
151 var browser = browsers[i];
152 var option = new Option(browser.name, browser.index);
153 browserSelect.appendChild(option);
154 }
155
156 this.updateCheckboxes_();
157 this.validateCommitButton_();
158 }
159 },
160
161 /**
162 * Clear import prefs set when user checks/unchecks a checkbox so that each
163 * checkbox goes back to the default "checked" state (or alternatively, to
164 * the state set by a recommended policy).
165 * @private
166 */
167 clearUserPrefs_: function() {
168 var importPrefs = ['import_history',
169 'import_bookmarks',
170 'import_saved_passwords',
171 'import_search_engine'];
172 for (var i = 0; i < importPrefs.length; i++)
173 Preferences.clearPref(importPrefs[i], true);
174 },
175
176 /**
177 * Update the dialog layout to reflect success state.
178 * @param {boolean} success If true, show success dialog elements.
179 * @private
180 */
181 updateSuccessState_: function(success) {
182 var sections = document.querySelectorAll('.import-data-configure');
183 for (var i = 0; i < sections.length; i++)
184 sections[i].hidden = success;
185
186 sections = document.querySelectorAll('.import-data-success');
187 for (var i = 0; i < sections.length; i++)
188 sections[i].hidden = !success;
189 },
190 };
191
192 ImportDataOverlay.clearUserPrefs = function() {
193 ImportDataOverlay.getInstance().clearUserPrefs_();
194 };
195
196 /**
197 * Update the supported browsers popup with given entries.
198 * @param {array} list of supported browsers name.
199 */
200 ImportDataOverlay.updateSupportedBrowsers = function(browsers) {
201 ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers);
202 };
203
204 /**
205 * Update the UI to reflect whether an import operation is in progress.
206 * @param {boolean} state True if an import operation is in progress.
207 */
208 ImportDataOverlay.setImportingState = function(state) {
209 var checkboxes =
210 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
211 for (var i = 0; i < checkboxes.length; i++)
212 checkboxes[i].setDisabled('Importing', state);
213 if (!state)
214 ImportDataOverlay.getInstance().updateCheckboxes_();
215 $('import-browsers').disabled = state;
216 $('import-throbber').style.visibility = state ? 'visible' : 'hidden';
217 ImportDataOverlay.getInstance().validateCommitButton_();
218 };
219
220 /**
221 * Remove the import overlay from display.
222 */
223 ImportDataOverlay.dismiss = function() {
224 ImportDataOverlay.clearUserPrefs();
225 OptionsPage.closeOverlay();
226 };
227
228 /**
229 * Show a message confirming the success of the import operation.
230 */
231 ImportDataOverlay.confirmSuccess = function() {
232 var showBookmarksMessage = $('import-favorites').checked;
233 ImportDataOverlay.setImportingState(false);
234 $('import-find-your-bookmarks').hidden = !showBookmarksMessage;
235 ImportDataOverlay.getInstance().updateSuccessState_(true);
236 };
237
238 /**
239 * Show the import data overlay.
240 */
241 ImportDataOverlay.show = function() {
242 // Make sure that any previous import success message is hidden, and
243 // we're showing the UI to import further data.
244 ImportDataOverlay.getInstance().updateSuccessState_(false);
Ben Murdochca12bfa2013-07-23 11:17:05 +0100245 ImportDataOverlay.getInstance().validateCommitButton_();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000246
247 OptionsPage.navigateToPage('importData');
248 };
249
250 // Export
251 return {
252 ImportDataOverlay: ImportDataOverlay
253 };
254});