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 */ var OptionsPage = options.OptionsPage; | |
7 /** @const */ var SettingsDialog = options.SettingsDialog; | |
8 | |
9 /** | |
10 * HomePageOverlay class | |
11 * Dialog that allows users to set the home page. | |
12 * @extends {SettingsDialog} | |
13 */ | |
14 function HomePageOverlay() { | |
15 SettingsDialog.call(this, 'homePageOverlay', | |
16 loadTimeData.getString('homePageOverlayTabTitle'), | |
17 'home-page-overlay', | |
18 $('home-page-confirm'), $('home-page-cancel')); | |
19 } | |
20 | |
21 cr.addSingletonGetter(HomePageOverlay); | |
22 | |
23 HomePageOverlay.prototype = { | |
24 __proto__: SettingsDialog.prototype, | |
25 | |
26 /** | |
27 * An autocomplete list that can be attached to the home page URL field. | |
28 * @type {cr.ui.AutocompleteList} | |
29 * @private | |
30 */ | |
31 autocompleteList_: null, | |
32 | |
33 /** | |
34 * Initialize the page. | |
35 */ | |
36 initializePage: function() { | |
37 // Call base class implementation to start preference initialization. | |
38 SettingsDialog.prototype.initializePage.call(this); | |
39 | |
40 var self = this; | |
41 $('homepage-use-ntp').onchange = this.updateHomePageInput_.bind(this); | |
42 $('homepage-use-url').onchange = this.updateHomePageInput_.bind(this); | |
43 | |
44 var urlField = $('homepage-url-field'); | |
45 urlField.addEventListener('keydown', function(event) { | |
46 // Focus the 'OK' button when the user hits enter since people expect | |
47 // feedback indicating that they are done editing. | |
48 if (event.keyIdentifier == 'Enter' && self.autocompleteList_.hidden) | |
49 $('home-page-confirm').focus(); | |
50 }); | |
51 urlField.addEventListener('change', this.updateFavicon_.bind(this)); | |
52 | |
53 var suggestionList = new cr.ui.AutocompleteList(); | |
54 suggestionList.autoExpands = true; | |
55 suggestionList.suggestionUpdateRequestCallback = | |
56 this.requestAutocompleteSuggestions_.bind(this); | |
57 $('home-page-overlay').appendChild(suggestionList); | |
58 this.autocompleteList_ = suggestionList; | |
59 | |
60 urlField.addEventListener('focus', function(event) { | |
61 self.autocompleteList_.attachToInput(urlField); | |
62 }); | |
63 urlField.addEventListener('blur', function(event) { | |
64 self.autocompleteList_.detach(); | |
65 }); | |
66 | |
67 // Text fields may change widths when the window changes size, so make | |
68 // sure the suggestion list stays in sync. | |
69 window.addEventListener('resize', function() { | |
70 self.autocompleteList_.syncWidthToInput(); | |
71 }); | |
72 }, | |
73 | |
74 /** @inheritDoc */ | |
75 didShowPage: function() { | |
76 this.updateHomePageInput_(); | |
77 this.updateFavicon_(); | |
78 }, | |
79 | |
80 /** | |
81 * Updates the state of the homepage text input. The input is enabled only | |
82 * if the |homepage-use-url| radio button is checked. | |
83 * @private | |
84 */ | |
85 updateHomePageInput_: function() { | |
86 var urlField = $('homepage-url-field'); | |
87 var homePageUseURL = $('homepage-use-url'); | |
88 urlField.setDisabled('radio-choice', !homePageUseURL.checked); | |
89 }, | |
90 | |
91 /** | |
92 * Updates the background of the url field to show the favicon for the | |
93 * URL that is currently typed in. | |
94 * @private | |
95 */ | |
96 updateFavicon_: function() { | |
97 var urlField = $('homepage-url-field'); | |
98 urlField.style.backgroundImage = url('chrome://favicon/' + | |
99 urlField.value); | |
100 }, | |
101 | |
102 /** | |
103 * Sends an asynchronous request for new autocompletion suggestions for the | |
104 * the given query. When new suggestions are available, the C++ handler will | |
105 * call updateAutocompleteSuggestions_. | |
106 * @param {string} query List of autocomplete suggestions. | |
107 * @private | |
108 */ | |
109 requestAutocompleteSuggestions_: function(query) { | |
110 chrome.send('requestAutocompleteSuggestionsForHomePage', [query]); | |
111 }, | |
112 | |
113 /** | |
114 * Updates the autocomplete suggestion list with the given entries. | |
115 * @param {Array} pages List of autocomplete suggestions. | |
116 * @private | |
117 */ | |
118 updateAutocompleteSuggestions_: function(suggestions) { | |
119 var list = this.autocompleteList_; | |
120 // If the trigger for this update was a value being selected from the | |
121 // current list, do nothing. | |
122 if (list.targetInput && list.selectedItem && | |
123 list.selectedItem['url'] == list.targetInput.value) | |
124 return; | |
125 list.suggestions = suggestions; | |
126 }, | |
127 | |
128 /** | |
129 * Sets the 'show home button' and 'home page is new tab page' preferences. | |
130 * (The home page url preference is set automatically by the SettingsDialog | |
131 * code.) | |
132 */ | |
133 handleConfirm: function() { | |
134 // Strip whitespace. | |
135 var urlField = $('homepage-url-field'); | |
136 var homePageValue = urlField.value.replace(/\s*/g, ''); | |
137 urlField.value = homePageValue; | |
138 | |
139 // Don't save an empty URL for the home page. If the user left the field | |
140 // empty, switch to the New Tab page. | |
141 if (!homePageValue) | |
142 $('homepage-use-ntp').checked = true; | |
143 | |
144 SettingsDialog.prototype.handleConfirm.call(this); | |
145 }, | |
146 }; | |
147 | |
148 HomePageOverlay.updateAutocompleteSuggestions = function() { | |
149 var instance = HomePageOverlay.getInstance(); | |
150 instance.updateAutocompleteSuggestions_.apply(instance, arguments); | |
151 }; | |
152 | |
153 // Export | |
154 return { | |
155 HomePageOverlay: HomePageOverlay | |
156 }; | |
157 }); | |
OLD | NEW |