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