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 var f
= document
.getElementById("content-frame");
136 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
139 exception_error("article_callback", e
);
142 var date
= new Date();
143 last_article_view
= date
.getTime() / 1000;
145 if (typeof correctPNG
!= 'undefined') {
149 if (_reload_feedlist_after_view
) {
150 setTimeout('updateFeedList(false, false)', 50);
151 _reload_feedlist_after_view
= false;
153 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
156 debug("parsing piggybacked counters: " + counters
);
157 parse_counters(counters
, false);
159 debug("counters container not found in reply");
167 function view(id
, feed_id
, skip_history
) {
170 debug("loading article: " + id
+ "/" + feed_id
);
172 active_real_feed_id
= feed_id
;
174 var cached_article
= cache_find(id
);
176 debug("cache check result: " + (cached_article
!= false));
178 /* if (!skip_history) {
179 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 (xmlhttp_ready(xmlhttp
)) {
200 cleanSelected("headlinesList");
202 var crow
= document
.getElementById("RROW-" + active_post_id
);
204 var article_is_unread
= crow
.className
.match("Unread");
205 debug("article is unread: " + article_is_unread
);
207 crow
.className
= crow
.className
.replace("Unread", "");
209 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
212 upd_img_pic
.src
= "images/blank_icon.gif";
215 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
216 markHeadline(active_post_id
);
218 var neighbor_ids
= getRelativePostIds(active_post_id
);
220 /* only request uncached articles */
222 var cids_to_request
= Array();
224 for (var i
= 0; i
< neighbor_ids
.length
; i
++) {
225 if (!cache_check(neighbor_ids
[i
])) {
226 cids_to_request
.push(neighbor_ids
[i
]);
230 debug("additional ids: " + cids_to_request
.toString());
232 /* additional info for piggyback counters */
234 if (tagsAreDisplayed()) {
235 query
= query
+ "&omode=lt";
237 query
= query
+ "&omode=flc";
240 var date
= new Date();
241 var timestamp
= Math
.round(date
.getTime() / 1000);
242 query
= query
+ "&ts=" + timestamp
;
244 query
= query
+ "&cids=" + cids_to_request
.toString();
246 if (!cached_article
) {
248 notify_progress("Loading, please wait...");
252 xmlhttp
.open("GET", query
, true);
253 xmlhttp
.onreadystatechange
=article_callback
;
255 } else if (cached_article
&& article_is_unread
) {
257 query
= query
+ "&mode=prefetch";
261 xmlhttp
.open("GET", query
, true);
262 xmlhttp
.onreadystatechange
=article_callback
;
265 render_article(cached_article
);
267 } else if (cached_article
) {
269 query
= query
+ "&mode=prefetch_old";
273 xmlhttp
.open("GET", query
, true);
274 xmlhttp
.onreadystatechange
=article_callback
;
277 render_article(cached_article
);
284 debug("xmlhttp busy (@view)");
289 exception_error("view", e
);
294 return toggleMark(id
);
297 function toggleMark(id
) {
299 if (!xmlhttp_ready(xmlhttp_rpc
)) {
304 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
306 var mark_img
= document
.getElementById("FMPIC-" + id
);
307 var vfeedu
= document
.getElementById("FEEDU--1");
308 var crow
= document
.getElementById("RROW-" + id
);
310 if (mark_img
.alt
!= "Reset mark") {
311 mark_img
.src
= "images/mark_set.png";
312 mark_img
.alt
= "Reset mark";
313 query
= query
+ "&mark=1";
315 if (vfeedu
&& crow
.className
.match("Unread")) {
316 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
320 mark_img
.src
= "images/mark_unset.png";
321 mark_img
.alt
= "Set mark";
322 query
= query
+ "&mark=0";
324 if (vfeedu
&& crow
.className
.match("Unread")) {
325 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
330 var vfeedctr
= document
.getElementById("FEEDCTR--1");
331 var vfeedr
= document
.getElementById("FEEDR--1");
333 if (vfeedu
&& vfeedctr
) {
334 if ((+vfeedu
.innerHTML
) > 0) {
335 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
336 vfeedr
.className
= vfeedr
.className
+ "Unread";
337 vfeedctr
.className
= "odd";
340 vfeedctr
.className
= "invisible";
341 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
345 debug("toggle starred for aid " + id
);
347 new Ajax
.Request(query
);
351 function correctHeadlinesOffset(id
) {
355 var hlist
= document
.getElementById("headlinesList");
356 var container
= document
.getElementById("headlinesInnerContainer");
357 var row
= document
.getElementById("RROW-" + id
);
359 var viewport
= container
.offsetHeight
;
361 var rel_offset_top
= row
.offsetTop
- container
.scrollTop
;
362 var rel_offset_bottom
= row
.offsetTop
+ row
.offsetHeight
- container
.scrollTop
;
364 debug("Rtop: " + rel_offset_top
+ " Rbtm: " + rel_offset_bottom
);
365 debug("Vport: " + viewport
);
367 if (rel_offset_top
<= 0 || rel_offset_top
> viewport
) {
368 container
.scrollTop
= row
.offsetTop
;
369 } else if (rel_offset_bottom
> viewport
) {
371 /* doesn't properly work with Opera in some cases because
372 Opera fucks up element scrolling */
374 container
.scrollTop
= row
.offsetTop
+ row
.offsetHeight
- viewport
;
378 exception_error("correctHeadlinesOffset", e
);
383 function moveToPost(mode
) {
385 // check for combined mode
386 if (!document
.getElementById("headlinesList"))
389 var rows
= getVisibleHeadlineIds();
394 if (!document
.getElementById('RROW-' + active_post_id
)) {
395 active_post_id
= false;
398 if (active_post_id
== false) {
399 next_id
= getFirstVisibleHeadlineId();
400 prev_id
= getLastVisibleHeadlineId();
402 for (var i
= 0; i
< rows
.length
; i
++) {
403 if (rows
[i
] == active_post_id
) {
410 if (mode
== "next") {
412 correctHeadlinesOffset(next_id
);
413 view(next_id
, getActiveFeedId());
417 if (mode
== "prev") {
419 correctHeadlinesOffset(prev_id
);
420 view(prev_id
, getActiveFeedId());
425 function toggleUnread(id
, cmode
) {
427 if (!xmlhttp_ready(xmlhttp_rpc
)) {
432 var row
= document
.getElementById("RROW-" + id
);
434 var nc
= row
.className
;
435 nc
= nc
.replace("Unread", "");
436 nc
= nc
.replace("Selected", "");
438 if (row
.className
.match("Unread")) {
441 row
.className
= nc
+ "Unread";
444 if (!cmode
) cmode
= 2;
446 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
447 param_escape(id
) + "&cmode=" + param_escape(cmode
);
449 notify_progress("Loading, please wait...");
451 xmlhttp_rpc
.open("GET", query
, true);
452 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
453 xmlhttp_rpc
.send(null);
459 exception_error("toggleUnread", e
);
463 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
465 if (!xmlhttp_ready(xmlhttp_rpc
)) {
473 rows
= cdmGetSelectedArticles();
475 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
478 if (rows
.length
== 0 && !no_error
) {
479 alert(__("No articles are selected."));
483 for (i
= 0; i
< rows
.length
; i
++) {
484 var row
= document
.getElementById("RROW-" + rows
[i
]);
486 var nc
= row
.className
;
487 nc
= nc
.replace("Unread", "");
488 nc
= nc
.replace("Selected", "");
490 if (row
.className
.match("Unread")) {
491 row
.className
= nc
+ "Selected";
493 row
.className
= nc
+ "UnreadSelected";
498 if (rows
.length
> 0) {
502 if (set_state
== undefined) {
504 } else if (set_state
== true) {
506 } else if (set_state
== false) {
510 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
511 param_escape(rows
.toString()) + "&cmode=" + cmode
;
513 _catchup_callback_func
= callback_func
;
515 notify_progress("Loading, please wait...");
517 xmlhttp_rpc
.open("GET", query
, true);
518 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
519 xmlhttp_rpc
.send(null);
524 exception_error("selectionToggleUnread", e
);
528 function selectionToggleMarked(cdm_mode
) {
530 if (!xmlhttp_ready(xmlhttp_rpc
)) {
538 rows
= cdmGetSelectedArticles();
540 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
543 if (rows
.length
== 0) {
544 alert(__("No articles are selected."));
548 for (i
= 0; i
< rows
.length
; i
++) {
549 var row
= document
.getElementById("RROW-" + rows
[i
]);
550 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
552 if (row
&& mark_img
) {
554 if (mark_img
.alt
== "Set mark") {
555 mark_img
.src
= "images/mark_set.png";
556 mark_img
.alt
= "Reset mark";
557 mark_img
.setAttribute('onclick',
558 'javascript:toggleMark('+rows
[i
]+', false)');
561 mark_img
.src
= "images/mark_unset.png";
562 mark_img
.alt
= "Set mark";
563 mark_img
.setAttribute('onclick',
564 'javascript:toggleMark('+rows
[i
]+', true)');
569 if (rows
.length
> 0) {
571 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
572 param_escape(rows
.toString()) + "&cmode=2";
574 xmlhttp_rpc
.open("GET", query
, true);
575 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
576 xmlhttp_rpc
.send(null);
581 exception_error("selectionToggleMarked", e
);
585 function cdmGetSelectedArticles() {
586 var sel_articles
= new Array();
587 var container
= document
.getElementById("headlinesInnerContainer");
589 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
590 var child
= container
.childNodes
[i
];
592 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
593 var c_id
= child
.id
.replace("RROW-", "");
594 sel_articles
.push(c_id
);
601 // mode = all,none,unread
602 function cdmSelectArticles(mode
) {
603 var container
= document
.getElementById("headlinesInnerContainer");
605 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
606 var child
= container
.childNodes
[i
];
608 if (child
.id
.match("RROW-")) {
609 var aid
= child
.id
.replace("RROW-", "");
611 var cb
= document
.getElementById("RCHK-" + aid
);
614 if (!child
.className
.match("Selected")) {
615 child
.className
= child
.className
+ "Selected";
618 } else if (mode
== "unread") {
619 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
620 child
.className
= child
.className
+ "Selected";
624 child
.className
= child
.className
.replace("Selected", "");
631 function catchupPage() {
633 if (document
.getElementById("headlinesList")) {
634 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
635 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
636 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
638 cdmSelectArticles('all');
639 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
640 cdmSelectArticles('none');
644 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
646 if (!xmlhttp_ready(xmlhttp_rpc
)) {
650 var title
= prompt("Please enter label title:", "");
654 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
655 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
656 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
657 "&title=" + param_escape(title
);
659 debug("LFS: " + query
);
661 xmlhttp_rpc
.open("GET", query
, true);
662 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
663 xmlhttp_rpc
.send(null);
668 function editArticleTags(id
, feed_id
, cdm_enabled
) {
669 _tag_active_post_id
= id
;
670 _tag_active_feed_id
= feed_id
;
671 _tag_active_cdm
= cdm_enabled
;
673 cache_invalidate(id
);
676 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
678 displayDlg('editArticleTags', id
);
682 function tag_saved_callback() {
683 if (xmlhttp_rpc
.readyState
== 4) {
685 debug("in tag_saved_callback");
690 if (tagsAreDisplayed()) {
691 _reload_feedlist_after_view
= true;
694 if (!_tag_active_cdm
) {
695 if (active_post_id
== _tag_active_post_id
) {
696 debug("reloading current article");
697 view(_tag_active_post_id
, _tag_active_feed_id
);
700 debug("reloading current feed");
705 exception_error("catchup_callback", e
);
710 function editTagsSave() {
712 if (!xmlhttp_ready(xmlhttp_rpc
)) {
716 notify_progress("Saving article tags...");
718 var form
= document
.forms
["tag_edit_form"];
720 var query
= Form
.serialize("tag_edit_form");
722 xmlhttp_rpc
.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query
, true);
723 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
724 xmlhttp_rpc
.send(null);
728 function editTagsInsert() {
731 var form
= document
.forms
["tag_edit_form"];
733 var found_tags
= form
.found_tags
;
734 var tags_str
= form
.tags_str
;
736 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
738 if (tags_str
.value
.length
> 0 &&
739 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
741 tags_str
.value
= tags_str
.value
+ ", ";
744 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
746 found_tags
.selectedIndex
= 0;
749 exception_error(e
, "editTagsInsert");
753 function cdmWatchdog() {
757 var ctr
= document
.getElementById("headlinesInnerContainer");
761 var ids
= new Array();
763 var e
= ctr
.firstChild
;
766 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
767 e
.id
.match("RROW-")) {
769 // article fits in viewport OR article is longer than viewport and
770 // its bottom is visible
772 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
773 ctr
.scrollTop
+ ctr
.offsetHeight
) {
775 // debug(e.id + " is visible " + e.offsetTop + "." +
776 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
777 // (ctr.scrollTop + ctr.offsetHeight));
779 ids
.push(e
.id
.replace("RROW-", ""));
781 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
782 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
783 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
785 ids
.push(e
.id
.replace("RROW-", ""));
789 // method 2: article bottom is visible and is in upper 1/2 of the viewport
791 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
792 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
794 ids.push(e.id.replace("RROW-", ""));
803 debug("cdmWatchdog, ids= " + ids
.toString());
805 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
807 for (var i
= 0; i
< ids
.length
; i
++) {
808 var e
= document
.getElementById("RROW-" + ids
[i
]);
810 e
.className
= e
.className
.replace("Unread", "");
814 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
815 param_escape(ids
.toString()) + "&cmode=0";
817 xmlhttp_rpc
.open("GET", query
, true);
818 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
819 xmlhttp_rpc
.send(null);
823 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
826 exception_error(e
, "cdmWatchdog");
832 function cache_inject(id
, article
) {
833 if (!cache_check(id
)) {
834 debug("cache_article: miss: " + id
);
836 var cache_obj
= new Array();
838 cache_obj
["id"] = id
;
839 cache_obj
["data"] = article
;
841 article_cache
.push(cache_obj
);
844 debug("cache_article: hit: " + id
);
848 function cache_find(id
) {
849 for (var i
= 0; i
< article_cache
.length
; i
++) {
850 if (article_cache
[i
]["id"] == id
) {
851 return article_cache
[i
]["data"];
857 function cache_check(id
) {
858 for (var i
= 0; i
< article_cache
.length
; i
++) {
859 if (article_cache
[i
]["id"] == id
) {
866 function cache_expire() {
867 while (article_cache
.length
> 20) {
868 article_cache
.shift();
872 function cache_invalidate(id
) {
875 while (i
< article_cache
.length
) {
876 if (article_cache
[i
]["id"] == id
) {
877 article_cache
.remove(i
);