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