browser_options.js revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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
5//
6// BrowserOptions class
7// Encapsulated handling of browser options page.
8//
9function BrowserOptions() {
10  OptionsPage.call(this, 'browser', templateData.browserPage, 'browserPage');
11}
12
13cr.addSingletonGetter(BrowserOptions);
14
15BrowserOptions.prototype = {
16  // Inherit BrowserOptions from OptionsPage.
17  __proto__: OptionsPage.prototype,
18
19  /**
20   * Initialize BrowserOptions page.
21   */
22  initializePage: function() {
23    // Call base class implementation to start preference initialization.
24    OptionsPage.prototype.initializePage.call(this);
25
26    // Wire up buttons.
27    $('startupAddButton').onclick = function(event) {
28      // TODO(stuartmorgan): Spawn add sub-dialog.
29    };
30    $('startupRemoveButton').onclick = function(event) {
31      // TODO(stuartmorgan): Remove selected element(s).
32    };
33    $('startupUseCurrentButton').onclick = function(event) {
34      // TODO(stuartmorgan): Add all open tabs (except this one).
35    };
36    $('defaultSearchManageEnginesButton').onclick = function(event) {
37      // TODO(stuartmorgan): Spawn search engine management sub-dialog.
38    };
39    $('defaultBrowserUseAsDefaultButton').onclick = function(event) {
40      chrome.send('becomeDefaultBrowser');
41    };
42
43    // Remove Windows-style accelerators from button labels.
44    // TODO(stuartmorgan): Remove this once the strings are updated.
45    $('startupAddButton').textContent =
46        localStrings.getStringWithoutAccelerator('startupAddButton');
47    $('startupRemoveButton').textContent =
48        localStrings.getStringWithoutAccelerator('startupRemoveButton');
49  },
50
51  /**
52   * Update the Default Browsers section based on the current state.
53   * @param {String} statusString Description of the current default state.
54   * @param {Boolean} isDefault Whether or not the browser is currently default.
55   */
56  updateDefaultBrowserState_: function(statusString, isDefault) {
57    var label = $('defaultBrowserState');
58    label.textContent = statusString;
59    if (isDefault) {
60      label.classList.add('current');
61    } else {
62      label.classList.remove('current');
63    }
64
65    $('defaultBrowserUseAsDefaultButton').disabled = isDefault;
66  },
67
68  /**
69   * Clears the search engine popup.
70   * @private
71   */
72  clearSearchEngines_: function() {
73    $('defaultSearchEngine').textContent = '';
74  },
75
76  /**
77   * Updates the search engine popup with the given entries.
78   * @param {Array} engines List of available search engines.
79   * @param {Integer} defaultValue The value of the current default engine.
80   */
81  updateSearchEngines_: function(engines, defaultValue) {
82    this.clearSearchEngines_();
83    engineSelect = $('defaultSearchEngine');
84    engineCount = engines.length
85    var defaultIndex = -1;
86    for (var i = 0; i < engineCount; i++) {
87      var engine = engines[i]
88      var option = new Option(engine['name'], engine['index']);
89      if (defaultValue == option.value)
90        defaultIndex = i;
91      engineSelect.appendChild(option);
92    }
93    if (defaultIndex >= 0)
94      engineSelect.selectedIndex = defaultIndex;
95  },
96
97  /**
98   * Set the default search engine based on the popup selection.
99   */
100  setDefaultBrowser: function() {
101    var engineSelect = $('defaultSearchEngine');
102    var selectedIndex = engineSelect.selectedIndex;
103    if (selectedIndex >= 0) {
104      var selection = engineSelect.options[selectedIndex];
105      chrome.send('setDefaultSearchEngine', [String(selection.value)]);
106    }
107  },
108};
109
110BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault) {
111  BrowserOptions.getInstance().updateDefaultBrowserState_(statusString,
112                                                          isDefault);
113}
114
115BrowserOptions.updateSearchEngines = function(engines, defaultValue) {
116  BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue);
117}
118