]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
optional support for showing content in an iframe
[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 _viewfeed_autoselect_first = false;
20 var _viewfeed_autoselect_last = false;
21
22 var _update_ids;
23 var _update_num_ids;
24
25 var search_query = "";
26
27 /*@cc_on @*/
28 /*@if (@_jscript_version >= 5)
29 // JScript gives us Conditional compilation, we can cope with old IE versions.
30 // and security blocked creation of the objects.
31 try {
32 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
33 } catch (e) {
34 try {
35 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
36 xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
37 xmlhttp_view = new ActiveXObject("Microsoft.XMLHTTP");
38 } catch (E) {
39 xmlhttp = false;
40 xmlhttp_rpc = false;
41 xmlhttp_view = false;
42 }
43 }
44 @end @*/
45
46 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
47 xmlhttp = new XMLHttpRequest();
48 xmlhttp_rpc = new XMLHttpRequest();
49 xmlhttp_view = new XMLHttpRequest();
50 }
51
52 function feedlist_callback() {
53 var container = document.getElementById('feeds');
54 if (xmlhttp.readyState == 4) {
55 container.innerHTML=xmlhttp.responseText;
56
57 if (first_run) {
58 scheduleFeedUpdate(false);
59 first_run = false;
60 } else {
61 notify("");
62 }
63 }
64 }
65
66 function feed_update_callback() {
67 if (xmlhttp_rpc.readyState == 4) {
68 var result = xmlhttp_rpc.responseText;
69 notify(_update_ids);
70 updateFeed(_update_ids.shift());
71 }
72 }
73
74 function viewfeed_callback() {
75 var container = document.getElementById('headlines');
76 if (xmlhttp.readyState == 4) {
77 container.innerHTML = xmlhttp.responseText;
78
79 var factive = document.getElementById("FACTIVE");
80 var funread = document.getElementById("FUNREAD");
81 var ftotal = document.getElementById("FTOTAL");
82
83 if (_viewfeed_autoselect_first == true) {
84 _viewfeed_autoselect_first = false;
85 view(getFirstVisibleHeadlineId(), active_feed_id);
86 }
87
88 if (_viewfeed_autoselect_last == true) {
89 _viewfeed_autoselect_last = false;
90 view(getLastVisibleHeadlineId(), active_feed_id);
91 }
92
93 if (ftotal && factive && funread) {
94 var feed_id = factive.innerHTML;
95
96 var feedr = document.getElementById("FEEDR-" + feed_id);
97 var feedt = document.getElementById("FEEDT-" + feed_id);
98 var feedu = document.getElementById("FEEDU-" + feed_id);
99
100 feedt.innerHTML = ftotal.innerHTML;
101 feedu.innerHTML = funread.innerHTML;
102
103 total_feed_entries = ftotal.innerHTML;
104
105 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
106 feedr.className = feedr.className + "Unread";
107 } else if (feedu.innerHTML <= 0) {
108 feedr.className = feedr.className.replace("Unread", "");
109 }
110
111 cleanSelected("feedsList");
112
113 feedr.className = feedr.className + "Selected";
114 }
115
116 var searchbox = document.getElementById("searchbox");
117 searchbox.value = search_query;
118
119 markHeadline(active_post_id);
120
121 notify("");
122
123 }
124 }
125
126 function view_callback() {
127 var container = document.getElementById('content');
128 if (xmlhttp_view.readyState == 4) {
129 container.innerHTML=xmlhttp_view.responseText;
130 markHeadline(active_post_id);
131 }
132 }
133
134 function refetch_callback() {
135
136 if (xmlhttp_rpc.readyState == 4) {
137 notify("All feeds updated");
138 var container = document.getElementById('feeds');
139 container.innerHTML = xmlhttp_rpc.responseText;
140 document.title = "Tiny Tiny RSS";
141 }
142 }
143
144 function updateFeed(feed_id) {
145
146 var query_str = "backend.php?op=rpc&subop=updateFeed&feed=" + feed_id;
147
148 if (xmlhttp_ready(xmlhttp_rpc)) {
149 xmlhttp_rpc.open("GET", query_str, true);
150 xmlhttp_rpc.onreadystatechange=feed_update_callback;
151 xmlhttp_rpc.send(null);
152 } else {
153 printLockingError();
154 }
155
156 }
157
158 function scheduleSepFeedUpdate(force) {
159 notify("Updating feeds in background (M2)...");
160
161 _update_ids = getFeedIds();
162 _update_num_ids = _update_ids.length;
163
164 updateFeed(_update_ids.pop());
165
166 }
167
168 function scheduleFeedUpdate(force) {
169
170 notify("Updating feeds in background...");
171
172 document.title = "Tiny Tiny RSS - Updating...";
173
174 var query_str = "backend.php?op=rpc&subop=";
175
176 if (force) {
177 query_str = query_str + "forceUpdateAllFeeds";
178 } else {
179 query_str = query_str + "updateAllFeeds";
180 }
181
182 if (xmlhttp_ready(xmlhttp_rpc)) {
183 xmlhttp_rpc.open("GET", query_str, true);
184 xmlhttp_rpc.onreadystatechange=refetch_callback;
185 xmlhttp_rpc.send(null);
186 } else {
187 printLockingError();
188 }
189 }
190
191 function updateFeedList(silent, fetch) {
192
193 if (silent != true) {
194 notify("Loading feed list...");
195 }
196
197 var query_str = "backend.php?op=feeds";
198
199 if (fetch) query_str = query_str + "&fetch=yes";
200
201 if (xmlhttp_ready(xmlhttp)) {
202 xmlhttp.open("GET", query_str, true);
203 xmlhttp.onreadystatechange=feedlist_callback;
204 xmlhttp.send(null);
205 } else {
206 printLockingError();
207 }
208 }
209
210 function catchupPage(feed) {
211
212 if (!xmlhttp_ready(xmlhttp)) {
213 printLockingError();
214 return
215 }
216
217 var content = document.getElementById("headlinesList");
218
219 var rows = new Array();
220
221 for (i = 0; i < content.rows.length; i++) {
222 var row_id = content.rows[i].id.replace("RROW-", "");
223 if (row_id.length > 0) {
224 if (content.rows[i].className.match("Unread")) {
225 rows.push(row_id);
226 content.rows[i].className = content.rows[i].className.replace("Unread", "");
227 }
228
229 var upd_img_pic = document.getElementById("FUPDPIC-" + row_id);
230 if (upd_img_pic) {
231 upd_img_pic.innerHTML = "";
232 }
233 }
234 }
235
236 if (rows.length > 0) {
237
238 var feedr = document.getElementById("FEEDR-" + feed);
239 var feedu = document.getElementById("FEEDU-" + feed);
240
241 feedu.innerHTML = feedu.innerHTML - rows.length;
242
243 if (feedu.innerHTML > 0 && !feedr.className.match("Unread")) {
244 feedr.className = feedr.className + "Unread";
245 } else if (feedu.innerHTML <= 0) {
246 feedr.className = feedr.className.replace("Unread", "");
247 }
248
249 var query_str = "backend.php?op=rpc&subop=catchupPage&ids=" +
250 param_escape(rows.toString());
251
252 notify("Marking this page as read...");
253
254 var button = document.getElementById("btnCatchupPage");
255
256 if (button) {
257 button.className = "disabledButton";
258 button.href = "";
259 }
260
261 xmlhttp.open("GET", query_str, true);
262 xmlhttp.onreadystatechange=notify_callback;
263 xmlhttp.send(null);
264
265 } else {
266 notify("No unread items on this page.");
267
268 }
269 }
270
271 function catchupAllFeeds() {
272
273 if (!xmlhttp_ready(xmlhttp)) {
274 printLockingError();
275 return
276 }
277 var query_str = "backend.php?op=feeds&subop=catchupAll";
278
279 notify("Marking all feeds as read...");
280
281 xmlhttp.open("GET", query_str, true);
282 xmlhttp.onreadystatechange=feedlist_callback;
283 xmlhttp.send(null);
284
285 }
286
287 function viewfeed(feed, skip, subop) {
288
289 enableHotkeys();
290
291 var searchbox = document.getElementById("searchbox");
292
293 if (searchbox) {
294 search_query = searchbox.value;
295 } else {
296 search_query = "";
297 }
298
299 var viewbox = document.getElementById("viewbox");
300
301 var view_mode;
302
303 if (viewbox) {
304 view_mode = viewbox.value;
305 } else {
306 view_mode = "All Posts";
307 }
308
309 /* if (active_feed_id == feed && subop != "ForceUpdate") {
310 notify("This feed is currently selected.");
311 return;
312 } */
313
314 if (skip < 0 || skip > total_feed_entries) {
315 return;
316 }
317
318 if (!xmlhttp_ready(xmlhttp)) {
319 printLockingError();
320 return
321 }
322
323 if (active_feed_id != feed || skip != active_offset) {
324 active_post_id = false;
325 }
326
327 active_feed_id = feed;
328 active_offset = skip;
329
330 var query = "backend.php?op=viewfeed&feed=" + param_escape(feed) +
331 "&skip=" + param_escape(skip) + "&subop=" + param_escape(subop) +
332 "&view=" + param_escape(view_mode);
333
334 if (search_query != "") {
335 query = query + "&search=" + param_escape(search_query);
336 }
337
338 xmlhttp.open("GET", query, true);
339 xmlhttp.onreadystatechange=viewfeed_callback;
340 xmlhttp.send(null);
341
342 notify("Loading headlines...");
343
344 }
345
346 function markHeadline(id) {
347 var row = document.getElementById("RROW-" + id);
348 if (row) {
349 row.className = row.className + "Selected";
350 }
351 }
352
353 function getFeedIds() {
354 var content = document.getElementById("feedsList");
355
356 var rows = new Array();
357
358 for (i = 0; i < content.rows.length; i++) {
359 var id = content.rows[i].id.replace("FEEDR-", "");
360 if (id.length > 0) {
361 rows.push(id);
362 }
363 }
364
365 return rows;
366 }
367
368 function cleanSelected(element) {
369 var content = document.getElementById(element);
370
371 var rows = new Array();
372
373 for (i = 0; i < content.rows.length; i++) {
374 content.rows[i].className = content.rows[i].className.replace("Selected", "");
375 }
376
377 }
378
379 function view(id,feed_id) {
380
381 enableHotkeys();
382
383 if (!xmlhttp_ready(xmlhttp_view)) {
384 printLockingError();
385 return
386 }
387
388 var crow = document.getElementById("RROW-" + id);
389
390 if (crow.className.match("Unread")) {
391 var umark = document.getElementById("FEEDU-" + feed_id);
392 umark.innerHTML = umark.innerHTML - 1;
393 crow.className = crow.className.replace("Unread", "");
394
395 if (umark.innerHTML == "0") {
396 var feedr = document.getElementById("FEEDR-" + feed_id);
397 feedr.className = feedr.className.replace("Unread", "");
398 }
399
400 total_unread--;
401 }
402
403 cleanSelected("headlinesList");
404 // crow.className = crow.className + "Selected";
405
406 var upd_img_pic = document.getElementById("FUPDPIC-" + id);
407
408 if (upd_img_pic) {
409 upd_img_pic.innerHTML = "";
410 }
411
412 // document.getElementById('content').innerHTML='Loading, please wait...';
413
414 var unread_rows = getVisibleUnreadHeadlines();
415
416 if (unread_rows.length == 0) {
417 var button = document.getElementById("btnCatchupPage");
418 if (button) {
419 button.className = "disabledButton";
420 button.href = "";
421 }
422 }
423
424 active_post_id = id;
425
426 var content = document.getElementById("content-frame");
427
428 if (content) {
429 content.src = "backend.php?op=view&addheader=true&id=" + param_escape(id);
430 markHeadline(active_post_id);
431 } else {
432 xmlhttp_view.open("GET", "backend.php?op=view&id=" + param_escape(id), true);
433 xmlhttp_view.onreadystatechange=view_callback;
434 xmlhttp_view.send(null);
435 }
436 }
437
438 function timeout() {
439 scheduleFeedUpdate(true);
440 setTimeout("timeout()", 1800*1000);
441 }
442
443 function resetSearch() {
444 document.getElementById("searchbox").value = "";
445 viewfeed(active_feed_id, 0, "");
446 }
447
448 function search(feed) {
449 viewfeed(feed, 0, "");
450 }
451
452 function localPiggieFunction(enable) {
453 if (enable) {
454 var query_str = "backend.php?op=feeds&subop=piggie";
455
456 if (xmlhttp_ready(xmlhttp)) {
457
458 xmlhttp.open("GET", query_str, true);
459 xmlhttp.onreadystatechange=feedlist_callback;
460 xmlhttp.send(null);
461 }
462 }
463 }
464
465 function relativeid_callback() {
466
467 if (xmlhttp_rpc.readyState == 4) {
468 notify(xmlhttp_rpc.responseText);
469 }
470
471 }
472
473 function getVisibleUnreadHeadlines() {
474 var content = document.getElementById("headlinesList");
475
476 var rows = new Array();
477
478 for (i = 0; i < content.rows.length; i++) {
479 var row_id = content.rows[i].id.replace("RROW-", "");
480 if (row_id.length > 0 && content.rows[i].className.match("Unread")) {
481 rows.push(row_id);
482 }
483 }
484 return rows;
485 }
486
487 function getVisibleHeadlineIds() {
488
489 var content = document.getElementById("headlinesList");
490
491 var rows = new Array();
492
493 for (i = 0; i < content.rows.length; i++) {
494 var row_id = content.rows[i].id.replace("RROW-", "");
495 if (row_id.length > 0) {
496 rows.push(row_id);
497 }
498 }
499 return rows;
500 }
501
502 function getFirstVisibleHeadlineId() {
503 var rows = getVisibleHeadlineIds();
504 return rows[0];
505 }
506
507 function getLastVisibleHeadlineId() {
508 var rows = getVisibleHeadlineIds();
509 return rows[rows.length-1];
510 }
511
512 function moveToPost(mode) {
513
514 var rows = getVisibleHeadlineIds();
515
516 var prev_id;
517 var next_id;
518
519 if (active_post_id == false) {
520 next_id = getFirstVisibleHeadlineId();
521 prev_id = getLastVisibleHeadlineId();
522 } else {
523 for (var i = 0; i < rows.length; i++) {
524 if (rows[i] == active_post_id) {
525 prev_id = rows[i-1];
526 next_id = rows[i+1];
527 }
528 }
529 }
530
531 if (mode == "next") {
532 if (next_id != undefined) {
533 view(next_id, active_feed_id);
534 } else {
535 _viewfeed_autoselect_first = true;
536 viewfeed(active_feed_id, active_offset+15);
537 }
538 }
539
540 if (mode == "prev") {
541 if ( prev_id != undefined) {
542 view(prev_id, active_feed_id);
543 } else {
544 _viewfeed_autoselect_last = true;
545 viewfeed(active_feed_id, active_offset-15);
546 }
547 }
548
549 }
550
551 function localHotkeyHandler(keycode) {
552
553 if (keycode == 78) {
554 return moveToPost('next');
555 }
556
557 if (keycode == 80) {
558 return moveToPost('prev');
559 }
560
561 if (keycode == 82) {
562 return scheduleFeedUpdate(true);
563 }
564
565 if (keycode == 85) {
566 return viewfeed(active_feed_id, active_offset, "ForceUpdate");
567 }
568
569 // notify("KC: " + keycode);
570
571 }
572
573 function toggleMark(id, toggle) {
574
575 // notify("Toggle mark: " + id + ", " + toggle);
576
577 if (!xmlhttp_ready(xmlhttp_rpc)) {
578 printLockingError();
579 return;
580 }
581
582 var mark_img = document.getElementById("FMARKPIC-" + id);
583
584 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
585
586 if (toggle == true) {
587 mark_img.src = "images/mark_set.png";
588 mark_img.alt = "Reset mark";
589 mark_img.setAttribute('onclick', 'javascript:toggleMark('+id+', false)');
590 query = query + "&mark=1";
591 } else {
592 mark_img.src = "images/mark_unset.png";
593 mark_img.alt = "Set mark";
594 mark_img.setAttribute('onclick', 'javascript:toggleMark('+id+', true)');
595 query = query + "&mark=0";
596 }
597
598 xmlhttp_rpc.open("GET", query, true);
599 xmlhttp_rpc.onreadystatechange=rpc_notify_callback;
600 xmlhttp_rpc.send(null);
601
602 }
603
604 function init() {
605
606 // IE kludge
607
608 if (xmlhttp && !xmlhttp_rpc) {
609 xmlhttp_rpc = xmlhttp;
610 xmlhttp_view = xmlhttp;
611 }
612
613 if (!xmlhttp || !xmlhttp_rpc || !xmlhttp_view) {
614 document.getElementById("headlines").innerHTML =
615 "<b>Fatal error:</b> This program needs XmlHttpRequest " +
616 "to function properly. Your browser doesn't seem to support it.";
617 return;
618 }
619
620 updateFeedList(false, false);
621 document.onkeydown = hotkey_handler;
622 setTimeout("timeout()", 1800*1000);
623
624 var content = document.getElementById("content");
625 }