Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / options / import_data_overlay.js
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.
4
5 cr.define('options', function() {
6   var Page = cr.ui.pageManager.Page;
7   var PageManager = cr.ui.pageManager.PageManager;
8
9   /**
10    * ImportDataOverlay class
11    * Encapsulated handling of the 'Import Data' overlay page.
12    * @class
13    */
14   function ImportDataOverlay() {
15     Page.call(this,
16               'importData',
17               loadTimeData.getString('importDataOverlayTabTitle'),
18               'import-data-overlay');
19   }
20
21   cr.addSingletonGetter(ImportDataOverlay);
22
23   ImportDataOverlay.prototype = {
24     // Inherit from Page.
25     __proto__: Page.prototype,
26
27     /** @override */
28     initializePage: function() {
29       Page.prototype.initializePage.call(this);
30
31       var self = this;
32       var checkboxes =
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_();
37         };
38       }
39
40       $('import-browsers').onchange = function() {
41         self.updateCheckboxes_();
42         self.validateCommitButton_();
43         self.updateBottomBar_();
44       };
45
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       };
54
55       $('import-data-cancel').onclick = function() {
56         ImportDataOverlay.dismiss();
57       };
58
59       $('import-choose-file').onclick = function() {
60         chrome.send('chooseBookmarksFile');
61       };
62
63       $('import-data-confirm').onclick = function() {
64         ImportDataOverlay.dismiss();
65       };
66
67       // Form controls are disabled until the profile list has been loaded.
68       self.setAllControlsEnabled_(false);
69     },
70
71     /**
72      * Sets the enabled and checked state of the commit button.
73      * @private
74      */
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;
81     },
82
83     /**
84      * Sets the enabled state of all the checkboxes and the commit button.
85      * @private
86      */
87     setAllControlsEnabled_: function(enabled) {
88       var checkboxes =
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;
94 <if expr="is_macosx">
95       $('mac-password-keychain').hidden = !enabled;
96 </if>
97     },
98
99     /**
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.
104      * @private
105      */
106     setUpCheckboxState_: function(checkbox, enabled) {
107       checkbox.setDisabled('noProfileData', !enabled);
108     },
109
110     /**
111      * Update the enabled and visible states of all the checkboxes.
112      * @private
113      */
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;
119
120       var browserProfile;
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';
130       }
131     },
132
133     /**
134      * Show or hide gray message at the bottom.
135      * @private
136      */
137     updateBottomBar_: function() {
138       var index = $('import-browsers').selectedIndex;
139       var browserProfile;
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;
145 </if>
146     },
147
148     /**
149      * Update the supported browsers popup with given entries.
150      * @param {array} browsers List of supported browsers name.
151      * @private
152      */
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;
159
160       if (browserCount == 0) {
161         var option = new Option(loadTimeData.getString('noProfileFound'), 0);
162         browserSelect.appendChild(option);
163
164         this.setAllControlsEnabled_(false);
165       } else {
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);
171         }
172
173         this.updateCheckboxes_();
174         this.validateCommitButton_();
175         this.updateBottomBar_();
176       }
177     },
178
179     /**
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).
183      * @private
184      */
185     clearUserPrefs_: function() {
186       var importPrefs = ['import_history',
187                          'import_bookmarks',
188                          'import_saved_passwords',
189                          'import_search_engine'];
190       for (var i = 0; i < importPrefs.length; i++)
191         Preferences.clearPref(importPrefs[i], true);
192     },
193
194     /**
195      * Update the dialog layout to reflect success state.
196      * @param {boolean} success If true, show success dialog elements.
197      * @private
198      */
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;
203
204       sections = document.querySelectorAll('.import-data-success');
205       for (var i = 0; i < sections.length; i++)
206         sections[i].hidden = !success;
207     },
208   };
209
210   ImportDataOverlay.clearUserPrefs = function() {
211     ImportDataOverlay.getInstance().clearUserPrefs_();
212   };
213
214   /**
215    * Update the supported browsers popup with given entries.
216    * @param {array} list of supported browsers name.
217    */
218   ImportDataOverlay.updateSupportedBrowsers = function(browsers) {
219     ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers);
220   };
221
222   /**
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.
225    */
226   ImportDataOverlay.setImportingState = function(importing) {
227     var checkboxes =
228         document.querySelectorAll('#import-checkboxes input[type=checkbox]');
229     for (var i = 0; i < checkboxes.length; i++)
230         checkboxes[i].setDisabled('Importing', importing);
231     if (!importing)
232       ImportDataOverlay.getInstance().updateCheckboxes_();
233     $('import-browsers').disabled = importing;
234     $('import-throbber').style.visibility = importing ? 'visible' : 'hidden';
235     ImportDataOverlay.getInstance().validateCommitButton_();
236   };
237
238   /**
239    * Remove the import overlay from display.
240    */
241   ImportDataOverlay.dismiss = function() {
242     ImportDataOverlay.clearUserPrefs();
243     PageManager.closeOverlay();
244   };
245
246   /**
247    * Show a message confirming the success of the import operation.
248    */
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);
254   };
255
256   /**
257    * Show the import data overlay.
258    */
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_();
264
265     PageManager.showPageByName('importData');
266   };
267
268   // Export
269   return {
270     ImportDataOverlay: ImportDataOverlay
271   };
272 });