personal_options.js revision 3f50c38dc070f4bb515c1b64450dae14f316474e
1// Copyright (c) 2010 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
7  var OptionsPage = options.OptionsPage;
8
9  // State variables.
10  var syncEnabled = false;
11  var syncSetupCompleted = false;
12
13  /**
14   * Encapsulated handling of personal options page.
15   * @constructor
16   */
17  function PersonalOptions() {
18    OptionsPage.call(this, 'personal', templateData.personalPage,
19                     'personal-page');
20  }
21
22  cr.addSingletonGetter(PersonalOptions);
23
24  PersonalOptions.prototype = {
25    // Inherit PersonalOptions from OptionsPage.
26    __proto__: options.OptionsPage.prototype,
27
28    // Initialize PersonalOptions page.
29    initializePage: function() {
30      // Call base class implementation to start preference initialization.
31      OptionsPage.prototype.initializePage.call(this);
32
33      var self = this;
34      $('sync-action-link').onclick = function(event) {
35        chrome.send('showSyncLoginDialog');
36      };
37      $('start-stop-sync').onclick = function(event) {
38        if (self.syncSetupCompleted)
39          self.showStopSyncingOverlay_();
40        else
41          chrome.send('showSyncLoginDialog');
42      };
43      $('privacy-dashboard-link').onclick = function(event) {
44        chrome.send('openPrivacyDashboardTabAndActivate');
45      };
46      $('manage-passwords').onclick = function(event) {
47        OptionsPage.showPageByName('passwordManager');
48        OptionsPage.showTab($('passwords-nav-tab'));
49        chrome.send('coreOptionsUserMetricsAction',
50            ['Options_ShowPasswordManager']);
51      };
52      $('autofill-settings').onclick = function(event) {
53        OptionsPage.showPageByName('autoFillOptions');
54        chrome.send('coreOptionsUserMetricsAction',
55            ['Options_ShowAutoFillSettings']);
56      };
57      $('themes-reset').onclick = function(event) {
58        chrome.send('themesReset');
59      };
60
61      // Initialize sync select control.
62      $('sync-select').onchange = function(event) {
63        self.updateSyncSelection_();
64      }
65
66      var syncCheckboxes = $('sync-table').getElementsByTagName('input');
67      for (var i = 0; i < syncCheckboxes.length; i++) {
68        if (syncCheckboxes[i].type == "checkbox") {
69          syncCheckboxes[i].onclick = function(event) {
70            chrome.send('updatePreferredDataTypes');
71          };
72        }
73      }
74
75      if (!cr.isChromeOS) {
76        $('import-data').onclick = function(event) {
77          OptionsPage.showOverlay('importDataOverlay');
78          chrome.send('coreOptionsUserMetricsAction', ['Import_ShowDlg']);
79        };
80
81        if (navigator.platform.match(/linux|BSD/i)) {
82          $('themes-GTK-button').onclick = function(event) {
83            chrome.send('themesSetGTK');
84          };
85        }
86      } else {
87        chrome.send('loadAccountPicture');
88      }
89
90      // Disable the screen lock checkbox for the guest mode.
91      if (cr.commandLine.options['--bwsi'])
92        $('enable-screen-lock').disabled = true;
93    },
94
95    /**
96     * Updates the sync datatype checkboxes based on the selected sync option.
97     * @private
98     */
99    updateSyncSelection_: function() {
100      var idx = $('sync-select').selectedIndex;
101      var syncCheckboxes = $('sync-table').getElementsByTagName('input');
102      if (idx == 0) {
103        for (var i = 0; i < syncCheckboxes.length; i++) {
104          syncCheckboxes[i].disabled = false;
105        }
106      } else if (idx == 1) {
107        for (var i = 0; i < syncCheckboxes.length; i++) {
108          // Merely setting checked = true is not enough to trigger the pref
109          // being set; thus, we simulate the click.
110          if (!syncCheckboxes[i].checked)
111            syncCheckboxes[i].click();
112
113          syncCheckboxes[i].disabled = true;
114        }
115      }
116    },
117
118    showStopSyncingOverlay_: function(event) {
119      AlertOverlay.show(localStrings.getString('stop_syncing_title'),
120                        localStrings.getString('stop_syncing_explanation'),
121                        localStrings.getString('stop_syncing_confirm'),
122                        localStrings.getString('cancel'),
123                        function() { chrome.send('stopSyncing'); });
124    },
125
126    setElementVisible_: function(element, visible) {
127      if (visible)
128        element.classList.remove('hidden');
129      else
130        element.classList.add('hidden');
131    },
132
133    setElementClassSyncError_: function(element, visible) {
134      visible ? element.classList.add('sync-error') :
135                element.classList.remove('sync-error');
136    },
137
138    setSyncEnabled_: function(enabled) {
139      this.syncEnabled = enabled;
140    },
141
142    setSyncSetupCompleted_: function(completed) {
143      this.syncSetupCompleted = completed;
144    },
145
146    setAccountPicture_: function(image) {
147      $('account-picture').src = image;
148    },
149
150    setSyncStatus_: function(status) {
151      $('sync-status').textContent = status;
152    },
153
154    setSyncStatusErrorVisible_: function(visible) {
155      this.setElementClassSyncError_($('sync-status'), visible);
156    },
157
158    setSyncActionLinkErrorVisible_: function(visible) {
159      this.setElementClassSyncError_($('sync-action-link'), visible);
160    },
161
162    setSyncActionLinkEnabled_: function(enabled) {
163      $('sync-action-link').disabled = !enabled;
164    },
165
166    setSyncActionLinkLabel_: function(status) {
167      $('sync-action-link').textContent = status;
168
169      // link-button does is not zero-area when the contents of the button are
170      // empty, so explicitly hide the element.
171      this.setElementVisible_($('sync-action-link'), status.length != 0);
172    },
173
174    setStartStopButtonVisible_: function(visible) {
175      this.setElementVisible_($('start-stop-sync'), visible);
176    },
177
178    setStartStopButtonEnabled_: function(enabled) {
179      $('start-stop-sync').disabled = !enabled;
180    },
181
182    setStartStopButtonLabel_: function(label) {
183      $('start-stop-sync').textContent = label;
184    },
185
186    setGtkThemeButtonEnabled_: function(enabled) {
187      if (!cr.isChromeOS && navigator.platform.match(/linux|BSD/i)) {
188        $('themes-GTK-button').disabled = !enabled;
189      }
190    },
191
192    setThemesResetButtonEnabled_: function(enabled) {
193      $('themes-reset').disabled = !enabled;
194    },
195
196    hideSyncSection_: function() {
197      this.setElementVisible_($('sync-section'), false);
198    },
199
200    /**
201     * Toggles the visibility of the data type checkboxes based on whether they
202     * are enabled on not.
203     * @param {Object} dict A mapping from data type to a boolean indicating
204     *     whether it is enabled.
205     * @private
206     */
207    setRegisteredDataTypes_: function(dict) {
208      for (var type in dict) {
209        if (type.match(/Registered$/) && !dict[type]) {
210          node = $(type.replace(/([a-z]+)Registered$/i, '$1').toLowerCase()
211                   + '-check');
212          if (node)
213            node.parentNode.style.display = 'none';
214        }
215      }
216    },
217  };
218
219  // Forward public APIs to private implementations.
220  [
221    'setSyncEnabled',
222    'setSyncSetupCompleted',
223    'setAccountPicture',
224    'setSyncStatus',
225    'setSyncStatusErrorVisible',
226    'setSyncActionLinkErrorVisible',
227    'setSyncActionLinkEnabled',
228    'setSyncActionLinkLabel',
229    'setStartStopButtonVisible',
230    'setStartStopButtonEnabled',
231    'setStartStopButtonLabel',
232    'setGtkThemeButtonEnabled',
233    'setThemesResetButtonEnabled',
234    'hideSyncSection',
235    'setRegisteredDataTypes',
236  ].forEach(function(name) {
237    PersonalOptions[name] = function(value) {
238      PersonalOptions.getInstance()[name + '_'](value);
239    };
240  });
241
242  // Export
243  return {
244    PersonalOptions: PersonalOptions
245  };
246
247});
248