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>");
203 xmlhttp_ctr
= Ajax
.getTransport();
207 if (xmlhttp_ready(xmlhttp
)) {
211 cleanSelected("headlinesList");
213 var crow
= document
.getElementById("RROW-" + active_post_id
);
215 var article_is_unread
= crow
.className
.match("Unread");
216 debug("article is unread: " + article_is_unread
);
218 crow
.className
= crow
.className
.replace("Unread", "");
220 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
223 upd_img_pic
.src
= "images/blank_icon.gif";
226 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
227 markHeadline(active_post_id
);
229 var neighbor_ids
= getRelativePostIds(active_post_id
);
231 /* only request uncached articles */
233 var cids_to_request
= Array();
235 for (var i
= 0; i
< neighbor_ids
.length
; i
++) {
236 if (!cache_check(neighbor_ids
[i
])) {
237 cids_to_request
.push(neighbor_ids
[i
]);
241 debug("additional ids: " + cids_to_request
.toString());
243 /* additional info for piggyback counters */
245 if (tagsAreDisplayed()) {
246 query
= query
+ "&omode=lt";
248 query
= query
+ "&omode=flc";
251 var date
= new Date();
252 var timestamp
= Math
.round(date
.getTime() / 1000);
253 query
= query
+ "&ts=" + timestamp
;
255 query
= query
+ "&cids=" + cids_to_request
.toString();
257 if (!cached_article
) {
259 notify_progress("Loading, please wait...");
263 xmlhttp
.open("GET", query
, true);
264 xmlhttp
.onreadystatechange
=article_callback
;
266 } else if (cached_article
&& article_is_unread
) {
268 query
= query
+ "&mode=prefetch";
272 xmlhttp
.open("GET", query
, true);
273 xmlhttp
.onreadystatechange
=article_callback
;
276 render_article(cached_article
);
278 } else if (cached_article
) {
280 query
= query
+ "&mode=prefetch_old";
284 xmlhttp
.open("GET", query
, true);
285 xmlhttp
.onreadystatechange
=article_callback
;
288 render_article(cached_article
);
295 debug("xmlhttp busy (@view)");
300 exception_error("view", e
);
305 return toggleMark(id
);
308 function toggleMark(id
) {
310 if (!xmlhttp_ready(xmlhttp_rpc
)) {
315 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
317 var mark_img
= document
.getElementById("FMPIC-" + id
);
318 var vfeedu
= document
.getElementById("FEEDU--1");
319 var crow
= document
.getElementById("RROW-" + id
);
321 if (mark_img
.alt
!= "Reset mark") {
322 mark_img
.src
= "images/mark_set.png";
323 mark_img
.alt
= "Reset mark";
324 query
= query
+ "&mark=1";
326 if (vfeedu
&& crow
.className
.match("Unread")) {
327 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
331 mark_img
.src
= "images/mark_unset.png";
332 mark_img
.alt
= "Set mark";
333 query
= query
+ "&mark=0";
335 if (vfeedu
&& crow
.className
.match("Unread")) {
336 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
341 var vfeedctr
= document
.getElementById("FEEDCTR--1");
342 var vfeedr
= document
.getElementById("FEEDR--1");
344 if (vfeedu
&& vfeedctr
) {
345 if ((+vfeedu
.innerHTML
) > 0) {
346 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
347 vfeedr
.className
= vfeedr
.className
+ "Unread";
348 vfeedctr
.className
= "odd";
351 vfeedctr
.className
= "invisible";
352 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
356 debug("toggle starred for aid " + id
);
358 new Ajax
.Request(query
);
362 function correctHeadlinesOffset(id
) {
366 var hlist
= document
.getElementById("headlinesList");
367 var container
= document
.getElementById("headlinesInnerContainer");
368 var row
= document
.getElementById("RROW-" + id
);
370 var viewport
= container
.offsetHeight
;
372 var rel_offset_top
= row
.offsetTop
- container
.scrollTop
;
373 var rel_offset_bottom
= row
.offsetTop
+ row
.offsetHeight
- container
.scrollTop
;
375 debug("Rtop: " + rel_offset_top
+ " Rbtm: " + rel_offset_bottom
);
376 debug("Vport: " + viewport
);
378 if (rel_offset_top
<= 0 || rel_offset_top
> viewport
) {
379 container
.scrollTop
= row
.offsetTop
;
380 } else if (rel_offset_bottom
> viewport
) {
382 /* doesn't properly work with Opera in some cases because
383 Opera fucks up element scrolling */
385 container
.scrollTop
= row
.offsetTop
+ row
.offsetHeight
- viewport
;
389 exception_error("correctHeadlinesOffset", e
);
394 function moveToPost(mode
) {
396 // check for combined mode
397 if (!document
.getElementById("headlinesList"))
400 var rows
= getVisibleHeadlineIds();
405 if (!document
.getElementById('RROW-' + active_post_id
)) {
406 active_post_id
= false;
409 if (active_post_id
== false) {
410 next_id
= getFirstVisibleHeadlineId();
411 prev_id
= getLastVisibleHeadlineId();
413 for (var i
= 0; i
< rows
.length
; i
++) {
414 if (rows
[i
] == active_post_id
) {
421 if (mode
== "next") {
423 correctHeadlinesOffset(next_id
);
424 view(next_id
, getActiveFeedId());
428 if (mode
== "prev") {
430 correctHeadlinesOffset(prev_id
);
431 view(prev_id
, getActiveFeedId());
436 function toggleUnread(id
, cmode
) {
438 if (!xmlhttp_ready(xmlhttp_rpc
)) {
443 var row
= document
.getElementById("RROW-" + id
);
445 var nc
= row
.className
;
446 nc
= nc
.replace("Unread", "");
447 nc
= nc
.replace("Selected", "");
449 if (row
.className
.match("Unread")) {
452 row
.className
= nc
+ "Unread";
455 if (!cmode
) cmode
= 2;
457 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
458 param_escape(id
) + "&cmode=" + param_escape(cmode
);
460 notify_progress("Loading, please wait...");
462 xmlhttp_rpc
.open("GET", query
, true);
463 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
464 xmlhttp_rpc
.send(null);
470 exception_error("toggleUnread", e
);
474 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
476 if (!xmlhttp_ready(xmlhttp_rpc
)) {
484 rows
= cdmGetSelectedArticles();
486 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
489 if (rows
.length
== 0 && !no_error
) {
490 alert(__("No articles are selected."));
494 for (i
= 0; i
< rows
.length
; i
++) {
495 var row
= document
.getElementById("RROW-" + rows
[i
]);
497 var nc
= row
.className
;
498 nc
= nc
.replace("Unread", "");
499 nc
= nc
.replace("Selected", "");
501 if (row
.className
.match("Unread")) {
502 row
.className
= nc
+ "Selected";
504 row
.className
= nc
+ "UnreadSelected";
509 if (rows
.length
> 0) {
513 if (set_state
== undefined) {
515 } else if (set_state
== true) {
517 } else if (set_state
== false) {
521 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
522 param_escape(rows
.toString()) + "&cmode=" + cmode
;
524 _catchup_callback_func
= callback_func
;
526 notify_progress("Loading, please wait...");
528 xmlhttp_rpc
.open("GET", query
, true);
529 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
530 xmlhttp_rpc
.send(null);
535 exception_error("selectionToggleUnread", e
);
539 function selectionToggleMarked(cdm_mode
) {
541 if (!xmlhttp_ready(xmlhttp_rpc
)) {
549 rows
= cdmGetSelectedArticles();
551 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
554 if (rows
.length
== 0) {
555 alert(__("No articles are selected."));
559 for (i
= 0; i
< rows
.length
; i
++) {
560 var row
= document
.getElementById("RROW-" + rows
[i
]);
561 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
563 if (row
&& mark_img
) {
565 if (mark_img
.alt
== "Set mark") {
566 mark_img
.src
= "images/mark_set.png";
567 mark_img
.alt
= "Reset mark";
568 mark_img
.setAttribute('onclick',
569 'javascript:toggleMark('+rows
[i
]+', false)');
572 mark_img
.src
= "images/mark_unset.png";
573 mark_img
.alt
= "Set mark";
574 mark_img
.setAttribute('onclick',
575 'javascript:toggleMark('+rows
[i
]+', true)');
580 if (rows
.length
> 0) {
582 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
583 param_escape(rows
.toString()) + "&cmode=2";
585 xmlhttp_rpc
.open("GET", query
, true);
586 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
587 xmlhttp_rpc
.send(null);
592 exception_error("selectionToggleMarked", e
);
596 function cdmGetSelectedArticles() {
597 var sel_articles
= new Array();
598 var container
= document
.getElementById("headlinesInnerContainer");
600 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
601 var child
= container
.childNodes
[i
];
603 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
604 var c_id
= child
.id
.replace("RROW-", "");
605 sel_articles
.push(c_id
);
612 // mode = all,none,unread
613 function cdmSelectArticles(mode
) {
614 var container
= document
.getElementById("headlinesInnerContainer");
616 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
617 var child
= container
.childNodes
[i
];
619 if (child
.id
.match("RROW-")) {
620 var aid
= child
.id
.replace("RROW-", "");
622 var cb
= document
.getElementById("RCHK-" + aid
);
625 if (!child
.className
.match("Selected")) {
626 child
.className
= child
.className
+ "Selected";
629 } else if (mode
== "unread") {
630 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
631 child
.className
= child
.className
+ "Selected";
635 child
.className
= child
.className
.replace("Selected", "");
642 function catchupPage() {
644 if (document
.getElementById("headlinesList")) {
645 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
646 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
647 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
649 cdmSelectArticles('all');
650 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
651 cdmSelectArticles('none');
655 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
657 if (!xmlhttp_ready(xmlhttp_rpc
)) {
661 var title
= prompt("Please enter label title:", "");
665 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
666 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
667 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
668 "&title=" + param_escape(title
);
670 debug("LFS: " + query
);
672 xmlhttp_rpc
.open("GET", query
, true);
673 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
674 xmlhttp_rpc
.send(null);
679 function editArticleTags(id
, feed_id
, cdm_enabled
) {
680 _tag_active_post_id
= id
;
681 _tag_active_feed_id
= feed_id
;
682 _tag_active_cdm
= cdm_enabled
;
684 cache_invalidate(id
);
687 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
689 displayDlg('editArticleTags', id
);
693 function tag_saved_callback() {
694 if (xmlhttp_rpc
.readyState
== 4) {
696 debug("in tag_saved_callback");
701 if (tagsAreDisplayed()) {
702 _reload_feedlist_after_view
= true;
705 if (!_tag_active_cdm
) {
706 if (active_post_id
== _tag_active_post_id
) {
707 debug("reloading current article");
708 view(_tag_active_post_id
, _tag_active_feed_id
);
711 debug("reloading current feed");
716 exception_error("catchup_callback", e
);
721 function editTagsSave() {
723 if (!xmlhttp_ready(xmlhttp_rpc
)) {
727 notify_progress("Saving article tags...");
729 var form
= document
.forms
["tag_edit_form"];
731 var query
= Form
.serialize("tag_edit_form");
733 query
= "backend.php?op=rpc&subop=setArticleTags&" + query
;
737 xmlhttp_rpc
.open("GET", query
, true);
738 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
739 xmlhttp_rpc
.send(null);
743 function editTagsInsert() {
746 var form
= document
.forms
["tag_edit_form"];
748 var found_tags
= form
.found_tags
;
749 var tags_str
= form
.tags_str
;
751 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
753 if (tags_str
.value
.length
> 0 &&
754 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
756 tags_str
.value
= tags_str
.value
+ ", ";
759 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
761 found_tags
.selectedIndex
= 0;
764 exception_error(e
, "editTagsInsert");
768 function cdmWatchdog() {
772 var ctr
= document
.getElementById("headlinesInnerContainer");
776 var ids
= new Array();
778 var e
= ctr
.firstChild
;
781 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
782 e
.id
.match("RROW-")) {
784 // article fits in viewport OR article is longer than viewport and
785 // its bottom is visible
787 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
788 ctr
.scrollTop
+ ctr
.offsetHeight
) {
790 // debug(e.id + " is visible " + e.offsetTop + "." +
791 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
792 // (ctr.scrollTop + ctr.offsetHeight));
794 ids
.push(e
.id
.replace("RROW-", ""));
796 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
797 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
798 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
800 ids
.push(e
.id
.replace("RROW-", ""));
804 // method 2: article bottom is visible and is in upper 1/2 of the viewport
806 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
807 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
809 ids.push(e.id.replace("RROW-", ""));
818 debug("cdmWatchdog, ids= " + ids
.toString());
820 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
822 for (var i
= 0; i
< ids
.length
; i
++) {
823 var e
= document
.getElementById("RROW-" + ids
[i
]);
825 e
.className
= e
.className
.replace("Unread", "");
829 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
830 param_escape(ids
.toString()) + "&cmode=0";
832 xmlhttp_rpc
.open("GET", query
, true);
833 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
834 xmlhttp_rpc
.send(null);
838 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
841 exception_error(e
, "cdmWatchdog");
847 function cache_inject(id
, article
) {
848 if (!cache_check(id
)) {
849 debug("cache_article: miss: " + id
);
851 var cache_obj
= new Array();
853 cache_obj
["id"] = id
;
854 cache_obj
["data"] = article
;
856 article_cache
.push(cache_obj
);
859 debug("cache_article: hit: " + id
);
863 function cache_find(id
) {
864 for (var i
= 0; i
< article_cache
.length
; i
++) {
865 if (article_cache
[i
]["id"] == id
) {
866 return article_cache
[i
]["data"];
872 function cache_check(id
) {
873 for (var i
= 0; i
< article_cache
.length
; i
++) {
874 if (article_cache
[i
]["id"] == id
) {
881 function cache_expire() {
882 while (article_cache
.length
> 20) {
883 article_cache
.shift();
887 function cache_invalidate(id
) {
892 while (i
< article_cache
.length
) {
893 if (article_cache
[i
]["id"] == id
) {
894 debug("cache_invalidate: removed id " + id
);
895 article_cache
.splice(i
, 1);
900 debug("cache_invalidate: id not found: " + id
);
903 exception_error("cache_invalidate", e
);