Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / resources / options / home_page_overlay.js
blob6649f930e2e80a9eff1e76f54479d2722e2e6cb3
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;
9   /**
10    * HomePageOverlay class
11    * Dialog that allows users to set the home page.
12    * @extends {SettingsDialog}
13    */
14   function HomePageOverlay() {
15     SettingsDialog.call(this, 'homePageOverlay',
16                         loadTimeData.getString('homePageOverlayTabTitle'),
17                         'home-page-overlay',
18                         $('home-page-confirm'), $('home-page-cancel'));
19   }
21   cr.addSingletonGetter(HomePageOverlay);
23   HomePageOverlay.prototype = {
24     __proto__: SettingsDialog.prototype,
26     /**
27      * An autocomplete list that can be attached to the home page URL field.
28      * @type {cr.ui.AutocompleteList}
29      * @private
30      */
31     autocompleteList_: null,
33     /**
34      * Initialize the page.
35      */
36     initializePage: function() {
37       // Call base class implementation to start preference initialization.
38       SettingsDialog.prototype.initializePage.call(this);
40       var self = 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();
51       });
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);
63       });
64       urlField.addEventListener('blur', function(event) {
65         self.autocompleteList_.detach();
66       });
67     },
69     /** @override */
70     didShowPage: function() {
71       this.updateFavicon_();
72     },
74     /**
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
79      * NTP.
80      * @param {Event} Pref change event.
81      */
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;
87     },
89     /**
90      * Updates the background of the url field to show the favicon for the
91      * URL that is currently typed in.
92      * @private
93      */
94     updateFavicon_: function() {
95       var urlField = $('homepage-url-field');
96       urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
97     },
99     /**
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.
104      * @private
105      */
106     requestAutocompleteSuggestions_: function(query) {
107       chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
108     },
110     /**
111      * Updates the autocomplete suggestion list with the given entries.
112      * @param {Array} pages List of autocomplete suggestions.
113      * @private
114      */
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) {
121         return;
122       }
123       list.suggestions = suggestions;
124     },
126     /**
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
129      * code.)
130      */
131     handleConfirm: function() {
132       // Strip whitespace.
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.
139       if (!homePageValue)
140         $('homepage-use-ntp').checked = true;
142       SettingsDialog.prototype.handleConfirm.call(this);
143     },
144   };
146   HomePageOverlay.updateAutocompleteSuggestions = function() {
147     var instance = HomePageOverlay.getInstance();
148     instance.updateAutocompleteSuggestions_.apply(instance, arguments);
149   };
151   // Export
152   return {
153     HomePageOverlay: HomePageOverlay
154   };