]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
b2adeb1ced75630078a6c9155dfdc216f715b314
[tt-rss.git] / tt-rss.js
1 /*
2 This program is Copyright (c) 2003-2005 Andrew Dolgov <cthulhoo@gmail.com>
3 Licensed under GPL v.2 or (at your preference) any later version.
4 */
5
6 var xmlhttp = false;
7 var xmlhttp_rpc = false;
8 var xmlhttp_view = false;
9
10 var total_unread = 0;
11 var first_run = true;
12
13 var active_post_id = false;
14 var active_feed_id = false;
15 var active_offset = false;
16
17 var total_feed_entries = false;
18
19 var search_query = "";
20
21 /*@cc_on @*/
22 /*@if (@_jscript_version >= 5)
23 // JScript gives us Conditional compilation, we can cope with old IE versions.
24 // and security blocked creation of the objects.
25 try {
26 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
27 } catch (e) {
28 try {
29 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
30 xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
31 xmlhttp_view = new ActiveXObject("Microsoft.XMLHTTP");
32 } catch (E) {
33 xmlhttp = false;
34 xmlhttp_rpc = false;
35 xmlhttp_view = false;
36 }
37 }
38 @end @*/
39
40 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
41 xmlhttp = new XMLHttpRequest();
42 xmlhttp_rpc = new XMLHttpRequest();
43 xmlhttp_view = new XMLHttpRequest();
44 }
45
46 function feedlist_callback() {
47 var container = document.getElementById('feeds');
48 if (xmlhttp.readyState == 4) {
49 container.innerHTML=xmlhttp.responseText;
50
51 if (first_run) {
52 scheduleFeedUpdate(false);
53 first_run = false;
54 } else {
55 notify("");
56 }
57 }
58 }
59
60 function refetch_callback() {
61
62 if (xmlhttp_rpc.readyState == 4) {
63 notify("All feeds updated");
64 var container = document.getElementById('feeds');
65 container.innerHTML = xmlhttp_rpc.responseText;
66 document.title = "Tiny Tiny RSS";
67 }
68 }
69
70 function updateFeed(feed_id) {
71
72 var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
73
74 if (xmlhttp_ready(xmlhttp_rpc)) {
75 xmlhttp_rpc.open("GET", query_str, true);
76 xmlhttp_rpc.onreadystatechange=feed_update_callback;
77 xmlhttp_rpc.send(null);
78 } else {
79 printLockingError();
80 }
81
82 }
83
84 function scheduleFeedUpdate(force) {
85
86 notify("Updating feeds in background...");
87
88 document.title = "Tiny Tiny RSS - Updating...";
89
90 var query_str = "backend.php?op=rpc&subop=";
91
92 if (force) {
93 query_str = query_str + "forceUpdateAllFeeds";
94 } else {
95 query_str = query_str + "updateAllFeeds";
96 }
97
98 if (xmlhttp_ready(xmlhttp_rpc)) {
99 xmlhttp_rpc.open("GET", query_str, true);
100 xmlhttp_rpc.onreadystatechange=refetch_callback;
101 xmlhttp_rpc.send(null);
102 } else {
103 printLockingError();
104 }
105 }
106
107 function updateFeedList(silent, fetch) {
108
109 if (silent != true) {
110 notify("Loading feed list...");
111 }
112
113 var query_str = "backend.php?op=feeds";
114
115 if (fetch) query_str = query_str + "&fetch=yes";
116
117 if (xmlhttp_ready(xmlhttp)) {
118 xmlhttp.open("GET", query_str, true);
119 xmlhttp.onreadystatechange=feedlist_callback;
120 xmlhttp.send(null);
121 } else {
122 printLockingError();
123 }
124 }
125
126 /*
127 function catchupPage(feed) {
128
129 if (!xmlhttp_ready(xmlhttp)) {
130 printLockingError();
131 return
132 }
133
134 var content = document.getElementById("headlinesList");
135
136 var rows = new Array();
137
138 for (i = 0; i < content.rows.length; i++) {
139 var row_id = content.rows[i].id.replace("RROW-", "");
140 if (row_id.length > 0) {
141 if (content.rows[i].className.match("Unread")) {
142 rows.push(row_id);
143 content.rows[i].className = content.rows[i].className.replace("Unread", "");
144 }
145
146 var upd_img_pic = document.getElementById("FUPDPIC-" + row_id);
147 if (upd_img_pic) {
148 upd_img_pic.innerHTML = "";
149 }
150 }
151 }
152
153 if (rows.length > 0) {
154
155 var feedr = document.getElementById("FEEDR-" + feed);
156 var feedu = document.getElementById("FEEDU-" + feed);
157
158 feedu.innerHTML = feedu.innerHTML - rows.length;
159
160 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
161 feedr.className = feedr.className + "Unread";
162 } else if (feedu.innerHTML <= 0) {
163 feedr.className = feedr.className.replace("Unread", "");
164 }
165
166 var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" +
167 param_escape(rows.toString());
168
169 notify("Marking this page as read...");
170
171 var button = document.getElementById("btnCatchupPage");
172
173 if (button) {
174 button.className = "disabledButton";
175 button.href = "";
176 }
177
178 xmlhttp.open("GET", query_str, true);
179 xmlhttp.onreadystatechange=notify_callback;
180 xmlhttp.send(null);
181
182 } else {
183 notify("No unread items on this page.");
184
185 }
186 } */
187
188 function catchupAllFeeds() {
189
190 if (!xmlhttp_ready(xmlhttp)) {
191 printLockingError();
192 return
193 }
194 var query_str = "backend.php?op=feeds&subop=catchupAll";
195
196 notify("Marking all feeds as read...");
197
198 xmlhttp.open("GET", query_str, true);
199 xmlhttp.onreadystatechange=feedlist_callback;
200 xmlhttp.send(null);
201
202 }
203
204 function viewCurrentFeed(skip, subop) {
205 if (active_feed_id) {
206 viewfeed(active_feed_id, skip, subop);
207 }
208 }
209
210 function viewfeed(feed, skip, subop) {
211
212 enableHotkeys();
213
214 var searchbox = document.getElementById("searchbox");
215
216 if (searchbox) {
217 search_query = searchbox.value;
218 } else {
219 search_query = "";
220 }
221
222 var viewbox = document.getElementById("viewbox");
223
224 var view_mode;
225
226 if (viewbox) {
227 view_mode = viewbox.value;
228 } else {
229 view_mode = "All Posts";
230 }
231
232 var limitbox = document.getElementById("limitbox");
233
234 var limit;
235
236 if (limitbox) {
237 limit = limitbox.value;
238 } else {
239 limit = "All";
240 }
241
242 if (skip < 0 || skip > total_feed_entries) {
243 return;
244 }
245
246 if (!xmlhttp_ready(xmlhttp)) {
247 printLockingError();
248 return
249 }
250
251 if (active_feed_id != feed || skip != active_offset) {
252 active_post_id = false;
253 }
254
255 active_feed_id = feed;
256 active_offset = skip;
257
258 if (subop == "MarkAllRead") {
259
260 var feedr = document.getElementById("FEEDR-" + feed);
261 var feedt = document.getElementById("FEEDT-" + feed);
262 var feedu = document.getElementById("FEEDU-" + feed);
263
264 feedu.innerHTML = "0";
265
266 if (feedr.className.match("Unread")) {
267 feedr.className = feedr.className.replace("Unread", "");
268 }
269 }
270
271 var query = "backend.php?op=viewfeed&feed=" + param_escape(feed) +
272 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) +
273 "&view=" + param_escape(view_mode) + "&limit=" + limit;
274
275 if (search_query != "") {
276 query = query + "&search=" + param_escape(search_query);
277 }
278
279 var headlines_frame = document.getElementById("headlines-frame");
280
281 headlines_frame.src = query + "&addheader=true";
282
283 var feedr = document.getElementById("FEEDR-" + feed);
284
285 cleanSelected("feedsList");
286 feedr.className = feedr.className + "Selected";
287
288 var ftitle_d = document.getElementById("headlinesTitle");
289 var ftitle_s = document.getElementById("FEEDN-" + feed);
290
291 ftitle_d.innerHTML = ftitle_s.innerHTML;
292
293 notify("");
294
295 }
296
297 function timeout() {
298 scheduleFeedUpdate(true);
299 setTimeout("timeout()", 1800*1000);
300 }
301
302 function resetSearch() {
303 document.getElementById("searchbox").value = "";
304 viewfeed(active_feed_id, 0, "");
305 }
306
307 function search() {
308 if (active_feed_id) {
309 viewfeed(active_feed_id, 0, "");
310 } else {
311 notify("Please select some feed first.");
312 }
313 }
314
315 function localPiggieFunction(enable) {
316 if (enable) {
317 var query_str = "backend.php?op=feeds&subop=piggie";
318
319 if (xmlhttp_ready(xmlhttp)) {
320
321 xmlhttp.open("GET", query_str, true);
322 xmlhttp.onreadystatechange=feedlist_callback;
323 xmlhttp.send(null);
324 }
325 }
326 }
327
328 /*
329 function moveToPost(mode) {
330
331 var rows = getVisibleHeadlineIds();
332
333 var prev_id;
334 var next_id;
335
336 if (active_post_id == false) {
337 next_id = getFirstVisibleHeadlineId();
338 prev_id = getLastVisibleHeadlineId();
339 } else {
340 for (var i = 0; i < rows.length; i++) {
341 if (rows[i] == active_post_id) {
342 prev_id = rows[i-1];
343 next_id = rows[i+1];
344 }
345 }
346 }
347
348 if (mode == "next") {
349 if (next_id != undefined) {
350 view(next_id, active_feed_id);
351 } else {
352 _viewfeed_autoselect_first = true;
353 viewfeed(active_feed_id, active_offset+15);
354 }
355 }
356
357 if (mode == "prev") {
358 if ( prev_id != undefined) {
359 view(prev_id, active_feed_id);
360 } else {
361 _viewfeed_autoselect_last = true;
362 viewfeed(active_feed_id, active_offset-15);
363 }
364 }
365
366 }
367 */
368
369 function localHotkeyHandler(keycode) {
370
371 /* if (keycode == 78) {
372 return moveToPost('next');
373 }
374
375 if (keycode == 80) {
376 return moveToPost('prev');
377 } */
378
379 if (keycode == 82) {
380 return scheduleFeedUpdate(true);
381 }
382
383 if (keycode == 85) {
384 return viewfeed(active_feed_id, active_offset, "ForceUpdate");
385 }
386
387 // notify("KC: " + keycode);
388
389 }
390
391 function init() {
392
393 // IE kludge
394
395 if (xmlhttp && !xmlhttp_rpc) {
396 xmlhttp_rpc = xmlhttp;
397 xmlhttp_view = xmlhttp;
398 }
399
400 if (!xmlhttp || !xmlhttp_rpc || !xmlhttp_view) {
401 document.getElementById("headlines").innerHTML =
402 "<b>Fatal error:</b> This program needs XmlHttpRequest " +
403 "to function properly. Your browser doesn't seem to support it.";
404 return;
405 }
406
407 updateFeedList(false, false);
408 document.onkeydown = hotkey_handler;
409 setTimeout("timeout()", 1800*1000);
410
411 var content = document.getElementById("content");
412 }