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 ArrayDataModel = cr.ui.ArrayDataModel;
      7   /** @const */ var SettingsDialog = options.SettingsDialog;
      8 
      9   /**
     10    * StartupOverlay class
     11    * Encapsulated handling of the 'Set Startup pages' overlay page.
     12    * @constructor
     13    * @extends {options.SettingsDialog}
     14    */
     15   function StartupOverlay() {
     16     SettingsDialog.call(this, 'startup',
     17         loadTimeData.getString('startupPagesOverlayTabTitle'),
     18         'startup-overlay',
     19         assertInstanceof($('startup-overlay-confirm'), HTMLButtonElement),
     20         assertInstanceof($('startup-overlay-cancel'), HTMLButtonElement));
     21   };
     22 
     23   cr.addSingletonGetter(StartupOverlay);
     24 
     25   StartupOverlay.prototype = {
     26     __proto__: SettingsDialog.prototype,
     27 
     28     /**
     29      * An autocomplete list that can be attached to a text field during editing.
     30      * @type {HTMLElement}
     31      * @private
     32      */
     33     autocompleteList_: null,
     34 
     35     startup_pages_pref_: {
     36       'name': 'session.startup_urls',
     37       'disabled': false
     38     },
     39 
     40     /** @override */
     41     initializePage: function() {
     42       SettingsDialog.prototype.initializePage.call(this);
     43 
     44       var self = this;
     45 
     46       var startupPagesList = $('startupPagesList');
     47       options.browser_options.StartupPageList.decorate(startupPagesList);
     48       startupPagesList.autoExpands = true;
     49 
     50       $('startupUseCurrentButton').onclick = function(event) {
     51         chrome.send('setStartupPagesToCurrentPages');
     52       };
     53 
     54       Preferences.getInstance().addEventListener(
     55           this.startup_pages_pref_.name,
     56           this.handleStartupPageListChange_.bind(this));
     57 
     58       var suggestionList = new cr.ui.AutocompleteList();
     59       suggestionList.autoExpands = true;
     60       suggestionList.requestSuggestions =
     61           this.requestAutocompleteSuggestions_.bind(this);
     62       $('startup-overlay').appendChild(suggestionList);
     63       this.autocompleteList_ = suggestionList;
     64       startupPagesList.autocompleteList = suggestionList;
     65     },
     66 
     67     /** @override */
     68     handleConfirm: function() {
     69       SettingsDialog.prototype.handleConfirm.call(this);
     70       chrome.send('commitStartupPrefChanges');
     71       // Set the startup behavior to "open specific set of pages" so that the
     72       // pages the user selected actually get opened on startup.
     73       Preferences.setIntegerPref('session.restore_on_startup', 4, true);
     74     },
     75 
     76     /** @override */
     77     handleCancel: function() {
     78       SettingsDialog.prototype.handleCancel.call(this);
     79       chrome.send('cancelStartupPrefChanges');
     80     },
     81 
     82     /**
     83      * Sets the enabled state of the custom startup page list
     84      * @param {boolean} disable True to disable, false to enable
     85      */
     86     setControlsDisabled: function(disable) {
     87       var startupPagesList = $('startupPagesList');
     88       startupPagesList.disabled = disable;
     89       startupPagesList.setAttribute('tabindex', disable ? -1 : 0);
     90       // Explicitly set disabled state for input text elements.
     91       var inputs = startupPagesList.querySelectorAll("input[type='text']");
     92       for (var i = 0; i < inputs.length; i++)
     93         inputs[i].disabled = disable;
     94       $('startupUseCurrentButton').disabled = disable;
     95     },
     96 
     97     /**
     98      * Enables or disables the the custom startup page list controls
     99      * based on the whether the 'pages to restore on startup' pref is enabled.
    100      */
    101     updateControlStates: function() {
    102       this.setControlsDisabled(
    103           this.startup_pages_pref_.disabled);
    104     },
    105 
    106     /**
    107      * Handles change events of the preference
    108      * 'session.startup_urls'.
    109      * @param {Event} event Preference changed event.
    110      * @private
    111      */
    112     handleStartupPageListChange_: function(event) {
    113       this.startup_pages_pref_.disabled = event.value.disabled;
    114       this.updateControlStates();
    115     },
    116 
    117     /**
    118      * Updates the startup pages list with the given entries.
    119      * @param {!Array} pages List of startup pages.
    120      * @private
    121      */
    122     updateStartupPages_: function(pages) {
    123       var model = new ArrayDataModel(pages);
    124       // Add a "new page" row.
    125       model.push({modelIndex: -1});
    126       $('startupPagesList').dataModel = model;
    127     },
    128 
    129     /**
    130      * Sends an asynchronous request for new autocompletion suggestions for the
    131      * the given query. When new suggestions are available, the C++ handler will
    132      * call updateAutocompleteSuggestions_.
    133      * @param {string} query List of autocomplete suggestions.
    134      * @private
    135      */
    136     requestAutocompleteSuggestions_: function(query) {
    137       chrome.send('requestAutocompleteSuggestionsForStartupPages', [query]);
    138     },
    139 
    140     /**
    141      * Updates the autocomplete suggestion list with the given entries.
    142      * @param {Array} suggestions List of autocomplete suggestions.
    143      * @private
    144      */
    145     updateAutocompleteSuggestions_: function(suggestions) {
    146       var list = this.autocompleteList_;
    147       // If the trigger for this update was a value being selected from the
    148       // current list, do nothing.
    149       if (list.targetInput && list.selectedItem &&
    150           list.selectedItem.url == list.targetInput.value) {
    151         return;
    152       }
    153       list.suggestions = suggestions;
    154     },
    155   };
    156 
    157   // Forward public APIs to private implementations.
    158   cr.makePublic(StartupOverlay, [
    159     'updateStartupPages',
    160     'updateAutocompleteSuggestions',
    161   ]);
    162 
    163   // Export
    164   return {
    165     StartupOverlay: StartupOverlay
    166   };
    167 });
    168