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 // Various text messages within the edit dialog.
6 var assistText
= chrome
.i18n
.getMessage("rss_subscription_feed_url_assist");
8 // Specifies the index of the item we are editing or -1 if adding new.
10 // Whether we are currently editing the default item.
11 var editingDefault
= false;
14 document
.title
= chrome
.i18n
.getMessage("rss_subscription_edit_dialog_title");
16 // Make sure the dialog is not visible.
17 document
.getElementById('dialogBackground').style
.display
= "none";
19 // Make sure the buttons are disabled to begin with.
20 document
.getElementById('editReader').disabled
= true;
21 document
.getElementById('removeReader').disabled
= true;
22 document
.getElementById('setDefault').disabled
= true;
24 if (!storageEnabled
) {
25 document
.getElementById('addReader').disabled
= true;
26 document
.getElementById('readerListbox').disabled
= true;
27 document
.getElementById('alwaysUseDefault').disabled
= true;
28 document
.getElementById('resetList').disabled
= true;
30 alert(chrome
.i18n
.getMessage("rss_subscription_no_localstorage"));
35 if (window
.localStorage
.readerList
) {
36 feedReaderList
= JSON
.parse(window
.localStorage
.readerList
);
38 feedReaderList
= defaultReaderList();
39 window
.localStorage
.readerList
= JSON
.stringify(feedReaderList
);
42 // If there is no default, set the first item as default.
43 if (isDefaultReader("") && feedReaderList
.length
> 0)
44 window
.localStorage
.defaultReader
= feedReaderList
[0].url
;
46 // Populate the list of readers.
47 var readerListbox
= document
.getElementById('readerListbox');
48 while (readerListbox
.options
.length
> 0)
49 readerListbox
.remove(0);
50 for (i
= 0; i
< feedReaderList
.length
; ++i
) {
51 var description
= feedReaderList
[i
].description
;
52 if (isDefaultReader(feedReaderList
[i
].url
))
53 description
+= " " + chrome
.i18n
.getMessage("rss_subscription_default");
54 readerListbox
.add(new Option(description
, feedReaderList
[i
].url
));
57 // Set up the 'show preview?' checkbox.
58 var skipPreview
= document
.getElementById('alwaysUseDefault');
59 skipPreview
.checked
= window
.localStorage
.showPreviewPage
== "No";
62 function toggleFeedPreview() {
63 var alwaysUseDefault
= document
.getElementById('alwaysUseDefault');
64 if (alwaysUseDefault
.checked
)
65 window
.localStorage
.showPreviewPage
= "No";
67 delete window
.localStorage
.showPreviewPage
;
70 function setDefault() {
71 var readerListbox
= document
.getElementById('readerListbox');
72 var selection
= readerListbox
.options
[readerListbox
.selectedIndex
];
73 window
.localStorage
.defaultReader
= selection
.value
;
75 // Reinititalize the page.
79 function resetList() {
80 if (!confirm(chrome
.i18n
.getMessage("rss_subscription_reset_list_confirm"))) {
84 delete window
.localStorage
.readerList
;
85 delete window
.localStorage
.defaultReader
;
86 delete window
.localStorage
.showPreviewPage
;
88 // Reinititalize the page.
92 function onSelectionChanged() {
93 var selected
= readerListbox
.selectedIndex
> -1;
94 // To edit a reader something must be selected.
95 document
.getElementById('editReader').disabled
= !selected
;
96 // To set default, the current selection cannot already be default.
97 document
.getElementById('setDefault').disabled
= !selected
||
98 isDefaultReader(readerListbox
[readerListbox
.selectedIndex
].value
);
99 // To remove the selected reader it must not be the last item.
100 document
.getElementById('removeReader').disabled
=
101 !selected
|| readerListbox
.options
.length
< 2;
104 function editReader(index
) {
105 var readerListbox
= document
.getElementById('readerListbox');
108 // Adding a new item, make sure the text boxes are empty.
109 document
.getElementById('urlText').value
= "";
110 document
.getElementById('descriptionText').value
= "";
111 editingIndex
= -1; // New item.
112 editingDefault
= true; // New items become default items.
113 } else if (index
== 0) {
114 // Editing some item, find the current item index and store it.
115 editingIndex
= readerListbox
.selectedIndex
;
116 var oldOption
= readerListbox
.options
[editingIndex
];
117 document
.getElementById('urlText').value
= oldOption
.value
;
118 document
.getElementById('descriptionText').value
=
119 oldOption
.text
.replace(
120 ' ' + chrome
.i18n
.getMessage("rss_subscription_default"), '');
121 editingDefault
= isDefaultReader(oldOption
.value
);
127 function removeReader() {
128 var index
= readerListbox
.selectedIndex
;
129 var feedReaderList
= JSON
.parse(window
.localStorage
.readerList
);
131 confirm(chrome
.i18n
.getMessage("rss_subscription_remove_confirm",
132 feedReaderList
[index
].description
));
134 var wasDefault
= isDefaultReader(feedReaderList
[index
].url
);
135 // Remove the item from the list.
136 feedReaderList
.splice(index
, 1);
137 window
.localStorage
.readerList
= JSON
.stringify(feedReaderList
);
140 window
.localStorage
.defaultReader
= feedReaderList
[0].url
;
147 * Shows the Edit Feed Reader dialog.
149 function showDialog() {
150 document
.getElementById('urlAssist').innerText
= assistText
;
151 document
.getElementById('save').disabled
= true;
153 // Show the dialog box.
154 document
.getElementById('dialogBackground').style
.display
= "-webkit-box";
158 * Hides the Edit Feed Reader dialog.
160 function hideDialog() {
161 document
.getElementById('dialogBackground').style
.display
= "none";
165 * Validates the input in the form (making sure something is entered in both
166 * fields and that %s is not missing from the url field.
168 function validateInput() {
169 document
.getElementById('statusMsg').innerText
= "";
171 var description
= document
.getElementById('descriptionText');
172 var url
= document
.getElementById('urlText');
174 var valid
= (description
.value
.length
> 0 &&
175 url
.value
.length
> 0 &&
176 url
.value
.indexOf("%s") > -1);
178 document
.getElementById('save').disabled
= !valid
;
182 * Handler for saving the values.
186 var feedReaderList
= JSON
.parse(window
.localStorage
.readerList
);
188 var url
= document
.getElementById('urlText').value
;
189 var description
= document
.getElementById('descriptionText').value
;
191 if (editingIndex
== -1) {
192 // Construct a new list.
193 var newFeedList
= [];
196 newFeedList
.push({ 'url': url
, 'description': description
});
198 for (i
= 0; i
< feedReaderList
.length
; ++i
)
199 newFeedList
.push(feedReaderList
[i
]);
201 feedReaderList
= newFeedList
;
203 feedReaderList
[editingIndex
].url
= url
;
204 feedReaderList
[editingIndex
].description
= description
;
207 window
.localStorage
.readerList
= JSON
.stringify(feedReaderList
);
209 // Retain item default status, in case the url changed while editing item.
211 window
.localStorage
.defaultReader
= url
;
215 // Reload the values from scratch.
219 function listen(id
, eventType
, method
) {
220 document
.getElementById(id
).addEventListener(eventType
, method
);
223 document
.addEventListener('DOMContentLoaded', function () {
225 i18nReplace('rss_subscription_options');
226 i18nReplaceImpl('addReader', 'rss_subscription_add_reader');
227 i18nReplaceImpl('editReader', 'rss_subscription_edit_reader');
228 i18nReplaceImpl('removeReader', 'rss_subscription_remove_reader');
229 i18nReplaceImpl('setDefault', 'rss_subscription_make_default_reader');
230 i18nReplaceImpl('resetList', 'rss_subscription_reset_list');
231 i18nReplace('rss_subscription_always_use_default');
232 i18nReplaceImpl('dialogHeader', 'rss_subscription_edit_dialog_title');
233 i18nReplace('rss_subscription_feed_description');
234 i18nReplace('rss_subscription_feed_url');
235 i18nReplaceImpl('save', 'rss_subscription_save_button', 'value');
236 i18nReplaceImpl('rss_subscription_close_button',
237 'rss_subscription_close_button', 'value');
238 // Init event listeners.
239 listen('readerListbox', 'change', onSelectionChanged
);
240 listen('addReader', 'click', function () { editReader(-1); });
241 listen('editReader', 'click', function () { editReader(0); });
242 listen('removeReader', 'click', removeReader
);
243 listen('setDefault', 'click', setDefault
);
244 listen('resetList', 'click', resetList
);
245 listen('alwaysUseDefault', 'change', toggleFeedPreview
);
246 listen('descriptionText', 'keyup', validateInput
);
247 listen('urlText', 'keyup', validateInput
);
248 listen('save', 'click', save
);
249 listen('rss_subscription_close_button', 'click', hideDialog
);
251 // Reload the values from scratch.