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");
50 f
.innerHTML
= headlines
.firstChild
.nodeValue
;
52 debug("headlines_callback: returned no data");
53 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
58 for (var i
= 0; i
< articles
.length
; i
++) {
59 var a_id
= articles
[i
].getAttribute("id");
60 debug("found id: " + a_id
);
61 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
64 debug("no cached articles received");
68 debug("parsing piggybacked counters: " + counters
);
69 parse_counters(counters
, false);
71 debug("counters container not found in reply");
74 debug("headlines_callback: returned no XML object");
75 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
78 if (typeof correctPNG
!= 'undefined') {
82 if (_cdm_wd_timeout
) window
.clearTimeout(_cdm_wd_timeout
);
84 if (!document
.getElementById("headlinesList") &&
85 getInitParam("cdm_auto_catchup") == 1) {
86 debug("starting CDM watchdog");
87 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 5000);
88 _cdm_wd_vishist
= new Array();
90 debug("not in CDM mode or watchdog disabled");
93 if (_tag_cdm_scroll
) {
95 document
.getElementById("headlinesInnerContainer").scrollTop
= _tag_cdm_scroll
;
96 _tag_cdm_scroll
= false;
104 function render_article(article
) {
106 var f
= document
.getElementById("content-frame");
111 f
.innerHTML
= article
;
114 exception_error("render_article", e
);
118 function article_callback() {
119 if (xmlhttp
.readyState
== 4) {
120 debug("article_callback");
123 if (xmlhttp
.responseXML
) {
124 var reply
= xmlhttp
.responseXML
.firstChild
.firstChild
;
126 var articles
= xmlhttp
.responseXML
.getElementsByTagName("article");
128 for (var i
= 0; i
< articles
.length
; i
++) {
129 var a_id
= articles
[i
].getAttribute("id");
131 debug("found id: " + a_id
);
133 if (a_id
== active_post_id
) {
134 debug("active article, rendering...");
135 render_article(articles
[i
].firstChild
.nodeValue
);
138 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
142 debug("article_callback: returned no XML object");
143 var f
= document
.getElementById("content-frame");
144 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
147 exception_error("article_callback", e
);
150 var date
= new Date();
151 last_article_view
= date
.getTime() / 1000;
153 if (typeof correctPNG
!= 'undefined') {
157 if (_reload_feedlist_after_view
) {
158 setTimeout('updateFeedList(false, false)', 50);
159 _reload_feedlist_after_view
= false;
161 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
164 debug("parsing piggybacked counters: " + counters
);
165 parse_counters(counters
, false);
167 debug("counters container not found in reply");
175 function view(id
, feed_id
, skip_history
) {
178 debug("loading article: " + id
+ "/" + feed_id
);
180 active_real_feed_id
= feed_id
;
182 var cached_article
= cache_find(id
);
184 debug("cache check result: " + (cached_article
!= false));
186 /* if (!skip_history) {
187 history_push("ARTICLE:" + id + ":" + feed_id);
192 //setActiveFeedId(feed_id);
194 var query
= "backend.php?op=view&id=" + param_escape(id
) +
195 "&feed=" + param_escape(feed_id
);
197 var date
= new Date();
199 if (!xmlhttp_ready(xmlhttp
) && last_article_view
< date
.getTime() / 1000 - 15) {
200 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
204 if (xmlhttp_ready(xmlhttp
)) {
208 cleanSelected("headlinesList");
210 var crow
= document
.getElementById("RROW-" + active_post_id
);
212 var article_is_unread
= crow
.className
.match("Unread");
213 debug("article is unread: " + article_is_unread
);
215 crow
.className
= crow
.className
.replace("Unread", "");
217 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
220 upd_img_pic
.src
= "images/blank_icon.gif";
223 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
224 markHeadline(active_post_id
);
226 var neighbor_ids
= getRelativePostIds(active_post_id
);
228 /* only request uncached articles */
230 var cids_to_request
= Array();
232 for (var i
= 0; i
< neighbor_ids
.length
; i
++) {
233 if (!cache_check(neighbor_ids
[i
])) {
234 cids_to_request
.push(neighbor_ids
[i
]);
238 debug("additional ids: " + cids_to_request
.toString());
240 /* additional info for piggyback counters */
242 if (tagsAreDisplayed()) {
243 query
= query
+ "&omode=lt";
245 query
= query
+ "&omode=flc";
248 var date
= new Date();
249 var timestamp
= Math
.round(date
.getTime() / 1000);
250 query
= query
+ "&ts=" + timestamp
;
252 query
= query
+ "&cids=" + cids_to_request
.toString();
254 if (!cached_article
) {
256 notify_progress("Loading, please wait...");
260 xmlhttp
.open("GET", query
, true);
261 xmlhttp
.onreadystatechange
=article_callback
;
263 } else if (cached_article
&& article_is_unread
) {
265 query
= query
+ "&mode=prefetch";
269 xmlhttp
.open("GET", query
, true);
270 xmlhttp
.onreadystatechange
=article_callback
;
273 render_article(cached_article
);
275 } else if (cached_article
) {
277 query
= query
+ "&mode=prefetch_old";
281 xmlhttp
.open("GET", query
, true);
282 xmlhttp
.onreadystatechange
=article_callback
;
285 render_article(cached_article
);
292 debug("xmlhttp busy (@view)");
297 exception_error("view", e
);
302 return toggleMark(id
);
305 function toggleMark(id
) {
307 if (!xmlhttp_ready(xmlhttp_rpc
)) {
312 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
314 var mark_img
= document
.getElementById("FMPIC-" + id
);
315 var vfeedu
= document
.getElementById("FEEDU--1");
316 var crow
= document
.getElementById("RROW-" + id
);
318 if (mark_img
.alt
!= "Reset mark") {
319 mark_img
.src
= "images/mark_set.png";
320 mark_img
.alt
= "Reset mark";
321 query
= query
+ "&mark=1";
323 if (vfeedu
&& crow
.className
.match("Unread")) {
324 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
328 mark_img
.src
= "images/mark_unset.png";
329 mark_img
.alt
= "Set mark";
330 query
= query
+ "&mark=0";
332 if (vfeedu
&& crow
.className
.match("Unread")) {
333 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
338 var vfeedctr
= document
.getElementById("FEEDCTR--1");
339 var vfeedr
= document
.getElementById("FEEDR--1");
341 if (vfeedu
&& vfeedctr
) {
342 if ((+vfeedu
.innerHTML
) > 0) {
343 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
344 vfeedr
.className
= vfeedr
.className
+ "Unread";
345 vfeedctr
.className
= "odd";
348 vfeedctr
.className
= "invisible";
349 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
353 debug("toggle starred for aid " + id
);
355 new Ajax
.Request(query
);
359 function correctHeadlinesOffset(id
) {
363 var hlist
= document
.getElementById("headlinesList");
364 var container
= document
.getElementById("headlinesInnerContainer");
365 var row
= document
.getElementById("RROW-" + id
);
367 var viewport
= container
.offsetHeight
;
369 var rel_offset_top
= row
.offsetTop
- container
.scrollTop
;
370 var rel_offset_bottom
= row
.offsetTop
+ row
.offsetHeight
- container
.scrollTop
;
372 debug("Rtop: " + rel_offset_top
+ " Rbtm: " + rel_offset_bottom
);
373 debug("Vport: " + viewport
);
375 if (rel_offset_top
<= 0 || rel_offset_top
> viewport
) {
376 container
.scrollTop
= row
.offsetTop
;
377 } else if (rel_offset_bottom
> viewport
) {
379 /* doesn't properly work with Opera in some cases because
380 Opera fucks up element scrolling */
382 container
.scrollTop
= row
.offsetTop
+ row
.offsetHeight
- viewport
;
386 exception_error("correctHeadlinesOffset", e
);
391 function moveToPost(mode
) {
393 // check for combined mode
394 if (!document
.getElementById("headlinesList"))
397 var rows
= getVisibleHeadlineIds();
402 if (!document
.getElementById('RROW-' + active_post_id
)) {
403 active_post_id
= false;
406 if (active_post_id
== false) {
407 next_id
= getFirstVisibleHeadlineId();
408 prev_id
= getLastVisibleHeadlineId();
410 for (var i
= 0; i
< rows
.length
; i
++) {
411 if (rows
[i
] == active_post_id
) {
418 if (mode
== "next") {
420 correctHeadlinesOffset(next_id
);
421 view(next_id
, getActiveFeedId());
425 if (mode
== "prev") {
427 correctHeadlinesOffset(prev_id
);
428 view(prev_id
, getActiveFeedId());
433 function toggleUnread(id
, cmode
) {
435 if (!xmlhttp_ready(xmlhttp_rpc
)) {
440 var row
= document
.getElementById("RROW-" + id
);
442 var nc
= row
.className
;
443 nc
= nc
.replace("Unread", "");
444 nc
= nc
.replace("Selected", "");
446 if (row
.className
.match("Unread")) {
449 row
.className
= nc
+ "Unread";
452 if (!cmode
) cmode
= 2;
454 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
455 param_escape(id
) + "&cmode=" + param_escape(cmode
);
457 notify_progress("Loading, please wait...");
459 xmlhttp_rpc
.open("GET", query
, true);
460 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
461 xmlhttp_rpc
.send(null);
467 exception_error("toggleUnread", e
);
471 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
473 if (!xmlhttp_ready(xmlhttp_rpc
)) {
481 rows
= cdmGetSelectedArticles();
483 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
486 if (rows
.length
== 0 && !no_error
) {
487 alert(__("No articles are selected."));
491 for (i
= 0; i
< rows
.length
; i
++) {
492 var row
= document
.getElementById("RROW-" + rows
[i
]);
494 var nc
= row
.className
;
495 nc
= nc
.replace("Unread", "");
496 nc
= nc
.replace("Selected", "");
498 if (row
.className
.match("Unread")) {
499 row
.className
= nc
+ "Selected";
501 row
.className
= nc
+ "UnreadSelected";
506 if (rows
.length
> 0) {
510 if (set_state
== undefined) {
512 } else if (set_state
== true) {
514 } else if (set_state
== false) {
518 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
519 param_escape(rows
.toString()) + "&cmode=" + cmode
;
521 _catchup_callback_func
= callback_func
;
523 notify_progress("Loading, please wait...");
525 xmlhttp_rpc
.open("GET", query
, true);
526 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
527 xmlhttp_rpc
.send(null);
532 exception_error("selectionToggleUnread", e
);
536 function selectionToggleMarked(cdm_mode
) {
538 if (!xmlhttp_ready(xmlhttp_rpc
)) {
546 rows
= cdmGetSelectedArticles();
548 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
551 if (rows
.length
== 0) {
552 alert(__("No articles are selected."));
556 for (i
= 0; i
< rows
.length
; i
++) {
557 var row
= document
.getElementById("RROW-" + rows
[i
]);
558 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
560 if (row
&& mark_img
) {
562 if (mark_img
.alt
== "Set mark") {
563 mark_img
.src
= "images/mark_set.png";
564 mark_img
.alt
= "Reset mark";
565 mark_img
.setAttribute('onclick',
566 'javascript:toggleMark('+rows
[i
]+', false)');
569 mark_img
.src
= "images/mark_unset.png";
570 mark_img
.alt
= "Set mark";
571 mark_img
.setAttribute('onclick',
572 'javascript:toggleMark('+rows
[i
]+', true)');
577 if (rows
.length
> 0) {
579 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
580 param_escape(rows
.toString()) + "&cmode=2";
582 xmlhttp_rpc
.open("GET", query
, true);
583 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
584 xmlhttp_rpc
.send(null);
589 exception_error("selectionToggleMarked", e
);
593 function cdmGetSelectedArticles() {
594 var sel_articles
= new Array();
595 var container
= document
.getElementById("headlinesInnerContainer");
597 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
598 var child
= container
.childNodes
[i
];
600 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
601 var c_id
= child
.id
.replace("RROW-", "");
602 sel_articles
.push(c_id
);
609 // mode = all,none,unread
610 function cdmSelectArticles(mode
) {
611 var container
= document
.getElementById("headlinesInnerContainer");
613 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
614 var child
= container
.childNodes
[i
];
616 if (child
.id
.match("RROW-")) {
617 var aid
= child
.id
.replace("RROW-", "");
619 var cb
= document
.getElementById("RCHK-" + aid
);
622 if (!child
.className
.match("Selected")) {
623 child
.className
= child
.className
+ "Selected";
626 } else if (mode
== "unread") {
627 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
628 child
.className
= child
.className
+ "Selected";
632 child
.className
= child
.className
.replace("Selected", "");
639 function catchupPage() {
641 if (document
.getElementById("headlinesList")) {
642 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
643 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
644 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
646 cdmSelectArticles('all');
647 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
648 cdmSelectArticles('none');
652 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
654 if (!xmlhttp_ready(xmlhttp_rpc
)) {
658 var title
= prompt("Please enter label title:", "");
662 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
663 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
664 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
665 "&title=" + param_escape(title
);
667 debug("LFS: " + query
);
669 xmlhttp_rpc
.open("GET", query
, true);
670 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
671 xmlhttp_rpc
.send(null);
676 function editArticleTags(id
, feed_id
, cdm_enabled
) {
677 _tag_active_post_id
= id
;
678 _tag_active_feed_id
= feed_id
;
679 _tag_active_cdm
= cdm_enabled
;
681 cache_invalidate(id
);
684 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
686 displayDlg('editArticleTags', id
);
690 function tag_saved_callback() {
691 if (xmlhttp_rpc
.readyState
== 4) {
693 debug("in tag_saved_callback");
698 if (tagsAreDisplayed()) {
699 _reload_feedlist_after_view
= true;
702 if (!_tag_active_cdm
) {
703 if (active_post_id
== _tag_active_post_id
) {
704 debug("reloading current article");
705 view(_tag_active_post_id
, _tag_active_feed_id
);
708 debug("reloading current feed");
713 exception_error("catchup_callback", e
);
718 function editTagsSave() {
720 if (!xmlhttp_ready(xmlhttp_rpc
)) {
724 notify_progress("Saving article tags...");
726 var form
= document
.forms
["tag_edit_form"];
728 var query
= Form
.serialize("tag_edit_form");
730 xmlhttp_rpc
.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query
, true);
731 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
732 xmlhttp_rpc
.send(null);
736 function editTagsInsert() {
739 var form
= document
.forms
["tag_edit_form"];
741 var found_tags
= form
.found_tags
;
742 var tags_str
= form
.tags_str
;
744 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
746 if (tags_str
.value
.length
> 0 &&
747 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
749 tags_str
.value
= tags_str
.value
+ ", ";
752 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
754 found_tags
.selectedIndex
= 0;
757 exception_error(e
, "editTagsInsert");
761 function cdmWatchdog() {
765 var ctr
= document
.getElementById("headlinesInnerContainer");
769 var ids
= new Array();
771 var e
= ctr
.firstChild
;
774 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
775 e
.id
.match("RROW-")) {
777 // article fits in viewport OR article is longer than viewport and
778 // its bottom is visible
780 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
781 ctr
.scrollTop
+ ctr
.offsetHeight
) {
783 // debug(e.id + " is visible " + e.offsetTop + "." +
784 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
785 // (ctr.scrollTop + ctr.offsetHeight));
787 ids
.push(e
.id
.replace("RROW-", ""));
789 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
790 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
791 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
793 ids
.push(e
.id
.replace("RROW-", ""));
797 // method 2: article bottom is visible and is in upper 1/2 of the viewport
799 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
800 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
802 ids.push(e.id.replace("RROW-", ""));
811 debug("cdmWatchdog, ids= " + ids
.toString());
813 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
815 for (var i
= 0; i
< ids
.length
; i
++) {
816 var e
= document
.getElementById("RROW-" + ids
[i
]);
818 e
.className
= e
.className
.replace("Unread", "");
822 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
823 param_escape(ids
.toString()) + "&cmode=0";
825 xmlhttp_rpc
.open("GET", query
, true);
826 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
827 xmlhttp_rpc
.send(null);
831 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
834 exception_error(e
, "cdmWatchdog");
840 function cache_inject(id
, article
) {
841 if (!cache_check(id
)) {
842 debug("cache_article: miss: " + id
);
844 var cache_obj
= new Array();
846 cache_obj
["id"] = id
;
847 cache_obj
["data"] = article
;
849 article_cache
.push(cache_obj
);
852 debug("cache_article: hit: " + id
);
856 function cache_find(id
) {
857 for (var i
= 0; i
< article_cache
.length
; i
++) {
858 if (article_cache
[i
]["id"] == id
) {
859 return article_cache
[i
]["data"];
865 function cache_check(id
) {
866 for (var i
= 0; i
< article_cache
.length
; i
++) {
867 if (article_cache
[i
]["id"] == id
) {
874 function cache_expire() {
875 while (article_cache
.length
> 20) {
876 article_cache
.shift();
880 function cache_invalidate(id
) {
885 while (i
< article_cache
.length
) {
886 if (article_cache
[i
]["id"] == id
) {
887 debug("cache_invalidate: removed id " + id
);
888 article_cache
.splice(i
, 1);
893 debug("cache_invalidate: id not found: " + id
);
896 exception_error("cache_invalidate", e
);