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_();
53 self
.updateBottomBar_();
56 $('import-data-commit').onclick = function() {
57 chrome
.send('importData', [
58 String($('import-browsers').selectedIndex
),
59 String($('import-history').checked
),
60 String($('import-favorites').checked
),
61 String($('import-passwords').checked
),
62 String($('import-search').checked
),
63 String($('import-autofill-form-data').checked
)]);
66 $('import-data-cancel').onclick = function() {
67 ImportDataOverlay
.dismiss();
70 $('import-choose-file').onclick = function() {
71 chrome
.send('chooseBookmarksFile');
74 $('import-data-confirm').onclick = function() {
75 ImportDataOverlay
.dismiss();
78 // Form controls are disabled until the profile list has been loaded.
79 self
.setAllControlsEnabled_(false);
83 * Sets the enabled and checked state of the commit button.
86 validateCommitButton_: function() {
87 var somethingToImport
=
88 importable('history') || importable('favorites') ||
89 importable('passwords') || importable('search') ||
90 importable('autofill-form-data');
91 $('import-data-commit').disabled
= !somethingToImport
;
92 $('import-choose-file').disabled
= !$('import-favorites').checked
;
96 * Sets the enabled state of all the checkboxes and the commit button.
99 setAllControlsEnabled_: function(enabled
) {
101 document
.querySelectorAll('#import-checkboxes input[type=checkbox]');
102 for (var i
= 0; i
< checkboxes
.length
; i
++)
103 this.setUpCheckboxState_(checkboxes
[i
], enabled
);
104 $('import-data-commit').disabled
= !enabled
;
105 $('import-choose-file').hidden
= !enabled
;
106 <if expr
="is_macosx">
107 $('mac-password-keychain').hidden
= !enabled
;
112 * Sets the enabled state of a checkbox element.
113 * @param {Object} checkbox A checkbox element.
114 * @param {boolean} enabled The enabled state of the checkbox. If false,
115 * the checkbox is disabled. If true, the checkbox is enabled.
118 setUpCheckboxState_: function(checkbox
, enabled
) {
119 checkbox
.setDisabled('noProfileData', !enabled
);
123 * Update the enabled and visible states of all the checkboxes.
126 updateCheckboxes_: function() {
127 var index
= $('import-browsers').selectedIndex
;
128 var bookmarksFileSelected
= index
== this.browserProfiles
.length
- 1;
129 $('import-choose-file').hidden
= !bookmarksFileSelected
;
130 $('import-data-commit').hidden
= bookmarksFileSelected
;
133 if (this.browserProfiles
.length
> index
)
134 browserProfile
= this.browserProfiles
[index
];
135 var importOptions
= ['history',
139 'autofill-form-data'];
140 for (var i
= 0; i
< importOptions
.length
; i
++) {
141 var id
= 'import-' + importOptions
[i
];
142 var enable
= browserProfile
&& browserProfile
[importOptions
[i
]];
143 this.setUpCheckboxState_($(id
), enable
);
144 $(id
+ '-with-label').hidden
= !enable
;
149 * Show or hide gray message at the bottom.
152 updateBottomBar_: function() {
153 var index
= $('import-browsers').selectedIndex
;
155 if (this.browserProfiles
.length
> index
)
156 browserProfile
= this.browserProfiles
[index
];
157 var enable
= browserProfile
&& browserProfile
['show_bottom_bar'];
158 <if expr
="is_macosx">
159 $('mac-password-keychain').hidden
= !enable
;
164 * Update the supported browsers popup with given entries.
165 * @param {Array} browsers List of supported browsers name.
168 updateSupportedBrowsers_: function(browsers
) {
169 this.browserProfiles
= browsers
;
170 var browserSelect
= $('import-browsers');
171 browserSelect
.remove(0); // Remove the 'Loading...' option.
172 browserSelect
.textContent
= '';
173 var browserCount
= browsers
.length
;
175 if (browserCount
== 0) {
176 var option
= new Option(loadTimeData
.getString('noProfileFound'), 0);
177 browserSelect
.appendChild(option
);
179 this.setAllControlsEnabled_(false);
181 this.setAllControlsEnabled_(true);
182 for (var i
= 0; i
< browserCount
; i
++) {
183 var browser
= browsers
[i
];
184 var option
= new Option(browser
.name
, browser
.index
);
185 browserSelect
.appendChild(option
);
188 this.updateCheckboxes_();
189 this.validateCommitButton_();
190 this.updateBottomBar_();
195 * Clear import prefs set when user checks/unchecks a checkbox so that each
196 * checkbox goes back to the default "checked" state (or alternatively, to
197 * the state set by a recommended policy).
200 clearUserPrefs_: function() {
201 var importPrefs
= ['import_history',
203 'import_saved_passwords',
204 'import_search_engine',
205 'import_autofill_form_data'];
206 for (var i
= 0; i
< importPrefs
.length
; i
++)
207 Preferences
.clearPref(importPrefs
[i
], true);
211 * Update the dialog layout to reflect success state.
212 * @param {boolean} success If true, show success dialog elements.
215 updateSuccessState_: function(success
) {
216 var sections
= document
.querySelectorAll('.import-data-configure');
217 for (var i
= 0; i
< sections
.length
; i
++)
218 sections
[i
].hidden
= success
;
220 sections
= document
.querySelectorAll('.import-data-success');
221 for (var i
= 0; i
< sections
.length
; i
++)
222 sections
[i
].hidden
= !success
;
226 ImportDataOverlay
.clearUserPrefs = function() {
227 ImportDataOverlay
.getInstance().clearUserPrefs_();
231 * Update the supported browsers popup with given entries.
232 * @param {Array} browsers List of supported browsers name.
234 ImportDataOverlay
.updateSupportedBrowsers = function(browsers
) {
235 ImportDataOverlay
.getInstance().updateSupportedBrowsers_(browsers
);
239 * Update the UI to reflect whether an import operation is in progress.
240 * @param {boolean} importing True if an import operation is in progress.
242 ImportDataOverlay
.setImportingState = function(importing
) {
244 document
.querySelectorAll('#import-checkboxes input[type=checkbox]');
245 for (var i
= 0; i
< checkboxes
.length
; i
++)
246 checkboxes
[i
].setDisabled('Importing', importing
);
248 ImportDataOverlay
.getInstance().updateCheckboxes_();
249 $('import-browsers').disabled
= importing
;
250 $('import-throbber').style
.visibility
= importing
? 'visible' : 'hidden';
251 ImportDataOverlay
.getInstance().validateCommitButton_();
255 * Remove the import overlay from display.
257 ImportDataOverlay
.dismiss = function() {
258 ImportDataOverlay
.clearUserPrefs();
259 PageManager
.closeOverlay();
263 * Show a message confirming the success of the import operation.
265 ImportDataOverlay
.confirmSuccess = function() {
266 var showBookmarksMessage
= $('import-favorites').checked
;
267 ImportDataOverlay
.setImportingState(false);
268 $('import-find-your-bookmarks').hidden
= !showBookmarksMessage
;
269 ImportDataOverlay
.getInstance().updateSuccessState_(true);
273 * Show the import data overlay.
275 ImportDataOverlay
.show = function() {
276 // Make sure that any previous import success message is hidden, and
277 // we're showing the UI to import further data.
278 ImportDataOverlay
.getInstance().updateSuccessState_(false);
279 ImportDataOverlay
.getInstance().validateCommitButton_();
281 PageManager
.showPageByName('importData');
286 ImportDataOverlay
: ImportDataOverlay