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
),
53 String($('import-autofill-form-data').checked
)]);
56 $('import-data-cancel').onclick = function() {
57 ImportDataOverlay
.dismiss();
60 $('import-choose-file').onclick = function() {
61 chrome
.send('chooseBookmarksFile');
64 $('import-data-confirm').onclick = function() {
65 ImportDataOverlay
.dismiss();
68 // Form controls are disabled until the profile list has been loaded.
69 self
.setAllControlsEnabled_(false);
73 * Sets the enabled and checked state of the commit button.
76 validateCommitButton_: function() {
77 var somethingToImport
=
78 $('import-history').checked
|| $('import-favorites').checked
||
79 $('import-passwords').checked
|| $('import-search').checked
||
80 $('import-autofill-form-data').checked
;
81 $('import-data-commit').disabled
= !somethingToImport
;
82 $('import-choose-file').disabled
= !$('import-favorites').checked
;
86 * Sets the enabled state of all the checkboxes and the commit button.
89 setAllControlsEnabled_: function(enabled
) {
91 document
.querySelectorAll('#import-checkboxes input[type=checkbox]');
92 for (var i
= 0; i
< checkboxes
.length
; i
++)
93 this.setUpCheckboxState_(checkboxes
[i
], enabled
);
94 $('import-data-commit').disabled
= !enabled
;
95 $('import-choose-file').hidden
= !enabled
;
97 $('mac-password-keychain').hidden
= !enabled
;
102 * Sets the enabled state of a checkbox element.
103 * @param {Object} checkbox A checkbox element.
104 * @param {boolean} enabled The enabled state of the checkbox. If false,
105 * the checkbox is disabled. If true, the checkbox is enabled.
108 setUpCheckboxState_: function(checkbox
, enabled
) {
109 checkbox
.setDisabled('noProfileData', !enabled
);
113 * Update the enabled and visible states of all the checkboxes.
116 updateCheckboxes_: function() {
117 var index
= $('import-browsers').selectedIndex
;
118 var bookmarksFileSelected
= index
== this.browserProfiles
.length
- 1;
119 $('import-choose-file').hidden
= !bookmarksFileSelected
;
120 $('import-data-commit').hidden
= bookmarksFileSelected
;
123 if (this.browserProfiles
.length
> index
)
124 browserProfile
= this.browserProfiles
[index
];
125 var importOptions
= ['history',
129 'autofill-form-data'];
130 for (var i
= 0; i
< importOptions
.length
; i
++) {
131 var checkbox
= $('import-' + importOptions
[i
]);
132 var enable
= browserProfile
&& browserProfile
[importOptions
[i
]];
133 this.setUpCheckboxState_(checkbox
, enable
);
134 var checkboxWithLabel
= $('import-' + importOptions
[i
] + '-with-label');
135 checkboxWithLabel
.style
.display
= enable
? '' : 'none';
140 * Show or hide gray message at the bottom.
143 updateBottomBar_: function() {
144 var index
= $('import-browsers').selectedIndex
;
146 if (this.browserProfiles
.length
> index
)
147 browserProfile
= this.browserProfiles
[index
];
148 var enable
= browserProfile
&& browserProfile
['show_bottom_bar'];
149 <if expr
="is_macosx">
150 $('mac-password-keychain').hidden
= !enable
;
155 * Update the supported browsers popup with given entries.
156 * @param {Array} browsers List of supported browsers name.
159 updateSupportedBrowsers_: function(browsers
) {
160 this.browserProfiles
= browsers
;
161 var browserSelect
= $('import-browsers');
162 browserSelect
.remove(0); // Remove the 'Loading...' option.
163 browserSelect
.textContent
= '';
164 var browserCount
= browsers
.length
;
166 if (browserCount
== 0) {
167 var option
= new Option(loadTimeData
.getString('noProfileFound'), 0);
168 browserSelect
.appendChild(option
);
170 this.setAllControlsEnabled_(false);
172 this.setAllControlsEnabled_(true);
173 for (var i
= 0; i
< browserCount
; i
++) {
174 var browser
= browsers
[i
];
175 var option
= new Option(browser
.name
, browser
.index
);
176 browserSelect
.appendChild(option
);
179 this.updateCheckboxes_();
180 this.validateCommitButton_();
181 this.updateBottomBar_();
186 * Clear import prefs set when user checks/unchecks a checkbox so that each
187 * checkbox goes back to the default "checked" state (or alternatively, to
188 * the state set by a recommended policy).
191 clearUserPrefs_: function() {
192 var importPrefs
= ['import_history',
194 'import_saved_passwords',
195 'import_search_engine',
196 'import_autofill_form_data'];
197 for (var i
= 0; i
< importPrefs
.length
; i
++)
198 Preferences
.clearPref(importPrefs
[i
], true);
202 * Update the dialog layout to reflect success state.
203 * @param {boolean} success If true, show success dialog elements.
206 updateSuccessState_: function(success
) {
207 var sections
= document
.querySelectorAll('.import-data-configure');
208 for (var i
= 0; i
< sections
.length
; i
++)
209 sections
[i
].hidden
= success
;
211 sections
= document
.querySelectorAll('.import-data-success');
212 for (var i
= 0; i
< sections
.length
; i
++)
213 sections
[i
].hidden
= !success
;
217 ImportDataOverlay
.clearUserPrefs = function() {
218 ImportDataOverlay
.getInstance().clearUserPrefs_();
222 * Update the supported browsers popup with given entries.
223 * @param {Array} browsers List of supported browsers name.
225 ImportDataOverlay
.updateSupportedBrowsers = function(browsers
) {
226 ImportDataOverlay
.getInstance().updateSupportedBrowsers_(browsers
);
230 * Update the UI to reflect whether an import operation is in progress.
231 * @param {boolean} importing True if an import operation is in progress.
233 ImportDataOverlay
.setImportingState = function(importing
) {
235 document
.querySelectorAll('#import-checkboxes input[type=checkbox]');
236 for (var i
= 0; i
< checkboxes
.length
; i
++)
237 checkboxes
[i
].setDisabled('Importing', importing
);
239 ImportDataOverlay
.getInstance().updateCheckboxes_();
240 $('import-browsers').disabled
= importing
;
241 $('import-throbber').style
.visibility
= importing
? 'visible' : 'hidden';
242 ImportDataOverlay
.getInstance().validateCommitButton_();
246 * Remove the import overlay from display.
248 ImportDataOverlay
.dismiss = function() {
249 ImportDataOverlay
.clearUserPrefs();
250 PageManager
.closeOverlay();
254 * Show a message confirming the success of the import operation.
256 ImportDataOverlay
.confirmSuccess = function() {
257 var showBookmarksMessage
= $('import-favorites').checked
;
258 ImportDataOverlay
.setImportingState(false);
259 $('import-find-your-bookmarks').hidden
= !showBookmarksMessage
;
260 ImportDataOverlay
.getInstance().updateSuccessState_(true);
264 * Show the import data overlay.
266 ImportDataOverlay
.show = function() {
267 // Make sure that any previous import success message is hidden, and
268 // we're showing the UI to import further data.
269 ImportDataOverlay
.getInstance().updateSuccessState_(false);
270 ImportDataOverlay
.getInstance().validateCommitButton_();
272 PageManager
.showPageByName('importData');
277 ImportDataOverlay
: ImportDataOverlay