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.
5 cr.define('options', function() {
6 /** @const */ var OptionsPage = options.OptionsPage;
7 /** @const */ var SettingsDialog = options.SettingsDialog;
10 * HomePageOverlay class
11 * Dialog that allows users to set the home page.
12 * @extends {SettingsDialog}
14 function HomePageOverlay() {
15 SettingsDialog.call(this, 'homePageOverlay',
16 loadTimeData.getString('homePageOverlayTabTitle'),
18 $('home-page-confirm'), $('home-page-cancel'));
21 cr.addSingletonGetter(HomePageOverlay);
23 HomePageOverlay.prototype = {
24 __proto__: SettingsDialog.prototype,
27 * An autocomplete list that can be attached to the home page URL field.
28 * @type {cr.ui.AutocompleteList}
31 autocompleteList_: null,
34 * Initialize the page.
36 initializePage: function() {
37 // Call base class implementation to start preference initialization.
38 SettingsDialog.prototype.initializePage.call(this);
41 options.Preferences.getInstance().addEventListener(
42 'homepage_is_newtabpage',
43 this.handleHomepageIsNTPPrefChange.bind(this));
45 var urlField = $('homepage-url-field');
46 urlField.addEventListener('keydown', function(event) {
47 // Don't auto-submit when the user selects something from the
48 // auto-complete list.
49 if (event.keyIdentifier == 'Enter' && !self.autocompleteList_.hidden)
50 event.stopPropagation();
52 urlField.addEventListener('change', this.updateFavicon_.bind(this));
54 var suggestionList = new cr.ui.AutocompleteList();
55 suggestionList.autoExpands = true;
56 suggestionList.requestSuggestions =
57 this.requestAutocompleteSuggestions_.bind(this);
58 $('home-page-overlay').appendChild(suggestionList);
59 this.autocompleteList_ = suggestionList;
61 urlField.addEventListener('focus', function(event) {
62 self.autocompleteList_.attachToInput(urlField);
64 urlField.addEventListener('blur', function(event) {
65 self.autocompleteList_.detach();
70 didShowPage: function() {
71 this.updateFavicon_();
75 * Updates the state of the homepage text input and its controlled setting
76 * indicator when the |homepage_is_newtabpage| pref changes. The input is
77 * enabled only if the homepage is not the NTP. The indicator is always
78 * enabled but treats the input's value as read-only if the homepage is the
80 * @param {Event} Pref change event.
82 handleHomepageIsNTPPrefChange: function(event) {
83 var urlField = $('homepage-url-field');
84 var urlFieldIndicator = $('homepage-url-field-indicator');
85 urlField.setDisabled('homepage-is-ntp', event.value.value);
86 urlFieldIndicator.readOnly = event.value.value;
90 * Updates the background of the url field to show the favicon for the
91 * URL that is currently typed in.
94 updateFavicon_: function() {
95 var urlField = $('homepage-url-field');
96 urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
100 * Sends an asynchronous request for new autocompletion suggestions for the
101 * the given query. When new suggestions are available, the C++ handler will
102 * call updateAutocompleteSuggestions_.
103 * @param {string} query List of autocomplete suggestions.
106 requestAutocompleteSuggestions_: function(query) {
107 chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
111 * Updates the autocomplete suggestion list with the given entries.
112 * @param {Array} pages List of autocomplete suggestions.
115 updateAutocompleteSuggestions_: function(suggestions) {
116 var list = this.autocompleteList_;
117 // If the trigger for this update was a value being selected from the
118 // current list, do nothing.
119 if (list.targetInput && list.selectedItem &&
120 list.selectedItem.url == list.targetInput.value) {
123 list.suggestions = suggestions;
127 * Sets the 'show home button' and 'home page is new tab page' preferences.
128 * (The home page url preference is set automatically by the SettingsDialog
131 handleConfirm: function() {
133 var urlField = $('homepage-url-field');
134 var homePageValue = urlField.value.replace(/\s*/g, '');
135 urlField.value = homePageValue;
137 // Don't save an empty URL for the home page. If the user left the field
138 // empty, switch to the New Tab page.
140 $('homepage-use-ntp').checked = true;
142 SettingsDialog.prototype.handleConfirm.call(this);
146 HomePageOverlay.updateAutocompleteSuggestions = function() {
147 var instance = HomePageOverlay.getInstance();
148 instance.updateAutocompleteSuggestions_.apply(instance, arguments);
153 HomePageOverlay: HomePageOverlay