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 SettingsDialog = options.SettingsDialog;
10 * StartupOverlay class
11 * Encapsulated handling of the 'Set Startup pages' overlay page.
15 function StartupOverlay() {
16 SettingsDialog.call(this, 'startup',
17 loadTimeData.getString('startupPagesOverlayTabTitle'),
19 $('startup-overlay-confirm'),
20 $('startup-overlay-cancel'));
23 cr.addSingletonGetter(StartupOverlay);
25 StartupOverlay.prototype = {
26 __proto__: SettingsDialog.prototype,
29 * An autocomplete list that can be attached to a text field during editing.
33 autocompleteList_: null,
35 startup_pages_pref_: {
36 'name': 'session.startup_urls',
41 initializePage: function() {
42 SettingsDialog.prototype.initializePage.call(this);
46 var startupPagesList = $('startupPagesList');
47 options.browser_options.StartupPageList.decorate(startupPagesList);
48 startupPagesList.autoExpands = true;
50 $('startupUseCurrentButton').onclick = function(event) {
51 chrome.send('setStartupPagesToCurrentPages');
54 Preferences.getInstance().addEventListener(
55 this.startup_pages_pref_.name,
56 this.handleStartupPageListChange_.bind(this));
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;
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);
77 handleCancel: function() {
78 SettingsDialog.prototype.handleCancel.call(this);
79 chrome.send('cancelStartupPrefChanges');
83 * Sets the enabled state of the custom startup page list
84 * @param {boolean} disable True to disable, false to enable
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;
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.
101 updateControlStates: function() {
102 this.setControlsDisabled(
103 this.startup_pages_pref_.disabled);
107 * Handles change events of the preference
108 * 'session.startup_urls'.
109 * @param {event} preference changed event.
112 handleStartupPageListChange_: function(event) {
113 this.startup_pages_pref_.disabled = event.value.disabled;
114 this.updateControlStates();
118 * Updates the startup pages list with the given entries.
119 * @param {Array} pages List of startup pages.
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;
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.
136 requestAutocompleteSuggestions_: function(query) {
137 chrome.send('requestAutocompleteSuggestionsForStartupPages', [query]);
141 * Updates the autocomplete suggestion list with the given entries.
142 * @param {Array} pages List of autocomplete suggestions.
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) {
153 list.suggestions = suggestions;
157 // Forward public APIs to private implementations.
159 'updateStartupPages',
160 'updateAutocompleteSuggestions',
161 ].forEach(function(name) {
162 StartupOverlay[name] = function() {
163 var instance = StartupOverlay.getInstance();
164 return instance[name + '_'].apply(instance, arguments);
170 StartupOverlay: StartupOverlay