Home | History | Annotate | Download | only in options
      1 // Copyright (c) 2011 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 OptionsPage = options.OptionsPage;
      7 
      8   /**
      9    * ImportDataOverlay class
     10    * Encapsulated handling of the 'Import Data' overlay page.
     11    * @class
     12    */
     13   function ImportDataOverlay() {
     14     OptionsPage.call(this,
     15                      'importData',
     16                      templateData.importDataOverlayTabTitle,
     17                      'import-data-overlay');
     18   }
     19 
     20   cr.addSingletonGetter(ImportDataOverlay);
     21 
     22   ImportDataOverlay.prototype = {
     23     // Inherit from OptionsPage.
     24     __proto__: OptionsPage.prototype,
     25 
     26     /**
     27      * Initialize the page.
     28      */
     29     initializePage: function() {
     30       // Call base class implementation to start preference initialization.
     31       OptionsPage.prototype.initializePage.call(this);
     32 
     33       var self = this;
     34       var checkboxes =
     35           document.querySelectorAll('#import-checkboxes input[type=checkbox]');
     36       for (var i = 0; i < checkboxes.length; i++) {
     37         checkboxes[i].onchange = function() {
     38           self.validateCommitButton_();
     39         };
     40       }
     41 
     42       $('import-browsers').onchange = function() {
     43         self.updateCheckboxes_();
     44         self.validateCommitButton_();
     45       };
     46 
     47       $('import-data-commit').onclick = function() {
     48         chrome.send('importData', [
     49             String($('import-browsers').selectedIndex),
     50             String($('import-history').checked),
     51             String($('import-favorites').checked),
     52             String($('import-passwords').checked),
     53             String($('import-search').checked)]);
     54       };
     55 
     56       $('import-data-cancel').onclick = function() {
     57         ImportDataOverlay.dismiss();
     58       };
     59 
     60       // Form controls are disabled until the profile list has been loaded.
     61       self.setControlsSensitive_(false);
     62     },
     63 
     64     /**
     65      * Set enabled and checked state of the commit button.
     66      * @private
     67      */
     68     validateCommitButton_: function() {
     69       var somethingToImport =
     70           $('import-history').checked || $('import-favorites').checked ||
     71           $('import-passwords').checked || $('import-search').checked;
     72       $('import-data-commit').disabled = !somethingToImport;
     73     },
     74 
     75     /**
     76      * Sets the sensitivity of all the checkboxes and the commit button.
     77      * @private
     78      */
     79     setControlsSensitive_: function(sensitive) {
     80       var checkboxes =
     81           document.querySelectorAll('#import-checkboxes input[type=checkbox]');
     82       for (var i = 0; i < checkboxes.length; i++)
     83         this.setUpCheckboxState_(checkboxes[i], sensitive);
     84       $('import-data-commit').disabled = !sensitive;
     85     },
     86 
     87     /**
     88      * Set enabled and checked states a checkbox element.
     89      * @param {Object} checkbox A checkbox element.
     90      * @param {boolean} enabled The enabled state of the chekbox.
     91      * @private
     92      */
     93     setUpCheckboxState_: function(checkbox, enabled) {
     94       checkbox.disabled = !enabled;
     95       checkbox.checked = enabled;
     96     },
     97 
     98     /**
     99      * Update the enabled and checked states of all checkboxes.
    100      * @private
    101      */
    102     updateCheckboxes_: function() {
    103       var index = $('import-browsers').selectedIndex;
    104       var browserProfile;
    105       if (this.browserProfiles.length > index)
    106         browserProfile = this.browserProfiles[index];
    107       var importOptions = ['history', 'favorites', 'passwords', 'search'];
    108       for (var i = 0; i < importOptions.length; i++) {
    109         var checkbox = $('import-' + importOptions[i]);
    110         this.setUpCheckboxState_(checkbox,
    111             browserProfile ? browserProfile[importOptions[i]] : false);
    112       }
    113     },
    114 
    115     /**
    116      * Update the supported browsers popup with given entries.
    117      * @param {array} browsers List of supported browsers name.
    118      * @private
    119      */
    120     updateSupportedBrowsers_: function(browsers) {
    121       this.browserProfiles = browsers;
    122       var browserSelect = $('import-browsers');
    123       browserSelect.remove(0);  // Remove the 'Loading...' option.
    124       browserSelect.textContent = '';
    125       var browserCount = browsers.length;
    126 
    127       if (browserCount == 0) {
    128         var option = new Option(templateData.noProfileFound, 0);
    129         browserSelect.appendChild(option);
    130 
    131         this.setControlsSensitive_(false);
    132       } else {
    133         this.setControlsSensitive_(true);
    134         for (var i = 0; i < browserCount; i++) {
    135           var browser = browsers[i]
    136           var option = new Option(browser['name'], browser['index']);
    137           browserSelect.appendChild(option);
    138         }
    139 
    140         this.updateCheckboxes_();
    141         this.validateCommitButton_();
    142       }
    143     },
    144   };
    145 
    146   /**
    147    * Update the supported browsers popup with given entries.
    148    * @param {array} list of supported browsers name.
    149    */
    150   ImportDataOverlay.updateSupportedBrowsers = function(browsers) {
    151     ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers);
    152   };
    153 
    154   /**
    155    * Update the UI to reflect whether an import operation is in progress.
    156    * @param {boolean} state True if an import operation is in progress.
    157    */
    158   ImportDataOverlay.setImportingState = function(state) {
    159     if (state) {
    160       var checkboxes =
    161           document.querySelectorAll('#import-checkboxes input[type=checkbox]');
    162       for (var i = 0; i < checkboxes.length; i++) {
    163         checkboxes[i].disabled = true;
    164       }
    165     } else {
    166       ImportDataOverlay.getInstance().updateCheckboxes_();
    167     }
    168     $('import-browsers').disabled = state;
    169     $('import-throbber').style.visibility = state ? "visible" : "hidden";
    170     ImportDataOverlay.getInstance().validateCommitButton_();
    171   };
    172 
    173   /**
    174    * Remove the import overlay from display.
    175    */
    176   ImportDataOverlay.dismiss = function() {
    177     ImportDataOverlay.setImportingState(false);
    178     OptionsPage.closeOverlay();
    179   };
    180 
    181   // Export
    182   return {
    183     ImportDataOverlay: ImportDataOverlay
    184   };
    185 });
    186