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.
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 initializePage: function() {
35 SettingsDialog.prototype.initializePage.call(this);
38 options.Preferences.getInstance().addEventListener(
39 'homepage_is_newtabpage',
40 this.handleHomepageIsNTPPrefChange.bind(this));
42 var urlField = $('homepage-url-field');
43 urlField.addEventListener('keydown', function(event) {
44 // Don't auto-submit when the user selects something from the
45 // auto-complete list.
46 if (event.keyIdentifier == 'Enter' && !self.autocompleteList_.hidden)
47 event.stopPropagation();
49 urlField.addEventListener('change', this.updateFavicon_.bind(this));
51 var suggestionList = new cr.ui.AutocompleteList();
52 suggestionList.autoExpands = true;
53 suggestionList.requestSuggestions =
54 this.requestAutocompleteSuggestions_.bind(this);
55 $('home-page-overlay').appendChild(suggestionList);
56 this.autocompleteList_ = suggestionList;
58 urlField.addEventListener('focus', function(event) {
59 self.autocompleteList_.attachToInput(urlField);
61 urlField.addEventListener('blur', function(event) {
62 self.autocompleteList_.detach();
67 didShowPage: function() {
68 this.updateFavicon_();
72 * Updates the state of the homepage text input and its controlled setting
73 * indicator when the |homepage_is_newtabpage| pref changes. The input is
74 * enabled only if the homepage is not the NTP. The indicator is always
75 * enabled but treats the input's value as read-only if the homepage is the
77 * @param {Event} Pref change event.
79 handleHomepageIsNTPPrefChange: function(event) {
80 var urlField = $('homepage-url-field');
81 var urlFieldIndicator = $('homepage-url-field-indicator');
82 urlField.setDisabled('homepage-is-ntp', event.value.value);
83 urlFieldIndicator.readOnly = event.value.value;
87 * Updates the background of the url field to show the favicon for the
88 * URL that is currently typed in.
91 updateFavicon_: function() {
92 var urlField = $('homepage-url-field');
93 urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
97 * Sends an asynchronous request for new autocompletion suggestions for the
98 * the given query. When new suggestions are available, the C++ handler will
99 * call updateAutocompleteSuggestions_.
100 * @param {string} query List of autocomplete suggestions.
103 requestAutocompleteSuggestions_: function(query) {
104 chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
108 * Updates the autocomplete suggestion list with the given entries.
109 * @param {Array} pages List of autocomplete suggestions.
112 updateAutocompleteSuggestions_: function(suggestions) {
113 var list = this.autocompleteList_;
114 // If the trigger for this update was a value being selected from the
115 // current list, do nothing.
116 if (list.targetInput && list.selectedItem &&
117 list.selectedItem.url == list.targetInput.value) {
120 list.suggestions = suggestions;
124 * Sets the 'show home button' and 'home page is new tab page' preferences.
125 * (The home page url preference is set automatically by the SettingsDialog
128 handleConfirm: function() {
130 var urlField = $('homepage-url-field');
131 var homePageValue = urlField.value.replace(/\s*/g, '');
132 urlField.value = homePageValue;
134 // Don't save an empty URL for the home page. If the user left the field
135 // empty, switch to the New Tab page.
137 $('homepage-use-ntp').checked = true;
139 SettingsDialog.prototype.handleConfirm.call(this);
143 HomePageOverlay.updateAutocompleteSuggestions = function() {
144 var instance = HomePageOverlay.getInstance();
145 instance.updateAutocompleteSuggestions_.apply(instance, arguments);
150 HomePageOverlay: HomePageOverlay