-
Notifications
You must be signed in to change notification settings - Fork 21
/
options.js
253 lines (209 loc) · 8.59 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Various text messages within the edit dialog.
var assistText = chrome.i18n.getMessage("rss_subscription_feed_url_assist");
// Specifies the index of the item we are editing or -1 if adding new.
var editingIndex = -1;
// Whether we are currently editing the default item.
var editingDefault = false;
function main() {
document.title = chrome.i18n.getMessage("rss_subscription_edit_dialog_title");
// Make sure the dialog is not visible.
document.getElementById('dialogBackground').style.display = "none";
// Make sure the buttons are disabled to begin with.
document.getElementById('editReader').disabled = true;
document.getElementById('removeReader').disabled = true;
document.getElementById('setDefault').disabled = true;
if (!storageEnabled) {
document.getElementById('addReader').disabled = true;
document.getElementById('readerListbox').disabled = true;
document.getElementById('alwaysUseDefault').disabled = true;
document.getElementById('resetList').disabled = true;
alert(chrome.i18n.getMessage("rss_subscription_no_localstorage"));
return;
}
var feedReaderList;
if (window.localStorage.readerList) {
feedReaderList = JSON.parse(window.localStorage.readerList);
} else {
feedReaderList = defaultReaderList();
window.localStorage.readerList = JSON.stringify(feedReaderList);
}
// If there is no default, set the first item as default.
if (isDefaultReader("") && feedReaderList.length > 0)
window.localStorage.defaultReader = feedReaderList[0].url;
// Populate the list of readers.
var readerListbox = document.getElementById('readerListbox');
while (readerListbox.options.length > 0)
readerListbox.remove(0);
for (i = 0; i < feedReaderList.length; ++i) {
var description = feedReaderList[i].description;
if (isDefaultReader(feedReaderList[i].url))
description += " " + chrome.i18n.getMessage("rss_subscription_default");
readerListbox.add(new Option(description, feedReaderList[i].url));
}
// Set up the 'show preview?' checkbox.
var skipPreview = document.getElementById('alwaysUseDefault');
skipPreview.checked = window.localStorage.showPreviewPage == "No";
}
function toggleFeedPreview() {
var alwaysUseDefault = document.getElementById('alwaysUseDefault');
if (alwaysUseDefault.checked)
window.localStorage.showPreviewPage = "No";
else
delete window.localStorage.showPreviewPage;
}
function setDefault() {
var readerListbox = document.getElementById('readerListbox');
var selection = readerListbox.options[readerListbox.selectedIndex];
window.localStorage.defaultReader = selection.value;
// Reinititalize the page.
main();
}
function resetList() {
if (!confirm(chrome.i18n.getMessage("rss_subscription_reset_list_confirm"))) {
return;
}
delete window.localStorage.readerList;
delete window.localStorage.defaultReader;
delete window.localStorage.showPreviewPage;
// Reinititalize the page.
main();
}
function onSelectionChanged() {
var selected = readerListbox.selectedIndex > -1;
// To edit a reader something must be selected.
document.getElementById('editReader').disabled = !selected;
// To set default, the current selection cannot already be default.
document.getElementById('setDefault').disabled = !selected ||
isDefaultReader(readerListbox[readerListbox.selectedIndex].value);
// To remove the selected reader it must not be the last item.
document.getElementById('removeReader').disabled =
!selected || readerListbox.options.length < 2;
}
function editReader(index) {
var readerListbox = document.getElementById('readerListbox');
if (index == -1) {
// Adding a new item, make sure the text boxes are empty.
document.getElementById('urlText').value = "";
document.getElementById('descriptionText').value = "";
editingIndex = -1; // New item.
editingDefault = true; // New items become default items.
} else if (index == 0) {
// Editing some item, find the current item index and store it.
editingIndex = readerListbox.selectedIndex;
var oldOption = readerListbox.options[editingIndex];
document.getElementById('urlText').value = oldOption.value;
document.getElementById('descriptionText').value =
oldOption.text.replace(
' ' + chrome.i18n.getMessage("rss_subscription_default"), '');
editingDefault = isDefaultReader(oldOption.value);
}
showDialog();
}
function removeReader() {
var index = readerListbox.selectedIndex;
var feedReaderList = JSON.parse(window.localStorage.readerList);
var reply =
confirm(chrome.i18n.getMessage("rss_subscription_remove_confirm",
feedReaderList[index].description));
if (reply) {
var wasDefault = isDefaultReader(feedReaderList[index].url);
// Remove the item from the list.
feedReaderList.splice(index, 1);
window.localStorage.readerList = JSON.stringify(feedReaderList);
if (wasDefault)
window.localStorage.defaultReader = feedReaderList[0].url;
}
main();
}
/**
* Shows the Edit Feed Reader dialog.
*/
function showDialog() {
document.getElementById('urlAssist').innerText = assistText;
document.getElementById('save').disabled = true;
// Show the dialog box.
document.getElementById('dialogBackground').style.display = "-webkit-box";
}
/**
* Hides the Edit Feed Reader dialog.
*/
function hideDialog() {
document.getElementById('dialogBackground').style.display = "none";
}
/**
* Validates the input in the form (making sure something is entered in both
* fields and that %s is not missing from the url field.
*/
function validateInput() {
document.getElementById('statusMsg').innerText = "";
var description = document.getElementById('descriptionText');
var url = document.getElementById('urlText');
var valid = (description.value.length > 0 &&
url.value.length > 0 &&
url.value.indexOf("%s") > -1);
document.getElementById('save').disabled = !valid;
}
/**
* Handler for saving the values.
*/
function save() {
// Get the old list.
var feedReaderList = JSON.parse(window.localStorage.readerList);
var url = document.getElementById('urlText').value;
var description = document.getElementById('descriptionText').value;
if (editingIndex == -1) {
// Construct a new list.
var newFeedList = [];
// Add the new item.
newFeedList.push({ 'url': url, 'description': description });
for (i = 0; i < feedReaderList.length; ++i)
newFeedList.push(feedReaderList[i]);
feedReaderList = newFeedList;
} else {
feedReaderList[editingIndex].url = url;
feedReaderList[editingIndex].description = description;
}
window.localStorage.readerList = JSON.stringify(feedReaderList);
// Retain item default status, in case the url changed while editing item.
if (editingDefault)
window.localStorage.defaultReader = url;
hideDialog();
// Reload the values from scratch.
main();
}
function listen(id, eventType, method) {
document.getElementById(id).addEventListener(eventType, method);
}
document.addEventListener('DOMContentLoaded', function () {
// Localize.
i18nReplace('rss_subscription_options');
i18nReplaceImpl('addReader', 'rss_subscription_add_reader');
i18nReplaceImpl('editReader', 'rss_subscription_edit_reader');
i18nReplaceImpl('removeReader', 'rss_subscription_remove_reader');
i18nReplaceImpl('setDefault', 'rss_subscription_make_default_reader');
i18nReplaceImpl('resetList', 'rss_subscription_reset_list');
i18nReplace('rss_subscription_always_use_default');
i18nReplaceImpl('dialogHeader', 'rss_subscription_edit_dialog_title');
i18nReplace('rss_subscription_feed_description');
i18nReplace('rss_subscription_feed_url');
i18nReplaceImpl('save', 'rss_subscription_save_button', 'value');
i18nReplaceImpl('rss_subscription_close_button',
'rss_subscription_close_button', 'value');
// Init event listeners.
listen('readerListbox', 'change', onSelectionChanged);
listen('addReader', 'click', function () { editReader(-1); });
listen('editReader', 'click', function () { editReader(0); });
listen('removeReader', 'click', removeReader);
listen('setDefault', 'click', setDefault);
listen('resetList', 'click', resetList);
listen('alwaysUseDefault', 'change', toggleFeedPreview);
listen('descriptionText', 'keyup', validateInput);
listen('urlText', 'keyup', validateInput);
listen('save', 'click', save);
listen('rss_subscription_close_button', 'click', hideDialog);
// Reload the values from scratch.
main();
});