Home | History | Annotate | Download | only in options
      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 
      5 cr.define('options', function() {
      6   /** @const */ var OptionsPage = options.OptionsPage;
      7   /** @const */ var SettingsDialog = options.SettingsDialog;
      8 
      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   }
     20 
     21   cr.addSingletonGetter(HomePageOverlay);
     22 
     23   HomePageOverlay.prototype = {
     24     __proto__: SettingsDialog.prototype,
     25 
     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,
     32 
     33     /**
     34      * Initialize the page.
     35      */
     36     initializePage: function() {
     37       // Call base class implementation to start preference initialization.
     38       SettingsDialog.prototype.initializePage.call(this);
     39 
     40       var self = this;
     41       options.Preferences.getInstance().addEventListener(
     42           'homepage_is_newtabpage',
     43           this.handleHomepageIsNTPPrefChange.bind(this));
     44 
     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));
     53 
     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;
     60 
     61       urlField.addEventListener('focus', function(event) {
     62         self.autocompleteList_.attachToInput(urlField);
     63       });
     64       urlField.addEventListener('blur', function(event) {
     65         self.autocompleteList_.detach();
     66       });
     67 
     68       // Text fields may change widths and positions when the window changes
     69       // size, so make sure the suggestion list stays in sync.
     70       window.addEventListener('resize', function() {
     71         self.autocompleteList_.syncWidthAndPositionToInput();
     72       });
     73     },
     74 
     75     /** @override */
     76     didShowPage: function() {
     77       this.updateFavicon_();
     78     },
     79 
     80     /**
     81      * Updates the state of the homepage text input and its controlled setting
     82      * indicator when the |homepage_is_newtabpage| pref changes. The input is
     83      * enabled only if the homepage is not the NTP. The indicator is always
     84      * enabled but treats the input's value as read-only if the homepage is the
     85      * NTP.
     86      * @param {Event} Pref change event.
     87      */
     88     handleHomepageIsNTPPrefChange: function(event) {
     89       var urlField = $('homepage-url-field');
     90       var urlFieldIndicator = $('homepage-url-field-indicator');
     91       urlField.setDisabled('homepage-is-ntp', event.value.value);
     92       urlFieldIndicator.readOnly = event.value.value;
     93     },
     94 
     95     /**
     96      * Updates the background of the url field to show the favicon for the
     97      * URL that is currently typed in.
     98      * @private
     99      */
    100     updateFavicon_: function() {
    101       var urlField = $('homepage-url-field');
    102       urlField.style.backgroundImage = getFaviconImageSet(urlField.value);
    103     },
    104 
    105     /**
    106      * Sends an asynchronous request for new autocompletion suggestions for the
    107      * the given query. When new suggestions are available, the C++ handler will
    108      * call updateAutocompleteSuggestions_.
    109      * @param {string} query List of autocomplete suggestions.
    110      * @private
    111      */
    112     requestAutocompleteSuggestions_: function(query) {
    113       chrome.send('requestAutocompleteSuggestionsForHomePage', [query]);
    114     },
    115 
    116     /**
    117      * Updates the autocomplete suggestion list with the given entries.
    118      * @param {Array} pages List of autocomplete suggestions.
    119      * @private
    120      */
    121     updateAutocompleteSuggestions_: function(suggestions) {
    122       var list = this.autocompleteList_;
    123       // If the trigger for this update was a value being selected from the
    124       // current list, do nothing.
    125       if (list.targetInput && list.selectedItem &&
    126           list.selectedItem.url == list.targetInput.value) {
    127         return;
    128       }
    129       list.suggestions = suggestions;
    130     },
    131 
    132     /**
    133      * Sets the 'show home button' and 'home page is new tab page' preferences.
    134      * (The home page url preference is set automatically by the SettingsDialog
    135      * code.)
    136      */
    137     handleConfirm: function() {
    138       // Strip whitespace.
    139       var urlField = $('homepage-url-field');
    140       var homePageValue = urlField.value.replace(/\s*/g, '');
    141       urlField.value = homePageValue;
    142 
    143       // Don't save an empty URL for the home page. If the user left the field
    144       // empty, switch to the New Tab page.
    145       if (!homePageValue)
    146         $('homepage-use-ntp').checked = true;
    147 
    148       SettingsDialog.prototype.handleConfirm.call(this);
    149     },
    150   };
    151 
    152   HomePageOverlay.updateAutocompleteSuggestions = function() {
    153     var instance = HomePageOverlay.getInstance();
    154     instance.updateAutocompleteSuggestions_.apply(instance, arguments);
    155   };
    156 
    157   // Export
    158   return {
    159     HomePageOverlay: HomePageOverlay
    160   };
    161 });
    162