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");
25 if (_catchup_callback_func
) {
26 setTimeout(_catchup_callback_func
, 100);
29 all_counters_callback();
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");
44 if (xmlhttp
.responseXML
) {
45 var headlines
= xmlhttp
.responseXML
.getElementsByTagName("headlines")[0];
46 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
47 var articles
= xmlhttp
.responseXML
.getElementsByTagName("article");
48 var runtime_info
= xmlhttp
.responseXML
.getElementsByTagName("runtime-info");
51 f
.innerHTML
= headlines
.firstChild
.nodeValue
;
53 debug("headlines_callback: returned no data");
54 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
59 for (var i
= 0; i
< articles
.length
; i
++) {
60 var a_id
= articles
[i
].getAttribute("id");
61 debug("found id: " + a_id
);
62 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
65 debug("no cached articles received");
69 debug("parsing piggybacked counters: " + counters
);
70 parse_counters(counters
, false);
72 debug("counters container not found in reply");
76 debug("parsing runtime info: " + runtime_info
[0]);
77 parse_runtime_info(runtime_info
[0]);
79 debug("counters container not found in reply");
83 debug("headlines_callback: returned no XML object");
84 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
87 if (typeof correctPNG
!= 'undefined') {
91 if (_cdm_wd_timeout
) window
.clearTimeout(_cdm_wd_timeout
);
93 if (!document
.getElementById("headlinesList") &&
94 getInitParam("cdm_auto_catchup") == 1) {
95 debug("starting CDM watchdog");
96 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 5000);
97 _cdm_wd_vishist
= new Array();
99 debug("not in CDM mode or watchdog disabled");
102 if (_tag_cdm_scroll
) {
104 document
.getElementById("headlinesInnerContainer").scrollTop
= _tag_cdm_scroll
;
105 _tag_cdm_scroll
= false;
113 function render_article(article
) {
115 var f
= document
.getElementById("content-frame");
120 f
.innerHTML
= article
;
123 exception_error("render_article", e
);
127 function article_callback() {
128 if (xmlhttp
.readyState
== 4) {
129 debug("article_callback");
132 if (xmlhttp
.responseXML
) {
133 var reply
= xmlhttp
.responseXML
.firstChild
.firstChild
;
135 var articles
= xmlhttp
.responseXML
.getElementsByTagName("article");
137 for (var i
= 0; i
< articles
.length
; i
++) {
138 var a_id
= articles
[i
].getAttribute("id");
140 debug("found id: " + a_id
);
142 if (a_id
== active_post_id
) {
143 debug("active article, rendering...");
144 render_article(articles
[i
].firstChild
.nodeValue
);
147 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
151 debug("article_callback: returned no XML object");
152 var f
= document
.getElementById("content-frame");
153 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
156 exception_error("article_callback", e
);
159 var date
= new Date();
160 last_article_view
= date
.getTime() / 1000;
162 if (typeof correctPNG
!= 'undefined') {
166 if (_reload_feedlist_after_view
) {
167 setTimeout('updateFeedList(false, false)', 50);
168 _reload_feedlist_after_view
= false;
170 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
173 debug("parsing piggybacked counters: " + counters
);
174 parse_counters(counters
, false);
176 debug("counters container not found in reply");
184 function view(id
, feed_id
, skip_history
) {
187 debug("loading article: " + id
+ "/" + feed_id
);
189 active_real_feed_id
= feed_id
;
191 var cached_article
= cache_find(id
);
193 debug("cache check result: " + (cached_article
!= false));
195 /* if (!skip_history) {
196 history_push("ARTICLE:" + id + ":" + feed_id);
201 //setActiveFeedId(feed_id);
203 var query
= "backend.php?op=view&id=" + param_escape(id
) +
204 "&feed=" + param_escape(feed_id
);
206 var date
= new Date();
208 if (!xmlhttp_ready(xmlhttp
) && last_article_view
< date
.getTime() / 1000 - 15) {
209 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
212 debug("trying alternative reset method for Safari");
213 xmlhttp
= Ajax
.getTransport();
217 if (xmlhttp_ready(xmlhttp
)) {
221 cleanSelected("headlinesList");
223 var crow
= document
.getElementById("RROW-" + active_post_id
);
225 var article_is_unread
= crow
.className
.match("Unread");
226 debug("article is unread: " + article_is_unread
);
228 crow
.className
= crow
.className
.replace("Unread", "");
230 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
233 upd_img_pic
.src
= "images/blank_icon.gif";
236 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
237 markHeadline(active_post_id
);
239 var neighbor_ids
= getRelativePostIds(active_post_id
);
241 /* only request uncached articles */
243 var cids_to_request
= Array();
245 for (var i
= 0; i
< neighbor_ids
.length
; i
++) {
246 if (!cache_check(neighbor_ids
[i
])) {
247 cids_to_request
.push(neighbor_ids
[i
]);
251 debug("additional ids: " + cids_to_request
.toString());
253 /* additional info for piggyback counters */
255 if (tagsAreDisplayed()) {
256 query
= query
+ "&omode=lt";
258 query
= query
+ "&omode=flc";
261 var date
= new Date();
262 var timestamp
= Math
.round(date
.getTime() / 1000);
263 query
= query
+ "&ts=" + timestamp
;
265 query
= query
+ "&cids=" + cids_to_request
.toString();
267 if (!cached_article
) {
269 notify_progress("Loading, please wait...");
273 xmlhttp
.open("GET", query
, true);
274 xmlhttp
.onreadystatechange
=article_callback
;
276 } else if (cached_article
&& article_is_unread
) {
278 query
= query
+ "&mode=prefetch";
282 xmlhttp
.open("GET", query
, true);
283 xmlhttp
.onreadystatechange
=article_callback
;
286 render_article(cached_article
);
288 } else if (cached_article
) {
290 query
= query
+ "&mode=prefetch_old";
294 xmlhttp
.open("GET", query
, true);
295 xmlhttp
.onreadystatechange
=article_callback
;
298 render_article(cached_article
);
305 debug("xmlhttp busy (@view)");
310 exception_error("view", e
);
315 return toggleMark(id
);
318 function toggleMark(id
) {
320 if (!xmlhttp_ready(xmlhttp_rpc
)) {
325 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
327 var mark_img
= document
.getElementById("FMPIC-" + id
);
328 var vfeedu
= document
.getElementById("FEEDU--1");
329 var crow
= document
.getElementById("RROW-" + id
);
331 if (mark_img
.alt
!= "Reset mark") {
332 mark_img
.src
= "images/mark_set.png";
333 mark_img
.alt
= "Reset mark";
334 query
= query
+ "&mark=1";
336 if (vfeedu
&& crow
.className
.match("Unread")) {
337 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
341 mark_img
.src
= "images/mark_unset.png";
342 mark_img
.alt
= "Set mark";
343 query
= query
+ "&mark=0";
345 if (vfeedu
&& crow
.className
.match("Unread")) {
346 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
351 var vfeedctr
= document
.getElementById("FEEDCTR--1");
352 var vfeedr
= document
.getElementById("FEEDR--1");
354 if (vfeedu
&& vfeedctr
) {
355 if ((+vfeedu
.innerHTML
) > 0) {
356 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
357 vfeedr
.className
= vfeedr
.className
+ "Unread";
358 vfeedctr
.className
= "odd";
361 vfeedctr
.className
= "invisible";
362 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
366 debug("toggle starred for aid " + id
);
368 new Ajax
.Request(query
);
372 function correctHeadlinesOffset(id
) {
376 var hlist
= document
.getElementById("headlinesList");
377 var container
= document
.getElementById("headlinesInnerContainer");
378 var row
= document
.getElementById("RROW-" + id
);
380 var viewport
= container
.offsetHeight
;
382 var rel_offset_top
= row
.offsetTop
- container
.scrollTop
;
383 var rel_offset_bottom
= row
.offsetTop
+ row
.offsetHeight
- container
.scrollTop
;
385 debug("Rtop: " + rel_offset_top
+ " Rbtm: " + rel_offset_bottom
);
386 debug("Vport: " + viewport
);
388 if (rel_offset_top
<= 0 || rel_offset_top
> viewport
) {
389 container
.scrollTop
= row
.offsetTop
;
390 } else if (rel_offset_bottom
> viewport
) {
392 /* doesn't properly work with Opera in some cases because
393 Opera fucks up element scrolling */
395 container
.scrollTop
= row
.offsetTop
+ row
.offsetHeight
- viewport
;
399 exception_error("correctHeadlinesOffset", e
);
404 function moveToPost(mode
) {
406 // check for combined mode
407 if (!document
.getElementById("headlinesList"))
410 var rows
= getVisibleHeadlineIds();
415 if (!document
.getElementById('RROW-' + active_post_id
)) {
416 active_post_id
= false;
419 if (active_post_id
== false) {
420 next_id
= getFirstVisibleHeadlineId();
421 prev_id
= getLastVisibleHeadlineId();
423 for (var i
= 0; i
< rows
.length
; i
++) {
424 if (rows
[i
] == active_post_id
) {
431 if (mode
== "next") {
433 correctHeadlinesOffset(next_id
);
434 view(next_id
, getActiveFeedId());
438 if (mode
== "prev") {
440 correctHeadlinesOffset(prev_id
);
441 view(prev_id
, getActiveFeedId());
446 function toggleUnread(id
, cmode
) {
448 if (!xmlhttp_ready(xmlhttp_rpc
)) {
453 var row
= document
.getElementById("RROW-" + id
);
455 var nc
= row
.className
;
456 nc
= nc
.replace("Unread", "");
457 nc
= nc
.replace("Selected", "");
459 if (row
.className
.match("Unread")) {
462 row
.className
= nc
+ "Unread";
465 if (!cmode
) cmode
= 2;
467 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
468 param_escape(id
) + "&cmode=" + param_escape(cmode
);
470 notify_progress("Loading, please wait...");
472 xmlhttp_rpc
.open("GET", query
, true);
473 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
474 xmlhttp_rpc
.send(null);
480 exception_error("toggleUnread", e
);
484 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
486 if (!xmlhttp_ready(xmlhttp_rpc
)) {
494 rows
= cdmGetSelectedArticles();
496 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
499 if (rows
.length
== 0 && !no_error
) {
500 alert(__("No articles are selected."));
504 for (i
= 0; i
< rows
.length
; i
++) {
505 var row
= document
.getElementById("RROW-" + rows
[i
]);
507 var nc
= row
.className
;
508 nc
= nc
.replace("Unread", "");
509 nc
= nc
.replace("Selected", "");
511 if (row
.className
.match("Unread")) {
512 row
.className
= nc
+ "Selected";
514 row
.className
= nc
+ "UnreadSelected";
519 if (rows
.length
> 0) {
523 if (set_state
== undefined) {
525 } else if (set_state
== true) {
527 } else if (set_state
== false) {
531 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
532 param_escape(rows
.toString()) + "&cmode=" + cmode
;
534 _catchup_callback_func
= callback_func
;
536 notify_progress("Loading, please wait...");
538 xmlhttp_rpc
.open("GET", query
, true);
539 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
540 xmlhttp_rpc
.send(null);
545 exception_error("selectionToggleUnread", e
);
549 function selectionToggleMarked(cdm_mode
) {
551 if (!xmlhttp_ready(xmlhttp_rpc
)) {
559 rows
= cdmGetSelectedArticles();
561 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
564 if (rows
.length
== 0) {
565 alert(__("No articles are selected."));
569 for (i
= 0; i
< rows
.length
; i
++) {
570 var row
= document
.getElementById("RROW-" + rows
[i
]);
571 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
573 if (row
&& mark_img
) {
575 if (mark_img
.alt
== "Set mark") {
576 mark_img
.src
= "images/mark_set.png";
577 mark_img
.alt
= "Reset mark";
578 mark_img
.setAttribute('onclick',
579 'javascript:toggleMark('+rows
[i
]+', false)');
582 mark_img
.src
= "images/mark_unset.png";
583 mark_img
.alt
= "Set mark";
584 mark_img
.setAttribute('onclick',
585 'javascript:toggleMark('+rows
[i
]+', true)');
590 if (rows
.length
> 0) {
592 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
593 param_escape(rows
.toString()) + "&cmode=2";
595 xmlhttp_rpc
.open("GET", query
, true);
596 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
597 xmlhttp_rpc
.send(null);
602 exception_error("selectionToggleMarked", e
);
606 function cdmGetSelectedArticles() {
607 var sel_articles
= new Array();
608 var container
= document
.getElementById("headlinesInnerContainer");
610 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
611 var child
= container
.childNodes
[i
];
613 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
614 var c_id
= child
.id
.replace("RROW-", "");
615 sel_articles
.push(c_id
);
622 // mode = all,none,unread
623 function cdmSelectArticles(mode
) {
624 var container
= document
.getElementById("headlinesInnerContainer");
626 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
627 var child
= container
.childNodes
[i
];
629 if (child
.id
.match("RROW-")) {
630 var aid
= child
.id
.replace("RROW-", "");
632 var cb
= document
.getElementById("RCHK-" + aid
);
635 if (!child
.className
.match("Selected")) {
636 child
.className
= child
.className
+ "Selected";
639 } else if (mode
== "unread") {
640 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
641 child
.className
= child
.className
+ "Selected";
645 child
.className
= child
.className
.replace("Selected", "");
652 function catchupPage() {
654 if (document
.getElementById("headlinesList")) {
655 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
656 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
657 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
659 cdmSelectArticles('all');
660 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
661 cdmSelectArticles('none');
665 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
667 if (!xmlhttp_ready(xmlhttp_rpc
)) {
671 var title
= prompt("Please enter label title:", "");
675 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
676 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
677 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
678 "&title=" + param_escape(title
);
680 debug("LFS: " + query
);
682 xmlhttp_rpc
.open("GET", query
, true);
683 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
684 xmlhttp_rpc
.send(null);
689 function editArticleTags(id
, feed_id
, cdm_enabled
) {
690 _tag_active_post_id
= id
;
691 _tag_active_feed_id
= feed_id
;
692 _tag_active_cdm
= cdm_enabled
;
694 cache_invalidate(id
);
697 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
699 displayDlg('editArticleTags', id
);
703 function tag_saved_callback() {
704 if (xmlhttp_rpc
.readyState
== 4) {
706 debug("in tag_saved_callback");
711 if (tagsAreDisplayed()) {
712 _reload_feedlist_after_view
= true;
715 if (!_tag_active_cdm
) {
716 if (active_post_id
== _tag_active_post_id
) {
717 debug("reloading current article");
718 view(_tag_active_post_id
, _tag_active_feed_id
);
721 debug("reloading current feed");
726 exception_error("catchup_callback", e
);
731 function editTagsSave() {
733 if (!xmlhttp_ready(xmlhttp_rpc
)) {
737 notify_progress("Saving article tags...");
739 var form
= document
.forms
["tag_edit_form"];
741 var query
= Form
.serialize("tag_edit_form");
743 query
= "backend.php?op=rpc&subop=setArticleTags&" + query
;
747 xmlhttp_rpc
.open("GET", query
, true);
748 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
749 xmlhttp_rpc
.send(null);
753 function editTagsInsert() {
756 var form
= document
.forms
["tag_edit_form"];
758 var found_tags
= form
.found_tags
;
759 var tags_str
= form
.tags_str
;
761 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
763 if (tags_str
.value
.length
> 0 &&
764 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
766 tags_str
.value
= tags_str
.value
+ ", ";
769 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
771 found_tags
.selectedIndex
= 0;
774 exception_error(e
, "editTagsInsert");
778 function cdmWatchdog() {
782 var ctr
= document
.getElementById("headlinesInnerContainer");
786 var ids
= new Array();
788 var e
= ctr
.firstChild
;
791 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
792 e
.id
.match("RROW-")) {
794 // article fits in viewport OR article is longer than viewport and
795 // its bottom is visible
797 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
798 ctr
.scrollTop
+ ctr
.offsetHeight
) {
800 // debug(e.id + " is visible " + e.offsetTop + "." +
801 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
802 // (ctr.scrollTop + ctr.offsetHeight));
804 ids
.push(e
.id
.replace("RROW-", ""));
806 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
807 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
808 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
810 ids
.push(e
.id
.replace("RROW-", ""));
814 // method 2: article bottom is visible and is in upper 1/2 of the viewport
816 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
817 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
819 ids.push(e.id.replace("RROW-", ""));
828 debug("cdmWatchdog, ids= " + ids
.toString());
830 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
832 for (var i
= 0; i
< ids
.length
; i
++) {
833 var e
= document
.getElementById("RROW-" + ids
[i
]);
835 e
.className
= e
.className
.replace("Unread", "");
839 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
840 param_escape(ids
.toString()) + "&cmode=0";
842 xmlhttp_rpc
.open("GET", query
, true);
843 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
844 xmlhttp_rpc
.send(null);
848 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
851 exception_error(e
, "cdmWatchdog");
857 function cache_inject(id
, article
) {
858 if (!cache_check(id
)) {
859 debug("cache_article: miss: " + id
);
861 var cache_obj
= new Array();
863 cache_obj
["id"] = id
;
864 cache_obj
["data"] = article
;
866 article_cache
.push(cache_obj
);
869 debug("cache_article: hit: " + id
);
873 function cache_find(id
) {
874 for (var i
= 0; i
< article_cache
.length
; i
++) {
875 if (article_cache
[i
]["id"] == id
) {
876 return article_cache
[i
]["data"];
882 function cache_check(id
) {
883 for (var i
= 0; i
< article_cache
.length
; i
++) {
884 if (article_cache
[i
]["id"] == id
) {
891 function cache_expire() {
892 while (article_cache
.length
> 20) {
893 article_cache
.shift();
897 function cache_invalidate(id
) {
902 while (i
< article_cache
.length
) {
903 if (article_cache
[i
]["id"] == id
) {
904 debug("cache_invalidate: removed id " + id
);
905 article_cache
.splice(i
, 1);
910 debug("cache_invalidate: id not found: " + id
);
913 exception_error("cache_invalidate", e
);