OLD | NEW |
| (Empty) |
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.urls_to_restore_on_startup', | |
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.suggestionUpdateRequestCallback = | |
64 this.requestAutocompleteSuggestions_.bind(this); | |
65 $('startup-overlay').appendChild(suggestionList); | |
66 this.autocompleteList_ = suggestionList; | |
67 startupPagesList.autocompleteList = suggestionList; | |
68 }, | |
69 | |
70 /** @inheritDoc */ | |
71 handleConfirm: function() { | |
72 SettingsDialog.prototype.handleConfirm.call(this); | |
73 chrome.send('commitStartupPrefChanges'); | |
74 }, | |
75 | |
76 /** @inheritDoc */ | |
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.urls_to_restore_on_startup'. | |
109 * @param {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({ | |
126 'modelIndex': '-1' | |
127 }); | |
128 $('startupPagesList').dataModel = model; | |
129 }, | |
130 | |
131 /** | |
132 * Sends an asynchronous request for new autocompletion suggestions for the | |
133 * the given query. When new suggestions are available, the C++ handler will | |
134 * call updateAutocompleteSuggestions_. | |
135 * @param {string} query List of autocomplete suggestions. | |
136 * @private | |
137 */ | |
138 requestAutocompleteSuggestions_: function(query) { | |
139 chrome.send('requestAutocompleteSuggestionsForStartupPages', [query]); | |
140 }, | |
141 | |
142 /** | |
143 * Updates the autocomplete suggestion list with the given entries. | |
144 * @param {Array} pages List of autocomplete suggestions. | |
145 * @private | |
146 */ | |
147 updateAutocompleteSuggestions_: function(suggestions) { | |
148 var list = this.autocompleteList_; | |
149 // If the trigger for this update was a value being selected from the | |
150 // current list, do nothing. | |
151 if (list.targetInput && list.selectedItem && | |
152 list.selectedItem['url'] == list.targetInput.value) { | |
153 return; | |
154 } | |
155 list.suggestions = suggestions; | |
156 }, | |
157 }; | |
158 | |
159 // Forward public APIs to private implementations. | |
160 [ | |
161 'updateStartupPages', | |
162 'updateAutocompleteSuggestions', | |
163 ].forEach(function(name) { | |
164 StartupOverlay[name] = function() { | |
165 var instance = StartupOverlay.getInstance(); | |
166 return instance[name + '_'].apply(instance, arguments); | |
167 }; | |
168 }); | |
169 | |
170 // Export | |
171 return { | |
172 StartupOverlay: StartupOverlay | |
173 }; | |
174 }); | |
OLD | NEW |