browser_options.js revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
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  const OptionsPage = options.OptionsPage;
7  const ArrayDataModel = cr.ui.ArrayDataModel;
8  const ListSelectionModel = cr.ui.ListSelectionModel;
9
10  //
11  // BrowserOptions class
12  // Encapsulated handling of browser options page.
13  //
14  function BrowserOptions() {
15    OptionsPage.call(this, 'browser', templateData.browserPage, 'browserPage');
16  }
17
18  cr.addSingletonGetter(BrowserOptions);
19
20  BrowserOptions.prototype = {
21    // Inherit BrowserOptions from OptionsPage.
22    __proto__: options.OptionsPage.prototype,
23
24    /**
25     * Initialize BrowserOptions page.
26     */
27    initializePage: function() {
28      // Call base class implementation to start preference initialization.
29      OptionsPage.prototype.initializePage.call(this);
30
31      // Wire up controls.
32      $('startupAddButton').onclick = function(event) {
33        OptionsPage.showOverlay('addStartupPageOverlay');
34      };
35      $('startupRemoveButton').onclick =
36          this.removeSelectedStartupPages_.bind(this);
37      $('startupUseCurrentButton').onclick = function(event) {
38        chrome.send('setStartupPagesToCurrentPages');
39      };
40      $('defaultSearchManageEnginesButton').onclick = function(event) {
41        OptionsPage.showPageByName('searchEngines');
42        chrome.send('coreOptionsUserMetricsAction',
43            ['Options_ManageSearchEngines']);
44      };
45      if (!cr.isChromeOS) {
46        $('defaultBrowserUseAsDefaultButton').onclick = function(event) {
47          chrome.send('becomeDefaultBrowser');
48        };
49      }
50
51      var list = $('startupPages');
52      options.browser_options.StartupPageList.decorate(list);
53      list.selectionModel = new ListSelectionModel;
54
55      list.selectionModel.addEventListener(
56          'change', this.updateRemoveButtonState_.bind(this));
57
58      this.addEventListener('visibleChange', function(event) {
59        $('startupPages').redraw();
60      });
61
62      // Initialize control enabled states.
63      Preferences.getInstance().addEventListener('session.restore_on_startup',
64          this.updateCustomStartupPageControlStates_.bind(this));
65      Preferences.getInstance().addEventListener('homepage_is_newtabpage',
66          this.updateHomepageFieldState_.bind(this));
67      this.updateCustomStartupPageControlStates_();
68      this.updateHomepageFieldState_();
69
70      // Remove Windows-style accelerators from button labels.
71      // TODO(stuartmorgan): Remove this once the strings are updated.
72      $('startupAddButton').textContent =
73          localStrings.getStringWithoutAccelerator('startupAddButton');
74      $('startupRemoveButton').textContent =
75          localStrings.getStringWithoutAccelerator('startupRemoveButton');
76    },
77
78    /**
79     * Update the Default Browsers section based on the current state.
80     * @private
81     * @param {string} statusString Description of the current default state.
82     * @param {boolean} isDefault Whether or not the browser is currently
83     *     default.
84     */
85    updateDefaultBrowserState_: function(statusString, isDefault) {
86      var label = $('defaultBrowserState');
87      label.textContent = statusString;
88      if (isDefault) {
89        label.classList.add('current');
90      } else {
91        label.classList.remove('current');
92      }
93
94      $('defaultBrowserUseAsDefaultButton').disabled = isDefault;
95    },
96
97    /**
98     * Clears the search engine popup.
99     * @private
100     */
101    clearSearchEngines_: function() {
102      $('defaultSearchEngine').textContent = '';
103    },
104
105    /**
106     * Updates the search engine popup with the given entries.
107     * @param {Array} engines List of available search engines.
108     * @param {Integer} defaultValue The value of the current default engine.
109     */
110    updateSearchEngines_: function(engines, defaultValue) {
111      this.clearSearchEngines_();
112      engineSelect = $('defaultSearchEngine');
113      engineCount = engines.length;
114      var defaultIndex = -1;
115      for (var i = 0; i < engineCount; i++) {
116        var engine = engines[i];
117        var option = new Option(engine['name'], engine['index']);
118        if (defaultValue == option.value)
119          defaultIndex = i;
120        engineSelect.appendChild(option);
121      }
122      if (defaultIndex >= 0)
123        engineSelect.selectedIndex = defaultIndex;
124    },
125
126    /**
127     * Returns true if the custom startup page control block should
128     * be enabled.
129     * @private
130     * @returns {boolean} Whether the startup page controls should be
131     *     enabled.
132     */
133    shouldEnableCustomStartupPageControls_: function(pages) {
134      return $('startupShowPagesButton').checked;
135    },
136
137    /**
138     * Updates the startup pages list with the given entries.
139     * @private
140     * @param {Array} pages List of startup pages.
141     */
142    updateStartupPages_: function(pages) {
143      $('startupPages').dataModel = new ArrayDataModel(pages);
144      this.updateRemoveButtonState_();
145    },
146
147    /**
148     * Sets the enabled state of the custom startup page list controls
149     * based on the current startup radio button selection.
150     * @private
151     */
152    updateCustomStartupPageControlStates_: function() {
153      var disable = !this.shouldEnableCustomStartupPageControls_();
154      $('startupAddButton').disabled = disable;
155      $('startupUseCurrentButton').disabled = disable;
156      this.updateRemoveButtonState_();
157    },
158
159    /**
160     * Sets the enabled state of the startup page Remove button based on
161     * the current selection in the startup pages list.
162     * @private
163     */
164    updateRemoveButtonState_: function() {
165      var groupEnabled = this.shouldEnableCustomStartupPageControls_();
166      $('startupRemoveButton').disabled = !groupEnabled ||
167          ($('startupPages').selectionModel.selectedIndex == -1);
168    },
169
170    /**
171     * Removes the selected startup pages.
172     * @private
173     */
174    removeSelectedStartupPages_: function() {
175      var selections =
176          $('startupPages').selectionModel.selectedIndexes.map(String);
177      chrome.send('removeStartupPages', selections);
178    },
179
180    /**
181     * Adds the given startup page at the current selection point.
182     * @private
183     */
184    addStartupPage_: function(url) {
185      var firstSelection = $('startupPages').selectionModel.selectedIndex;
186      chrome.send('addStartupPage', [url, String(firstSelection)]);
187    },
188
189    /**
190     * Sets the enabled state of the homepage field based on the current
191     * homepage radio button selection.
192     * @private
193     */
194    updateHomepageFieldState_: function() {
195      $('homepageURL').disabled = !$('homepageUseURLButton').checked;
196    },
197
198    /**
199     * Set the default search engine based on the popup selection.
200     */
201    setDefaultSearchEngine: function() {
202      var engineSelect = $('defaultSearchEngine');
203      var selectedIndex = engineSelect.selectedIndex;
204      if (selectedIndex >= 0) {
205        var selection = engineSelect.options[selectedIndex];
206        chrome.send('setDefaultSearchEngine', [String(selection.value)]);
207      }
208    },
209  };
210
211  BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault) {
212    if (!cr.isChromeOS) {
213      BrowserOptions.getInstance().updateDefaultBrowserState_(statusString,
214                                                              isDefault);
215    }
216  };
217
218  BrowserOptions.updateSearchEngines = function(engines, defaultValue) {
219    BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue);
220  };
221
222  BrowserOptions.updateStartupPages = function(pages) {
223    BrowserOptions.getInstance().updateStartupPages_(pages);
224  };
225
226  BrowserOptions.addStartupPage = function(url) {
227    BrowserOptions.getInstance().addStartupPage_(url);
228  };
229
230  // Export
231  return {
232    BrowserOptions: BrowserOptions
233  };
234
235});
236
237