• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4
5cr.define('options', function() {
6  /** @const */ var Page = cr.ui.pageManager.Page;
7  /** @const */ var SettingsDialog = options.SettingsDialog;
8
9  /**
10   * HomePageOverlay class
11   * Dialog that allows users to set the home page.
12   * @constructor
13   * @extends {options.SettingsDialog}
14   */
15  function HomePageOverlay() {
16    SettingsDialog.call(this, 'homePageOverlay',
17        loadTimeData.getString('homePageOverlayTabTitle'),
18        'home-page-overlay',
19        assertInstanceof($('home-page-confirm'), HTMLButtonElement),
20        assertInstanceof($('home-page-cancel'), HTMLButtonElement));
21  }
22
23  cr.addSingletonGetter(HomePageOverlay);
24
25  HomePageOverlay.prototype = {
26    __proto__: SettingsDialog.prototype,
27
28    /**
29     * An autocomplete list that can be attached to the home page URL field.
30     * @type {cr.ui.AutocompleteList}
31     * @private
32     */
33    autocompleteList_: null,
34
35    /** @override */
36    initializePage: function() {
37      SettingsDialog.prototype.initializePage.call(this);
38
39      var self = this;
40      options.Preferences.getInstance().addEventListener(
41          'homepage_is_newtabpage',
42          this.handleHomepageIsNTPPrefChange.bind(this));
43
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();
50      });
51      urlField.addEventListener('change', this.updateFavicon_.bind(this));
52
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;
59
60      urlField.addEventListener('focus', function(event) {
61        self.autocompleteList_.attachToInput(urlField);
62      });
63      urlField.addEventListener('blur', function(event) {
64        self.autocompleteList_.detach();
65      });
66    },
67
68    /** @override */
69    didShowPage: function() {
70      this.updateFavicon_();
71    },
72
73    /**
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
78     * NTP.
79     * @param {Event} event Pref change event.
80     */
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;
86    },
87
88    /**
89     * Updates the background of the url field to show the favicon for the
90     * URL that is currently typed in.
91     * @private
92     */
93    updateFavicon_: function() {
94      var urlField = $('homepage-url-field');
95      urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
96    },
97
98    /**
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.
103     * @private
104     */
105    requestAutocompleteSuggestions_: function(query) {
106      chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
107    },
108
109    /**
110     * Updates the autocomplete suggestion list with the given entries.
111     * @param {Array} suggestions List of autocomplete suggestions.
112     * @private
113     */
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) {
120        return;
121      }
122      list.suggestions = suggestions;
123    },
124
125    /**
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
128     * code.)
129     */
130    handleConfirm: function() {
131      // Strip whitespace.
132      var urlField = $('homepage-url-field');
133      var homePageValue = urlField.value.replace(/\s*/g, '');
134      urlField.value = homePageValue;
135
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.
138      if (!homePageValue)
139        $('homepage-use-ntp').checked = true;
140
141      SettingsDialog.prototype.handleConfirm.call(this);
142    },
143  };
144
145  HomePageOverlay.updateAutocompleteSuggestions = function() {
146    var instance = HomePageOverlay.getInstance();
147    instance.updateAutocompleteSuggestions_.apply(instance, arguments);
148  };
149
150  // Export
151  return {
152    HomePageOverlay: HomePageOverlay
153  };
154});
155