import_data_overlay.js revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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
5cr.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            String($('import-autofill-form-data').checked)]);
54      };
55
56      $('import-data-cancel').onclick = function() {
57        ImportDataOverlay.dismiss();
58      };
59
60      $('import-choose-file').onclick = function() {
61        chrome.send('chooseBookmarksFile');
62      };
63
64      $('import-data-confirm').onclick = function() {
65        ImportDataOverlay.dismiss();
66      };
67
68      // Form controls are disabled until the profile list has been loaded.
69      self.setAllControlsEnabled_(false);
70    },
71
72    /**
73     * Sets the enabled and checked state of the commit button.
74     * @private
75     */
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;
83    },
84
85    /**
86     * Sets the enabled state of all the checkboxes and the commit button.
87     * @private
88     */
89    setAllControlsEnabled_: function(enabled) {
90      var checkboxes =
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;
96<if expr="is_macosx">
97      $('mac-password-keychain').hidden = !enabled;
98</if>
99    },
100
101    /**
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.
106     * @private
107     */
108    setUpCheckboxState_: function(checkbox, enabled) {
109      checkbox.setDisabled('noProfileData', !enabled);
110    },
111
112    /**
113     * Update the enabled and visible states of all the checkboxes.
114     * @private
115     */
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;
121
122      var browserProfile;
123      if (this.browserProfiles.length > index)
124        browserProfile = this.browserProfiles[index];
125      var importOptions = ['history',
126                           'favorites',
127                           'passwords',
128                           'search',
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';
136      }
137    },
138
139    /**
140     * Show or hide gray message at the bottom.
141     * @private
142     */
143    updateBottomBar_: function() {
144      var index = $('import-browsers').selectedIndex;
145      var browserProfile;
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;
151</if>
152    },
153
154    /**
155     * Update the supported browsers popup with given entries.
156     * @param {Array} browsers List of supported browsers name.
157     * @private
158     */
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;
165
166      if (browserCount == 0) {
167        var option = new Option(loadTimeData.getString('noProfileFound'), 0);
168        browserSelect.appendChild(option);
169
170        this.setAllControlsEnabled_(false);
171      } else {
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);
177        }
178
179        this.updateCheckboxes_();
180        this.validateCommitButton_();
181        this.updateBottomBar_();
182      }
183    },
184
185    /**
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).
189     * @private
190     */
191    clearUserPrefs_: function() {
192      var importPrefs = ['import_history',
193                         'import_bookmarks',
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);
199    },
200
201    /**
202     * Update the dialog layout to reflect success state.
203     * @param {boolean} success If true, show success dialog elements.
204     * @private
205     */
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;
210
211      sections = document.querySelectorAll('.import-data-success');
212      for (var i = 0; i < sections.length; i++)
213        sections[i].hidden = !success;
214    },
215  };
216
217  ImportDataOverlay.clearUserPrefs = function() {
218    ImportDataOverlay.getInstance().clearUserPrefs_();
219  };
220
221  /**
222   * Update the supported browsers popup with given entries.
223   * @param {Array} browsers List of supported browsers name.
224   */
225  ImportDataOverlay.updateSupportedBrowsers = function(browsers) {
226    ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers);
227  };
228
229  /**
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.
232   */
233  ImportDataOverlay.setImportingState = function(importing) {
234    var checkboxes =
235        document.querySelectorAll('#import-checkboxes input[type=checkbox]');
236    for (var i = 0; i < checkboxes.length; i++)
237        checkboxes[i].setDisabled('Importing', importing);
238    if (!importing)
239      ImportDataOverlay.getInstance().updateCheckboxes_();
240    $('import-browsers').disabled = importing;
241    $('import-throbber').style.visibility = importing ? 'visible' : 'hidden';
242    ImportDataOverlay.getInstance().validateCommitButton_();
243  };
244
245  /**
246   * Remove the import overlay from display.
247   */
248  ImportDataOverlay.dismiss = function() {
249    ImportDataOverlay.clearUserPrefs();
250    PageManager.closeOverlay();
251  };
252
253  /**
254   * Show a message confirming the success of the import operation.
255   */
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);
261  };
262
263  /**
264   * Show the import data overlay.
265   */
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_();
271
272    PageManager.showPageByName('importData');
273  };
274
275  // Export
276  return {
277    ImportDataOverlay: ImportDataOverlay
278  };
279});
280