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 'managed': false | |
28 }, | |
29 | |
30 homepage_pref_: { | |
31 'name': 'homepage', | |
32 'value': '', | |
33 'managed': false | |
34 }, | |
35 | |
36 homepage_is_newtabpage_pref_: { | |
37 'name': 'homepage_is_newtabpage', | |
38 'value': true, | |
39 'managed': false | |
40 }, | |
41 | |
42 /** | |
43 * At autocomplete list that can be attached to a text field during editing. | |
44 * @type {HTMLElement} | |
45 * @private | |
46 */ | |
47 autocompleteList_: null, | |
48 | |
49 // The cached value of the instant.confirm_dialog_shown preference. | |
50 instantConfirmDialogShown_: false, | |
51 | |
52 /** | |
53 * Initialize BrowserOptions page. | |
54 */ | |
55 initializePage: function() { | |
56 // Call base class implementation to start preference initialization. | |
57 OptionsPage.prototype.initializePage.call(this); | |
58 | |
59 // Wire up controls. | |
60 $('startupUseCurrentButton').onclick = function(event) { | |
61 chrome.send('setStartupPagesToCurrentPages'); | |
62 }; | |
63 $('toolbarShowBookmarksBar').onchange = function() { | |
64 chrome.send('toggleShowBookmarksBar'); | |
65 }; | |
66 $('defaultSearchManageEnginesButton').onclick = function(event) { | |
67 OptionsPage.navigateToPage('searchEngines'); | |
68 chrome.send('coreOptionsUserMetricsAction', | |
69 ['Options_ManageSearchEngines']); | |
70 }; | |
71 $('defaultSearchEngine').onchange = this.setDefaultSearchEngine_; | |
72 | |
73 var self = this; | |
74 $('instantEnableCheckbox').onclick = function(event) { | |
75 if (this.checked && !self.instantConfirmDialogShown_) { | |
76 // Leave disabled for now. The PrefCheckbox handler already set it to | |
77 // true so undo that. | |
78 Preferences.setBooleanPref(this.pref, false, this.metric); | |
79 OptionsPage.navigateToPage('instantConfirm'); | |
80 } | |
81 }; | |
82 | |
83 Preferences.getInstance().addEventListener('instant.confirm_dialog_shown', | |
84 this.onInstantConfirmDialogShownChanged_.bind(this)); | |
85 | |
86 var homepageField = $('homepageURL'); | |
87 $('homepageUseNTPButton').onchange = | |
88 this.handleHomepageUseNTPButtonChange_.bind(this); | |
89 $('homepageUseURLButton').onchange = | |
90 this.handleHomepageUseURLButtonChange_.bind(this); | |
91 var homepageChangeHandler = this.handleHomepageURLChange_.bind(this); | |
92 homepageField.addEventListener('change', homepageChangeHandler); | |
93 homepageField.addEventListener('input', homepageChangeHandler); | |
94 homepageField.addEventListener('focus', function(event) { | |
95 self.autocompleteList_.attachToInput(homepageField); | |
96 }); | |
97 homepageField.addEventListener('blur', function(event) { | |
98 self.autocompleteList_.detach(); | |
99 }); | |
100 homepageField.addEventListener('keydown', function(event) { | |
101 // Remove focus when the user hits enter since people expect feedback | |
102 // indicating that they are done editing. | |
103 if (event.keyIdentifier == 'Enter') | |
104 homepageField.blur(); | |
105 }); | |
106 // Text fields may change widths when the window changes size, so make | |
107 // sure the suggestion list stays in sync. | |
108 window.addEventListener('resize', function() { | |
109 self.autocompleteList_.syncWidthToInput(); | |
110 }); | |
111 | |
112 // Ensure that changes are committed when closing the page. | |
113 window.addEventListener('unload', function() { | |
114 if (document.activeElement == homepageField) | |
115 homepageField.blur(); | |
116 }); | |
117 | |
118 if (!cr.isChromeOS) { | |
119 $('defaultBrowserUseAsDefaultButton').onclick = function(event) { | |
120 chrome.send('becomeDefaultBrowser'); | |
121 }; | |
122 } | |
123 | |
124 var startupPagesList = $('startupPagesList'); | |
125 options.browser_options.StartupPageList.decorate(startupPagesList); | |
126 startupPagesList.autoExpands = true; | |
127 | |
128 // Check if we are in the guest mode. | |
129 if (cr.commandLine.options['--bwsi']) { | |
130 // Hide the startup section. | |
131 $('startupSection').classList.add('hidden'); | |
132 } else { | |
133 // Initialize control enabled states. | |
134 Preferences.getInstance().addEventListener('session.restore_on_startup', | |
135 this.updateCustomStartupPageControlStates_.bind(this)); | |
136 Preferences.getInstance().addEventListener( | |
137 this.startup_pages_pref_.name, | |
138 this.handleStartupPageListChange_.bind(this)); | |
139 Preferences.getInstance().addEventListener( | |
140 this.homepage_pref_.name, | |
141 this.handleHomepageChange_.bind(this)); | |
142 Preferences.getInstance().addEventListener( | |
143 this.homepage_is_newtabpage_pref_.name, | |
144 this.handleHomepageIsNewTabPageChange_.bind(this)); | |
145 | |
146 this.updateCustomStartupPageControlStates_(); | |
147 } | |
148 | |
149 var suggestionList = new options.AutocompleteList(); | |
150 suggestionList.autoExpands = true; | |
151 suggestionList.suggestionUpdateRequestCallback = | |
152 this.requestAutocompleteSuggestions_.bind(this); | |
153 $('main-content').appendChild(suggestionList); | |
154 this.autocompleteList_ = suggestionList; | |
155 startupPagesList.autocompleteList = suggestionList; | |
156 }, | |
157 | |
158 /** | |
159 * Called when the value of the instant.confirm_dialog_shown preference | |
160 * changes. Cache this value. | |
161 * @param {Event} event Change event. | |
162 * @private | |
163 */ | |
164 onInstantConfirmDialogShownChanged_: function(event) { | |
165 this.instantConfirmDialogShown_ = event.value['value']; | |
166 }, | |
167 | |
168 /** | |
169 * Update the Default Browsers section based on the current state. | |
170 * @param {string} statusString Description of the current default state. | |
171 * @param {boolean} isDefault Whether or not the browser is currently | |
172 * default. | |
173 * @param {boolean} canBeDefault Whether or not the browser can be default. | |
174 * @private | |
175 */ | |
176 updateDefaultBrowserState_: function(statusString, isDefault, | |
177 canBeDefault) { | |
178 var label = $('defaultBrowserState'); | |
179 label.textContent = statusString; | |
180 | |
181 $('defaultBrowserUseAsDefaultButton').disabled = !canBeDefault || | |
182 isDefault; | |
183 }, | |
184 | |
185 /** | |
186 * Clears the search engine popup. | |
187 * @private | |
188 */ | |
189 clearSearchEngines_: function() { | |
190 $('defaultSearchEngine').textContent = ''; | |
191 }, | |
192 | |
193 /** | |
194 * Updates the search engine popup with the given entries. | |
195 * @param {Array} engines List of available search engines. | |
196 * @param {number} defaultValue The value of the current default engine. | |
197 */ | |
198 updateSearchEngines_: function(engines, defaultValue) { | |
199 this.clearSearchEngines_(); | |
200 engineSelect = $('defaultSearchEngine'); | |
201 engineCount = engines.length; | |
202 var defaultIndex = -1; | |
203 for (var i = 0; i < engineCount; i++) { | |
204 var engine = engines[i]; | |
205 var option = new Option(engine['name'], engine['index']); | |
206 if (defaultValue == option.value) | |
207 defaultIndex = i; | |
208 engineSelect.appendChild(option); | |
209 } | |
210 if (defaultIndex >= 0) | |
211 engineSelect.selectedIndex = defaultIndex; | |
212 }, | |
213 | |
214 /** | |
215 * Returns true if the custom startup page control block should | |
216 * be enabled. | |
217 * @returns {boolean} Whether the startup page controls should be | |
218 * enabled. | |
219 */ | |
220 shouldEnableCustomStartupPageControls: function(pages) { | |
221 return $('startupShowPagesButton').checked && | |
222 !this.startup_pages_pref_.managed; | |
223 }, | |
224 | |
225 /** | |
226 * Updates the startup pages list with the given entries. | |
227 * @param {Array} pages List of startup pages. | |
228 * @private | |
229 */ | |
230 updateStartupPages_: function(pages) { | |
231 var model = new ArrayDataModel(pages); | |
232 // Add a "new page" row. | |
233 model.push({ | |
234 'modelIndex': '-1' | |
235 }); | |
236 $('startupPagesList').dataModel = model; | |
237 }, | |
238 | |
239 /** | |
240 * Handles change events of the radio button 'homepageUseURLButton'. | |
241 * @param {event} change event. | |
242 * @private | |
243 */ | |
244 handleHomepageUseURLButtonChange_: function(event) { | |
245 Preferences.setBooleanPref(this.homepage_is_newtabpage_pref_.name, false); | |
246 }, | |
247 | |
248 /** | |
249 * Handles change events of the radio button 'homepageUseNTPButton'. | |
250 * @param {event} change event. | |
251 * @private | |
252 */ | |
253 handleHomepageUseNTPButtonChange_: function(event) { | |
254 Preferences.setBooleanPref(this.homepage_is_newtabpage_pref_.name, true); | |
255 }, | |
256 | |
257 /** | |
258 * Handles input and change events of the text field 'homepageURL'. | |
259 * @param {event} input/change event. | |
260 * @private | |
261 */ | |
262 handleHomepageURLChange_: function(event) { | |
263 var homepageField = $('homepageURL'); | |
264 var doFixup = event.type == 'change' ? '1' : '0'; | |
265 chrome.send('setHomePage', [homepageField.value, doFixup]); | |
266 }, | |
267 | |
268 /** | |
269 * Handle change events of the preference 'homepage'. | |
270 * @param {event} preference changed event. | |
271 * @private | |
272 */ | |
273 handleHomepageChange_: function(event) { | |
274 this.homepage_pref_.value = event.value['value']; | |
275 this.homepage_pref_.managed = event.value['managed']; | |
276 if (this.isHomepageURLNewTabPageURL_() && !this.homepage_pref_.managed && | |
277 !this.homepage_is_newtabpage_pref_.managed) { | |
278 var useNewTabPage = this.isHomepageIsNewTabPageChoiceSelected_(); | |
279 Preferences.setStringPref(this.homepage_pref_.name, '') | |
280 Preferences.setBooleanPref(this.homepage_is_newtabpage_pref_.name, | |
281 useNewTabPage) | |
282 } | |
283 this.updateHomepageControlStates_(); | |
284 }, | |
285 | |
286 /** | |
287 * Handle change events of the preference homepage_is_newtabpage. | |
288 * @param {event} preference changed event. | |
289 * @private | |
290 */ | |
291 handleHomepageIsNewTabPageChange_: function(event) { | |
292 this.homepage_is_newtabpage_pref_.value = event.value['value']; | |
293 this.homepage_is_newtabpage_pref_.managed = event.value['managed']; | |
294 this.updateHomepageControlStates_(); | |
295 }, | |
296 | |
297 /** | |
298 * Update homepage preference UI controls. Here's a table describing the | |
299 * desired characteristics of the homepage choice radio value, its enabled | |
300 * state and the URL field enabled state. They depend on the values of the | |
301 * managed bits for homepage (m_hp) and homepageIsNewTabPage (m_ntp) | |
302 * preferences, as well as the value of the homepageIsNewTabPage preference | |
303 * (ntp) and whether the homepage preference is equal to the new tab page | |
304 * URL (hpisntp). | |
305 * | |
306 * m_hp m_ntp ntp hpisntp| choice value| choice enabled| URL field enabled | |
307 * ------------------------------------------------------------------------ | |
308 * 0 0 0 0 | homepage | 1 | 1 | |
309 * 0 0 0 1 | new tab page| 1 | 0 | |
310 * 0 0 1 0 | new tab page| 1 | 0 | |
311 * 0 0 1 1 | new tab page| 1 | 0 | |
312 * 0 1 0 0 | homepage | 0 | 1 | |
313 * 0 1 0 1 | homepage | 0 | 1 | |
314 * 0 1 1 0 | new tab page| 0 | 0 | |
315 * 0 1 1 1 | new tab page| 0 | 0 | |
316 * 1 0 0 0 | homepage | 1 | 0 | |
317 * 1 0 0 1 | new tab page| 0 | 0 | |
318 * 1 0 1 0 | new tab page| 1 | 0 | |
319 * 1 0 1 1 | new tab page| 0 | 0 | |
320 * 1 1 0 0 | homepage | 0 | 0 | |
321 * 1 1 0 1 | new tab page| 0 | 0 | |
322 * 1 1 1 0 | new tab page| 0 | 0 | |
323 * 1 1 1 1 | new tab page| 0 | 0 | |
324 * | |
325 * thus, we have: | |
326 * | |
327 * choice value is new tab page === ntp || (hpisntp && (m_hp || !m_ntp)) | |
328 * choice enabled === !m_ntp && !(m_hp && hpisntp) | |
329 * URL field enabled === !ntp && !mhp && !(hpisntp && !m_ntp) | |
330 * | |
331 * which also make sense if you think about them. | |
332 * @private | |
333 */ | |
334 updateHomepageControlStates_: function() { | |
335 var homepageField = $('homepageURL'); | |
336 homepageField.disabled = !this.isHomepageURLFieldEnabled_(); | |
337 if (homepageField.value != this.homepage_pref_.value) | |
338 homepageField.value = this.homepage_pref_.value; | |
339 homepageField.style.backgroundImage = url('chrome://favicon/' + | |
340 this.homepage_pref_.value); | |
341 var disableChoice = !this.isHomepageChoiceEnabled_(); | |
342 $('homepageUseURLButton').disabled = disableChoice; | |
343 $('homepageUseNTPButton').disabled = disableChoice; | |
344 var useNewTabPage = this.isHomepageIsNewTabPageChoiceSelected_(); | |
345 $('homepageUseNTPButton').checked = useNewTabPage; | |
346 $('homepageUseURLButton').checked = !useNewTabPage; | |
347 }, | |
348 | |
349 /** | |
350 * Tests whether the value of the 'homepage' preference equls the new tab | |
351 * page url (chrome://newtab). | |
352 * @returns {boolean} True if the 'homepage' value equals the new tab page | |
353 * url. | |
354 * @private | |
355 */ | |
356 isHomepageURLNewTabPageURL_ : function() { | |
357 return (this.homepage_pref_.value.toLowerCase() == 'chrome://newtab'); | |
358 }, | |
359 | |
360 /** | |
361 * Tests whether the Homepage choice "Use New Tab Page" is selected. | |
362 * @returns {boolean} True if "Use New Tab Page" is selected. | |
363 * @private | |
364 */ | |
365 isHomepageIsNewTabPageChoiceSelected_: function() { | |
366 return (this.homepage_is_newtabpage_pref_.value || | |
367 (this.isHomepageURLNewTabPageURL_() && | |
368 (this.homepage_pref_.managed || | |
369 !this.homepage_is_newtabpage_pref_.managed))); | |
370 }, | |
371 | |
372 /** | |
373 * Tests whether the home page choice controls are enabled. | |
374 * @returns {boolean} True if the home page choice controls are enabled. | |
375 * @private | |
376 */ | |
377 isHomepageChoiceEnabled_: function() { | |
378 return (!this.homepage_is_newtabpage_pref_.managed && | |
379 !(this.homepage_pref_.managed && | |
380 this.isHomepageURLNewTabPageURL_())); | |
381 }, | |
382 | |
383 /** | |
384 * Checks whether the home page field should be enabled. | |
385 * @returns {boolean} True if the home page field should be enabled. | |
386 * @private | |
387 */ | |
388 isHomepageURLFieldEnabled_: function() { | |
389 return (!this.homepage_is_newtabpage_pref_.value && | |
390 !this.homepage_pref_.managed && | |
391 !(this.isHomepageURLNewTabPageURL_() && | |
392 !this.homepage_is_newtabpage_pref_.managed)); | |
393 }, | |
394 | |
395 /** | |
396 * Sets the enabled state of the custom startup page list controls | |
397 * based on the current startup radio button selection. | |
398 * @private | |
399 */ | |
400 updateCustomStartupPageControlStates_: function() { | |
401 var disable = !this.shouldEnableCustomStartupPageControls(); | |
402 $('startupPagesList').disabled = disable; | |
403 $('startupUseCurrentButton').disabled = disable; | |
404 }, | |
405 | |
406 /** | |
407 * Handle change events of the preference | |
408 * 'session.urls_to_restore_on_startup'. | |
409 * @param {event} preference changed event. | |
410 * @private | |
411 */ | |
412 handleStartupPageListChange_: function(event) { | |
413 this.startup_pages_pref_.managed = event.value['managed']; | |
414 this.updateCustomStartupPageControlStates_(); | |
415 }, | |
416 | |
417 /** | |
418 * Set the default search engine based on the popup selection. | |
419 */ | |
420 setDefaultSearchEngine_: function() { | |
421 var engineSelect = $('defaultSearchEngine'); | |
422 var selectedIndex = engineSelect.selectedIndex; | |
423 if (selectedIndex >= 0) { | |
424 var selection = engineSelect.options[selectedIndex]; | |
425 chrome.send('setDefaultSearchEngine', [String(selection.value)]); | |
426 } | |
427 }, | |
428 | |
429 /** | |
430 * Sends an asynchronous request for new autocompletion suggestions for the | |
431 * the given query. When new suggestions are available, the C++ handler will | |
432 * call updateAutocompleteSuggestions_. | |
433 * @param {string} query List of autocomplete suggestions. | |
434 * @private | |
435 */ | |
436 requestAutocompleteSuggestions_: function(query) { | |
437 chrome.send('requestAutocompleteSuggestions', [query]); | |
438 }, | |
439 | |
440 /** | |
441 * Updates the autocomplete suggestion list with the given entries. | |
442 * @param {Array} pages List of autocomplete suggestions. | |
443 * @private | |
444 */ | |
445 updateAutocompleteSuggestions_: function(suggestions) { | |
446 var list = this.autocompleteList_; | |
447 // If the trigger for this update was a value being selected from the | |
448 // current list, do nothing. | |
449 if (list.targetInput && list.selectedItem && | |
450 list.selectedItem['url'] == list.targetInput.value) | |
451 return; | |
452 list.suggestions = suggestions; | |
453 }, | |
454 }; | |
455 | |
456 BrowserOptions.updateDefaultBrowserState = function(statusString, isDefault, | |
457 canBeDefault) { | |
458 if (!cr.isChromeOS) { | |
459 BrowserOptions.getInstance().updateDefaultBrowserState_(statusString, | |
460 isDefault, | |
461 canBeDefault); | |
462 } | |
463 }; | |
464 | |
465 BrowserOptions.updateSearchEngines = function(engines, defaultValue) { | |
466 BrowserOptions.getInstance().updateSearchEngines_(engines, defaultValue); | |
467 }; | |
468 | |
469 BrowserOptions.updateStartupPages = function(pages) { | |
470 BrowserOptions.getInstance().updateStartupPages_(pages); | |
471 }; | |
472 | |
473 BrowserOptions.updateAutocompleteSuggestions = function(suggestions) { | |
474 BrowserOptions.getInstance().updateAutocompleteSuggestions_(suggestions); | |
475 }; | |
476 | |
477 // Export | |
478 return { | |
479 BrowserOptions: BrowserOptions | |
480 }; | |
481 | |
482 }); | |
OLD | NEW |