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;
6 var _tag_active_post_id
= false;
7 var _tag_active_feed_id
= false;
8 var _tag_active_cdm
= false;
10 // FIXME: kludge, to restore scrollTop after tag editor terminates
11 var _tag_cdm_scroll
= false;
13 // FIXME: kludges, needs proper implementation
14 var _reload_feedlist_after_view
= false;
16 var _cdm_wd_timeout
= false;
17 var _cdm_wd_vishist
= new Array();
19 var article_cache
= new Array();
21 function catchup_callback() {
22 if (xmlhttp_rpc
.readyState
== 4) {
24 debug("catchup_callback");
26 all_counters_callback();
27 if (_catchup_callback_func
) {
28 setTimeout(_catchup_callback_func
, 10);
31 exception_error("catchup_callback", e
);
36 function headlines_callback() {
37 if (xmlhttp
.readyState
== 4) {
38 debug("headlines_callback");
39 var f
= document
.getElementById("headlines-frame");
41 if (feed_cur_page
== 0) {
42 debug("resetting headlines scrollTop");
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");
53 if (feed_cur_page
== 0) {
55 f
.innerHTML
= headlines
.firstChild
.nodeValue
;
57 debug("headlines_callback: returned no data");
58 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
63 debug("adding some more headlines...");
65 var c
= document
.getElementById("headlinesList");
68 c
= document
.getElementById("headlinesInnerContainer");
71 c
.innerHTML
= c
.innerHTML
+ headlines
.firstChild
.nodeValue
;
73 debug("headlines_callback: returned no data");
74 notify_error("Error while trying to load more headlines");
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
);
86 debug("no cached articles received");
90 debug("parsing piggybacked counters: " + counters
);
91 parse_counters(counters
, false);
93 debug("counters container not found in reply");
97 debug("parsing runtime info: " + runtime_info
[0]);
98 parse_runtime_info(runtime_info
[0]);
100 debug("counters container not found in reply");
104 debug("headlines_callback: returned no XML object");
105 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
108 if (typeof correctPNG
!= 'undefined') {
112 if (_cdm_wd_timeout
) window
.clearTimeout(_cdm_wd_timeout
);
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();
120 debug("not in CDM mode or watchdog disabled");
123 if (_tag_cdm_scroll
) {
125 document
.getElementById("headlinesInnerContainer").scrollTop
= _tag_cdm_scroll
;
126 _tag_cdm_scroll
= false;
127 debug("resetting headlinesInner scrollTop");
136 function render_article(article
) {
138 var f
= document
.getElementById("content-frame");
143 f
.innerHTML
= article
;
146 exception_error("render_article", e
);
150 function article_callback() {
151 if (xmlhttp
.readyState
== 4) {
152 debug("article_callback");
155 if (xmlhttp
.responseXML
) {
156 var reply
= xmlhttp
.responseXML
.firstChild
.firstChild
;
158 var articles
= xmlhttp
.responseXML
.getElementsByTagName("article");
160 for (var i
= 0; i
< articles
.length
; i
++) {
161 var a_id
= articles
[i
].getAttribute("id");
163 debug("found id: " + a_id
);
165 if (a_id
== active_post_id
) {
166 debug("active article, rendering...");
167 render_article(articles
[i
].firstChild
.nodeValue
);
170 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
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>";
179 exception_error("article_callback", e
);
182 var date
= new Date();
183 last_article_view
= date
.getTime() / 1000;
185 if (typeof correctPNG
!= 'undefined') {
189 if (_reload_feedlist_after_view
) {
190 setTimeout('updateFeedList(false, false)', 50);
191 _reload_feedlist_after_view
= false;
193 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
196 debug("parsing piggybacked counters: " + counters
);
197 parse_counters(counters
, false);
199 debug("counters container not found in reply");
207 function view(id
, feed_id
, skip_history
) {
210 debug("loading article: " + id
+ "/" + feed_id
);
212 active_real_feed_id
= feed_id
;
214 var cached_article
= cache_find(id
);
216 debug("cache check result: " + (cached_article
!= false));
220 //setActiveFeedId(feed_id);
222 var query
= "backend.php?op=view&id=" + param_escape(id
) +
223 "&feed=" + param_escape(feed_id
);
225 var date
= new Date();
227 if (!xmlhttp_ready(xmlhttp
) && last_article_view
< date
.getTime() / 1000 - 15) {
228 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
231 debug("trying alternative reset method for Safari");
232 xmlhttp
= Ajax
.getTransport();
236 if (xmlhttp_ready(xmlhttp
)) {
240 cleanSelected("headlinesList");
242 var crow
= document
.getElementById("RROW-" + active_post_id
);
244 var article_is_unread
= crow
.className
.match("Unread");
245 debug("article is unread: " + article_is_unread
);
247 crow
.className
= crow
.className
.replace("Unread", "");
249 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
252 upd_img_pic
.src
= "images/blank_icon.gif";
255 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
256 markHeadline(active_post_id
);
258 var neighbor_ids
= getRelativePostIds(active_post_id
);
260 /* only request uncached articles */
262 var cids_to_request
= Array();
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
]);
270 debug("additional ids: " + cids_to_request
.toString());
272 /* additional info for piggyback counters */
274 if (tagsAreDisplayed()) {
275 query
= query
+ "&omode=lt";
277 query
= query
+ "&omode=flc";
280 var date
= new Date();
281 var timestamp
= Math
.round(date
.getTime() / 1000);
282 query
= query
+ "&ts=" + timestamp
;
284 query
= query
+ "&cids=" + cids_to_request
.toString();
286 if (!cached_article
) {
288 notify_progress("Loading, please wait...");
292 xmlhttp
.open("GET", query
, true);
293 xmlhttp
.onreadystatechange
=article_callback
;
295 } else if (cached_article
&& article_is_unread
) {
297 query
= query
+ "&mode=prefetch";
301 xmlhttp
.open("GET", query
, true);
302 xmlhttp
.onreadystatechange
=article_callback
;
305 render_article(cached_article
);
307 } else if (cached_article
) {
309 query
= query
+ "&mode=prefetch_old";
313 xmlhttp
.open("GET", query
, true);
314 xmlhttp
.onreadystatechange
=article_callback
;
317 render_article(cached_article
);
324 debug("xmlhttp busy (@view)");
329 exception_error("view", e
);
334 return toggleMark(id
);
337 function toggleMark(id
) {
339 if (!xmlhttp_ready(xmlhttp_rpc
)) {
344 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
346 var mark_img
= document
.getElementById("FMPIC-" + id
);
347 var vfeedu
= document
.getElementById("FEEDU--1");
348 var crow
= document
.getElementById("RROW-" + id
);
350 if (mark_img
.alt
!= "Reset mark") {
351 mark_img
.src
= "images/mark_set.png";
352 mark_img
.alt
= "Reset mark";
353 query
= query
+ "&mark=1";
355 if (vfeedu
&& crow
.className
.match("Unread")) {
356 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
360 mark_img
.src
= "images/mark_unset.png";
361 mark_img
.alt
= "Set mark";
362 query
= query
+ "&mark=0";
364 if (vfeedu
&& crow
.className
.match("Unread")) {
365 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
370 var vfeedctr
= document
.getElementById("FEEDCTR--1");
371 var vfeedr
= document
.getElementById("FEEDR--1");
373 if (vfeedu
&& vfeedctr
) {
374 if ((+vfeedu
.innerHTML
) > 0) {
375 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
376 vfeedr
.className
= vfeedr
.className
+ "Unread";
377 vfeedctr
.className
= "odd";
380 vfeedctr
.className
= "invisible";
381 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
385 debug("toggle starred for aid " + id
);
387 new Ajax
.Request(query
);
391 function correctHeadlinesOffset(id
) {
395 var hlist
= document
.getElementById("headlinesList");
396 var container
= document
.getElementById("headlinesInnerContainer");
397 var row
= document
.getElementById("RROW-" + id
);
399 var viewport
= container
.offsetHeight
;
401 var rel_offset_top
= row
.offsetTop
- container
.scrollTop
;
402 var rel_offset_bottom
= row
.offsetTop
+ row
.offsetHeight
- container
.scrollTop
;
404 debug("Rtop: " + rel_offset_top
+ " Rbtm: " + rel_offset_bottom
);
405 debug("Vport: " + viewport
);
407 if (rel_offset_top
<= 0 || rel_offset_top
> viewport
) {
408 container
.scrollTop
= row
.offsetTop
;
409 } else if (rel_offset_bottom
> viewport
) {
411 /* doesn't properly work with Opera in some cases because
412 Opera fucks up element scrolling */
414 container
.scrollTop
= row
.offsetTop
+ row
.offsetHeight
- viewport
;
418 exception_error("correctHeadlinesOffset", e
);
423 function moveToPost(mode
) {
425 // check for combined mode
426 if (!document
.getElementById("headlinesList"))
429 var rows
= getVisibleHeadlineIds();
434 if (!document
.getElementById('RROW-' + active_post_id
)) {
435 active_post_id
= false;
438 if (active_post_id
== false) {
439 next_id
= getFirstVisibleHeadlineId();
440 prev_id
= getLastVisibleHeadlineId();
442 for (var i
= 0; i
< rows
.length
; i
++) {
443 if (rows
[i
] == active_post_id
) {
450 if (mode
== "next") {
452 correctHeadlinesOffset(next_id
);
453 view(next_id
, getActiveFeedId());
457 if (mode
== "prev") {
459 correctHeadlinesOffset(prev_id
);
460 view(prev_id
, getActiveFeedId());
465 function toggleUnread(id
, cmode
) {
467 if (!xmlhttp_ready(xmlhttp_rpc
)) {
472 var row
= document
.getElementById("RROW-" + id
);
474 var nc
= row
.className
;
475 nc
= nc
.replace("Unread", "");
476 nc
= nc
.replace("Selected", "");
478 if (row
.className
.match("Unread")) {
481 row
.className
= nc
+ "Unread";
484 if (!cmode
) cmode
= 2;
486 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
487 param_escape(id
) + "&cmode=" + param_escape(cmode
);
489 notify_progress("Loading, please wait...");
491 xmlhttp_rpc
.open("GET", query
, true);
492 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
493 xmlhttp_rpc
.send(null);
499 exception_error("toggleUnread", e
);
503 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
505 if (!xmlhttp_ready(xmlhttp_rpc
)) {
513 rows
= cdmGetSelectedArticles();
515 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
518 if (rows
.length
== 0 && !no_error
) {
519 alert(__("No articles are selected."));
523 for (i
= 0; i
< rows
.length
; i
++) {
524 var row
= document
.getElementById("RROW-" + rows
[i
]);
526 var nc
= row
.className
;
527 nc
= nc
.replace("Unread", "");
528 nc
= nc
.replace("Selected", "");
530 if (row
.className
.match("Unread")) {
531 row
.className
= nc
+ "Selected";
533 row
.className
= nc
+ "UnreadSelected";
538 if (rows
.length
> 0) {
542 if (set_state
== undefined) {
544 } else if (set_state
== true) {
546 } else if (set_state
== false) {
550 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
551 param_escape(rows
.toString()) + "&cmode=" + cmode
;
553 _catchup_callback_func
= callback_func
;
555 notify_progress("Loading, please wait...");
557 xmlhttp_rpc
.open("GET", query
, true);
558 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
559 xmlhttp_rpc
.send(null);
564 exception_error("selectionToggleUnread", e
);
568 function selectionToggleMarked(cdm_mode
) {
570 if (!xmlhttp_ready(xmlhttp_rpc
)) {
578 rows
= cdmGetSelectedArticles();
580 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
583 if (rows
.length
== 0) {
584 alert(__("No articles are selected."));
588 for (i
= 0; i
< rows
.length
; i
++) {
589 var row
= document
.getElementById("RROW-" + rows
[i
]);
590 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
592 if (row
&& mark_img
) {
594 if (mark_img
.alt
== "Set mark") {
595 mark_img
.src
= "images/mark_set.png";
596 mark_img
.alt
= "Reset mark";
597 mark_img
.setAttribute('onclick',
598 'javascript:toggleMark('+rows
[i
]+', false)');
601 mark_img
.src
= "images/mark_unset.png";
602 mark_img
.alt
= "Set mark";
603 mark_img
.setAttribute('onclick',
604 'javascript:toggleMark('+rows
[i
]+', true)');
609 if (rows
.length
> 0) {
611 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
612 param_escape(rows
.toString()) + "&cmode=2";
614 xmlhttp_rpc
.open("GET", query
, true);
615 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
616 xmlhttp_rpc
.send(null);
621 exception_error("selectionToggleMarked", e
);
625 function cdmGetSelectedArticles() {
626 var sel_articles
= new Array();
627 var container
= document
.getElementById("headlinesInnerContainer");
629 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
630 var child
= container
.childNodes
[i
];
632 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
633 var c_id
= child
.id
.replace("RROW-", "");
634 sel_articles
.push(c_id
);
641 // mode = all,none,unread
642 function cdmSelectArticles(mode
) {
643 var container
= document
.getElementById("headlinesInnerContainer");
645 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
646 var child
= container
.childNodes
[i
];
648 if (child
.id
.match("RROW-")) {
649 var aid
= child
.id
.replace("RROW-", "");
651 var cb
= document
.getElementById("RCHK-" + aid
);
654 if (!child
.className
.match("Selected")) {
655 child
.className
= child
.className
+ "Selected";
658 } else if (mode
== "unread") {
659 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
660 child
.className
= child
.className
+ "Selected";
664 child
.className
= child
.className
.replace("Selected", "");
671 function catchupPage() {
673 var fn
= getFeedName(getActiveFeedId(), active_feed_is_cat
);
675 var str
= "Mark all visible articles in " + fn
+ " as read?";
677 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str
)) {
681 if (document
.getElementById("headlinesList")) {
682 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
683 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
684 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
686 cdmSelectArticles('all');
687 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
688 cdmSelectArticles('none');
692 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
694 if (!xmlhttp_ready(xmlhttp_rpc
)) {
698 var title
= prompt("Please enter label title:", "");
702 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
703 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
704 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
705 "&title=" + param_escape(title
);
707 debug("LFS: " + query
);
709 xmlhttp_rpc
.open("GET", query
, true);
710 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
711 xmlhttp_rpc
.send(null);
716 function editArticleTags(id
, feed_id
, cdm_enabled
) {
717 _tag_active_post_id
= id
;
718 _tag_active_feed_id
= feed_id
;
719 _tag_active_cdm
= cdm_enabled
;
721 cache_invalidate(id
);
724 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
726 displayDlg('editArticleTags', id
);
730 function tag_saved_callback() {
731 if (xmlhttp_rpc
.readyState
== 4) {
733 debug("in tag_saved_callback");
738 if (tagsAreDisplayed()) {
739 _reload_feedlist_after_view
= true;
742 if (!_tag_active_cdm
) {
743 if (active_post_id
== _tag_active_post_id
) {
744 debug("reloading current article");
745 view(_tag_active_post_id
, _tag_active_feed_id
);
748 debug("reloading current feed");
753 exception_error("catchup_callback", e
);
758 function editTagsSave() {
760 if (!xmlhttp_ready(xmlhttp_rpc
)) {
764 notify_progress("Saving article tags...");
766 var form
= document
.forms
["tag_edit_form"];
768 var query
= Form
.serialize("tag_edit_form");
770 query
= "backend.php?op=rpc&subop=setArticleTags&" + query
;
774 xmlhttp_rpc
.open("GET", query
, true);
775 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
776 xmlhttp_rpc
.send(null);
780 function editTagsInsert() {
783 var form
= document
.forms
["tag_edit_form"];
785 var found_tags
= form
.found_tags
;
786 var tags_str
= form
.tags_str
;
788 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
790 if (tags_str
.value
.length
> 0 &&
791 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
793 tags_str
.value
= tags_str
.value
+ ", ";
796 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
798 found_tags
.selectedIndex
= 0;
801 exception_error(e
, "editTagsInsert");
805 function cdmWatchdog() {
809 var ctr
= document
.getElementById("headlinesInnerContainer");
813 var ids
= new Array();
815 var e
= ctr
.firstChild
;
818 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
819 e
.id
.match("RROW-")) {
821 // article fits in viewport OR article is longer than viewport and
822 // its bottom is visible
824 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
825 ctr
.scrollTop
+ ctr
.offsetHeight
) {
827 // debug(e.id + " is visible " + e.offsetTop + "." +
828 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
829 // (ctr.scrollTop + ctr.offsetHeight));
831 ids
.push(e
.id
.replace("RROW-", ""));
833 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
834 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
835 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
837 ids
.push(e
.id
.replace("RROW-", ""));
841 // method 2: article bottom is visible and is in upper 1/2 of the viewport
843 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
844 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
846 ids.push(e.id.replace("RROW-", ""));
855 debug("cdmWatchdog, ids= " + ids
.toString());
857 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
859 for (var i
= 0; i
< ids
.length
; i
++) {
860 var e
= document
.getElementById("RROW-" + ids
[i
]);
862 e
.className
= e
.className
.replace("Unread", "");
866 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
867 param_escape(ids
.toString()) + "&cmode=0";
869 xmlhttp_rpc
.open("GET", query
, true);
870 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
871 xmlhttp_rpc
.send(null);
875 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
878 exception_error(e
, "cdmWatchdog");
884 function cache_inject(id
, article
) {
885 if (!cache_check(id
)) {
886 debug("cache_article: miss: " + id
);
888 var cache_obj
= new Array();
890 cache_obj
["id"] = id
;
891 cache_obj
["data"] = article
;
893 article_cache
.push(cache_obj
);
896 debug("cache_article: hit: " + id
);
900 function cache_find(id
) {
901 for (var i
= 0; i
< article_cache
.length
; i
++) {
902 if (article_cache
[i
]["id"] == id
) {
903 return article_cache
[i
]["data"];
909 function cache_check(id
) {
910 for (var i
= 0; i
< article_cache
.length
; i
++) {
911 if (article_cache
[i
]["id"] == id
) {
918 function cache_expire() {
919 while (article_cache
.length
> 20) {
920 article_cache
.shift();
924 function cache_invalidate(id
) {
929 while (i
< article_cache
.length
) {
930 if (article_cache
[i
]["id"] == id
) {
931 debug("cache_invalidate: removed id " + id
);
932 article_cache
.splice(i
, 1);
937 debug("cache_invalidate: id not found: " + id
);
940 exception_error("cache_invalidate", e
);
944 function getActiveArticleId() {
945 return active_post_id
;
948 function cdmMouseIn(elem
) {
950 if (elem
.id
&& elem
.id
.match("RROW-")) {
951 var id
= elem
.id
.replace("RROW-", "");
955 exception_error("cdmMouseIn", e
);
960 function cdmMouseOut(elem
) {
961 active_post_id
= false;
964 function headlines_scroll_handler() {
967 var e
= document
.getElementById("headlinesInnerContainer");
969 if (e
.scrollTop
+ e
.offsetHeight
== e
.scrollHeight
) {
970 debug("more cowbell!");
976 exception_error("headlines_scroll_handler", e
);