OLD | NEW |
(Empty) | |
| 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 const OptionsPage = options.OptionsPage; |
| 7 const ArrayDataModel = cr.ui.ArrayDataModel; |
| 8 |
| 9 // |
| 10 // BrowserOptions class |
| 11 // Encapsulated handling of browser options page. |
| 12 // |
| 13 function BrowserOptions() { |
| 14 OptionsPage.call(this, 'browser', |
| 15 templateData.browserPageTabTitle, |
| 16 'browserPage'); |
| 17 } |
| 18 |
| 19 cr.addSingletonGetter(BrowserOptions); |
| 20 |
| 21 BrowserOptions.prototype = { |
| 22 // Inherit BrowserOptions from OptionsPage. |
| 23 __proto__: options.OptionsPage.prototype, |
| 24 |
| 25 startup_pages_pref_: { |
| 26 'name': 'session.urls_to_restore_on_startup', |
| 27 'disabled': false |
| 28 }, |
| 29 |
| 30 /** |
| 31 * At autocomplete list that can be attached to a text field during editing. |
| 32 * @type {HTMLElement} |
| 33 * @private |
| 34 */ |
| 35 autocompleteList_: null, |
| 36 |
| 37 // The cached value of the instant.confirm_dialog_shown preference. |
| 38 instantConfirmDialogShown_: false, |
| 39 |
| 40 /** |
| 41 * Initialize BrowserOptions page. |
| 42 */ |
| 43 initializePage: function() { |
| 44 // Call base class implementation to start preference initialization. |
| 45 OptionsPage.prototype.initializePage.call(this); |
| 46 |
| 47 // Wire up controls. |
| 48 $('startupUseCurrentButton').onclick = function(event) { |
| 49 chrome.send('setStartupPagesToCurrentPages'); |
| 50 }; |
| 51 $('defaultSearchManageEnginesButton').onclick = function(event) { |
| 52 OptionsPage.navigateToPage('searchEngines'); |
| 53 chrome.send('coreOptionsUserMetricsAction', |
| 54 ['Options_ManageSearchEngines']); |
| 55 }; |
| 56 $('defaultSearchEngine').onchange = this.setDefaultSearchEngine_; |
| 57 |
| 58 var self = this; |
| 59 $('instantEnabledCheckbox').customChangeHandler = function(event) { |
| 60 if (this.checked) { |
| 61 if (self.instantConfirmDialogShown_) |
| 62 chrome.send('enableInstant'); |
| 63 else |
| 64 OptionsPage.navigateToPage('instantConfirm'); |
| 65 } else { |
| 66 chrome.send('disableInstant'); |
| 67 } |
| 68 return true; |
| 69 }; |
| 70 |
| 71 $('instantFieldTrialCheckbox').addEventListener('change', |
| 72 function(event) { |
| 73 this.checked = true; |
| 74 chrome.send('disableInstant'); |
| 75 }); |
| 76 |
| 77 Preferences.getInstance().addEventListener('instant.confirm_dialog_shown', |
| 78 this.onInstantConfirmDialogShownChanged_.bind(this)); |
| 79 |
| 80 Preferences.getInstance().addEventListener('instant.enabled', |
| 81 this.onInstantEnabledChanged_.bind(this)); |
| 82 |
| 83 Preferences.getInstance().addEventListener( |
| 84 $('homepageUseNTPButton').pref, |
| 85 this.onHomepageUseNTPChanged_); |
| 86 var homepageField = $('homepageURL'); |
| 87 homepageField.addEventListener('focus', function(event) { |
| 88 self.autocompleteList_.attachToInput(homepageField); |
| 89 }); |
| 90 homepageField.addEventListener('blur', function(event) { |
| 91 self.autocompleteList_.detach(); |
| 92 }); |
| 93 homepageField.addEventListener('keydown', function(event) { |
| 94 // Remove focus when the user hits enter since people expect feedback |
| 95 // indicating that they are done editing. |
| 96 if (event.keyIdentifier == 'Enter') |
| 97 homepageField.blur(); |
| 98 }); |
| 99 |
| 100 // Text fields may change widths when the window changes size, so make |
| 101 // sure the suggestion list stays in sync. |
| 102 window.addEventListener('resize', function() { |
| 103 self.autocompleteList_.syncWidthToInput(); |
| 104 }); |
| 105 |
| 106 // Ensure that changes are committed when closing the page. |
| 107 window.addEventListener('unload', function() { |
| 108 if (document.activeElement == homepageField) |
| 109 homepageField.blur(); |
| 110 }); |
| 111 |
| 112 if (!cr.isChromeOS) { |
| 113 $('defaultBrowserUseAsDefaultButton').onclick = function(event) { |
| 114 chrome.send('becomeDefaultBrowser'); |
| 115 }; |
| 116 } |
| 117 |
| 118 var startupPagesList = $('startupPagesList'); |
| 119 options.browser_options.StartupPageList.decorate(startupPagesList); |
| 120 startupPagesList.autoExpands = true; |
| 121 |
| 122 // Check if we are in the guest mode. |
| 123 if (cr.commandLine && cr.commandLine.options['--bwsi']) { |
| 124 // Hide the startup section. |
| 125 $('startupSection').hidden = true; |
| 126 } else { |
| 127 // Initialize control enabled states. |
| 128 Preferences.getInstance().addEventListener('session.restore_on_startup', |
| 129 this.updateCustomStartupPageControlStates_.bind(this)); |
| 130 Preferences.getInstance().addEventListener( |
| 131 this.startup_pages_pref_.name, |
| 132 this.handleStartupPageListChange_.bind(this)); |
| 133 |
| 134 this.updateCustomStartupPageControlStates_(); |
| 135 } |
| 136 |
| 137 var suggestionList = new options.AutocompleteList(); |
| 138 suggestionList.autoExpands = true; |
| 139 suggestionList.suggestionUpdateRequestCallback = |
| 140 this.requestAutocompleteSuggestions_.bind(this); |
| 141 $('main-content').appendChild(suggestionList); |
| 142 this.autocompleteList_ = suggestionList; |
| 143 startupPagesList.autocompleteList = suggestionList; |
| 144 }, |
| 145 |
| 146 /** |
| 147 * Called when the value of the instant.confirm_dialog_shown preference |
| 148 * changes. Cache this value. |
| 149 * @param {Event} event Change event. |
| 150 * @private |
| 151 */ |
| 152 onInstantConfirmDialogShownChanged_: function(event) { |
| 153 this.instantConfirmDialogShown_ = event.value['value']; |
| 154 }, |
| 155 |
| 156 /** |
| 157 * Called when the value of the instant.enabled preference changes. Request |
| 158 * the state of the Instant field trial experiment. |
| 159 * @param {Event} event Change event. |
| 160 * @private |
| 161 */ |
| 162 onInstantEnabledChanged_: function(event) { |
| 163 chrome.send('getInstantFieldTrialStatus'); |
| 164 }, |
| 165 |
| 166 /** |
| 167 * Called to set the Instant field trial status. |
| 168 * @param {boolean} enabled If true, the experiment is enabled. |
| 169 * @private |
| 170 */ |
| 171 setInstantFieldTrialStatus_: function(enabled) { |
| 172 $('instantEnabledCheckbox').hidden = enabled; |
| 173 $('instantFieldTrialCheckbox').hidden = !enabled; |
| 174 $('instantLabel').htmlFor = enabled ? 'instantFieldTrialCheckbox' |
| 175 : 'instantEnabledCheckbox'; |
| 176 }, |
| 177 |
| 178 /** |
| 179 * Called when the value of the homepage-use-NTP pref changes. |
| 180 * Updates the disabled state of the homepage text field. |
| 181 * Notice that the text field can be disabled for other reasons too |
| 182 * (it can be managed by policy, for instance). |
| 183 * @param {Event} event Change event. |
| 184 * @private |
| 185 */ |
| 186 onHomepageUseNTPChanged_: function(event) { |
| 187 var homepageField = $('homepageURL'); |
| 188 var homepageUseURLButton = $('homepageUseURLButton'); |
| 189 homepageField.setDisabled('radioNotSelected', |
| 190 !homepageUseURLButton.checked); |
| 191 }, |
| 192 |
| 193 /** |
| 194 * Update the Default Browsers section based on the current state. |
| 195 * @param {string} statusString Description of the current default state. |
| 196 * @param {boolean} isDefault Whether or not the browser is currently |
| 197 * default. |
| 198 * @param {boolean} canBeDefault Whether or not the browser can be default. |
| 199 * @private |
| 200 */ |
| 201 updateDefaultBrowserState_: function(statusString, isDefault, |
| 202 canBeDefault) { |
| 203 var label = $('defaultBrowserState'); |
| 204 label.textContent = statusString; |
| 205 |
| 206 $('defaultBrowserUseAsDefaultButton').disabled = !canBeDefault || |
| 207 isDefault; |
| 208 }, |
| 209 |
| 210 /** |
| 211 * Clears the search engine popup. |
| 212 * @private |
| 213 */ |
| 214 clearSearchEngines_: function() { |
| 215 $('defaultSearchEngine').textContent = ''; |
| 216 }, |
| 217 |
| 218 /** |
| 219 * Updates the search engine popup with the given entries. |
| 220 * @param {Array} engines List of available search engines. |
| 221 * @param {number} defaultValue The value of the current default engine. |
| 222 * @param {boolean} defaultManaged Whether the default search provider is |
| 223 * managed. If true, the default search provider can't be changed. |
| 224 */ |
| 225 updateSearchEngines_: function(engines, defaultValue, defaultManaged) { |
| 226 this.clearSearchEngines_(); |
| 227 engineSelect = $('defaultSearchEngine'); |
| 228 engineSelect.disabled = defaultManaged; |
| 229 engineCount = engines.length; |
| 230 var defaultIndex = -1; |
| 231 for (var i = 0; i < engineCount; i++) { |
| 232 var engine = engines[i]; |
| 233 var option = new Option(engine['name'], engine['index']); |
| 234 if (defaultValue == option.value) |
| 235 defaultIndex = i; |
| 236 engineSelect.appendChild(option); |
| 237 } |
| 238 if (defaultIndex >= 0) |
| 239 engineSelect.selectedIndex = defaultIndex; |
| 240 }, |
| 241 |
| 242 /** |
| 243 * Returns true if the custom startup page control block should |
| 244 * be enabled. |
| 245 * @returns {boolean} Whether the startup page controls should be |
| 246 * enabled. |
| 247 */ |
| 248 shouldEnableCustomStartupPageControls: function(pages) { |
| 249 return $('startupShowPagesButton').checked && |
| 250 !this.startup_pages_pref_.disabled; |
| 251 }, |
| 252 |
| 253 /** |
| 254 * Updates the startup pages list with the given entries. |
| 255 * @param {Array} pages List of startup pages. |
| 256 * @private |
| 257 */ |
| 258 updateStartupPages_: function(pages) { |
| 259 var model = new ArrayDataModel(pages); |
| 260 // Add a "new page" row. |
| 261 model.push({ |
| 262 'modelIndex': '-1' |
| 263 }); |
| 264 $('startupPagesList').dataModel = model; |
| 265 }, |
| 266 |
| 267 /** |
| 268 * Sets the enabled state of the custom startup page list controls |
| 269 * based on the current startup radio button selection. |
| 270 * @private |
| 271 */ |
| 272 updateCustomStartupPageControlStates_: function() { |
| 273 var disable = !this.shouldEnableCustomStartupPageControls(); |
| 274 var startupPagesList = $('startupPagesList'); |
| 275 startupPagesList.disabled = disable; |
| 276 startupPagesList.setAttribute('tabindex', disable ? -1 : 0); |
| 277 // Explicitly set disabled state for input text elements. |
| 278 var inputs = startupPagesList.querySelectorAll("input[type='text']"); |
| 279 for (var i = 0; i < inputs.length; i++) |
| 280 inputs[i].disabled = disable; |
| 281 $('startupUseCurrentButton').disabled = disable; |
| 282 }, |
| 283 |
| 284 /** |
| 285 * Handle change events of the preference |
| 286 * 'session.urls_to_restore_on_startup'. |
| 287 * @param {event} preference changed event. |
| 288 * @private |
| 289 */ |
| 290 handleStartupPageListChange_: function(event) { |
| 291 this.startup_pages_pref_.disabled = event.value['disabled']; |
| 292 this.updateCustomStartupPageControlStates_(); |
| 293 }, |
| 294 |
| 295 /** |
| 296 * Set the default search engine based on the popup selection. |
| 297 */ |
| 298 setDefaultSearchEngine_: function() { |
| 299 var engineSelect = $('defaultSearchEngine'); |
| 300 var selectedIndex = engineSelect.selectedIndex; |
| 301 if (selectedIndex >= 0) { |
| 302 var selection = engineSelect.options[selectedIndex]; |
| 303 chrome.send('setDefaultSearchEngine', [String(selection.value)]); |
| 304 } |
| 305 }, |
| 306 |
| 307 /** |
| 308 * Sends an asynchronous request for new autocompletion suggestions for the |
| 309 * the given query. When new suggestions are available, the C++ handler will |
| 310 * call updateAutocompleteSuggestions_. |
| 311 * @param {string} query List of autocomplete suggestions. |
| 312 * @private |
| 313 */ |
| 314 requestAutocompleteSuggestions_: function(query) { |
| 315 chrome.send('requestAutocompleteSuggestions', [query]); |
| 316 }, |
| 317 |
| 318 /** |
| 319 * Updates the autocomplete suggestion list with the given entries. |
| 320 * @param {Array} pages List of autocomplete suggestions. |
| 321 * @private |
| 322 */ |
| 323 updateAutocompleteSuggestions_: function(suggestions) { |
| 324 var list = this.autocompleteList_; |
| 325 // If the trigger for this update was a value being selected from the |
| 326 // current list, do nothing. |
| 327 if (list.targetInput && list.selectedItem && |
| 328 list.selectedItem['url'] == list.targetInput.value) |
| 329 return; |
| 330 list.suggestions = suggestions; |
| 331 }, |
| 332 }; |
| 333 |
| 334 BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault, |
| 335 canBeDefault) { |
| 336 if (!cr.isChromeOS) { |
| 337 BrowserOptions.getInstance().updateDefaultBrowserState_(statusString, |
| 338 isDefault, |
| 339 canBeDefault); |
| 340 } |
| 341 }; |
| 342 |
| 343 BrowserOptions.updateSearchEngines = function(engines, defaultValue, |
| 344 defaultManaged) { |
| 345 BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue, |
| 346 defaultManaged); |
| 347 }; |
| 348 |
| 349 BrowserOptions.updateStartupPages = function(pages) { |
| 350 BrowserOptions.getInstance().updateStartupPages_(pages); |
| 351 }; |
| 352 |
| 353 BrowserOptions.updateAutocompleteSuggestions = function(suggestions) { |
| 354 BrowserOptions.getInstance().updateAutocompleteSuggestions_(suggestions); |
| 355 }; |
| 356 |
| 357 BrowserOptions.setInstantFieldTrialStatus = function(enabled) { |
| 358 BrowserOptions.getInstance().setInstantFieldTrialStatus_(enabled); |
| 359 }; |
| 360 |
| 361 // Export |
| 362 return { |
| 363 BrowserOptions: BrowserOptions |
| 364 }; |
| 365 |
| 366 }); |
OLD | NEW |