Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / resources / options / home_page_overlay.js
blob84d3a788932bd3e68b18e495a7550228d416d21d
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;
9 /**
10 * HomePageOverlay class
11 * Dialog that allows users to set the home page.
12 * @constructor
13 * @extends {options.SettingsDialog}
15 function HomePageOverlay() {
16 SettingsDialog.call(this, 'homePageOverlay',
17 loadTimeData.getString('homePageOverlayTabTitle'),
18 'home-page-overlay',
19 $('home-page-confirm'), $('home-page-cancel'));
22 cr.addSingletonGetter(HomePageOverlay);
24 HomePageOverlay.prototype = {
25 __proto__: SettingsDialog.prototype,
27 /**
28 * An autocomplete list that can be attached to the home page URL field.
29 * @type {cr.ui.AutocompleteList}
30 * @private
32 autocompleteList_: null,
34 /** @override */
35 initializePage: function() {
36 SettingsDialog.prototype.initializePage.call(this);
38 var self = this;
39 options.Preferences.getInstance().addEventListener(
40 'homepage_is_newtabpage',
41 this.handleHomepageIsNTPPrefChange.bind(this));
43 var urlField = $('homepage-url-field');
44 urlField.addEventListener('keydown', function(event) {
45 // Don't auto-submit when the user selects something from the
46 // auto-complete list.
47 if (event.keyIdentifier == 'Enter' && !self.autocompleteList_.hidden)
48 event.stopPropagation();
49 });
50 urlField.addEventListener('change', this.updateFavicon_.bind(this));
52 var suggestionList = new cr.ui.AutocompleteList();
53 suggestionList.autoExpands = true;
54 suggestionList.requestSuggestions =
55 this.requestAutocompleteSuggestions_.bind(this);
56 $('home-page-overlay').appendChild(suggestionList);
57 this.autocompleteList_ = suggestionList;
59 urlField.addEventListener('focus', function(event) {
60 self.autocompleteList_.attachToInput(urlField);
61 });
62 urlField.addEventListener('blur', function(event) {
63 self.autocompleteList_.detach();
64 });
67 /** @override */
68 didShowPage: function() {
69 this.updateFavicon_();
72 /**
73 * Updates the state of the homepage text input and its controlled setting
74 * indicator when the |homepage_is_newtabpage| pref changes. The input is
75 * enabled only if the homepage is not the NTP. The indicator is always
76 * enabled but treats the input's value as read-only if the homepage is the
77 * NTP.
78 * @param {Event} event Pref change event.
80 handleHomepageIsNTPPrefChange: function(event) {
81 var urlField = $('homepage-url-field');
82 var urlFieldIndicator = $('homepage-url-field-indicator');
83 urlField.setDisabled('homepage-is-ntp', event.value.value);
84 urlFieldIndicator.readOnly = event.value.value;
87 /**
88 * Updates the background of the url field to show the favicon for the
89 * URL that is currently typed in.
90 * @private
92 updateFavicon_: function() {
93 var urlField = $('homepage-url-field');
94 urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
97 /**
98 * Sends an asynchronous request for new autocompletion suggestions for the
99 * the given query. When new suggestions are available, the C++ handler will
100 * call updateAutocompleteSuggestions_.
101 * @param {string} query List of autocomplete suggestions.
102 * @private
104 requestAutocompleteSuggestions_: function(query) {
105 chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
109 * Updates the autocomplete suggestion list with the given entries.
110 * @param {Array} suggestions List of autocomplete suggestions.
111 * @private
113 updateAutocompleteSuggestions_: function(suggestions) {
114 var list = this.autocompleteList_;
115 // If the trigger for this update was a value being selected from the
116 // current list, do nothing.
117 if (list.targetInput && list.selectedItem &&
118 list.selectedItem.url == list.targetInput.value) {
119 return;
121 list.suggestions = suggestions;
125 * Sets the 'show home button' and 'home page is new tab page' preferences.
126 * (The home page url preference is set automatically by the SettingsDialog
127 * code.)
129 handleConfirm: function() {
130 // Strip whitespace.
131 var urlField = $('homepage-url-field');
132 var homePageValue = urlField.value.replace(/\s*/g, '');
133 urlField.value = homePageValue;
135 // Don't save an empty URL for the home page. If the user left the field
136 // empty, switch to the New Tab page.
137 if (!homePageValue)
138 $('homepage-use-ntp').checked = true;
140 SettingsDialog.prototype.handleConfirm.call(this);
144 HomePageOverlay.updateAutocompleteSuggestions = function() {
145 var instance = HomePageOverlay.getInstance();
146 instance.updateAutocompleteSuggestions_.apply(instance, arguments);
149 // Export
150 return {
151 HomePageOverlay: HomePageOverlay