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