summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/options/import_data_overlay.js
blob: 455636bbb46101b94e91948e652ac33c1a3d1b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('options', function() {

  var OptionsPage = options.OptionsPage;

  /**
   * ImportDataOverlay class
   * Encapsulated handling of the 'Import Data' overlay page.
   * @class
   */
  function ImportDataOverlay() {
    OptionsPage.call(this, 'importDataOverlay',
                     templateData.import_data_title,
                     'importDataOverlay');
  }

  ImportDataOverlay.throbIntervalId = 0
  ImportDataOverlay.checkboxMask = '';

  cr.addSingletonGetter(ImportDataOverlay);

  ImportDataOverlay.prototype = {
    // Inherit ImportDataOverlay from OptionsPage.
    __proto__: OptionsPage.prototype,

    /**
     * Initialize the page.
     */
    initializePage: function() {
      // Call base class implementation to starts preference initialization.
      OptionsPage.prototype.initializePage.call(this);

      var self = this;
      var checkboxList =
          document.querySelectorAll('#checkboxList input[type=checkbox]');
      for (var i = 0; i < checkboxList.length; i++) {
        if(checkboxList[i].type == 'checkbox')
          checkboxList[i].onchange = function(e) {
            self.countCheckboxes_();
          };
      }

      $('import-data-commit').onclick = function(e) {
        /** The first digit in paramList indicates browser selected
         *  The rest indicate the checkboxes (1 is checked, 0 is not)
         */
        var selectedBrowser = $('supported-browsers').selectedIndex;
        var paramList =
            String(selectedBrowser) + ImportDataOverlay.checkboxMask;

        chrome.send('importData', [paramList]);
      }
    },

    countCheckboxes_: function() {
      ImportDataOverlay.checkboxMask = "";
      var checkboxList =
          document.querySelectorAll('#checkboxList input[type=checkbox]');
      for (var i = 0; i < checkboxList.length; i++) {
        if (checkboxList[i].type == 'checkbox') {
          if(checkboxList[i].checked)
            ImportDataOverlay.checkboxMask += "1";
          else
            ImportDataOverlay.checkboxMask += "0";
        }
      }
      if (ImportDataOverlay.checkboxMask.indexOf("1") == -1)
        $('import-data-commit').disabled = true;
      else
        $('import-data-commit').disabled = false;
    },

    /**
     * Clear the supported browsers popup
     * @private
     */
    clearSupportedBrowsers_: function() {
      $('supported-browsers').textContent = '';
    },

    /**
     * Update the supported browsers popup with given entries.
     * @param {Array} list of supported browsers name.
     */
    updateSupportedBrowsers_: function(browsers) {
      this.clearSupportedBrowsers_();
      browserSelect = $('supported-browsers');
      browserCount = browsers.length;

      if(browserCount == 0) {
        var option = new Option(templateData.no_profile_found, 0);
        browserSelect.appendChild(option);

        ImportDataOverlay.setDisable(true);
      }
      else {
        for (var i = 0; i < browserCount; i++) {
          var browser = browsers[i]
          var option = new Option(browser['name'], browser['index']);
          browserSelect.appendChild(option);

        ImportDataOverlay.setDisable(false);
        this.countCheckboxes_();
        }
      }
    },
  };

  ImportDataOverlay.loadImporter = function() {
    chrome.send('loadImporter');
  };

  ImportDataOverlay.updateSupportedBrowsers = function(browsers) {
    ImportDataOverlay.getInstance().updateSupportedBrowsers_(browsers);
  };

  ImportDataOverlay.setDisable = function(state) {
    $('supported-browsers').disabled = state;
    $('import-favorites').disabled = state;
    $('import-search').disabled = state;
    $('import-passwords').disabled = state;
    $('import-history').disabled = state;
    $('import-data-commit').disabled = state;
  };

  ImportDataOverlay.setImportingState = function(state) {
    ImportDataOverlay.setDisable(state);
    $('import-throbber').style.visibility = state ? "visible" : "hidden";

    function advanceThrobber() {
      var throbber = $('import-throbber');
      throbber.style.backgroundPositionX =
          ((parseInt(getComputedStyle(throbber).backgroundPositionX, 10) - 16)
          % 576) + 'px';
    }
    if (state) {
      ImportDataOverlay.throbIntervalId =
          setInterval(advanceThrobber, 30);
    } else {
      clearInterval(ImportDataOverlay.throbIntervalId);
    }
  };

  ImportDataOverlay.dismiss = function() {
    ImportDataOverlay.setImportingState(false);
    OptionsPage.clearOverlays();
  }

  // Export
  return {
    ImportDataOverlay: ImportDataOverlay
  };

});