]> git.wh0rd.org - tt-rss.git/blob - viewfeed.js
invalidate local cache when view mode/limit is changed
[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 clean_feed_selections() {
49 try {
50 var feeds = document.getElementById("feedList").getElementsByTagName("LI");
51
52 for (var i = 0; i < feeds.length; i++) {
53 if (feeds[i].id && feeds[i].id.match("FEEDR-")) {
54 feeds[i].className = feeds[i].className.replace("Selected", "");
55 }
56 }
57 } catch (e) {
58 exception_error("clean_feed_selections", e);
59 }
60 }
61
62 function headlines_callback2(transport, active_feed_id, is_cat, feed_cur_page) {
63 try {
64
65 debug("headlines_callback2 [page=" + feed_cur_page + "]");
66
67 clean_feed_selections();
68
69 setActiveFeedId(active_feed_id);
70
71 if (is_cat != undefined) {
72 active_feed_is_cat = is_cat;
73 }
74
75 if (!is_cat) {
76 var feedr = document.getElementById("FEEDR-" + active_feed_id);
77 if (feedr && !feedr.className.match("Selected")) {
78 feedr.className = feedr.className + "Selected";
79 }
80 }
81
82 var f = document.getElementById("headlines-frame");
83 try {
84 if (feed_cur_page == 0) {
85 debug("resetting headlines scrollTop");
86 f.scrollTop = 0;
87 }
88 } catch (e) { };
89
90 if (transport.responseXML) {
91 var headlines = transport.responseXML.getElementsByTagName("headlines")[0];
92 var headlines_count_obj = transport.responseXML.getElementsByTagName("headlines-count")[0];
93 var headlines_unread_obj = transport.responseXML.getElementsByTagName("headlines-unread")[0];
94 var disable_cache_obj = transport.responseXML.getElementsByTagName("disable-cache")[0];
95
96 var headlines_count = headlines_count_obj.getAttribute("value");
97 var headlines_unread = headlines_unread_obj.getAttribute("value");
98 var disable_cache = disable_cache_obj.getAttribute("value") != "0";
99
100 if (headlines_count == 0) {
101 _infscroll_disable = 1;
102 } else {
103 _infscroll_disable = 0;
104 }
105
106 var counters = transport.responseXML.getElementsByTagName("counters")[0];
107 var articles = transport.responseXML.getElementsByTagName("article");
108 var runtime_info = transport.responseXML.getElementsByTagName("runtime-info");
109
110 if (feed_cur_page == 0) {
111 if (headlines) {
112 f.innerHTML = headlines.firstChild.nodeValue;
113
114 var cache_prefix = "";
115
116 if (is_cat) {
117 cache_prefix = "C:";
118 } else {
119 cache_prefix = "F:";
120 }
121
122 cache_invalidate(cache_prefix + active_feed_id);
123
124 if (!disable_cache) {
125 cache_inject(cache_prefix + active_feed_id,
126 headlines.firstChild.nodeValue, headlines_unread);
127 }
128
129 } else {
130 debug("headlines_callback: returned no data");
131 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
132
133 }
134 } else {
135 if (headlines) {
136 if (headlines_count > 0) {
137 debug("adding some more headlines...");
138
139 var c = document.getElementById("headlinesList");
140
141 if (!c) {
142 c = document.getElementById("headlinesInnerContainer");
143 }
144
145 var ids = getSelectedArticleIds2();
146
147 c.innerHTML = c.innerHTML + headlines.firstChild.nodeValue;
148
149 debug("restore selected ids: " + ids);
150
151 for (var i = 0; i < ids.length; i++) {
152 markHeadline(ids[i]);
153 }
154
155 } else {
156 debug("no new headlines received");
157 }
158 } else {
159 debug("headlines_callback: returned no data");
160 notify_error("Error while trying to load more headlines");
161 }
162
163 }
164
165 if (articles) {
166 for (var i = 0; i < articles.length; i++) {
167 var a_id = articles[i].getAttribute("id");
168 debug("found id: " + a_id);
169 cache_inject(a_id, articles[i].firstChild.nodeValue);
170 }
171 } else {
172 debug("no cached articles received");
173 }
174
175 if (counters) {
176 debug("parsing piggybacked counters: " + counters);
177 parse_counters(counters, false);
178 } else {
179 debug("counters container not found in reply");
180 }
181
182 if (runtime_info) {
183 debug("parsing runtime info: " + runtime_info[0]);
184 parse_runtime_info(runtime_info[0]);
185 } else {
186 debug("counters container not found in reply");
187 }
188
189 } else {
190 debug("headlines_callback: returned no XML object");
191 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
192 }
193
194 if (typeof correctPNG != 'undefined') {
195 correctPNG();
196 }
197
198 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
199
200 if (!document.getElementById("headlinesList") &&
201 getInitParam("cdm_auto_catchup") == 1) {
202 debug("starting CDM watchdog");
203 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
204 _cdm_wd_vishist = new Array();
205 } else {
206 debug("not in CDM mode or watchdog disabled");
207 }
208
209 if (_tag_cdm_scroll) {
210 try {
211 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
212 _tag_cdm_scroll = false;
213 debug("resetting headlinesInner scrollTop");
214
215 } catch (e) { }
216 }
217
218 _feed_cur_page = feed_cur_page;
219 _infscroll_request_sent = 0;
220
221 notify("");
222 } catch (e) {
223 exception_error("headlines_callback2", e);
224 }
225 }
226
227 function render_article(article) {
228 try {
229 var f = document.getElementById("content-frame");
230 try {
231 f.scrollTop = 0;
232 } catch (e) { };
233
234 f.innerHTML = article;
235
236 } catch (e) {
237 exception_error("render_article", e);
238 }
239 }
240
241 function showArticleInHeadlines(id) {
242
243 try {
244
245 cleanSelected("headlinesList");
246
247 var crow = document.getElementById("RROW-" + id);
248
249 if (!crow) return;
250
251 var article_is_unread = crow.className.match("Unread");
252
253 crow.className = crow.className.replace("Unread", "");
254
255 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
256
257 var upd_img_pic = document.getElementById("FUPDPIC-" + id);
258
259 var cache_prefix = "";
260
261 if (activeFeedIsCat()) {
262 cache_prefix = "C:";
263 } else {
264 cache_prefix = "F:";
265 }
266
267 var view_mode = false;
268
269 try {
270 view_mode = document.forms['main_toolbar_form'].view_mode;
271 view_mode = view_mode[view_mode.selectedIndex].value;
272 } catch (e) {
273 exception_error("showArticleInHeadlines/viewmode", e, true);
274 }
275
276 if (upd_img_pic && upd_img_pic.src.match("updated.png")) {
277 upd_img_pic.src = "images/blank_icon.gif";
278
279 cache_invalidate(cache_prefix + getActiveFeedId());
280
281 cache_inject(cache_prefix + getActiveFeedId(),
282 document.getElementById("headlines-frame").innerHTML,
283 get_feed_unread(getActiveFeedId()));
284
285 } else if (article_is_unread && view_mode == "all_articles") {
286
287 cache_invalidate(cache_prefix + getActiveFeedId());
288
289 cache_inject(cache_prefix + getActiveFeedId(),
290 document.getElementById("headlines-frame").innerHTML,
291 get_feed_unread(getActiveFeedId())-1);
292
293 } else if (article_is_unread) {
294 cache_invalidate(cache_prefix + getActiveFeedId());
295 }
296
297 markHeadline(id);
298
299 } catch (e) {
300 exception_error("showArticleInHeadlines", e);
301 }
302 }
303
304 function article_callback2(transport, id, feed_id) {
305 try {
306 debug("article_callback2 " + id);
307
308 if (transport.responseXML) {
309
310 active_real_feed_id = feed_id;
311 active_post_id = id;
312
313 showArticleInHeadlines(id);
314
315 var reply = transport.responseXML.firstChild.firstChild;
316
317 var articles = transport.responseXML.getElementsByTagName("article");
318
319 for (var i = 0; i < articles.length; i++) {
320 var a_id = articles[i].getAttribute("id");
321
322 debug("found id: " + a_id);
323
324 if (a_id == active_post_id) {
325 debug("active article, rendering...");
326 render_article(articles[i].firstChild.nodeValue);
327 }
328
329 cache_inject(a_id, articles[i].firstChild.nodeValue);
330 }
331
332 } else {
333 debug("article_callback: returned no XML object");
334 var f = document.getElementById("content-frame");
335 f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
336 }
337
338 var date = new Date();
339 last_article_view = date.getTime() / 1000;
340
341 if (typeof correctPNG != 'undefined') {
342 correctPNG();
343 }
344
345 if (_reload_feedlist_after_view) {
346 setTimeout('updateFeedList(false, false)', 50);
347 _reload_feedlist_after_view = false;
348 } else {
349 var counters = transport.responseXML.getElementsByTagName("counters")[0];
350
351 if (counters) {
352 debug("parsing piggybacked counters: " + counters);
353 parse_counters(counters, false);
354 } else {
355 debug("counters container not found in reply");
356 }
357 }
358
359 notify("");
360 } catch (e) {
361 exception_error("article_callback2", e);
362 }
363 }
364
365 function view(id, feed_id, skip_history) {
366
367 try {
368 debug("loading article: " + id + "/" + feed_id);
369
370 var cached_article = cache_find(id);
371
372 debug("cache check result: " + (cached_article != false));
373
374 enableHotkeys();
375
376 //setActiveFeedId(feed_id);
377
378 var query = "backend.php?op=view&id=" + param_escape(id) +
379 "&feed=" + param_escape(feed_id);
380
381 var date = new Date();
382
383 var neighbor_ids = getRelativePostIds(active_post_id);
384
385 /* only request uncached articles */
386
387 var cids_to_request = Array();
388
389 for (var i = 0; i < neighbor_ids.length; i++) {
390 if (!cache_check(neighbor_ids[i])) {
391 cids_to_request.push(neighbor_ids[i]);
392 }
393 }
394
395 debug("additional ids: " + cids_to_request.toString());
396
397 /* additional info for piggyback counters */
398
399 if (tagsAreDisplayed()) {
400 query = query + "&omode=lt";
401 } else {
402 query = query + "&omode=flc";
403 }
404
405 var date = new Date();
406 var timestamp = Math.round(date.getTime() / 1000);
407 query = query + "&ts=" + timestamp;
408
409 query = query + "&cids=" + cids_to_request.toString();
410
411 var crow = document.getElementById("RROW-" + id);
412 var article_is_unread = crow.className.match("Unread");
413
414 showArticleInHeadlines(id);
415
416 if (!cached_article) {
417
418 notify_progress("Loading, please wait...");
419
420 } else if (cached_article && article_is_unread) {
421
422 query = query + "&mode=prefetch";
423
424 render_article(cached_article);
425
426 } else if (cached_article) {
427
428 query = query + "&mode=prefetch_old";
429 render_article(cached_article);
430 }
431
432 cache_expire();
433
434 new Ajax.Request(query, {
435 onComplete: function(transport) {
436 article_callback2(transport, id, feed_id);
437 } });
438
439 } catch (e) {
440 exception_error("view", e);
441 }
442 }
443
444 function tMark(id) {
445 return toggleMark(id);
446 }
447
448 function tPub(id) {
449 return togglePub(id);
450 }
451
452 function tMark_afh_off(effect) {
453 try {
454 var elem = effect.effects[0].element;
455
456 debug("tMark_afh_off : " + elem.id);
457
458 if (elem) {
459 elem.src = elem.src.replace("mark_set", "mark_unset");
460 elem.alt = __("Star article");
461 Element.show(elem);
462 }
463
464 } catch (e) {
465 exception_error("tMark_afh_off", e);
466 }
467 }
468
469 function tPub_afh_off(effect) {
470 try {
471 var elem = effect.effects[0].element;
472
473 debug("tPub_afh_off : " + elem.id);
474
475 if (elem) {
476 elem.src = elem.src.replace("pub_set", "pub_unset");
477 elem.alt = __("Publish article");
478 Element.show(elem);
479 }
480
481 } catch (e) {
482 exception_error("tPub_afh_off", e);
483 }
484 }
485
486 function toggleMark(id, client_only, no_effects) {
487
488 try {
489
490 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
491
492 query = query + "&afid=" + getActiveFeedId();
493
494 if (tagsAreDisplayed()) {
495 query = query + "&omode=tl";
496 } else {
497 query = query + "&omode=flc";
498 }
499
500 var mark_img = document.getElementById("FMPIC-" + id);
501 var vfeedu = document.getElementById("FEEDU--1");
502 var crow = document.getElementById("RROW-" + id);
503
504 if (mark_img.src.match("mark_unset")) {
505 mark_img.src = mark_img.src.replace("mark_unset", "mark_set");
506 mark_img.alt = __("Unstar article");
507 query = query + "&mark=1";
508
509 /* if (vfeedu && crow.className.match("Unread")) {
510 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
511 } */
512
513 } else {
514 //mark_img.src = "images/mark_unset.png";
515 mark_img.alt = __("Please wait...");
516 query = query + "&mark=0";
517
518 /* if (vfeedu && crow.className.match("Unread")) {
519 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
520 } */
521
522 if (document.getElementById("headlinesList") && !no_effects) {
523 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tMark_afh_off});
524 } else {
525 mark_img.src = mark_img.src.replace("mark_set", "mark_unset");
526 mark_img.alt = __("Star article");
527 }
528 }
529
530 /* var vfeedctr = document.getElementById("FEEDCTR--1");
531 var vfeedr = document.getElementById("FEEDR--1");
532
533 if (vfeedu && vfeedctr) {
534 if ((+vfeedu.innerHTML) > 0) {
535 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
536 vfeedr.className = vfeedr.className + "Unread";
537 vfeedctr.className = "odd";
538 }
539 } else {
540 vfeedctr.className = "invisible";
541 vfeedr.className = vfeedr.className.replace("Unread", "");
542 }
543 }
544
545 debug("toggle starred for aid " + id);
546
547 //new Ajax.Request(query); */
548
549 if (!client_only) {
550 debug(query);
551
552 new Ajax.Request(query, {
553 onComplete: function(transport) {
554 all_counters_callback2(transport);
555 } });
556
557 }
558
559 } catch (e) {
560 exception_error("toggleMark", e);
561 }
562 }
563
564 function togglePub(id, client_only, no_effects) {
565
566 try {
567
568 var query = "backend.php?op=rpc&id=" + id + "&subop=publ";
569
570 query = query + "&afid=" + getActiveFeedId();
571
572 if (tagsAreDisplayed()) {
573 query = query + "&omode=tl";
574 } else {
575 query = query + "&omode=flc";
576 }
577
578 var mark_img = document.getElementById("FPPIC-" + id);
579 var vfeedu = document.getElementById("FEEDU--2");
580 var crow = document.getElementById("RROW-" + id);
581
582 if (mark_img.src.match("pub_unset")) {
583 mark_img.src = mark_img.src.replace("pub_unset", "pub_set");
584 mark_img.alt = __("Unpublish article");
585 query = query + "&pub=1";
586
587 /* if (vfeedu && crow.className.match("Unread")) {
588 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
589 } */
590
591 } else {
592 //mark_img.src = "images/pub_unset.png";
593 mark_img.alt = __("Please wait...");
594 query = query + "&pub=0";
595
596 /* if (vfeedu && crow.className.match("Unread")) {
597 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
598 } */
599
600 if (document.getElementById("headlinesList") && !no_effects) {
601 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tPub_afh_off});
602 } else {
603 mark_img.src = mark_img.src.replace("pub_set", "pub_unset");
604 mark_img.alt = __("Publish article");
605 }
606 }
607
608 /* var vfeedctr = document.getElementById("FEEDCTR--2");
609 var vfeedr = document.getElementById("FEEDR--2");
610
611 if (vfeedu && vfeedctr) {
612 if ((+vfeedu.innerHTML) > 0) {
613 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
614 vfeedr.className = vfeedr.className + "Unread";
615 vfeedctr.className = "odd";
616 }
617 } else {
618 vfeedctr.className = "invisible";
619 vfeedr.className = vfeedr.className.replace("Unread", "");
620 }
621 }
622
623 debug("toggle published for aid " + id);
624
625 new Ajax.Request(query); */
626
627 if (!client_only) {
628 new Ajax.Request(query, {
629 onComplete: function(transport) {
630 all_counters_callback2(transport);
631 } });
632 }
633
634 } catch (e) {
635
636 exception_error("togglePub", e);
637 }
638 }
639
640 function correctHeadlinesOffset(id) {
641
642 try {
643
644 var hlist = document.getElementById("headlinesList");
645 var container = document.getElementById("headlinesInnerContainer");
646 var row = document.getElementById("RROW-" + id);
647
648 var viewport = container.offsetHeight;
649
650 var rel_offset_top = row.offsetTop - container.scrollTop;
651 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
652
653 debug("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
654 debug("Vport: " + viewport);
655
656 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
657 container.scrollTop = row.offsetTop;
658 } else if (rel_offset_bottom > viewport) {
659
660 /* doesn't properly work with Opera in some cases because
661 Opera fucks up element scrolling */
662
663 container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
664 }
665
666 } catch (e) {
667 exception_error("correctHeadlinesOffset", e);
668 }
669
670 }
671
672 function moveToPost(mode) {
673
674 try {
675
676 // check for combined mode
677 if (document.getElementById("headlinesList")) {
678
679 var rows = getVisibleHeadlineIds();
680
681 var prev_id = false;
682 var next_id = false;
683
684 if (!document.getElementById('RROW-' + active_post_id)) {
685 active_post_id = false;
686 }
687
688 if (active_post_id == false) {
689 next_id = getFirstVisibleHeadlineId();
690 prev_id = getLastVisibleHeadlineId();
691 } else {
692 for (var i = 0; i < rows.length; i++) {
693 if (rows[i] == active_post_id) {
694 prev_id = rows[i-1];
695 next_id = rows[i+1];
696 }
697 }
698 }
699
700 if (mode == "next") {
701 if (next_id) {
702 correctHeadlinesOffset(next_id);
703 view(next_id, getActiveFeedId());
704 }
705 }
706
707 if (mode == "prev") {
708 if (prev_id) {
709 correctHeadlinesOffset(prev_id);
710 view(prev_id, getActiveFeedId());
711 }
712 }
713 } else {
714 var rows = cdmGetUnreadArticles();
715
716 if (mode == "next") {
717
718 for (var i = 0; i < rows.length; i++) {
719
720 if (cdmArticleIsActuallyVisible(rows[i]) ||
721 cdmArticleIsBelowViewport(rows[i])) {
722
723 if (!cdmArticleIsActuallyVisible(rows[i])) {
724 cdmScrollToArticleId(rows[i]);
725 }
726 //setTimeout("toggleUnread(" + rows[i] + ", undefined, true)", 500);
727 toggleUnread(rows[i], undefined, true);
728
729 return;
730 }
731 }
732
733 cdmScrollViewport('bottom');
734
735 } else if (mode == "prev") {
736
737 for (var i = 0; i < rows.length; i++) {
738
739 if (cdmArticleIsAboveViewport(rows[i]) &&
740 !cdmArticleIsAboveViewport(rows[i+1])) {
741
742 cdmScrollToArticleId(rows[i]);
743 //setTimeout("toggleUnread(" + rows[i] + ", undefined, true)", 500);
744 toggleUnread(rows[i], undefined, true);
745
746 break;
747 }
748 }
749
750 }
751
752 }
753
754 } catch (e) {
755 exception_error(e, "moveToPost");
756 }
757 }
758
759 function toggleSelected(id) {
760 try {
761
762 var row = document.getElementById("RROW-" + id);
763 if (row) {
764 var nc = row.className;
765
766 if (!nc.match("Selected")) {
767 nc = nc + "Selected";
768 } else {
769 nc = nc.replace("Selected", "");
770 }
771
772 row.className = nc;
773 }
774 } catch (e) {
775 exception_error(e, "toggleSelected");
776 }
777 }
778
779 function toggleUnread_afh(effect) {
780 try {
781
782 var elem = effect.element;
783 elem.style.backgroundColor = "";
784
785 } catch (e) {
786 exception_error(e, "toggleUnread_afh");
787 }
788 }
789
790 function toggleUnread(id, cmode, effect) {
791 try {
792
793 var row = document.getElementById("RROW-" + id);
794 if (row) {
795 var nc = row.className;
796 nc = nc.replace("Unread", "");
797 nc = nc.replace("Selected", "");
798
799 // since we are removing selection from the object, uncheck
800 // corresponding checkbox
801
802 var cb = document.getElementById("RCHK-" + id);
803 if (cb) {
804 cb.checked = false;
805 }
806
807 // NOTE: I'm not sure that resetting selection here is a feature -fox
808
809 if (cmode == undefined || cmode == 2) {
810 if (row.className.match("Unread")) {
811 row.className = nc;
812
813 if (effect) {
814 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
815 afterFinish: toggleUnread_afh,
816 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
817 }
818
819 } else {
820 row.className = nc + "Unread";
821 }
822 } else if (cmode == 0) {
823 row.className = nc;
824
825 if (effect) {
826 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
827 afterFinish: toggleUnread_afh,
828 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
829 }
830 } else if (cmode == 1) {
831 row.className = nc + "Unread";
832 }
833
834 if (cmode == undefined) cmode = 2;
835
836 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
837 param_escape(id) + "&cmode=" + param_escape(cmode);
838
839 // notify_progress("Loading, please wait...");
840
841 new Ajax.Request(query, {
842 onComplete: function(transport) {
843 all_counters_callback2(transport);
844 } });
845
846 }
847
848
849 } catch (e) {
850 exception_error("toggleUnread", e);
851 }
852 }
853
854 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
855 try {
856 var rows;
857
858 if (cdm_mode) {
859 rows = cdmGetSelectedArticles();
860 } else {
861 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
862 }
863
864 if (rows.length == 0 && !no_error) {
865 alert(__("No articles are selected."));
866 return;
867 }
868
869 for (i = 0; i < rows.length; i++) {
870 var row = document.getElementById("RROW-" + rows[i]);
871 if (row) {
872 var nc = row.className;
873 nc = nc.replace("Unread", "");
874 nc = nc.replace("Selected", "");
875
876 if (set_state == undefined) {
877 if (row.className.match("Unread")) {
878 row.className = nc + "Selected";
879 } else {
880 row.className = nc + "UnreadSelected";
881 }
882 }
883
884 if (set_state == false) {
885 row.className = nc + "Selected";
886 }
887
888 if (set_state == true) {
889 row.className = nc + "UnreadSelected";
890 }
891 }
892 }
893
894 if (rows.length > 0) {
895
896 var cmode = "";
897
898 if (set_state == undefined) {
899 cmode = "2";
900 } else if (set_state == true) {
901 cmode = "1";
902 } else if (set_state == false) {
903 cmode = "0";
904 }
905
906 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
907 param_escape(rows.toString()) + "&cmode=" + cmode;
908
909 notify_progress("Loading, please wait...");
910
911 new Ajax.Request(query, {
912 onComplete: function(transport) {
913 catchup_callback2(transport, callback_func);
914 } });
915
916 }
917
918 } catch (e) {
919 exception_error("selectionToggleUnread", e);
920 }
921 }
922
923 function selectionToggleMarked(cdm_mode) {
924 try {
925
926 var rows;
927
928 if (cdm_mode) {
929 rows = cdmGetSelectedArticles();
930 } else {
931 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
932 }
933
934 if (rows.length == 0) {
935 alert(__("No articles are selected."));
936 return;
937 }
938
939 for (i = 0; i < rows.length; i++) {
940 toggleMark(rows[i], true, true);
941 }
942
943 if (rows.length > 0) {
944
945 var query = "backend.php?op=rpc&subop=markSelected&ids=" +
946 param_escape(rows.toString()) + "&cmode=2";
947
948 query = query + "&afid=" + getActiveFeedId();
949
950 /* if (tagsAreDisplayed()) {
951 query = query + "&omode=tl";
952 } else {
953 query = query + "&omode=flc";
954 } */
955
956 query = query + "&omode=lc";
957
958 new Ajax.Request(query, {
959 onComplete: function(transport) {
960 all_counters_callback2(transport);
961 } });
962
963 }
964
965 } catch (e) {
966 exception_error("selectionToggleMarked", e);
967 }
968 }
969
970 function selectionTogglePublished(cdm_mode) {
971 try {
972
973 var rows;
974
975 if (cdm_mode) {
976 rows = cdmGetSelectedArticles();
977 } else {
978 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
979 }
980
981 if (rows.length == 0) {
982 alert(__("No articles are selected."));
983 return;
984 }
985
986 for (i = 0; i < rows.length; i++) {
987 togglePub(rows[i], true, true);
988 }
989
990 if (rows.length > 0) {
991
992 var query = "backend.php?op=rpc&subop=publishSelected&ids=" +
993 param_escape(rows.toString()) + "&cmode=2";
994
995 query = query + "&afid=" + getActiveFeedId();
996
997 /* if (tagsAreDisplayed()) {
998 query = query + "&omode=tl";
999 } else {
1000 query = query + "&omode=flc";
1001 } */
1002
1003 query = query + "&omode=lc";
1004
1005 new Ajax.Request(query, {
1006 onComplete: function(transport) {
1007 all_counters_callback2(transport);
1008 } });
1009
1010 }
1011
1012 } catch (e) {
1013 exception_error("selectionToggleMarked", e);
1014 }
1015 }
1016
1017 function cdmGetSelectedArticles() {
1018 var sel_articles = new Array();
1019 var container = document.getElementById("headlinesInnerContainer");
1020
1021 for (i = 0; i < container.childNodes.length; i++) {
1022 var child = container.childNodes[i];
1023
1024 if (child.id.match("RROW-") && child.className.match("Selected")) {
1025 var c_id = child.id.replace("RROW-", "");
1026 sel_articles.push(c_id);
1027 }
1028 }
1029
1030 return sel_articles;
1031 }
1032
1033 function cdmGetVisibleArticles() {
1034 var sel_articles = new Array();
1035 var container = document.getElementById("headlinesInnerContainer");
1036
1037 for (i = 0; i < container.childNodes.length; i++) {
1038 var child = container.childNodes[i];
1039
1040 if (child.id.match("RROW-")) {
1041 var c_id = child.id.replace("RROW-", "");
1042 sel_articles.push(c_id);
1043 }
1044 }
1045
1046 return sel_articles;
1047 }
1048
1049 function cdmGetUnreadArticles() {
1050 var sel_articles = new Array();
1051 var container = document.getElementById("headlinesInnerContainer");
1052
1053 for (i = 0; i < container.childNodes.length; i++) {
1054 var child = container.childNodes[i];
1055
1056 if (child.id.match("RROW-") && child.className.match("Unread")) {
1057 var c_id = child.id.replace("RROW-", "");
1058 sel_articles.push(c_id);
1059 }
1060 }
1061
1062 return sel_articles;
1063 }
1064
1065
1066 // mode = all,none,unread
1067 function cdmSelectArticles(mode) {
1068 var container = document.getElementById("headlinesInnerContainer");
1069
1070 for (i = 0; i < container.childNodes.length; i++) {
1071 var child = container.childNodes[i];
1072
1073 if (child.id.match("RROW-")) {
1074 var aid = child.id.replace("RROW-", "");
1075
1076 var cb = document.getElementById("RCHK-" + aid);
1077
1078 if (mode == "all") {
1079 if (!child.className.match("Selected")) {
1080 child.className = child.className + "Selected";
1081 cb.checked = true;
1082 }
1083 } else if (mode == "unread") {
1084 if (child.className.match("Unread") && !child.className.match("Selected")) {
1085 child.className = child.className + "Selected";
1086 cb.checked = true;
1087 }
1088 } else {
1089 child.className = child.className.replace("Selected", "");
1090 cb.checked = false;
1091 }
1092 }
1093 }
1094 }
1095
1096 function catchupPage() {
1097
1098 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
1099
1100 var str = __("Mark all visible articles in %s as read?");
1101
1102 str = str.replace("%s", fn);
1103
1104 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1105 return;
1106 }
1107
1108 if (document.getElementById("headlinesList")) {
1109 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
1110 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
1111 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
1112 } else {
1113 cdmSelectArticles('all');
1114 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
1115 cdmSelectArticles('none');
1116 }
1117 }
1118
1119 function catchupSelection() {
1120
1121 try {
1122
1123 var rows;
1124
1125 if (document.getElementById("headlinesList")) {
1126 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
1127 } else {
1128 rows = cdmGetSelectedArticles();
1129 }
1130
1131 if (rows.length == 0) {
1132 alert(__("No articles are selected."));
1133 return;
1134 }
1135
1136
1137 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
1138
1139 var str = __("Mark %d selected articles in %s as read?");
1140
1141 str = str.replace("%d", rows.length);
1142 str = str.replace("%s", fn);
1143
1144 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1145 return;
1146 }
1147
1148 if (document.getElementById("headlinesList")) {
1149 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
1150 // selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
1151 } else {
1152 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
1153 // cdmSelectArticles('none');
1154 }
1155
1156 } catch (e) {
1157 exception_error("catchupSelection", e);
1158 }
1159 }
1160
1161
1162 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
1163
1164 if (!xmlhttp_ready(xmlhttp_rpc)) {
1165 printLockingError();
1166 }
1167
1168 var title = prompt(__("Please enter label title:"), "");
1169
1170 if (title) {
1171
1172 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
1173 "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
1174 "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) +
1175 "&title=" + param_escape(title);
1176
1177 debug("LFS: " + query);
1178
1179 new Ajax.Request(query, {
1180 onComplete: function(transport) {
1181 dlg_frefresh_callback(transport);
1182 } });
1183 }
1184 }
1185
1186 function editArticleTags(id, feed_id, cdm_enabled) {
1187 _tag_active_post_id = id;
1188 _tag_active_feed_id = feed_id;
1189 _tag_active_cdm = cdm_enabled;
1190
1191 cache_invalidate(id);
1192
1193 try {
1194 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
1195 } catch (e) { }
1196 displayDlg('editArticleTags', id);
1197 }
1198
1199
1200 function tag_saved_callback(transport) {
1201 try {
1202 debug("in tag_saved_callback");
1203
1204 closeInfoBox();
1205 notify("");
1206
1207 if (tagsAreDisplayed()) {
1208 _reload_feedlist_after_view = true;
1209 }
1210
1211 if (!_tag_active_cdm) {
1212 if (active_post_id == _tag_active_post_id) {
1213 debug("reloading current article");
1214 view(_tag_active_post_id, _tag_active_feed_id);
1215 }
1216 } else {
1217 debug("reloading current feed");
1218 viewCurrentFeed();
1219 }
1220
1221 } catch (e) {
1222 exception_error("catchup_callback", e);
1223 }
1224 }
1225
1226 function editTagsSave() {
1227
1228 notify_progress("Saving article tags...");
1229
1230 var form = document.forms["tag_edit_form"];
1231
1232 var query = Form.serialize("tag_edit_form");
1233
1234 query = "backend.php?op=rpc&subop=setArticleTags&" + query;
1235
1236 debug(query);
1237
1238 new Ajax.Request(query, {
1239 onComplete: function(transport) {
1240 tag_saved_callback(transport);
1241 } });
1242
1243 }
1244
1245 function editTagsInsert() {
1246 try {
1247
1248 var form = document.forms["tag_edit_form"];
1249
1250 var found_tags = form.found_tags;
1251 var tags_str = form.tags_str;
1252
1253 var tag = found_tags[found_tags.selectedIndex].value;
1254
1255 if (tags_str.value.length > 0 &&
1256 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
1257
1258 tags_str.value = tags_str.value + ", ";
1259 }
1260
1261 tags_str.value = tags_str.value + tag + ", ";
1262
1263 found_tags.selectedIndex = 0;
1264
1265 } catch (e) {
1266 exception_error(e, "editTagsInsert");
1267 }
1268 }
1269
1270 function cdmScrollViewport(where) {
1271 debug("cdmScrollViewport: " + where);
1272
1273 var ctr = document.getElementById("headlinesInnerContainer");
1274
1275 if (!ctr) return;
1276
1277 if (where == "bottom") {
1278 ctr.scrollTop = ctr.scrollHeight;
1279 } else {
1280 ctr.scrollTop = where;
1281 }
1282 }
1283
1284 function cdmArticleIsBelowViewport(id) {
1285 try {
1286 var ctr = document.getElementById("headlinesInnerContainer");
1287 var e = document.getElementById("RROW-" + id);
1288
1289 if (!e || !ctr) return;
1290
1291 // article starts below viewport
1292
1293 if (ctr.scrollTop < e.offsetTop) {
1294 return true;
1295 } else {
1296 return false;
1297 }
1298
1299 } catch (e) {
1300 exception_error(e, "cdmArticleIsVisible");
1301 }
1302 }
1303
1304 function cdmArticleIsAboveViewport(id) {
1305 try {
1306 var ctr = document.getElementById("headlinesInnerContainer");
1307 var e = document.getElementById("RROW-" + id);
1308
1309 if (!e || !ctr) return;
1310
1311 // article starts above viewport
1312
1313 if (ctr.scrollTop > e.offsetTop + e.offsetHeight) {
1314 return true;
1315 } else {
1316 return false;
1317 }
1318
1319 } catch (e) {
1320 exception_error(e, "cdmArticleIsVisible");
1321 }
1322 }
1323
1324 function cdmScrollToArticleId(id) {
1325 try {
1326 var ctr = document.getElementById("headlinesInnerContainer");
1327 var e = document.getElementById("RROW-" + id);
1328
1329 if (!e || !ctr) return;
1330
1331 ctr.scrollTop = e.offsetTop;
1332
1333 } catch (e) {
1334 exception_error(e, "cdmScrollToArticleId");
1335 }
1336 }
1337
1338 function cdmArticleIsActuallyVisible(id) {
1339 try {
1340 var ctr = document.getElementById("headlinesInnerContainer");
1341 var e = document.getElementById("RROW-" + id);
1342
1343 if (!e || !ctr) return;
1344
1345 // article fits in viewport OR article is longer than viewport and
1346 // its bottom is visible
1347
1348 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1349 ctr.scrollTop + ctr.offsetHeight) {
1350
1351 return true;
1352
1353 } else if (e.offsetHeight > ctr.offsetHeight &&
1354 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1355 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1356
1357 return true;
1358
1359 }
1360
1361 return false;
1362
1363 } catch (e) {
1364 exception_error(e, "cdmArticleIsVisible");
1365 }
1366 }
1367
1368 function cdmWatchdog() {
1369
1370 try {
1371
1372 var ctr = document.getElementById("headlinesInnerContainer");
1373
1374 if (!ctr) return;
1375
1376 var ids = new Array();
1377
1378 var e = ctr.firstChild;
1379
1380 while (e) {
1381 if (e.className && e.className == "cdmArticleUnread" && e.id &&
1382 e.id.match("RROW-")) {
1383
1384 // article fits in viewport OR article is longer than viewport and
1385 // its bottom is visible
1386
1387 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1388 ctr.scrollTop + ctr.offsetHeight) {
1389
1390 // debug(e.id + " is visible " + e.offsetTop + "." +
1391 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
1392 // (ctr.scrollTop + ctr.offsetHeight));
1393
1394 ids.push(e.id.replace("RROW-", ""));
1395
1396 } else if (e.offsetHeight > ctr.offsetHeight &&
1397 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1398 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1399
1400 ids.push(e.id.replace("RROW-", ""));
1401
1402 }
1403
1404 // method 2: article bottom is visible and is in upper 1/2 of the viewport
1405
1406 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1407 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
1408
1409 ids.push(e.id.replace("RROW-", ""));
1410
1411 } */
1412
1413 }
1414
1415 e = e.nextSibling;
1416 }
1417
1418 debug("cdmWatchdog, ids= " + ids.toString());
1419
1420 if (ids.length > 0) {
1421
1422 for (var i = 0; i < ids.length; i++) {
1423 var e = document.getElementById("RROW-" + ids[i]);
1424 if (e) {
1425 e.className = e.className.replace("Unread", "");
1426 }
1427 }
1428
1429 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
1430 param_escape(ids.toString()) + "&cmode=0";
1431
1432 new Ajax.Request(query, {
1433 onComplete: function(transport) {
1434 all_counters_callback2(transport);
1435 } });
1436
1437 }
1438
1439 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
1440
1441 } catch (e) {
1442 exception_error(e, "cdmWatchdog");
1443 }
1444
1445 }
1446
1447
1448 function cache_inject(id, article, param) {
1449 if (!cache_check_param(id, param)) {
1450 debug("cache_article: miss: " + id + " [p=" + param + "]");
1451
1452 var cache_obj = new Array();
1453
1454 cache_obj["id"] = id;
1455 cache_obj["data"] = article;
1456 cache_obj["param"] = param;
1457
1458 article_cache.push(cache_obj);
1459
1460 } else {
1461 debug("cache_article: hit: " + id + " [p=" + param + "]");
1462 }
1463 }
1464
1465 function cache_find(id) {
1466 for (var i = 0; i < article_cache.length; i++) {
1467 if (article_cache[i]["id"] == id) {
1468 return article_cache[i]["data"];
1469 }
1470 }
1471 return false;
1472 }
1473
1474 function cache_find_param(id, param) {
1475 for (var i = 0; i < article_cache.length; i++) {
1476 if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1477 return article_cache[i]["data"];
1478 }
1479 }
1480 return false;
1481 }
1482
1483 function cache_check(id) {
1484 for (var i = 0; i < article_cache.length; i++) {
1485 if (article_cache[i]["id"] == id) {
1486 return true;
1487 }
1488 }
1489 return false;
1490 }
1491
1492 function cache_check_param(id, param) {
1493 for (var i = 0; i < article_cache.length; i++) {
1494
1495 // debug("cache_check_param " + article_cache[i]["id"] + ":" +
1496 // article_cache[i]["param"] + " vs " + id + ":" + param);
1497
1498 if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1499 return true;
1500 }
1501 }
1502 return false;
1503 }
1504
1505 function cache_expire() {
1506 while (article_cache.length > 20) {
1507 article_cache.shift();
1508 }
1509 }
1510
1511 function cache_empty() {
1512 article_cache = new Array();
1513 }
1514
1515 function cache_invalidate(id) {
1516 var i = 0
1517
1518 try {
1519
1520 while (i < article_cache.length) {
1521 if (article_cache[i]["id"] == id) {
1522 debug("cache_invalidate: removed id " + id);
1523 article_cache.splice(i, 1);
1524 return true;
1525 }
1526 i++;
1527 }
1528 debug("cache_invalidate: id not found: " + id);
1529 return false;
1530 } catch (e) {
1531 exception_error("cache_invalidate", e);
1532 }
1533 }
1534
1535 function getActiveArticleId() {
1536 return active_post_id;
1537 }
1538
1539 function cdmMouseIn(elem) {
1540 try {
1541 if (elem.id && elem.id.match("RROW-")) {
1542 var id = elem.id.replace("RROW-", "");
1543 active_post_id = id;
1544 }
1545 } catch (e) {
1546 exception_error("cdmMouseIn", e);
1547 }
1548
1549 }
1550
1551 function cdmMouseOut(elem) {
1552 active_post_id = false;
1553 }
1554
1555 function headlines_scroll_handler() {
1556 try {
1557
1558 var e = document.getElementById("headlinesInnerContainer");
1559
1560 // don't do infinite scrolling when Limit == All
1561
1562 var toolbar_form = document.forms["main_toolbar_form"];
1563
1564 var limit = toolbar_form.limit[toolbar_form.limit.selectedIndex];
1565 if (limit.value != 0) {
1566
1567 debug((e.scrollTop + e.offsetHeight) + " vs " + e.scrollHeight + " dis? " +
1568 _infscroll_disable);
1569
1570 if (e.scrollTop + e.offsetHeight > e.scrollHeight - 100) {
1571 if (!_infscroll_disable) {
1572 debug("more cowbell!");
1573 viewNextFeedPage();
1574 }
1575 }
1576 }
1577
1578 } catch (e) {
1579 exception_error("headlines_scroll_handler", e);
1580 }
1581 }
1582
1583 function catchupRelativeToArticle(below) {
1584
1585 try {
1586
1587 if (!xmlhttp_ready(xmlhttp_rpc)) {
1588 printLockingError();
1589 }
1590
1591 if (!getActiveArticleId()) {
1592 alert(__("No article is selected."));
1593 return;
1594 }
1595
1596 var visible_ids;
1597
1598 if (document.getElementById("headlinesList")) {
1599 visible_ids = getVisibleHeadlineIds();
1600 } else {
1601 visible_ids = cdmGetVisibleArticles();
1602 }
1603
1604 var ids_to_mark = new Array();
1605
1606 if (!below) {
1607 for (var i = 0; i < visible_ids.length; i++) {
1608 if (visible_ids[i] != getActiveArticleId()) {
1609 var e = document.getElementById("RROW-" + visible_ids[i]);
1610
1611 if (e && e.className.match("Unread")) {
1612 ids_to_mark.push(visible_ids[i]);
1613 }
1614 } else {
1615 break;
1616 }
1617 }
1618 } else {
1619 for (var i = visible_ids.length-1; i >= 0; i--) {
1620 if (visible_ids[i] != getActiveArticleId()) {
1621 var e = document.getElementById("RROW-" + visible_ids[i]);
1622
1623 if (e && e.className.match("Unread")) {
1624 ids_to_mark.push(visible_ids[i]);
1625 }
1626 } else {
1627 break;
1628 }
1629 }
1630 }
1631
1632 if (ids_to_mark.length == 0) {
1633 alert(__("No articles found to mark"));
1634 } else {
1635 var msg = __("Mark %d article(s) as read?").replace("%d", ids_to_mark.length);
1636
1637 if (confirm(msg)) {
1638
1639 for (var i = 0; i < ids_to_mark.length; i++) {
1640 var e = document.getElementById("RROW-" + ids_to_mark[i]);
1641 e.className = e.className.replace("Unread", "");
1642 }
1643
1644 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
1645 param_escape(ids_to_mark.toString()) + "&cmode=0";
1646
1647 new Ajax.Request(query, {
1648 onComplete: function(transport) {
1649 catchup_callback2(transport);
1650 } });
1651
1652 }
1653 }
1654
1655 } catch (e) {
1656 exception_error("catchupRelativeToArticle", e);
1657 }
1658 }
1659
1660 function cdmExpandArticle(a_id) {
1661 try {
1662 var id = 'CICD-' + a_id;
1663
1664 Effect.Appear(id, {duration : 0.5,
1665 beforeStart: function(effect) {
1666 var h_id = 'CICH-' + a_id;
1667 var h_elem = document.getElementById(h_id);
1668 if (h_elem) { h_elem.style.display = "none"; }
1669
1670 toggleUnread(a_id, 0);
1671 }});
1672
1673
1674 } catch (e) {
1675 exception_error("appearBlockElementF", e);
1676 }
1677
1678 }
1679
1680