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