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);
23 ImportDataOverlay.prototype = {
25 __proto__: Page.prototype,
28 initializePage: function() {
29 Page.prototype.initializePage.call(this);
33 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
34 for (var i = 0; i < checkboxes.length; i++) {
35 checkboxes[i].onchange = function() {
36 self.validateCommitButton_();
40 $('import-browsers').onchange = function() {
41 self.updateCheckboxes_();
42 self.validateCommitButton_();
43 self.updateBottomBar_();
46 $('import-data-commit').onclick = function() {
47 chrome.send('importData', [
48 String($('import-browsers').selectedIndex),
49 String($('import-history').checked),
50 String($('import-favorites').checked),
51 String($('import-passwords').checked),
52 String($('import-search').checked)]);
55 $('import-data-cancel').onclick = function() {
56 ImportDataOverlay.dismiss();
59 $('import-choose-file').onclick = function() {
60 chrome.send('chooseBookmarksFile');
63 $('import-data-confirm').onclick = function() {
64 ImportDataOverlay.dismiss();
67 // Form controls are disabled until the profile list has been loaded.
68 self.setAllControlsEnabled_(false);
72 * Sets the enabled and checked state of the commit button.
75 validateCommitButton_: function() {
76 var somethingToImport =
77 $('import-history').checked || $('import-favorites').checked ||
78 $('import-passwords').checked || $('import-search').checked;
79 $('import-data-commit').disabled = !somethingToImport;
80 $('import-choose-file').disabled = !$('import-favorites').checked;
84 * Sets the enabled state of all the checkboxes and the commit button.
87 setAllControlsEnabled_: function(enabled) {
89 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
90 for (var i = 0; i < checkboxes.length; i++)
91 this.setUpCheckboxState_(checkboxes[i], enabled);
92 $('import-data-commit').disabled = !enabled;
93 $('import-choose-file').hidden = !enabled;
95 $('mac-password-keychain').hidden = !enabled;
100 * Sets the enabled state of a checkbox element.
101 * @param {Object} checkbox A checkbox element.
102 * @param {boolean} enabled The enabled state of the checkbox. If false,
103 * the checkbox is disabled. If true, the checkbox is enabled.
106 setUpCheckboxState_: function(checkbox, enabled) {
107 checkbox.setDisabled('noProfileData', !enabled);
111 * Update the enabled and visible states of all the checkboxes.
114 updateCheckboxes_: function() {
115 var index = $('import-browsers').selectedIndex;
116 var bookmarksFileSelected = index == this.browserProfiles.length - 1;
117 $('import-choose-file').hidden = !bookmarksFileSelected;
118 $('import-data-commit').hidden = bookmarksFileSelected;
121 if (this.browserProfiles.length > index)
122 browserProfile = this.browserProfiles[index];
123 var importOptions = ['history', 'favorites', 'passwords', 'search'];
124 for (var i = 0; i < importOptions.length; i++) {
125 var checkbox = $('import-' + importOptions[i]);
126 var enable = browserProfile && browserProfile[importOptions[i]];
127 this.setUpCheckboxState_(checkbox, enable);
128 var checkboxWithLabel = $('import-' + importOptions[i] + '-with-label');
129 checkboxWithLabel.style.display = enable ? '' : 'none';
134 * Show or hide gray message at the bottom.
137 updateBottomBar_: function() {
138 var index = $('import-browsers').selectedIndex;
140 if (this.browserProfiles.length > index)
141 browserProfile = this.browserProfiles[index];
142 var enable = browserProfile && browserProfile['show_bottom_bar'];
143 <if expr="is_macosx">
144 $('mac-password-keychain').hidden = !enable;
149 * Update the supported browsers popup with given entries.
150 * @param {array} browsers List of supported browsers name.
153 updateSupportedBrowsers_: function(browsers) {
154 this.browserProfiles = browsers;
155 var browserSelect = $('import-browsers');
156 browserSelect.remove(0); // Remove the 'Loading...' option.
157 browserSelect.textContent = '';
158 var browserCount = browsers.length;
160 if (browserCount == 0) {
161 var option = new Option(loadTimeData.getString('noProfileFound'), 0);
162 browserSelect.appendChild(option);
164 this.setAllControlsEnabled_(false);
166 this.setAllControlsEnabled_(true);
167 for (var i = 0; i < browserCount; i++) {
168 var browser = browsers[i];
169 var option = new Option(browser.name, browser.index);
170 browserSelect.appendChild(option);
173 this.updateCheckboxes_();
174 this.validateCommitButton_();
175 this.updateBottomBar_();
180 * Clear import prefs set when user checks/unchecks a checkbox so that each
181 * checkbox goes back to the default "checked" state (or alternatively, to
182 * the state set by a recommended policy).
185 clearUserPrefs_: function() {
186 var importPrefs = ['import_history',
188 'import_saved_passwords',
189 'import_search_engine'];
190 for (var i = 0; i < importPrefs.length; i++)
191 Preferences.clearPref(importPrefs[i], true);
195 * Update the dialog layout to reflect success state.
196 * @param {boolean} success If true, show success dialog elements.
199 updateSuccessState_: function(success) {
200 var sections = document.querySelectorAll('.import-data-configure');
201 for (var i = 0; i < sections.length; i++)
202 sections[i].hidden = success;
204 sections = document.querySelectorAll('.import-data-success');
205 for (var i = 0; i < sections.length; i++)
206 sections[i].hidden = !success;
210 ImportDataOverlay.clearUserPrefs = function() {
211 ImportDataOverlay.getInstance().clearUserPrefs_();
215 * Update the supported browsers popup with given entries.
216 * @param {array} list of supported browsers name.
218 ImportDataOverlay.updateSupportedBrowsers = function(browsers) {
219 ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers);
223 * Update the UI to reflect whether an import operation is in progress.
224 * @param {boolean} importing True if an import operation is in progress.
226 ImportDataOverlay.setImportingState = function(importing) {
228 document.querySelectorAll('#import-checkboxes input[type=checkbox]');
229 for (var i = 0; i < checkboxes.length; i++)
230 checkboxes[i].setDisabled('Importing', importing);
232 ImportDataOverlay.getInstance().updateCheckboxes_();
233 $('import-browsers').disabled = importing;
234 $('import-throbber').style.visibility = importing ? 'visible' : 'hidden';
235 ImportDataOverlay.getInstance().validateCommitButton_();
239 * Remove the import overlay from display.
241 ImportDataOverlay.dismiss = function() {
242 ImportDataOverlay.clearUserPrefs();
243 PageManager.closeOverlay();
247 * Show a message confirming the success of the import operation.
249 ImportDataOverlay.confirmSuccess = function() {
250 var showBookmarksMessage = $('import-favorites').checked;
251 ImportDataOverlay.setImportingState(false);
252 $('import-find-your-bookmarks').hidden = !showBookmarksMessage;
253 ImportDataOverlay.getInstance().updateSuccessState_(true);
257 * Show the import data overlay.
259 ImportDataOverlay.show = function() {
260 // Make sure that any previous import success message is hidden, and
261 // we're showing the UI to import further data.
262 ImportDataOverlay.getInstance().updateSuccessState_(false);
263 ImportDataOverlay.getInstance().validateCommitButton_();
265 PageManager.showPageByName('importData');
270 ImportDataOverlay: ImportDataOverlay