]> git.wh0rd.org - tt-rss.git/blob - viewfeed.js
headlines_callback: async
[tt-rss.git] / viewfeed.js
1 var active_post_id = false;
2 var last_article_view = false;
3 var active_real_feed_id = false;
4
5 var _tag_active_post_id = false;
6 var _tag_active_feed_id = false;
7 var _tag_active_cdm = false;
8
9 // FIXME: kludge, to restore scrollTop after tag editor terminates
10 var _tag_cdm_scroll = false;
11
12 // FIXME: kludges, needs proper implementation
13 var _reload_feedlist_after_view = false;
14
15 var _cdm_wd_timeout = false;
16 var _cdm_wd_vishist = new Array();
17
18 var article_cache = new Array();
19
20 function catchup_callback() {
21 if (xmlhttp_rpc.readyState == 4) {
22 try {
23 debug("catchup_callback");
24 notify("");
25 all_counters_callback2(xmlhttp_rpc);
26 if (_catchup_callback_func) {
27 setTimeout(_catchup_callback_func, 10);
28 }
29 } catch (e) {
30 exception_error("catchup_callback", e);
31 }
32 }
33 }
34
35 function catchup_callback2(transport, callback) {
36 try {
37 debug("catchup_callback2 " + transport + ", " + callback);
38 notify("");
39 all_counters_callback2(transport);
40 if (callback) {
41 setTimeout(callback, 10);
42 }
43 } catch (e) {
44 exception_error("catchup_callback2", e);
45 }
46 }
47
48 function headlines_callback2(transport, active_feed_id, active_feed_is_cat, feed_cur_page) {
49 debug("headlines_callback2");
50 var f = document.getElementById("headlines-frame");
51 try {
52 if (feed_cur_page == 0) {
53 debug("resetting headlines scrollTop");
54 f.scrollTop = 0;
55 }
56 } catch (e) { };
57
58 if (transport.responseXML) {
59 var headlines = transport.responseXML.getElementsByTagName("headlines")[0];
60 var counters = transport.responseXML.getElementsByTagName("counters")[0];
61 var articles = transport.responseXML.getElementsByTagName("article");
62 var runtime_info = transport.responseXML.getElementsByTagName("runtime-info");
63
64 if (feed_cur_page == 0) {
65 if (headlines) {
66 f.innerHTML = headlines.firstChild.nodeValue;
67 } else {
68 debug("headlines_callback: returned no data");
69 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
70
71 }
72 } else {
73 if (headlines) {
74 debug("adding some more headlines...");
75
76 var c = document.getElementById("headlinesList");
77
78 if (!c) {
79 c = document.getElementById("headlinesInnerContainer");
80 }
81
82 c.innerHTML = c.innerHTML + headlines.firstChild.nodeValue;
83 } else {
84 debug("headlines_callback: returned no data");
85 notify_error("Error while trying to load more headlines");
86 }
87
88 }
89
90 if (articles) {
91 for (var i = 0; i < articles.length; i++) {
92 var a_id = articles[i].getAttribute("id");
93 debug("found id: " + a_id);
94 cache_inject(a_id, articles[i].firstChild.nodeValue);
95 }
96 } else {
97 debug("no cached articles received");
98 }
99
100 if (counters) {
101 debug("parsing piggybacked counters: " + counters);
102 parse_counters(counters, false);
103 } else {
104 debug("counters container not found in reply");
105 }
106
107 if (runtime_info) {
108 debug("parsing runtime info: " + runtime_info[0]);
109 parse_runtime_info(runtime_info[0]);
110 } else {
111 debug("counters container not found in reply");
112 }
113
114 } else {
115 debug("headlines_callback: returned no XML object");
116 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
117 }
118
119 if (typeof correctPNG != 'undefined') {
120 correctPNG();
121 }
122
123 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
124
125 if (!document.getElementById("headlinesList") &&
126 getInitParam("cdm_auto_catchup") == 1) {
127 debug("starting CDM watchdog");
128 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
129 _cdm_wd_vishist = new Array();
130 } else {
131 debug("not in CDM mode or watchdog disabled");
132 }
133
134 if (_tag_cdm_scroll) {
135 try {
136 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
137 _tag_cdm_scroll = false;
138 debug("resetting headlinesInner scrollTop");
139
140 } catch (e) { }
141 }
142
143 notify("");
144 }
145
146 function render_article(article) {
147 try {
148 var f = document.getElementById("content-frame");
149 try {
150 f.scrollTop = 0;
151 } catch (e) { };
152
153 f.innerHTML = article;
154
155 } catch (e) {
156 exception_error("render_article", e);
157 }
158 }
159
160 function article_callback() {
161 if (xmlhttp.readyState == 4) {
162 debug("article_callback");
163
164 try {
165 if (xmlhttp.responseXML) {
166 var reply = xmlhttp.responseXML.firstChild.firstChild;
167
168 var articles = xmlhttp.responseXML.getElementsByTagName("article");
169
170 for (var i = 0; i < articles.length; i++) {
171 var a_id = articles[i].getAttribute("id");
172
173 debug("found id: " + a_id);
174
175 if (a_id == active_post_id) {
176 debug("active article, rendering...");
177 render_article(articles[i].firstChild.nodeValue);
178 }
179
180 cache_inject(a_id, articles[i].firstChild.nodeValue);
181 }
182
183 } else {
184 debug("article_callback: returned no XML object");
185 var f = document.getElementById("content-frame");
186 f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
187 }
188 } catch (e) {
189 exception_error("article_callback", e);
190 }
191
192 var date = new Date();
193 last_article_view = date.getTime() / 1000;
194
195 if (typeof correctPNG != 'undefined') {
196 correctPNG();
197 }
198
199 if (_reload_feedlist_after_view) {
200 setTimeout('updateFeedList(false, false)', 50);
201 _reload_feedlist_after_view = false;
202 } else {
203 var counters = xmlhttp.responseXML.getElementsByTagName("counters")[0];
204
205 if (counters) {
206 debug("parsing piggybacked counters: " + counters);
207 parse_counters(counters, false);
208 } else {
209 debug("counters container not found in reply");
210 }
211 }
212
213 notify("");
214 }
215 }
216
217 function view(id, feed_id, skip_history) {
218
219 try {
220 debug("loading article: " + id + "/" + feed_id);
221
222 active_real_feed_id = feed_id;
223
224 var cached_article = cache_find(id);
225
226 debug("cache check result: " + (cached_article != false));
227
228 enableHotkeys();
229
230 //setActiveFeedId(feed_id);
231
232 var query = "backend.php?op=view&id=" + param_escape(id) +
233 "&feed=" + param_escape(feed_id);
234
235 var date = new Date();
236
237 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
238 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
239 xmlhttp.abort();
240 if (is_safari()) {
241 debug("trying alternative reset method for Safari");
242 xmlhttp = Ajax.getTransport();
243 }
244 }
245
246 if (xmlhttp_ready(xmlhttp)) {
247
248 active_post_id = id;
249
250 cleanSelected("headlinesList");
251
252 var crow = document.getElementById("RROW-" + active_post_id);
253
254 var article_is_unread = crow.className.match("Unread");
255 debug("article is unread: " + article_is_unread);
256
257 crow.className = crow.className.replace("Unread", "");
258
259 var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
260
261 if (upd_img_pic) {
262 upd_img_pic.src = "images/blank_icon.gif";
263 }
264
265 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
266 markHeadline(active_post_id);
267
268 var neighbor_ids = getRelativePostIds(active_post_id);
269
270 /* only request uncached articles */
271
272 var cids_to_request = Array();
273
274 for (var i = 0; i < neighbor_ids.length; i++) {
275 if (!cache_check(neighbor_ids[i])) {
276 cids_to_request.push(neighbor_ids[i]);
277 }
278 }
279
280 debug("additional ids: " + cids_to_request.toString());
281
282 /* additional info for piggyback counters */
283
284 if (tagsAreDisplayed()) {
285 query = query + "&omode=lt";
286 } else {
287 query = query + "&omode=flc";
288 }
289
290 var date = new Date();
291 var timestamp = Math.round(date.getTime() / 1000);
292 query = query + "&ts=" + timestamp;
293
294 query = query + "&cids=" + cids_to_request.toString();
295
296 if (!cached_article) {
297
298 notify_progress("Loading, please wait...");
299
300 debug(query);
301
302 xmlhttp.open("GET", query, true);
303 xmlhttp.onreadystatechange=article_callback;
304 xmlhttp.send(null);
305 } else if (cached_article && article_is_unread) {
306
307 query = query + "&mode=prefetch";
308
309 debug(query);
310
311 xmlhttp.open("GET", query, true);
312 xmlhttp.onreadystatechange=article_callback;
313 xmlhttp.send(null);
314
315 render_article(cached_article);
316
317 } else if (cached_article) {
318
319 query = query + "&mode=prefetch_old";
320
321 debug(query);
322
323 xmlhttp.open("GET", query, true);
324 xmlhttp.onreadystatechange=article_callback;
325 xmlhttp.send(null);
326
327 render_article(cached_article);
328
329 }
330
331 cache_expire();
332
333 } else {
334 debug("xmlhttp busy (@view)");
335 printLockingError();
336 }
337
338 } catch (e) {
339 exception_error("view", e);
340 }
341 }
342
343 function tMark(id) {
344 return toggleMark(id);
345 }
346
347 function tPub(id) {
348 return togglePub(id);
349 }
350
351 function tMark_afh_off(effect) {
352 try {
353 var elem = effect.effects[0].element;
354
355 debug("tMark_afh_off : " + elem.id);
356
357 if (elem) {
358 elem.src = elem.src.replace("mark_set", "mark_unset");
359 elem.alt = __("Star article");
360 Element.show(elem);
361 }
362
363 } catch (e) {
364 exception_error("tMark_afh_off", e);
365 }
366 }
367
368 function tPub_afh_off(effect) {
369 try {
370 var elem = effect.effects[0].element;
371
372 debug("tPub_afh_off : " + elem.id);
373
374 if (elem) {
375 elem.src = elem.src.replace("pub_set", "pub_unset");
376 elem.alt = __("Publish article");
377 Element.show(elem);
378 }
379
380 } catch (e) {
381 exception_error("tPub_afh_off", e);
382 }
383 }
384
385 function toggleMark(id, client_only, no_effects) {
386
387 try {
388
389 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
390
391 query = query + "&afid=" + getActiveFeedId();
392
393 if (tagsAreDisplayed()) {
394 query = query + "&omode=tl";
395 } else {
396 query = query + "&omode=flc";
397 }
398
399 var mark_img = document.getElementById("FMPIC-" + id);
400 var vfeedu = document.getElementById("FEEDU--1");
401 var crow = document.getElementById("RROW-" + id);
402
403 if (mark_img.src.match("mark_unset")) {
404 mark_img.src = mark_img.src.replace("mark_unset", "mark_set");
405 mark_img.alt = __("Unstar article");
406 query = query + "&mark=1";
407
408 /* if (vfeedu && crow.className.match("Unread")) {
409 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
410 } */
411
412 } else {
413 //mark_img.src = "images/mark_unset.png";
414 mark_img.alt = __("Please wait...");
415 query = query + "&mark=0";
416
417 /* if (vfeedu && crow.className.match("Unread")) {
418 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
419 } */
420
421 if (document.getElementById("headlinesList") && !no_effects) {
422 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tMark_afh_off});
423 } else {
424 mark_img.src = mark_img.src.replace("mark_set", "mark_unset");
425 mark_img.alt = __("Star article");
426 }
427 }
428
429 /* var vfeedctr = document.getElementById("FEEDCTR--1");
430 var vfeedr = document.getElementById("FEEDR--1");
431
432 if (vfeedu && vfeedctr) {
433 if ((+vfeedu.innerHTML) > 0) {
434 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
435 vfeedr.className = vfeedr.className + "Unread";
436 vfeedctr.className = "odd";
437 }
438 } else {
439 vfeedctr.className = "invisible";
440 vfeedr.className = vfeedr.className.replace("Unread", "");
441 }
442 }
443
444 debug("toggle starred for aid " + id);
445
446 //new Ajax.Request(query); */
447
448 if (!client_only) {
449 debug(query);
450
451 new Ajax.Request(query, {
452 onComplete: function(transport) {
453 all_counters_callback2(transport);
454 } });
455
456 }
457
458 } catch (e) {
459 exception_error("toggleMark", e);
460 }
461 }
462
463 function togglePub(id, client_only, no_effects) {
464
465 try {
466
467 var query = "backend.php?op=rpc&id=" + id + "&subop=publ";
468
469 query = query + "&afid=" + getActiveFeedId();
470
471 if (tagsAreDisplayed()) {
472 query = query + "&omode=tl";
473 } else {
474 query = query + "&omode=flc";
475 }
476
477 var mark_img = document.getElementById("FPPIC-" + id);
478 var vfeedu = document.getElementById("FEEDU--2");
479 var crow = document.getElementById("RROW-" + id);
480
481 if (mark_img.src.match("pub_unset")) {
482 mark_img.src = mark_img.src.replace("pub_unset", "pub_set");
483 mark_img.alt = __("Unpublish article");
484 query = query + "&pub=1";
485
486 /* if (vfeedu && crow.className.match("Unread")) {
487 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
488 } */
489
490 } else {
491 //mark_img.src = "images/pub_unset.png";
492 mark_img.alt = __("Please wait...");
493 query = query + "&pub=0";
494
495 /* if (vfeedu && crow.className.match("Unread")) {
496 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
497 } */
498
499 if (document.getElementById("headlinesList") && !no_effects) {
500 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tPub_afh_off});
501 } else {
502 mark_img.src = mark_img.src.replace("pub_set", "pub_unset");
503 mark_img.alt = __("Publish article");
504 }
505 }
506
507 /* var vfeedctr = document.getElementById("FEEDCTR--2");
508 var vfeedr = document.getElementById("FEEDR--2");
509
510 if (vfeedu && vfeedctr) {
511 if ((+vfeedu.innerHTML) > 0) {
512 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
513 vfeedr.className = vfeedr.className + "Unread";
514 vfeedctr.className = "odd";
515 }
516 } else {
517 vfeedctr.className = "invisible";
518 vfeedr.className = vfeedr.className.replace("Unread", "");
519 }
520 }
521
522 debug("toggle published for aid " + id);
523
524 new Ajax.Request(query); */
525
526 if (!client_only) {
527 new Ajax.Request(query, {
528 onComplete: function(transport) {
529 all_counters_callback2(transport);
530 } });
531 }
532
533 } catch (e) {
534
535 exception_error("togglePub", e);
536 }
537 }
538
539 function correctHeadlinesOffset(id) {
540
541 try {
542
543 var hlist = document.getElementById("headlinesList");
544 var container = document.getElementById("headlinesInnerContainer");
545 var row = document.getElementById("RROW-" + id);
546
547 var viewport = container.offsetHeight;
548
549 var rel_offset_top = row.offsetTop - container.scrollTop;
550 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
551
552 debug("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
553 debug("Vport: " + viewport);
554
555 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
556 container.scrollTop = row.offsetTop;
557 } else if (rel_offset_bottom > viewport) {
558
559 /* doesn't properly work with Opera in some cases because
560 Opera fucks up element scrolling */
561
562 container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
563 }
564
565 } catch (e) {
566 exception_error("correctHeadlinesOffset", e);
567 }
568
569 }
570
571 function moveToPost(mode) {
572
573 // check for combined mode
574 if (!document.getElementById("headlinesList"))
575 return;
576
577 var rows = getVisibleHeadlineIds();
578
579 var prev_id = false;
580 var next_id = false;
581
582 if (!document.getElementById('RROW-' + active_post_id)) {
583 active_post_id = false;
584 }
585
586 if (active_post_id == false) {
587 next_id = getFirstVisibleHeadlineId();
588 prev_id = getLastVisibleHeadlineId();
589 } else {
590 for (var i = 0; i < rows.length; i++) {
591 if (rows[i] == active_post_id) {
592 prev_id = rows[i-1];
593 next_id = rows[i+1];
594 }
595 }
596 }
597
598 if (mode == "next") {
599 if (next_id) {
600 correctHeadlinesOffset(next_id);
601 view(next_id, getActiveFeedId());
602 }
603 }
604
605 if (mode == "prev") {
606 if (prev_id) {
607 correctHeadlinesOffset(prev_id);
608 view(prev_id, getActiveFeedId());
609 }
610 }
611 }
612
613 function toggleUnread(id, cmode) {
614 try {
615
616 var row = document.getElementById("RROW-" + id);
617 if (row) {
618 var nc = row.className;
619 nc = nc.replace("Unread", "");
620 nc = nc.replace("Selected", "");
621
622 if (row.className.match("Unread")) {
623 row.className = nc;
624 } else {
625 row.className = nc + "Unread";
626 }
627
628 if (!cmode) cmode = 2;
629
630 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
631 param_escape(id) + "&cmode=" + param_escape(cmode);
632
633 // notify_progress("Loading, please wait...");
634
635 new Ajax.Request(query, {
636 onComplete: function(transport) {
637 all_counters_callback2(transport);
638 } });
639
640 }
641
642
643 } catch (e) {
644 exception_error("toggleUnread", e);
645 }
646 }
647
648 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
649 try {
650 /* if (!xmlhttp_ready(xmlhttp_rpc)) {
651 printLockingError();
652 return;
653 } */
654
655 var rows;
656
657 if (cdm_mode) {
658 rows = cdmGetSelectedArticles();
659 } else {
660 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
661 }
662
663 if (rows.length == 0 && !no_error) {
664 alert(__("No articles are selected."));
665 return;
666 }
667
668 for (i = 0; i < rows.length; i++) {
669 var row = document.getElementById("RROW-" + rows[i]);
670 if (row) {
671 var nc = row.className;
672 nc = nc.replace("Unread", "");
673 nc = nc.replace("Selected", "");
674
675 if (set_state == undefined) {
676 if (row.className.match("Unread")) {
677 row.className = nc + "Selected";
678 } else {
679 row.className = nc + "UnreadSelected";
680 }
681 }
682
683 if (set_state == false) {
684 row.className = nc + "Selected";
685 }
686
687 if (set_state == true) {
688 row.className = nc + "UnreadSelected";
689 }
690 }
691 }
692
693 if (rows.length > 0) {
694
695 var cmode = "";
696
697 if (set_state == undefined) {
698 cmode = "2";
699 } else if (set_state == true) {
700 cmode = "1";
701 } else if (set_state == false) {
702 cmode = "0";
703 }
704
705 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
706 param_escape(rows.toString()) + "&cmode=" + cmode;
707
708 // _catchup_callback_func = callback_func;
709
710 debug(callback_func);
711
712 notify_progress("Loading, please wait...");
713
714 /* xmlhttp_rpc.open("GET", query, true);
715 xmlhttp_rpc.onreadystatechange=catchup_callback;
716 xmlhttp_rpc.send(null); */
717
718 new Ajax.Request(query, {
719 onComplete: function(transport) {
720 catchup_callback2(transport, callback_func);
721 } });
722
723 }
724
725 } catch (e) {
726 exception_error("selectionToggleUnread", e);
727 }
728 }
729
730 function selectionToggleMarked(cdm_mode) {
731 try {
732
733 var rows;
734
735 if (cdm_mode) {
736 rows = cdmGetSelectedArticles();
737 } else {
738 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
739 }
740
741 if (rows.length == 0) {
742 alert(__("No articles are selected."));
743 return;
744 }
745
746 for (i = 0; i < rows.length; i++) {
747 toggleMark(rows[i], true, true);
748 }
749
750 if (rows.length > 0) {
751
752 var query = "backend.php?op=rpc&subop=markSelected&ids=" +
753 param_escape(rows.toString()) + "&cmode=2";
754
755 query = query + "&afid=" + getActiveFeedId();
756
757 /* if (tagsAreDisplayed()) {
758 query = query + "&omode=tl";
759 } else {
760 query = query + "&omode=flc";
761 } */
762
763 query = query + "&omode=lc";
764
765 new Ajax.Request(query, {
766 onComplete: function(transport) {
767 all_counters_callback2(transport);
768 } });
769
770 }
771
772 } catch (e) {
773 exception_error("selectionToggleMarked", e);
774 }
775 }
776
777 function selectionTogglePublished(cdm_mode) {
778 try {
779
780 var rows;
781
782 if (cdm_mode) {
783 rows = cdmGetSelectedArticles();
784 } else {
785 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
786 }
787
788 if (rows.length == 0) {
789 alert(__("No articles are selected."));
790 return;
791 }
792
793 for (i = 0; i < rows.length; i++) {
794 togglePub(rows[i], true, true);
795 }
796
797 if (rows.length > 0) {
798
799 var query = "backend.php?op=rpc&subop=publishSelected&ids=" +
800 param_escape(rows.toString()) + "&cmode=2";
801
802 query = query + "&afid=" + getActiveFeedId();
803
804 /* if (tagsAreDisplayed()) {
805 query = query + "&omode=tl";
806 } else {
807 query = query + "&omode=flc";
808 } */
809
810 query = query + "&omode=lc";
811
812 new Ajax.Request(query, {
813 onComplete: function(transport) {
814 all_counters_callback2(transport);
815 } });
816
817 }
818
819 } catch (e) {
820 exception_error("selectionToggleMarked", e);
821 }
822 }
823
824 function cdmGetSelectedArticles() {
825 var sel_articles = new Array();
826 var container = document.getElementById("headlinesInnerContainer");
827
828 for (i = 0; i < container.childNodes.length; i++) {
829 var child = container.childNodes[i];
830
831 if (child.id.match("RROW-") && child.className.match("Selected")) {
832 var c_id = child.id.replace("RROW-", "");
833 sel_articles.push(c_id);
834 }
835 }
836
837 return sel_articles;
838 }
839
840 function cdmGetVisibleArticles() {
841 var sel_articles = new Array();
842 var container = document.getElementById("headlinesInnerContainer");
843
844 for (i = 0; i < container.childNodes.length; i++) {
845 var child = container.childNodes[i];
846
847 if (child.id.match("RROW-")) {
848 var c_id = child.id.replace("RROW-", "");
849 sel_articles.push(c_id);
850 }
851 }
852
853 return sel_articles;
854 }
855
856 function cdmGetUnreadArticles() {
857 var sel_articles = new Array();
858 var container = document.getElementById("headlinesInnerContainer");
859
860 for (i = 0; i < container.childNodes.length; i++) {
861 var child = container.childNodes[i];
862
863 if (child.id.match("RROW-") && child.className.match("Unread")) {
864 var c_id = child.id.replace("RROW-", "");
865 sel_articles.push(c_id);
866 }
867 }
868
869 return sel_articles;
870 }
871
872
873 // mode = all,none,unread
874 function cdmSelectArticles(mode) {
875 var container = document.getElementById("headlinesInnerContainer");
876
877 for (i = 0; i < container.childNodes.length; i++) {
878 var child = container.childNodes[i];
879
880 if (child.id.match("RROW-")) {
881 var aid = child.id.replace("RROW-", "");
882
883 var cb = document.getElementById("RCHK-" + aid);
884
885 if (mode == "all") {
886 if (!child.className.match("Selected")) {
887 child.className = child.className + "Selected";
888 cb.checked = true;
889 }
890 } else if (mode == "unread") {
891 if (child.className.match("Unread") && !child.className.match("Selected")) {
892 child.className = child.className + "Selected";
893 cb.checked = true;
894 }
895 } else {
896 child.className = child.className.replace("Selected", "");
897 cb.checked = false;
898 }
899 }
900 }
901 }
902
903 function catchupPage() {
904
905 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
906
907 var str = __("Mark all visible articles in %s as read?");
908
909 str = str.replace("%s", fn);
910
911 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
912 return;
913 }
914
915 if (document.getElementById("headlinesList")) {
916 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
917 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
918 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
919 } else {
920 cdmSelectArticles('all');
921 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
922 cdmSelectArticles('none');
923 }
924 }
925
926 function catchupSelection() {
927
928 try {
929
930 var rows;
931
932 if (document.getElementById("headlinesList")) {
933 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
934 } else {
935 rows = cdmGetSelectedArticles();
936 }
937
938 if (rows.length == 0) {
939 alert(__("No articles are selected."));
940 return;
941 }
942
943
944 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
945
946 var str = __("Mark all selected articles in %s as read?");
947
948 str = str.replace("%s", fn);
949
950 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
951 return;
952 }
953
954 if (document.getElementById("headlinesList")) {
955 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
956 // selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
957 } else {
958 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
959 // cdmSelectArticles('none');
960 }
961
962 } catch (e) {
963 exception_error("catchupSelection", e);
964 }
965 }
966
967
968 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
969
970 if (!xmlhttp_ready(xmlhttp_rpc)) {
971 printLockingError();
972 }
973
974 var title = prompt(__("Please enter label title:"), "");
975
976 if (title) {
977
978 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
979 "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
980 "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) +
981 "&title=" + param_escape(title);
982
983 debug("LFS: " + query);
984
985 xmlhttp_rpc.open("GET", query, true);
986 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
987 xmlhttp_rpc.send(null);
988 }
989
990 }
991
992 function editArticleTags(id, feed_id, cdm_enabled) {
993 _tag_active_post_id = id;
994 _tag_active_feed_id = feed_id;
995 _tag_active_cdm = cdm_enabled;
996
997 cache_invalidate(id);
998
999 try {
1000 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
1001 } catch (e) { }
1002 displayDlg('editArticleTags', id);
1003 }
1004
1005
1006 function tag_saved_callback() {
1007 if (xmlhttp_rpc.readyState == 4) {
1008 try {
1009 debug("in tag_saved_callback");
1010
1011 closeInfoBox();
1012 notify("");
1013
1014 if (tagsAreDisplayed()) {
1015 _reload_feedlist_after_view = true;
1016 }
1017
1018 if (!_tag_active_cdm) {
1019 if (active_post_id == _tag_active_post_id) {
1020 debug("reloading current article");
1021 view(_tag_active_post_id, _tag_active_feed_id);
1022 }
1023 } else {
1024 debug("reloading current feed");
1025 viewCurrentFeed();
1026 }
1027
1028 } catch (e) {
1029 exception_error("catchup_callback", e);
1030 }
1031 }
1032 }
1033
1034 function editTagsSave() {
1035
1036 if (!xmlhttp_ready(xmlhttp_rpc)) {
1037 printLockingError();
1038 }
1039
1040 notify_progress("Saving article tags...");
1041
1042 var form = document.forms["tag_edit_form"];
1043
1044 var query = Form.serialize("tag_edit_form");
1045
1046 query = "backend.php?op=rpc&subop=setArticleTags&" + query;
1047
1048 debug(query);
1049
1050 xmlhttp_rpc.open("GET", query, true);
1051 xmlhttp_rpc.onreadystatechange=tag_saved_callback;
1052 xmlhttp_rpc.send(null);
1053
1054 }
1055
1056 function editTagsInsert() {
1057 try {
1058
1059 var form = document.forms["tag_edit_form"];
1060
1061 var found_tags = form.found_tags;
1062 var tags_str = form.tags_str;
1063
1064 var tag = found_tags[found_tags.selectedIndex].value;
1065
1066 if (tags_str.value.length > 0 &&
1067 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
1068
1069 tags_str.value = tags_str.value + ", ";
1070 }
1071
1072 tags_str.value = tags_str.value + tag + ", ";
1073
1074 found_tags.selectedIndex = 0;
1075
1076 } catch (e) {
1077 exception_error(e, "editTagsInsert");
1078 }
1079 }
1080
1081 function cdmWatchdog() {
1082
1083 try {
1084
1085 var ctr = document.getElementById("headlinesInnerContainer");
1086
1087 if (!ctr) return;
1088
1089 var ids = new Array();
1090
1091 var e = ctr.firstChild;
1092
1093 while (e) {
1094 if (e.className && e.className == "cdmArticleUnread" && e.id &&
1095 e.id.match("RROW-")) {
1096
1097 // article fits in viewport OR article is longer than viewport and
1098 // its bottom is visible
1099
1100 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1101 ctr.scrollTop + ctr.offsetHeight) {
1102
1103 // debug(e.id + " is visible " + e.offsetTop + "." +
1104 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
1105 // (ctr.scrollTop + ctr.offsetHeight));
1106
1107 ids.push(e.id.replace("RROW-", ""));
1108
1109 } else if (e.offsetHeight > ctr.offsetHeight &&
1110 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1111 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1112
1113 ids.push(e.id.replace("RROW-", ""));
1114
1115 }
1116
1117 // method 2: article bottom is visible and is in upper 1/2 of the viewport
1118
1119 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1120 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
1121
1122 ids.push(e.id.replace("RROW-", ""));
1123
1124 } */
1125
1126 }
1127
1128 e = e.nextSibling;
1129 }
1130
1131 debug("cdmWatchdog, ids= " + ids.toString());
1132
1133 if (ids.length > 0) {
1134
1135 for (var i = 0; i < ids.length; i++) {
1136 var e = document.getElementById("RROW-" + ids[i]);
1137 if (e) {
1138 e.className = e.className.replace("Unread", "");
1139 }
1140 }
1141
1142 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
1143 param_escape(ids.toString()) + "&cmode=0";
1144
1145 new Ajax.Request(query, {
1146 onComplete: function(transport) {
1147 all_counters_callback2(transport);
1148 } });
1149
1150 }
1151
1152 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
1153
1154 } catch (e) {
1155 exception_error(e, "cdmWatchdog");
1156 }
1157
1158 }
1159
1160
1161 function cache_inject(id, article) {
1162 if (!cache_check(id)) {
1163 debug("cache_article: miss: " + id);
1164
1165 var cache_obj = new Array();
1166
1167 cache_obj["id"] = id;
1168 cache_obj["data"] = article;
1169
1170 article_cache.push(cache_obj);
1171
1172 } else {
1173 debug("cache_article: hit: " + id);
1174 }
1175 }
1176
1177 function cache_find(id) {
1178 for (var i = 0; i < article_cache.length; i++) {
1179 if (article_cache[i]["id"] == id) {
1180 return article_cache[i]["data"];
1181 }
1182 }
1183 return false;
1184 }
1185
1186 function cache_check(id) {
1187 for (var i = 0; i < article_cache.length; i++) {
1188 if (article_cache[i]["id"] == id) {
1189 return true;
1190 }
1191 }
1192 return false;
1193 }
1194
1195 function cache_expire() {
1196 while (article_cache.length > 20) {
1197 article_cache.shift();
1198 }
1199 }
1200
1201 function cache_invalidate(id) {
1202 var i = 0
1203
1204 try {
1205
1206 while (i < article_cache.length) {
1207 if (article_cache[i]["id"] == id) {
1208 debug("cache_invalidate: removed id " + id);
1209 article_cache.splice(i, 1);
1210 return true;
1211 }
1212 i++;
1213 }
1214 debug("cache_invalidate: id not found: " + id);
1215 return false;
1216 } catch (e) {
1217 exception_error("cache_invalidate", e);
1218 }
1219 }
1220
1221 function getActiveArticleId() {
1222 return active_post_id;
1223 }
1224
1225 function cdmMouseIn(elem) {
1226 try {
1227 if (elem.id && elem.id.match("RROW-")) {
1228 var id = elem.id.replace("RROW-", "");
1229 active_post_id = id;
1230 }
1231 } catch (e) {
1232 exception_error("cdmMouseIn", e);
1233 }
1234
1235 }
1236
1237 function cdmMouseOut(elem) {
1238 active_post_id = false;
1239 }
1240
1241 function headlines_scroll_handler() {
1242 try {
1243
1244 var e = document.getElementById("headlinesInnerContainer");
1245
1246 if (e.scrollTop + e.offsetHeight > e.scrollHeight - 300) {
1247 debug("more cowbell!");
1248
1249 viewNextFeedPage();
1250 }
1251
1252 } catch (e) {
1253 exception_error("headlines_scroll_handler", e);
1254 }
1255 }
1256
1257 function catchupRelativeToArticle(below) {
1258
1259 try {
1260
1261 if (!xmlhttp_ready(xmlhttp_rpc)) {
1262 printLockingError();
1263 }
1264
1265 if (!getActiveArticleId()) {
1266 alert(__("No article is selected."));
1267 return;
1268 }
1269
1270 var visible_ids;
1271
1272 if (document.getElementById("headlinesList")) {
1273 visible_ids = getVisibleHeadlineIds();
1274 } else {
1275 visible_ids = cdmGetVisibleArticles();
1276 }
1277
1278 var ids_to_mark = new Array();
1279
1280 if (!below) {
1281 for (var i = 0; i < visible_ids.length; i++) {
1282 if (visible_ids[i] != getActiveArticleId()) {
1283 var e = document.getElementById("RROW-" + visible_ids[i]);
1284
1285 if (e && e.className.match("Unread")) {
1286 ids_to_mark.push(visible_ids[i]);
1287 }
1288 } else {
1289 break;
1290 }
1291 }
1292 } else {
1293 for (var i = visible_ids.length-1; i >= 0; i--) {
1294 if (visible_ids[i] != getActiveArticleId()) {
1295 var e = document.getElementById("RROW-" + visible_ids[i]);
1296
1297 if (e && e.className.match("Unread")) {
1298 ids_to_mark.push(visible_ids[i]);
1299 }
1300 } else {
1301 break;
1302 }
1303 }
1304 }
1305
1306 if (ids_to_mark.length == 0) {
1307 alert(__("No articles found to mark"));
1308 } else {
1309 var msg = __("Mark %d article(s) as read?").replace("%d", ids_to_mark.length);
1310
1311 if (confirm(msg)) {
1312
1313 for (var i = 0; i < ids_to_mark.length; i++) {
1314 var e = document.getElementById("RROW-" + ids_to_mark[i]);
1315 e.className = e.className.replace("Unread", "");
1316 }
1317
1318 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
1319 param_escape(ids_to_mark.toString()) + "&cmode=0";
1320
1321 xmlhttp_rpc.open("GET", query, true);
1322 xmlhttp_rpc.onreadystatechange=catchup_callback;
1323 xmlhttp_rpc.send(null);
1324
1325 }
1326 }
1327
1328 } catch (e) {
1329 exception_error("catchupRelativeToArticle", e);
1330 }
1331 }