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.
5 cr.define('options', function() {
6 var Page = cr.ui.pageManager.Page;
7 var PageManager = cr.ui.pageManager.PageManager;
10 * ImportDataOverlay class
11 * Encapsulated handling of the 'Import Data' overlay page.
14 function ImportDataOverlay() {
17 loadTimeData.getString('importDataOverlayTabTitle'),
18 'import-data-overlay');
21 cr.addSingletonGetter(ImportDataOverlay);
24 * @param {string} type The type of data to import. Used in the element's ID.
26 function importable(type) {
27 var id = 'import-' + type;
28 return $(id).checked && !$(id + '-with-label').hidden;
31 ImportDataOverlay.prototype = {
33 __proto__: Page.prototype,
36 initializePage: function() {
37 Page.prototype.initializePage.call(this);
41 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
42 for (var i = 0; i < checkboxes.length; i++) {
43 checkboxes[i].customPrefChangeHandler = function(e) {
44 options.PrefCheckbox.prototype.defaultPrefChangeHandler.call(this, e);
45 self.validateCommitButton_();
50 $('import-browsers').onchange = function() {
51 self.updateCheckboxes_();
52 self.validateCommitButton_();
55 $('import-data-commit').onclick = function() {
56 chrome.send('importData', [
57 String($('import-browsers').selectedIndex),
58 String($('import-history').checked),
59 String($('import-favorites').checked),
60 String($('import-passwords').checked),
61 String($('import-search').checked),
62 String($('import-autofill-form-data').checked)]);
65 $('import-data-cancel').onclick = function() {
66 ImportDataOverlay.dismiss();
69 $('import-choose-file').onclick = function() {
70 chrome.send('chooseBookmarksFile');
73 $('import-data-confirm').onclick = function() {
74 ImportDataOverlay.dismiss();
77 // Form controls are disabled until the profile list has been loaded.
78 self.setAllControlsEnabled_(false);
82 * Sets the enabled and checked state of the commit button.
85 validateCommitButton_: function() {
86 var somethingToImport =
87 importable('history') || importable('favorites') ||
88 importable('passwords') || importable('search') ||
89 importable('autofill-form-data');
90 $('import-data-commit').disabled = !somethingToImport;
91 $('import-choose-file').disabled = !$('import-favorites').checked;
95 * Sets the enabled state of all the checkboxes and the commit button.
98 setAllControlsEnabled_: function(enabled) {
100 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
101 for (var i = 0; i < checkboxes.length; i++)
102 this.setUpCheckboxState_(checkboxes[i], enabled);
103 $('import-data-commit').disabled = !enabled;
104 $('import-choose-file').hidden = !enabled;
108 * Sets the enabled state of a checkbox element.
109 * @param {Object} checkbox A checkbox element.
110 * @param {boolean} enabled The enabled state of the checkbox. If false,
111 * the checkbox is disabled. If true, the checkbox is enabled.
114 setUpCheckboxState_: function(checkbox, enabled) {
115 checkbox.setDisabled('noProfileData', !enabled);
119 * Update the enabled and visible states of all the checkboxes.
122 updateCheckboxes_: function() {
123 var index = $('import-browsers').selectedIndex;
124 var bookmarksFileSelected = index == this.browserProfiles.length - 1;
125 $('import-choose-file').hidden = !bookmarksFileSelected;
126 $('import-data-commit').hidden = bookmarksFileSelected;
129 if (this.browserProfiles.length > index)
130 browserProfile = this.browserProfiles[index];
131 var importOptions = ['history',
135 'autofill-form-data'];
136 for (var i = 0; i < importOptions.length; i++) {
137 var id = 'import-' + importOptions[i];
138 var enable = browserProfile && browserProfile[importOptions[i]];
139 this.setUpCheckboxState_($(id), enable);
140 $(id + '-with-label').hidden = !enable;
145 * Update the supported browsers popup with given entries.
146 * @param {Array} browsers List of supported browsers name.
149 updateSupportedBrowsers_: function(browsers) {
150 this.browserProfiles = browsers;
151 var browserSelect = $('import-browsers');
152 browserSelect.remove(0); // Remove the 'Loading...' option.
153 browserSelect.textContent = '';
154 var browserCount = browsers.length;
156 if (browserCount == 0) {
157 var option = new Option(loadTimeData.getString('noProfileFound'), 0);
158 browserSelect.appendChild(option);
160 this.setAllControlsEnabled_(false);
162 this.setAllControlsEnabled_(true);
163 for (var i = 0; i < browserCount; i++) {
164 var browser = browsers[i];
165 var option = new Option(browser.name, browser.index);
166 browserSelect.appendChild(option);
169 this.updateCheckboxes_();
170 this.validateCommitButton_();
175 * Clear import prefs set when user checks/unchecks a checkbox so that each
176 * checkbox goes back to the default "checked" state (or alternatively, to
177 * the state set by a recommended policy).
180 clearUserPrefs_: function() {
181 var importPrefs = ['import_history',
183 'import_saved_passwords',
184 'import_search_engine',
185 'import_autofill_form_data'];
186 for (var i = 0; i < importPrefs.length; i++)
187 Preferences.clearPref(importPrefs[i], true);
191 * Update the dialog layout to reflect success state.
192 * @param {boolean} success If true, show success dialog elements.
195 updateSuccessState_: function(success) {
196 var sections = document.querySelectorAll('.import-data-configure');
197 for (var i = 0; i < sections.length; i++)
198 sections[i].hidden = success;
200 sections = document.querySelectorAll('.import-data-success');
201 for (var i = 0; i < sections.length; i++)
202 sections[i].hidden = !success;
206 ImportDataOverlay.clearUserPrefs = function() {
207 ImportDataOverlay.getInstance().clearUserPrefs_();
211 * Update the supported browsers popup with given entries.
212 * @param {Array} browsers List of supported browsers name.
214 ImportDataOverlay.updateSupportedBrowsers = function(browsers) {
215 ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers);
219 * Update the UI to reflect whether an import operation is in progress.
220 * @param {boolean} importing True if an import operation is in progress.
222 ImportDataOverlay.setImportingState = function(importing) {
224 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
225 for (var i = 0; i < checkboxes.length; i++)
226 checkboxes[i].setDisabled('Importing', importing);
228 ImportDataOverlay.getInstance().updateCheckboxes_();
229 $('import-browsers').disabled = importing;
230 $('import-throbber').style.visibility = importing ? 'visible' : 'hidden';
231 ImportDataOverlay.getInstance().validateCommitButton_();
235 * Remove the import overlay from display.
237 ImportDataOverlay.dismiss = function() {
238 ImportDataOverlay.clearUserPrefs();
239 PageManager.closeOverlay();
243 * Show a message confirming the success of the import operation.
245 ImportDataOverlay.confirmSuccess = function() {
246 var showBookmarksMessage = $('import-favorites').checked;
247 ImportDataOverlay.setImportingState(false);
248 $('import-find-your-bookmarks').hidden = !showBookmarksMessage;
249 ImportDataOverlay.getInstance().updateSuccessState_(true);
253 * Show the import data overlay.
255 ImportDataOverlay.show = function() {
256 // Make sure that any previous import success message is hidden, and
257 // we're showing the UI to import further data.
258 ImportDataOverlay.getInstance().updateSuccessState_(false);
259 ImportDataOverlay.getInstance().validateCommitButton_();
261 PageManager.showPageByName('importData');
266 ImportDataOverlay: ImportDataOverlay