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 ArrayDataModel = cr.ui.ArrayDataModel;
7 /** @const */ var OptionsPage = options.OptionsPage;
8 /** @const */ var SettingsDialog = options.SettingsDialog;
11 * StartupOverlay class
12 * Encapsulated handling of the 'Set Startup pages' overlay page.
16 function StartupOverlay() {
17 SettingsDialog.call(this, 'startup',
18 loadTimeData.getString('startupPagesOverlayTabTitle'),
20 $('startup-overlay-confirm'),
21 $('startup-overlay-cancel'));
24 cr.addSingletonGetter(StartupOverlay);
26 StartupOverlay.prototype = {
27 __proto__: SettingsDialog.prototype,
30 * An autocomplete list that can be attached to a text field during editing.
34 autocompleteList_: null,
36 startup_pages_pref_: {
37 'name': 'session.startup_urls',
42 * Initialize the page.
44 initializePage: function() {
45 SettingsDialog.prototype.initializePage.call(this);
49 var startupPagesList = $('startupPagesList');
50 options.browser_options.StartupPageList.decorate(startupPagesList);
51 startupPagesList.autoExpands = true;
53 $('startupUseCurrentButton').onclick = function(event) {
54 chrome.send('setStartupPagesToCurrentPages');
57 Preferences.getInstance().addEventListener(
58 this.startup_pages_pref_.name,
59 this.handleStartupPageListChange_.bind(this));
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;
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);
80 handleCancel: function() {
81 SettingsDialog.prototype.handleCancel.call(this);
82 chrome.send('cancelStartupPrefChanges');
86 * Sets the enabled state of the custom startup page list
87 * @param {boolean} disable True to disable, false to enable
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;
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.
104 updateControlStates: function() {
105 this.setControlsDisabled(
106 this.startup_pages_pref_.disabled);
110 * Handles change events of the preference
111 * 'session.startup_urls'.
112 * @param {event} preference changed event.
115 handleStartupPageListChange_: function(event) {
116 this.startup_pages_pref_.disabled = event.value.disabled;
117 this.updateControlStates();
121 * Updates the startup pages list with the given entries.
122 * @param {Array} pages List of startup pages.
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;
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.
139 requestAutocompleteSuggestions_: function(query) {
140 chrome.send('requestAutocompleteSuggestionsForStartupPages', [query]);
144 * Updates the autocomplete suggestion list with the given entries.
145 * @param {Array} pages List of autocomplete suggestions.
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) {
156 list.suggestions = suggestions;
160 // Forward public APIs to private implementations.
162 'updateStartupPages',
163 'updateAutocompleteSuggestions',
164 ].forEach(function(name) {
165 StartupOverlay[name] = function() {
166 var instance = StartupOverlay.getInstance();
167 return instance[name + '_'].apply(instance, arguments);
173 StartupOverlay: StartupOverlay