OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 $('autoLaunch').addEventListener('click', | |
118 this.handleAutoLaunchChanged_); | |
119 } | |
120 | |
121 var startupPagesList = $('startupPagesList'); | |
122 options.browser_options.StartupPageList.decorate(startupPagesList); | |
123 startupPagesList.autoExpands = true; | |
124 | |
125 // Check if we are in the guest mode. | |
126 if (cr.commandLine && cr.commandLine.options['--bwsi']) { | |
127 // Hide the startup section. | |
128 $('startupSection').hidden = true; | |
129 } else { | |
130 // Initialize control enabled states. | |
131 Preferences.getInstance().addEventListener('session.restore_on_startup', | |
132 this.updateCustomStartupPageControlStates_.bind(this)); | |
133 Preferences.getInstance().addEventListener( | |
134 this.startup_pages_pref_.name, | |
135 this.handleStartupPageListChange_.bind(this)); | |
136 | |
137 this.updateCustomStartupPageControlStates_(); | |
138 } | |
139 | |
140 var suggestionList = new cr.ui.AutocompleteList(); | |
141 suggestionList.autoExpands = true; | |
142 suggestionList.suggestionUpdateRequestCallback = | |
143 this.requestAutocompleteSuggestions_.bind(this); | |
144 $('main-content').appendChild(suggestionList); | |
145 this.autocompleteList_ = suggestionList; | |
146 startupPagesList.autocompleteList = suggestionList; | |
147 }, | |
148 | |
149 /** | |
150 * Called when the value of the instant.confirm_dialog_shown preference | |
151 * changes. Cache this value. | |
152 * @param {Event} event Change event. | |
153 * @private | |
154 */ | |
155 onInstantConfirmDialogShownChanged_: function(event) { | |
156 this.instantConfirmDialogShown_ = event.value['value']; | |
157 }, | |
158 | |
159 /** | |
160 * Called when the value of the instant.enabled preference changes. Request | |
161 * the state of the Instant field trial experiment. | |
162 * @param {Event} event Change event. | |
163 * @private | |
164 */ | |
165 onInstantEnabledChanged_: function(event) { | |
166 chrome.send('getInstantFieldTrialStatus'); | |
167 }, | |
168 | |
169 /** | |
170 * Called to set the Instant field trial status. | |
171 * @param {boolean} enabled If true, the experiment is enabled. | |
172 * @private | |
173 */ | |
174 setInstantFieldTrialStatus_: function(enabled) { | |
175 $('instantEnabledCheckbox').hidden = enabled; | |
176 $('instantFieldTrialCheckbox').hidden = !enabled; | |
177 $('instantLabel').htmlFor = enabled ? 'instantFieldTrialCheckbox' | |
178 : 'instantEnabledCheckbox'; | |
179 }, | |
180 | |
181 /** | |
182 * Called when the value of the homepage-use-NTP pref changes. | |
183 * Updates the disabled state of the homepage text field. | |
184 * Notice that the text field can be disabled for other reasons too | |
185 * (it can be managed by policy, for instance). | |
186 * @param {Event} event Change event. | |
187 * @private | |
188 */ | |
189 onHomepageUseNTPChanged_: function(event) { | |
190 var homepageField = $('homepageURL'); | |
191 var homepageUseURLButton = $('homepageUseURLButton'); | |
192 homepageField.setDisabled('radioNotSelected', | |
193 !homepageUseURLButton.checked); | |
194 }, | |
195 | |
196 /** | |
197 * Update the Default Browsers section based on the current state. | |
198 * @param {string} statusString Description of the current default state. | |
199 * @param {boolean} isDefault Whether or not the browser is currently | |
200 * default. | |
201 * @param {boolean} canBeDefault Whether or not the browser can be default. | |
202 * @private | |
203 */ | |
204 updateDefaultBrowserState_: function(statusString, isDefault, | |
205 canBeDefault) { | |
206 var label = $('defaultBrowserState'); | |
207 label.textContent = statusString; | |
208 | |
209 $('defaultBrowserUseAsDefaultButton').disabled = !canBeDefault || | |
210 isDefault; | |
211 }, | |
212 | |
213 /** | |
214 * Clears the search engine popup. | |
215 * @private | |
216 */ | |
217 clearSearchEngines_: function() { | |
218 $('defaultSearchEngine').textContent = ''; | |
219 }, | |
220 | |
221 /** | |
222 * Updates the search engine popup with the given entries. | |
223 * @param {Array} engines List of available search engines. | |
224 * @param {number} defaultValue The value of the current default engine. | |
225 * @param {boolean} defaultManaged Whether the default search provider is | |
226 * managed. If true, the default search provider can't be changed. | |
227 */ | |
228 updateSearchEngines_: function(engines, defaultValue, defaultManaged) { | |
229 this.clearSearchEngines_(); | |
230 engineSelect = $('defaultSearchEngine'); | |
231 engineSelect.disabled = defaultManaged; | |
232 engineCount = engines.length; | |
233 var defaultIndex = -1; | |
234 for (var i = 0; i < engineCount; i++) { | |
235 var engine = engines[i]; | |
236 var option = new Option(engine['name'], engine['index']); | |
237 if (defaultValue == option.value) | |
238 defaultIndex = i; | |
239 engineSelect.appendChild(option); | |
240 } | |
241 if (defaultIndex >= 0) | |
242 engineSelect.selectedIndex = defaultIndex; | |
243 }, | |
244 | |
245 /** | |
246 * Returns true if the custom startup page control block should | |
247 * be enabled. | |
248 * @returns {boolean} Whether the startup page controls should be | |
249 * enabled. | |
250 */ | |
251 shouldEnableCustomStartupPageControls: function(pages) { | |
252 return $('startupShowPagesButton').checked && | |
253 !this.startup_pages_pref_.disabled; | |
254 }, | |
255 | |
256 /** | |
257 * Updates the startup pages list with the given entries. | |
258 * @param {Array} pages List of startup pages. | |
259 * @private | |
260 */ | |
261 updateStartupPages_: function(pages) { | |
262 var model = new ArrayDataModel(pages); | |
263 // Add a "new page" row. | |
264 model.push({ | |
265 'modelIndex': '-1' | |
266 }); | |
267 $('startupPagesList').dataModel = model; | |
268 }, | |
269 | |
270 /** | |
271 * Sets the enabled state of the custom startup page list controls | |
272 * based on the current startup radio button selection. | |
273 * @private | |
274 */ | |
275 updateCustomStartupPageControlStates_: function() { | |
276 var disable = !this.shouldEnableCustomStartupPageControls(); | |
277 var startupPagesList = $('startupPagesList'); | |
278 startupPagesList.disabled = disable; | |
279 startupPagesList.setAttribute('tabindex', disable ? -1 : 0); | |
280 // Explicitly set disabled state for input text elements. | |
281 var inputs = startupPagesList.querySelectorAll("input[type='text']"); | |
282 for (var i = 0; i < inputs.length; i++) | |
283 inputs[i].disabled = disable; | |
284 $('startupUseCurrentButton').disabled = disable; | |
285 }, | |
286 | |
287 /** | |
288 * Handles change events of the preference | |
289 * 'session.urls_to_restore_on_startup'. | |
290 * @param {event} preference changed event. | |
291 * @private | |
292 */ | |
293 handleStartupPageListChange_: function(event) { | |
294 this.startup_pages_pref_.disabled = event.value['disabled']; | |
295 this.updateCustomStartupPageControlStates_(); | |
296 }, | |
297 | |
298 /** | |
299 * Sets the default search engine based on the popup selection. | |
300 */ | |
301 setDefaultSearchEngine_: function() { | |
302 var engineSelect = $('defaultSearchEngine'); | |
303 var selectedIndex = engineSelect.selectedIndex; | |
304 if (selectedIndex >= 0) { | |
305 var selection = engineSelect.options[selectedIndex]; | |
306 chrome.send('setDefaultSearchEngine', [String(selection.value)]); | |
307 } | |
308 }, | |
309 | |
310 /** | |
311 * Sets or clear whether Chrome should Auto-launch on computer startup. | |
312 */ | |
313 handleAutoLaunchChanged_: function() { | |
314 chrome.send('toggleAutoLaunch', [Boolean($('autoLaunch').checked)]); | |
315 }, | |
316 | |
317 /** | |
318 * Sends an asynchronous request for new autocompletion suggestions for the | |
319 * the given query. When new suggestions are available, the C++ handler will | |
320 * call updateAutocompleteSuggestions_. | |
321 * @param {string} query List of autocomplete suggestions. | |
322 * @private | |
323 */ | |
324 requestAutocompleteSuggestions_: function(query) { | |
325 chrome.send('requestAutocompleteSuggestions', [query]); | |
326 }, | |
327 | |
328 /** | |
329 * Updates the autocomplete suggestion list with the given entries. | |
330 * @param {Array} pages List of autocomplete suggestions. | |
331 * @private | |
332 */ | |
333 updateAutocompleteSuggestions_: function(suggestions) { | |
334 var list = this.autocompleteList_; | |
335 // If the trigger for this update was a value being selected from the | |
336 // current list, do nothing. | |
337 if (list.targetInput && list.selectedItem && | |
338 list.selectedItem['url'] == list.targetInput.value) | |
339 return; | |
340 list.suggestions = suggestions; | |
341 }, | |
342 | |
343 /** | |
344 * Shows the autoLaunch preference and initializes its checkbox value. | |
345 */ | |
346 updateAutoLaunchState_: function(enabled) { | |
347 $('autoLaunchOption').hidden = false; | |
348 $('autoLaunch').checked = enabled; | |
349 }, | |
350 }; | |
351 | |
352 BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault, | |
353 canBeDefault) { | |
354 if (!cr.isChromeOS) { | |
355 BrowserOptions.getInstance().updateDefaultBrowserState_(statusString, | |
356 isDefault, | |
357 canBeDefault); | |
358 } | |
359 }; | |
360 | |
361 BrowserOptions.updateSearchEngines = function(engines, defaultValue, | |
362 defaultManaged) { | |
363 BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue, | |
364 defaultManaged); | |
365 }; | |
366 | |
367 BrowserOptions.updateStartupPages = function(pages) { | |
368 BrowserOptions.getInstance().updateStartupPages_(pages); | |
369 }; | |
370 | |
371 BrowserOptions.updateAutocompleteSuggestions = function(suggestions) { | |
372 BrowserOptions.getInstance().updateAutocompleteSuggestions_(suggestions); | |
373 }; | |
374 | |
375 BrowserOptions.updateAutoLaunchState = function(enabled) { | |
376 BrowserOptions.getInstance().updateAutoLaunchState_(enabled); | |
377 }; | |
378 | |
379 BrowserOptions.setInstantFieldTrialStatus = function(enabled) { | |
380 BrowserOptions.getInstance().setInstantFieldTrialStatus_(enabled); | |
381 }; | |
382 | |
383 // Export | |
384 return { | |
385 BrowserOptions: BrowserOptions | |
386 }; | |
387 | |
388 }); | |
OLD | NEW |