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.
13 * @extends {options.SettingsDialog}
15 function StartupOverlay() {
16 SettingsDialog
.call(this, 'startup',
17 loadTimeData
.getString('startupPagesOverlayTabTitle'),
19 assertInstanceof($('startup-overlay-confirm'), HTMLButtonElement
),
20 assertInstanceof($('startup-overlay-cancel'), HTMLButtonElement
));
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 // Explicitly set disabled state for input text elements.
90 var inputs
= startupPagesList
.querySelectorAll("input[type='text']");
91 for (var i
= 0; i
< inputs
.length
; i
++)
92 inputs
[i
].disabled
= disable
;
93 $('startupUseCurrentButton').disabled
= disable
;
97 * Enables or disables the the custom startup page list controls
98 * based on the whether the 'pages to restore on startup' pref is enabled.
100 updateControlStates: function() {
101 this.setControlsDisabled(
102 this.startup_pages_pref_
.disabled
);
106 * Handles change events of the preference
107 * 'session.startup_urls'.
108 * @param {Event} event Preference changed event.
111 handleStartupPageListChange_: function(event
) {
112 this.startup_pages_pref_
.disabled
= event
.value
.disabled
;
113 this.updateControlStates();
117 * Updates the startup pages list with the given entries.
118 * @param {!Array} pages List of startup pages.
121 updateStartupPages_: function(pages
) {
122 var model
= new ArrayDataModel(pages
);
123 // Add a "new page" row.
124 model
.push({modelIndex
: -1});
125 $('startupPagesList').dataModel
= model
;
129 * Sends an asynchronous request for new autocompletion suggestions for the
130 * the given query. When new suggestions are available, the C++ handler will
131 * call updateAutocompleteSuggestions_.
132 * @param {string} query List of autocomplete suggestions.
135 requestAutocompleteSuggestions_: function(query
) {
136 chrome
.send('requestAutocompleteSuggestionsForStartupPages', [query
]);
140 * Updates the autocomplete suggestion list with the given entries.
141 * @param {Array} suggestions List of autocomplete suggestions.
144 updateAutocompleteSuggestions_: function(suggestions
) {
145 var list
= this.autocompleteList_
;
146 // If the trigger for this update was a value being selected from the
147 // current list, do nothing.
148 if (list
.targetInput
&& list
.selectedItem
&&
149 list
.selectedItem
.url
== list
.targetInput
.value
) {
152 list
.suggestions
= suggestions
;
156 // Forward public APIs to private implementations.
157 cr
.makePublic(StartupOverlay
, [
158 'updateStartupPages',
159 'updateAutocompleteSuggestions',
164 StartupOverlay
: StartupOverlay