]> git.wh0rd.org - tt-rss.git/blob - viewfeed.js
prefetch old articles when necessary
[tt-rss.git] / viewfeed.js
1 var active_post_id = false;
2 var _catchup_callback_func = false;
3 var last_article_view = false;
4 var active_real_feed_id = false;
5
6 var _tag_active_post_id = false;
7 var _tag_active_feed_id = false;
8 var _tag_active_cdm = false;
9
10 // FIXME: kludge, to restore scrollTop after tag editor terminates
11 var _tag_cdm_scroll = false;
12
13 // FIXME: kludges, needs proper implementation
14 var _reload_feedlist_after_view = false;
15
16 var _cdm_wd_timeout = false;
17 var _cdm_wd_vishist = new Array();
18
19 var article_cache = new Array();
20
21 function catchup_callback() {
22 if (xmlhttp_rpc.readyState == 4) {
23 try {
24 debug("catchup_callback");
25 if (_catchup_callback_func) {
26 setTimeout(_catchup_callback_func, 100);
27 }
28 notify("");
29 all_counters_callback();
30 } catch (e) {
31 exception_error("catchup_callback", e);
32 }
33 }
34 }
35
36 function headlines_callback() {
37 if (xmlhttp.readyState == 4) {
38 debug("headlines_callback");
39 var f = document.getElementById("headlines-frame");
40 try {
41 f.scrollTop = 0;
42 } catch (e) { };
43
44 if (xmlhttp.responseXML) {
45 var headlines = xmlhttp.responseXML.getElementsByTagName("headlines")[0];
46 var counters = xmlhttp.responseXML.getElementsByTagName("counters")[0];
47
48 f.innerHTML = headlines.firstChild.nodeValue;
49
50 if (counters) {
51 debug("parsing piggybacked counters: " + counters);
52 parse_counters(counters, false);
53 } else {
54 debug("counters container not found in reply");
55 }
56 } else {
57 debug("headlines_callback: returned no XML object");
58 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
59 }
60
61 if (typeof correctPNG != 'undefined') {
62 correctPNG();
63 }
64
65 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
66
67 if (!document.getElementById("headlinesList") &&
68 getInitParam("cdm_auto_catchup") == 1) {
69 debug("starting CDM watchdog");
70 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
71 _cdm_wd_vishist = new Array();
72 } else {
73 debug("not in CDM mode or watchdog disabled");
74 }
75
76 if (_tag_cdm_scroll) {
77 try {
78 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
79 _tag_cdm_scroll = false;
80 } catch (e) { }
81 }
82
83 notify("");
84 }
85 }
86
87 function render_article(article) {
88 try {
89 var f = document.getElementById("content-frame");
90 try {
91 f.scrollTop = 0;
92 } catch (e) { };
93
94 f.innerHTML = article;
95
96 } catch (e) {
97 exception_error("render_article", e);
98 }
99 }
100
101 function article_callback() {
102 if (xmlhttp.readyState == 4) {
103 debug("article_callback");
104
105 try {
106 if (xmlhttp.responseXML) {
107 var reply = xmlhttp.responseXML.firstChild.firstChild;
108
109 var articles = xmlhttp.responseXML.getElementsByTagName("article");
110
111 for (var i = 0; i < articles.length; i++) {
112 var a_id = articles[i].getAttribute("id");
113
114 debug("found id: " + a_id);
115
116 if (a_id == active_post_id) {
117 debug("active article, rendering...");
118 render_article(articles[i].firstChild.nodeValue);
119 }
120
121 cache_inject(a_id, articles[i].firstChild.nodeValue);
122 }
123
124 } else {
125 debug("article_callback: returned no XML object");
126 f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
127 }
128 } catch (e) {
129 exception_error("article_callback", e);
130 }
131
132 var date = new Date();
133 last_article_view = date.getTime() / 1000;
134
135 if (typeof correctPNG != 'undefined') {
136 correctPNG();
137 }
138
139 if (_reload_feedlist_after_view) {
140 setTimeout('updateFeedList(false, false)', 50);
141 _reload_feedlist_after_view = false;
142 } else {
143 var counters = xmlhttp.responseXML.getElementsByTagName("counters")[0];
144
145 if (counters) {
146 debug("parsing piggybacked counters: " + counters);
147 parse_counters(counters, false);
148 } else {
149 debug("counters container not found in reply");
150 }
151 }
152
153 notify("");
154 }
155 }
156
157 function view(id, feed_id, skip_history) {
158
159 try {
160 debug("loading article: " + id + "/" + feed_id);
161
162 active_real_feed_id = feed_id;
163
164 var cached_article = cache_find(id);
165
166 debug("cache check result: " + (cached_article != false));
167
168 /* if (!skip_history) {
169 history_push("ARTICLE:" + id + ":" + feed_id);
170 } */
171
172 enableHotkeys();
173
174 active_post_id = id;
175 //setActiveFeedId(feed_id);
176
177 var query = "backend.php?op=view&id=" + param_escape(id) +
178 "&feed=" + param_escape(feed_id);
179
180 var date = new Date();
181
182 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
183 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
184 xmlhttp.abort();
185 }
186
187 if (cached_article || xmlhttp_ready(xmlhttp)) {
188
189 cleanSelected("headlinesList");
190
191 var crow = document.getElementById("RROW-" + active_post_id);
192
193 var article_is_unread = crow.className.match("Unread");
194 debug("article is unread: " + article_is_unread);
195
196 crow.className = crow.className.replace("Unread", "");
197
198 var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
199
200 if (upd_img_pic) {
201 upd_img_pic.src = "images/blank_icon.gif";
202 }
203
204 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
205 markHeadline(active_post_id);
206
207 var neighbor_ids = getRelativePostIds(active_post_id);
208
209 /* only request uncached articles */
210
211 var cids_to_request = Array();
212
213 for (var i = 0; i < neighbor_ids.length; i++) {
214 if (!cache_check(neighbor_ids[i])) {
215 cids_to_request.push(neighbor_ids[i]);
216 }
217 }
218
219 debug("additional ids: " + cids_to_request.toString());
220
221 /* additional info for piggyback counters */
222
223 if (tagsAreDisplayed()) {
224 query = query + "&omode=lt";
225 } else {
226 query = query + "&omode=flc";
227 }
228
229 var date = new Date();
230 var timestamp = Math.round(date.getTime() / 1000);
231 query = query + "&ts=" + timestamp;
232
233 query = query + "&cids=" + cids_to_request.toString();
234
235 if (!cached_article) {
236
237 notify_progress("Loading, please wait...");
238
239 debug(query);
240
241 xmlhttp.open("GET", query, true);
242 xmlhttp.onreadystatechange=article_callback;
243 xmlhttp.send(null);
244 } else if (cached_article && article_is_unread) {
245
246 query = query + "&mode=prefetch";
247
248 debug(query);
249
250 xmlhttp.open("GET", query, true);
251 xmlhttp.onreadystatechange=article_callback;
252 xmlhttp.send(null);
253
254 render_article(cached_article);
255
256 } else if (cached_article) {
257
258 query = query + "&mode=prefetch_old";
259
260 debug(query);
261
262 xmlhttp.open("GET", query, true);
263 xmlhttp.onreadystatechange=article_callback;
264 xmlhttp.send(null);
265
266 render_article(cached_article);
267
268 }
269
270 cache_expire();
271
272 } else {
273 debug("xmlhttp busy (@view)");
274 printLockingError();
275 }
276
277 } catch (e) {
278 exception_error("view", e);
279 }
280 }
281
282 function toggleMark(id) {
283
284 if (!xmlhttp_ready(xmlhttp_rpc)) {
285 printLockingError();
286 return;
287 }
288
289 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
290
291 var mark_img = document.getElementById("FMARKPIC-" + id);
292 var vfeedu = document.getElementById("FEEDU--1");
293 var crow = document.getElementById("RROW-" + id);
294
295 if (mark_img.alt != "Reset mark") {
296 mark_img.src = "images/mark_set.png";
297 mark_img.alt = "Reset mark";
298 query = query + "&mark=1";
299
300 if (vfeedu && crow.className.match("Unread")) {
301 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
302 }
303
304 } else {
305 mark_img.src = "images/mark_unset.png";
306 mark_img.alt = "Set mark";
307 query = query + "&mark=0";
308
309 if (vfeedu && crow.className.match("Unread")) {
310 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
311 }
312
313 }
314
315 var vfeedctr = document.getElementById("FEEDCTR--1");
316 var vfeedr = document.getElementById("FEEDR--1");
317
318 if (vfeedu && vfeedctr) {
319 if ((+vfeedu.innerHTML) > 0) {
320 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
321 vfeedr.className = vfeedr.className + "Unread";
322 vfeedctr.className = "odd";
323 }
324 } else {
325 vfeedctr.className = "invisible";
326 vfeedr.className = vfeedr.className.replace("Unread", "");
327 }
328 }
329
330 debug("toggle starred for aid " + id);
331
332 new Ajax.Request(query);
333
334 }
335
336 function moveToPost(mode) {
337
338 // check for combined mode
339 if (!document.getElementById("headlinesList"))
340 return;
341
342 var rows = getVisibleHeadlineIds();
343
344 var prev_id;
345 var next_id;
346
347 if (!document.getElementById('RROW-' + active_post_id)) {
348 active_post_id = false;
349 }
350
351 if (active_post_id == false) {
352 next_id = getFirstVisibleHeadlineId();
353 prev_id = getLastVisibleHeadlineId();
354 } else {
355 for (var i = 0; i < rows.length; i++) {
356 if (rows[i] == active_post_id) {
357 prev_id = rows[i-1];
358 next_id = rows[i+1];
359 }
360 }
361 }
362
363 if (mode == "next") {
364 if (next_id != undefined) {
365 view(next_id, getActiveFeedId());
366 }
367 }
368
369 if (mode == "prev") {
370 if ( prev_id != undefined) {
371 view(prev_id, getActiveFeedId());
372 }
373 }
374 }
375
376 function toggleUnread(id, cmode) {
377 try {
378 if (!xmlhttp_ready(xmlhttp_rpc)) {
379 printLockingError();
380 return;
381 }
382
383 var row = document.getElementById("RROW-" + id);
384 if (row) {
385 var nc = row.className;
386 nc = nc.replace("Unread", "");
387 nc = nc.replace("Selected", "");
388
389 if (row.className.match("Unread")) {
390 row.className = nc;
391 } else {
392 row.className = nc + "Unread";
393 }
394
395 if (!cmode) cmode = 2;
396
397 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
398 param_escape(id) + "&cmode=" + param_escape(cmode);
399
400 notify_progress("Loading, please wait...");
401
402 xmlhttp_rpc.open("GET", query, true);
403 xmlhttp_rpc.onreadystatechange=all_counters_callback;
404 xmlhttp_rpc.send(null);
405
406 }
407
408
409 } catch (e) {
410 exception_error("toggleUnread", e);
411 }
412 }
413
414 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
415 try {
416 if (!xmlhttp_ready(xmlhttp_rpc)) {
417 printLockingError();
418 return;
419 }
420
421 var rows;
422
423 if (cdm_mode) {
424 rows = cdmGetSelectedArticles();
425 } else {
426 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
427 }
428
429 if (rows.length == 0 && !no_error) {
430 alert(__("No articles are selected."));
431 return;
432 }
433
434 for (i = 0; i < rows.length; i++) {
435 var row = document.getElementById("RROW-" + rows[i]);
436 if (row) {
437 var nc = row.className;
438 nc = nc.replace("Unread", "");
439 nc = nc.replace("Selected", "");
440
441 if (row.className.match("Unread")) {
442 row.className = nc + "Selected";
443 } else {
444 row.className = nc + "UnreadSelected";
445 }
446 }
447 }
448
449 if (rows.length > 0) {
450
451 var cmode = "";
452
453 if (set_state == undefined) {
454 cmode = "2";
455 } else if (set_state == true) {
456 cmode = "1";
457 } else if (set_state == false) {
458 cmode = "0";
459 }
460
461 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
462 param_escape(rows.toString()) + "&cmode=" + cmode;
463
464 _catchup_callback_func = callback_func;
465
466 notify_progress("Loading, please wait...");
467
468 xmlhttp_rpc.open("GET", query, true);
469 xmlhttp_rpc.onreadystatechange=catchup_callback;
470 xmlhttp_rpc.send(null);
471
472 }
473
474 } catch (e) {
475 exception_error("selectionToggleUnread", e);
476 }
477 }
478
479 function selectionToggleMarked(cdm_mode) {
480 try {
481 if (!xmlhttp_ready(xmlhttp_rpc)) {
482 printLockingError();
483 return;
484 }
485
486 var rows;
487
488 if (cdm_mode) {
489 rows = cdmGetSelectedArticles();
490 } else {
491 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
492 }
493
494 if (rows.length == 0) {
495 alert(__("No articles are selected."));
496 return;
497 }
498
499 for (i = 0; i < rows.length; i++) {
500 var row = document.getElementById("RROW-" + rows[i]);
501 var mark_img = document.getElementById("FMARKPIC-" + rows[i]);
502
503 if (row && mark_img) {
504
505 if (mark_img.alt == "Set mark") {
506 mark_img.src = "images/mark_set.png";
507 mark_img.alt = "Reset mark";
508 mark_img.setAttribute('onclick',
509 'javascript:toggleMark('+rows[i]+', false)');
510
511 } else {
512 mark_img.src = "images/mark_unset.png";
513 mark_img.alt = "Set mark";
514 mark_img.setAttribute('onclick',
515 'javascript:toggleMark('+rows[i]+', true)');
516 }
517 }
518 }
519
520 if (rows.length > 0) {
521
522 var query = "backend.php?op=rpc&subop=markSelected&ids=" +
523 param_escape(rows.toString()) + "&cmode=2";
524
525 xmlhttp_rpc.open("GET", query, true);
526 xmlhttp_rpc.onreadystatechange=all_counters_callback;
527 xmlhttp_rpc.send(null);
528
529 }
530
531 } catch (e) {
532 exception_error("selectionToggleMarked", e);
533 }
534 }
535
536 function cdmGetSelectedArticles() {
537 var sel_articles = new Array();
538 var container = document.getElementById("headlinesInnerContainer");
539
540 for (i = 0; i < container.childNodes.length; i++) {
541 var child = container.childNodes[i];
542
543 if (child.id.match("RROW-") && child.className.match("Selected")) {
544 var c_id = child.id.replace("RROW-", "");
545 sel_articles.push(c_id);
546 }
547 }
548
549 return sel_articles;
550 }
551
552 // mode = all,none,unread
553 function cdmSelectArticles(mode) {
554 var container = document.getElementById("headlinesInnerContainer");
555
556 for (i = 0; i < container.childNodes.length; i++) {
557 var child = container.childNodes[i];
558
559 if (child.id.match("RROW-")) {
560 var aid = child.id.replace("RROW-", "");
561
562 var cb = document.getElementById("RCHK-" + aid);
563
564 if (mode == "all") {
565 if (!child.className.match("Selected")) {
566 child.className = child.className + "Selected";
567 cb.checked = true;
568 }
569 } else if (mode == "unread") {
570 if (child.className.match("Unread") && !child.className.match("Selected")) {
571 child.className = child.className + "Selected";
572 cb.checked = true;
573 }
574 } else {
575 child.className = child.className.replace("Selected", "");
576 cb.checked = false;
577 }
578 }
579 }
580 }
581
582 function catchupPage() {
583
584 if (document.getElementById("headlinesList")) {
585 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
586 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
587 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
588 } else {
589 cdmSelectArticles('all');
590 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
591 cdmSelectArticles('none');
592 }
593 }
594
595 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
596
597 if (!xmlhttp_ready(xmlhttp_rpc)) {
598 printLockingError();
599 }
600
601 var title = prompt("Please enter label title:", "");
602
603 if (title) {
604
605 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
606 "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
607 "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) +
608 "&title=" + param_escape(title);
609
610 debug("LFS: " + query);
611
612 xmlhttp_rpc.open("GET", query, true);
613 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
614 xmlhttp_rpc.send(null);
615 }
616
617 }
618
619 function editArticleTags(id, feed_id, cdm_enabled) {
620 _tag_active_post_id = id;
621 _tag_active_feed_id = feed_id;
622 _tag_active_cdm = cdm_enabled;
623 try {
624 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
625 } catch (e) { }
626 displayDlg('editArticleTags', id);
627 }
628
629
630 function tag_saved_callback() {
631 if (xmlhttp_rpc.readyState == 4) {
632 try {
633 debug("in tag_saved_callback");
634
635 closeInfoBox();
636 notify("");
637
638 if (tagsAreDisplayed()) {
639 _reload_feedlist_after_view = true;
640 }
641
642 if (!_tag_active_cdm) {
643 if (active_post_id == _tag_active_post_id) {
644 debug("reloading current article");
645 view(_tag_active_post_id, _tag_active_feed_id);
646 }
647 } else {
648 debug("reloading current feed");
649 viewCurrentFeed();
650 }
651
652 } catch (e) {
653 exception_error("catchup_callback", e);
654 }
655 }
656 }
657
658 function editTagsSave() {
659
660 if (!xmlhttp_ready(xmlhttp_rpc)) {
661 printLockingError();
662 }
663
664 notify_progress("Saving article tags...");
665
666 var form = document.forms["tag_edit_form"];
667
668 var query = Form.serialize("tag_edit_form");
669
670 xmlhttp_rpc.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query, true);
671 xmlhttp_rpc.onreadystatechange=tag_saved_callback;
672 xmlhttp_rpc.send(null);
673
674 }
675
676 function editTagsInsert() {
677 try {
678
679 var form = document.forms["tag_edit_form"];
680
681 var found_tags = form.found_tags;
682 var tags_str = form.tags_str;
683
684 var tag = found_tags[found_tags.selectedIndex].value;
685
686 if (tags_str.value.length > 0 &&
687 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
688
689 tags_str.value = tags_str.value + ", ";
690 }
691
692 tags_str.value = tags_str.value + tag + ", ";
693
694 found_tags.selectedIndex = 0;
695
696 } catch (e) {
697 exception_error(e, "editTagsInsert");
698 }
699 }
700
701 function cdmWatchdog() {
702
703 try {
704
705 var ctr = document.getElementById("headlinesInnerContainer");
706
707 if (!ctr) return;
708
709 var ids = new Array();
710
711 var e = ctr.firstChild;
712
713 while (e) {
714 if (e.className && e.className == "cdmArticleUnread" && e.id &&
715 e.id.match("RROW-")) {
716
717 // article fits in viewport OR article is longer than viewport and
718 // its bottom is visible
719
720 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
721 ctr.scrollTop + ctr.offsetHeight) {
722
723 // debug(e.id + " is visible " + e.offsetTop + "." +
724 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
725 // (ctr.scrollTop + ctr.offsetHeight));
726
727 ids.push(e.id.replace("RROW-", ""));
728
729 } else if (e.offsetHeight > ctr.offsetHeight &&
730 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
731 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
732
733 ids.push(e.id.replace("RROW-", ""));
734
735 }
736
737 // method 2: article bottom is visible and is in upper 1/2 of the viewport
738
739 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
740 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
741
742 ids.push(e.id.replace("RROW-", ""));
743
744 } */
745
746 }
747
748 e = e.nextSibling;
749 }
750
751 debug("cdmWatchdog, ids= " + ids.toString());
752
753 if (ids.length > 0 && xmlhttp_ready(xmlhttp_rpc)) {
754
755 for (var i = 0; i < ids.length; i++) {
756 var e = document.getElementById("RROW-" + ids[i]);
757 if (e) {
758 e.className = e.className.replace("Unread", "");
759 }
760 }
761
762 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
763 param_escape(ids.toString()) + "&cmode=0";
764
765 xmlhttp_rpc.open("GET", query, true);
766 xmlhttp_rpc.onreadystatechange=all_counters_callback;
767 xmlhttp_rpc.send(null);
768
769 }
770
771 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
772
773 } catch (e) {
774 exception_error(e, "cdmWatchdog");
775 }
776
777 }
778
779
780 function cache_inject(id, article) {
781 if (!cache_check(id)) {
782 debug("cache_article: miss: " + id);
783
784 var cache_obj = new Array();
785
786 var d = new Date();
787
788 cache_obj["id"] = id;
789 cache_obj["entered"] = d.getTime() / 1000;
790 cache_obj["data"] = article;
791 cache_obj["last_access"] = 0;
792
793 //article_cache[id] = cache_obj;
794
795 article_cache.push(cache_obj);
796
797 } else {
798 debug("cache_article: hit: " + id);
799 }
800 }
801
802 function cache_find(id) {
803 for (var i = 0; i < article_cache.length; i++) {
804 if (article_cache[i]["id"] == id) {
805 var d = new Date();
806 article_cache[i]["last_access"] = d.getTime() / 1000;
807 return article_cache[i]["data"];
808 }
809 }
810 return false;
811 }
812
813 function cache_check(id) {
814 for (var i = 0; i < article_cache.length; i++) {
815 if (article_cache[i]["id"] == id) {
816 return true;
817 }
818 }
819 return false;
820 }
821
822 function cache_expire() {
823 while (article_cache.length > 20) {
824 article_cache.shift();
825 }
826 }