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");
49 f
.innerHTML
= headlines
.firstChild
.nodeValue
;
52 for (var i
= 0; i
< articles
.length
; i
++) {
53 var a_id
= articles
[i
].getAttribute("id");
54 debug("found id: " + a_id
);
55 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
60 debug("parsing piggybacked counters: " + counters
);
61 parse_counters(counters
, false);
63 debug("counters container not found in reply");
66 debug("headlines_callback: returned no XML object");
67 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
70 if (typeof correctPNG
!= 'undefined') {
74 if (_cdm_wd_timeout
) window
.clearTimeout(_cdm_wd_timeout
);
76 if (!document
.getElementById("headlinesList") &&
77 getInitParam("cdm_auto_catchup") == 1) {
78 debug("starting CDM watchdog");
79 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 5000);
80 _cdm_wd_vishist
= new Array();
82 debug("not in CDM mode or watchdog disabled");
85 if (_tag_cdm_scroll
) {
87 document
.getElementById("headlinesInnerContainer").scrollTop
= _tag_cdm_scroll
;
88 _tag_cdm_scroll
= false;
96 function render_article(article
) {
98 var f
= document
.getElementById("content-frame");
103 f
.innerHTML
= article
;
106 exception_error("render_article", e
);
110 function article_callback() {
111 if (xmlhttp
.readyState
== 4) {
112 debug("article_callback");
115 if (xmlhttp
.responseXML
) {
116 var reply
= xmlhttp
.responseXML
.firstChild
.firstChild
;
118 var articles
= xmlhttp
.responseXML
.getElementsByTagName("article");
120 for (var i
= 0; i
< articles
.length
; i
++) {
121 var a_id
= articles
[i
].getAttribute("id");
123 debug("found id: " + a_id
);
125 if (a_id
== active_post_id
) {
126 debug("active article, rendering...");
127 render_article(articles
[i
].firstChild
.nodeValue
);
130 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
134 debug("article_callback: returned no XML object");
135 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
138 exception_error("article_callback", e
);
141 var date
= new Date();
142 last_article_view
= date
.getTime() / 1000;
144 if (typeof correctPNG
!= 'undefined') {
148 if (_reload_feedlist_after_view
) {
149 setTimeout('updateFeedList(false, false)', 50);
150 _reload_feedlist_after_view
= false;
152 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
155 debug("parsing piggybacked counters: " + counters
);
156 parse_counters(counters
, false);
158 debug("counters container not found in reply");
166 function view(id
, feed_id
, skip_history
) {
169 debug("loading article: " + id
+ "/" + feed_id
);
171 active_real_feed_id
= feed_id
;
173 var cached_article
= cache_find(id
);
175 debug("cache check result: " + (cached_article
!= false));
177 /* if (!skip_history) {
178 history_push("ARTICLE:" + id + ":" + feed_id);
184 //setActiveFeedId(feed_id);
186 var query
= "backend.php?op=view&id=" + param_escape(id
) +
187 "&feed=" + param_escape(feed_id
);
189 var date
= new Date();
191 if (!xmlhttp_ready(xmlhttp
) && last_article_view
< date
.getTime() / 1000 - 15) {
192 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
196 if (cached_article
|| xmlhttp_ready(xmlhttp
)) {
198 cleanSelected("headlinesList");
200 var crow
= document
.getElementById("RROW-" + active_post_id
);
202 var article_is_unread
= crow
.className
.match("Unread");
203 debug("article is unread: " + article_is_unread
);
205 crow
.className
= crow
.className
.replace("Unread", "");
207 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
210 upd_img_pic
.src
= "images/blank_icon.gif";
213 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
214 markHeadline(active_post_id
);
216 var neighbor_ids
= getRelativePostIds(active_post_id
);
218 /* only request uncached articles */
220 var cids_to_request
= Array();
222 for (var i
= 0; i
< neighbor_ids
.length
; i
++) {
223 if (!cache_check(neighbor_ids
[i
])) {
224 cids_to_request
.push(neighbor_ids
[i
]);
228 debug("additional ids: " + cids_to_request
.toString());
230 /* additional info for piggyback counters */
232 if (tagsAreDisplayed()) {
233 query
= query
+ "&omode=lt";
235 query
= query
+ "&omode=flc";
238 var date
= new Date();
239 var timestamp
= Math
.round(date
.getTime() / 1000);
240 query
= query
+ "&ts=" + timestamp
;
242 query
= query
+ "&cids=" + cids_to_request
.toString();
244 if (!cached_article
) {
246 notify_progress("Loading, please wait...");
250 xmlhttp
.open("GET", query
, true);
251 xmlhttp
.onreadystatechange
=article_callback
;
253 } else if (cached_article
&& article_is_unread
) {
255 query
= query
+ "&mode=prefetch";
259 xmlhttp
.open("GET", query
, true);
260 xmlhttp
.onreadystatechange
=article_callback
;
263 render_article(cached_article
);
265 } else if (cached_article
) {
267 query
= query
+ "&mode=prefetch_old";
271 xmlhttp
.open("GET", query
, true);
272 xmlhttp
.onreadystatechange
=article_callback
;
275 render_article(cached_article
);
282 debug("xmlhttp busy (@view)");
287 exception_error("view", e
);
292 return toggleMark(id
);
295 function toggleMark(id
) {
297 if (!xmlhttp_ready(xmlhttp_rpc
)) {
302 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
304 var mark_img
= document
.getElementById("FMPIC-" + id
);
305 var vfeedu
= document
.getElementById("FEEDU--1");
306 var crow
= document
.getElementById("RROW-" + id
);
308 if (mark_img
.alt
!= "Reset mark") {
309 mark_img
.src
= "images/mark_set.png";
310 mark_img
.alt
= "Reset mark";
311 query
= query
+ "&mark=1";
313 if (vfeedu
&& crow
.className
.match("Unread")) {
314 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
318 mark_img
.src
= "images/mark_unset.png";
319 mark_img
.alt
= "Set mark";
320 query
= query
+ "&mark=0";
322 if (vfeedu
&& crow
.className
.match("Unread")) {
323 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
328 var vfeedctr
= document
.getElementById("FEEDCTR--1");
329 var vfeedr
= document
.getElementById("FEEDR--1");
331 if (vfeedu
&& vfeedctr
) {
332 if ((+vfeedu
.innerHTML
) > 0) {
333 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
334 vfeedr
.className
= vfeedr
.className
+ "Unread";
335 vfeedctr
.className
= "odd";
338 vfeedctr
.className
= "invisible";
339 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
343 debug("toggle starred for aid " + id
);
345 new Ajax
.Request(query
);
349 function moveToPost(mode
) {
351 // check for combined mode
352 if (!document
.getElementById("headlinesList"))
355 var rows
= getVisibleHeadlineIds();
360 if (!document
.getElementById('RROW-' + active_post_id
)) {
361 active_post_id
= false;
364 if (active_post_id
== false) {
365 next_id
= getFirstVisibleHeadlineId();
366 prev_id
= getLastVisibleHeadlineId();
368 for (var i
= 0; i
< rows
.length
; i
++) {
369 if (rows
[i
] == active_post_id
) {
376 if (mode
== "next") {
377 if (next_id
!= undefined) {
378 view(next_id
, getActiveFeedId());
382 if (mode
== "prev") {
383 if ( prev_id
!= undefined) {
384 view(prev_id
, getActiveFeedId());
389 function toggleUnread(id
, cmode
) {
391 if (!xmlhttp_ready(xmlhttp_rpc
)) {
396 var row
= document
.getElementById("RROW-" + id
);
398 var nc
= row
.className
;
399 nc
= nc
.replace("Unread", "");
400 nc
= nc
.replace("Selected", "");
402 if (row
.className
.match("Unread")) {
405 row
.className
= nc
+ "Unread";
408 if (!cmode
) cmode
= 2;
410 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
411 param_escape(id
) + "&cmode=" + param_escape(cmode
);
413 notify_progress("Loading, please wait...");
415 xmlhttp_rpc
.open("GET", query
, true);
416 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
417 xmlhttp_rpc
.send(null);
423 exception_error("toggleUnread", e
);
427 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
429 if (!xmlhttp_ready(xmlhttp_rpc
)) {
437 rows
= cdmGetSelectedArticles();
439 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
442 if (rows
.length
== 0 && !no_error
) {
443 alert(__("No articles are selected."));
447 for (i
= 0; i
< rows
.length
; i
++) {
448 var row
= document
.getElementById("RROW-" + rows
[i
]);
450 var nc
= row
.className
;
451 nc
= nc
.replace("Unread", "");
452 nc
= nc
.replace("Selected", "");
454 if (row
.className
.match("Unread")) {
455 row
.className
= nc
+ "Selected";
457 row
.className
= nc
+ "UnreadSelected";
462 if (rows
.length
> 0) {
466 if (set_state
== undefined) {
468 } else if (set_state
== true) {
470 } else if (set_state
== false) {
474 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
475 param_escape(rows
.toString()) + "&cmode=" + cmode
;
477 _catchup_callback_func
= callback_func
;
479 notify_progress("Loading, please wait...");
481 xmlhttp_rpc
.open("GET", query
, true);
482 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
483 xmlhttp_rpc
.send(null);
488 exception_error("selectionToggleUnread", e
);
492 function selectionToggleMarked(cdm_mode
) {
494 if (!xmlhttp_ready(xmlhttp_rpc
)) {
502 rows
= cdmGetSelectedArticles();
504 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
507 if (rows
.length
== 0) {
508 alert(__("No articles are selected."));
512 for (i
= 0; i
< rows
.length
; i
++) {
513 var row
= document
.getElementById("RROW-" + rows
[i
]);
514 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
516 if (row
&& mark_img
) {
518 if (mark_img
.alt
== "Set mark") {
519 mark_img
.src
= "images/mark_set.png";
520 mark_img
.alt
= "Reset mark";
521 mark_img
.setAttribute('onclick',
522 'javascript:toggleMark('+rows
[i
]+', false)');
525 mark_img
.src
= "images/mark_unset.png";
526 mark_img
.alt
= "Set mark";
527 mark_img
.setAttribute('onclick',
528 'javascript:toggleMark('+rows
[i
]+', true)');
533 if (rows
.length
> 0) {
535 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
536 param_escape(rows
.toString()) + "&cmode=2";
538 xmlhttp_rpc
.open("GET", query
, true);
539 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
540 xmlhttp_rpc
.send(null);
545 exception_error("selectionToggleMarked", e
);
549 function cdmGetSelectedArticles() {
550 var sel_articles
= new Array();
551 var container
= document
.getElementById("headlinesInnerContainer");
553 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
554 var child
= container
.childNodes
[i
];
556 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
557 var c_id
= child
.id
.replace("RROW-", "");
558 sel_articles
.push(c_id
);
565 // mode = all,none,unread
566 function cdmSelectArticles(mode
) {
567 var container
= document
.getElementById("headlinesInnerContainer");
569 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
570 var child
= container
.childNodes
[i
];
572 if (child
.id
.match("RROW-")) {
573 var aid
= child
.id
.replace("RROW-", "");
575 var cb
= document
.getElementById("RCHK-" + aid
);
578 if (!child
.className
.match("Selected")) {
579 child
.className
= child
.className
+ "Selected";
582 } else if (mode
== "unread") {
583 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
584 child
.className
= child
.className
+ "Selected";
588 child
.className
= child
.className
.replace("Selected", "");
595 function catchupPage() {
597 if (document
.getElementById("headlinesList")) {
598 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
599 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
600 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
602 cdmSelectArticles('all');
603 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
604 cdmSelectArticles('none');
608 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
610 if (!xmlhttp_ready(xmlhttp_rpc
)) {
614 var title
= prompt("Please enter label title:", "");
618 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
619 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
620 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
621 "&title=" + param_escape(title
);
623 debug("LFS: " + query
);
625 xmlhttp_rpc
.open("GET", query
, true);
626 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
627 xmlhttp_rpc
.send(null);
632 function editArticleTags(id
, feed_id
, cdm_enabled
) {
633 _tag_active_post_id
= id
;
634 _tag_active_feed_id
= feed_id
;
635 _tag_active_cdm
= cdm_enabled
;
637 cache_invalidate(id
);
640 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
642 displayDlg('editArticleTags', id
);
646 function tag_saved_callback() {
647 if (xmlhttp_rpc
.readyState
== 4) {
649 debug("in tag_saved_callback");
654 if (tagsAreDisplayed()) {
655 _reload_feedlist_after_view
= true;
658 if (!_tag_active_cdm
) {
659 if (active_post_id
== _tag_active_post_id
) {
660 debug("reloading current article");
661 view(_tag_active_post_id
, _tag_active_feed_id
);
664 debug("reloading current feed");
669 exception_error("catchup_callback", e
);
674 function editTagsSave() {
676 if (!xmlhttp_ready(xmlhttp_rpc
)) {
680 notify_progress("Saving article tags...");
682 var form
= document
.forms
["tag_edit_form"];
684 var query
= Form
.serialize("tag_edit_form");
686 xmlhttp_rpc
.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query
, true);
687 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
688 xmlhttp_rpc
.send(null);
692 function editTagsInsert() {
695 var form
= document
.forms
["tag_edit_form"];
697 var found_tags
= form
.found_tags
;
698 var tags_str
= form
.tags_str
;
700 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
702 if (tags_str
.value
.length
> 0 &&
703 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
705 tags_str
.value
= tags_str
.value
+ ", ";
708 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
710 found_tags
.selectedIndex
= 0;
713 exception_error(e
, "editTagsInsert");
717 function cdmWatchdog() {
721 var ctr
= document
.getElementById("headlinesInnerContainer");
725 var ids
= new Array();
727 var e
= ctr
.firstChild
;
730 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
731 e
.id
.match("RROW-")) {
733 // article fits in viewport OR article is longer than viewport and
734 // its bottom is visible
736 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
737 ctr
.scrollTop
+ ctr
.offsetHeight
) {
739 // debug(e.id + " is visible " + e.offsetTop + "." +
740 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
741 // (ctr.scrollTop + ctr.offsetHeight));
743 ids
.push(e
.id
.replace("RROW-", ""));
745 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
746 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
747 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
749 ids
.push(e
.id
.replace("RROW-", ""));
753 // method 2: article bottom is visible and is in upper 1/2 of the viewport
755 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
756 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
758 ids.push(e.id.replace("RROW-", ""));
767 debug("cdmWatchdog, ids= " + ids
.toString());
769 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
771 for (var i
= 0; i
< ids
.length
; i
++) {
772 var e
= document
.getElementById("RROW-" + ids
[i
]);
774 e
.className
= e
.className
.replace("Unread", "");
778 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
779 param_escape(ids
.toString()) + "&cmode=0";
781 xmlhttp_rpc
.open("GET", query
, true);
782 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
783 xmlhttp_rpc
.send(null);
787 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
790 exception_error(e
, "cdmWatchdog");
796 function cache_inject(id
, article
) {
797 if (!cache_check(id
)) {
798 debug("cache_article: miss: " + id
);
800 var cache_obj
= new Array();
802 cache_obj
["id"] = id
;
803 cache_obj
["data"] = article
;
805 article_cache
.push(cache_obj
);
808 debug("cache_article: hit: " + id
);
812 function cache_find(id
) {
813 for (var i
= 0; i
< article_cache
.length
; i
++) {
814 if (article_cache
[i
]["id"] == id
) {
815 return article_cache
[i
]["data"];
821 function cache_check(id
) {
822 for (var i
= 0; i
< article_cache
.length
; i
++) {
823 if (article_cache
[i
]["id"] == id
) {
830 function cache_expire() {
831 while (article_cache
.length
> 20) {
832 article_cache
.shift();
836 function cache_invalidate(id
) {
839 while (i
< article_cache
.length
) {
840 if (article_cache
[i
]["id"] == id
) {
841 article_cache
.remove(i
);