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 Page = cr.ui.pageManager.Page;
7 /** @const */ var SettingsDialog = options.SettingsDialog;
10 * HomePageOverlay class
11 * Dialog that allows users to set the home page.
13 * @extends {options.SettingsDialog}
15 function HomePageOverlay() {
16 SettingsDialog.call(this, 'homePageOverlay',
17 loadTimeData.getString('homePageOverlayTabTitle'),
19 assertInstanceof($('home-page-confirm'), HTMLButtonElement),
20 assertInstanceof($('home-page-cancel'), HTMLButtonElement));
23 cr.addSingletonGetter(HomePageOverlay);
25 HomePageOverlay.prototype = {
26 __proto__: SettingsDialog.prototype,
29 * An autocomplete list that can be attached to the home page URL field.
30 * @type {cr.ui.AutocompleteList}
33 autocompleteList_: null,
36 initializePage: function() {
37 SettingsDialog.prototype.initializePage.call(this);
40 options.Preferences.getInstance().addEventListener(
41 'homepage_is_newtabpage',
42 this.handleHomepageIsNTPPrefChange.bind(this));
44 var urlField = $('homepage-url-field');
45 urlField.addEventListener('keydown', function(event) {
46 // Don't auto-submit when the user selects something from the
47 // auto-complete list.
48 if (event.keyIdentifier == 'Enter' && !self.autocompleteList_.hidden)
49 event.stopPropagation();
51 urlField.addEventListener('change', this.updateFavicon_.bind(this));
53 var suggestionList = new cr.ui.AutocompleteList();
54 suggestionList.autoExpands = true;
55 suggestionList.requestSuggestions =
56 this.requestAutocompleteSuggestions_.bind(this);
57 $('home-page-overlay').appendChild(suggestionList);
58 this.autocompleteList_ = suggestionList;
60 urlField.addEventListener('focus', function(event) {
61 self.autocompleteList_.attachToInput(urlField);
63 urlField.addEventListener('blur', function(event) {
64 self.autocompleteList_.detach();
69 didShowPage: function() {
70 this.updateFavicon_();
74 * Updates the state of the homepage text input and its controlled setting
75 * indicator when the |homepage_is_newtabpage| pref changes. The input is
76 * enabled only if the homepage is not the NTP. The indicator is always
77 * enabled but treats the input's value as read-only if the homepage is the
79 * @param {Event} event Pref change event.
81 handleHomepageIsNTPPrefChange: function(event) {
82 var urlField = $('homepage-url-field');
83 var urlFieldIndicator = $('homepage-url-field-indicator');
84 urlField.setDisabled('homepage-is-ntp', event.value.value);
85 urlFieldIndicator.readOnly = event.value.value;
89 * Updates the background of the url field to show the favicon for the
90 * URL that is currently typed in.
93 updateFavicon_: function() {
94 var urlField = $('homepage-url-field');
95 urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
99 * Sends an asynchronous request for new autocompletion suggestions for the
100 * the given query. When new suggestions are available, the C++ handler will
101 * call updateAutocompleteSuggestions_.
102 * @param {string} query List of autocomplete suggestions.
105 requestAutocompleteSuggestions_: function(query) {
106 chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
110 * Updates the autocomplete suggestion list with the given entries.
111 * @param {Array} suggestions List of autocomplete suggestions.
114 updateAutocompleteSuggestions_: function(suggestions) {
115 var list = this.autocompleteList_;
116 // If the trigger for this update was a value being selected from the
117 // current list, do nothing.
118 if (list.targetInput && list.selectedItem &&
119 list.selectedItem.url == list.targetInput.value) {
122 list.suggestions = suggestions;
126 * Sets the 'show home button' and 'home page is new tab page' preferences.
127 * (The home page url preference is set automatically by the SettingsDialog
130 handleConfirm: function() {
132 var urlField = $('homepage-url-field');
133 var homePageValue = urlField.value.replace(/\s*/g, '');
134 urlField.value = homePageValue;
136 // Don't save an empty URL for the home page. If the user left the field
137 // empty, switch to the New Tab page.
139 $('homepage-use-ntp').checked = true;
141 SettingsDialog.prototype.handleConfirm.call(this);
145 HomePageOverlay.updateAutocompleteSuggestions = function() {
146 var instance = HomePageOverlay.getInstance();
147 instance.updateAutocompleteSuggestions_.apply(instance, arguments);
152 HomePageOverlay: HomePageOverlay