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];
48 f
.innerHTML
= headlines
.firstChild
.nodeValue
;
51 debug("parsing piggybacked counters: " + counters
);
52 parse_counters(counters
, false);
54 debug("counters container not found in reply");
57 debug("headlines_callback: returned no XML object");
58 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
61 if (typeof correctPNG
!= 'undefined') {
65 if (_cdm_wd_timeout
) window
.clearTimeout(_cdm_wd_timeout
);
67 if (!document
.getElementById("headlinesList") &&
68 getInitParam("cdm_auto_catchup") == 1) {
69 debug("starting CDM watchdog");
70 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 5000);
71 _cdm_wd_vishist
= new Array();
73 debug("not in CDM mode or watchdog disabled");
76 if (_tag_cdm_scroll
) {
78 document
.getElementById("headlinesInnerContainer").scrollTop
= _tag_cdm_scroll
;
79 _tag_cdm_scroll
= false;
87 function render_article(article
) {
89 var f
= document
.getElementById("content-frame");
94 f
.innerHTML
= article
;
97 exception_error("render_article", e
);
101 function article_callback() {
102 if (xmlhttp
.readyState
== 4) {
103 debug("article_callback");
106 if (xmlhttp
.responseXML
) {
107 var reply
= xmlhttp
.responseXML
.firstChild
.firstChild
;
109 var articles
= xmlhttp
.responseXML
.getElementsByTagName("article");
111 for (var i
= 0; i
< articles
.length
; i
++) {
112 var a_id
= articles
[i
].getAttribute("id");
114 debug("found id: " + a_id
);
116 if (a_id
== active_post_id
) {
117 debug("active article, rendering...");
118 render_article(articles
[i
].firstChild
.nodeValue
);
121 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
125 debug("article_callback: returned no XML object");
126 f
.innerHTML
= "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
129 exception_error("article_callback", e
);
132 var date
= new Date();
133 last_article_view
= date
.getTime() / 1000;
135 if (typeof correctPNG
!= 'undefined') {
139 if (_reload_feedlist_after_view
) {
140 setTimeout('updateFeedList(false, false)', 50);
141 _reload_feedlist_after_view
= false;
143 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
146 debug("parsing piggybacked counters: " + counters
);
147 parse_counters(counters
, false);
149 debug("counters container not found in reply");
157 function view(id
, feed_id
, skip_history
) {
160 debug("loading article: " + id
+ "/" + feed_id
);
162 active_real_feed_id
= feed_id
;
164 var cached_article
= cache_find(id
);
166 debug("cache check result: " + (cached_article
!= false));
168 /* if (!skip_history) {
169 history_push("ARTICLE:" + id + ":" + feed_id);
175 //setActiveFeedId(feed_id);
177 var query
= "backend.php?op=view&id=" + param_escape(id
) +
178 "&feed=" + param_escape(feed_id
);
180 var date
= new Date();
182 if (!xmlhttp_ready(xmlhttp
) && last_article_view
< date
.getTime() / 1000 - 15) {
183 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
187 if (cached_article
|| xmlhttp_ready(xmlhttp
)) {
189 cleanSelected("headlinesList");
191 var crow
= document
.getElementById("RROW-" + active_post_id
);
193 var article_is_unread
= crow
.className
.match("Unread");
194 debug("article is unread: " + article_is_unread
);
196 crow
.className
= crow
.className
.replace("Unread", "");
198 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
201 upd_img_pic
.src
= "images/blank_icon.gif";
204 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
205 markHeadline(active_post_id
);
207 var neighbor_ids
= getRelativePostIds(active_post_id
);
209 /* only request uncached articles */
211 var cids_to_request
= Array();
213 for (var i
= 0; i
< neighbor_ids
.length
; i
++) {
214 if (!cache_check(neighbor_ids
[i
])) {
215 cids_to_request
.push(neighbor_ids
[i
]);
219 debug("additional ids: " + cids_to_request
.toString());
221 /* additional info for piggyback counters */
223 if (tagsAreDisplayed()) {
224 query
= query
+ "&omode=lt";
226 query
= query
+ "&omode=flc";
229 var date
= new Date();
230 var timestamp
= Math
.round(date
.getTime() / 1000);
231 query
= query
+ "&ts=" + timestamp
;
233 query
= query
+ "&cids=" + cids_to_request
.toString();
235 if (!cached_article
) {
237 notify_progress("Loading, please wait...");
241 xmlhttp
.open("GET", query
, true);
242 xmlhttp
.onreadystatechange
=article_callback
;
244 } else if (cached_article
&& article_is_unread
) {
246 query
= query
+ "&mode=prefetch";
250 xmlhttp
.open("GET", query
, true);
251 xmlhttp
.onreadystatechange
=article_callback
;
254 render_article(cached_article
);
256 } else if (cached_article
) {
258 query
= query
+ "&mode=prefetch_old";
262 xmlhttp
.open("GET", query
, true);
263 xmlhttp
.onreadystatechange
=article_callback
;
266 render_article(cached_article
);
273 debug("xmlhttp busy (@view)");
278 exception_error("view", e
);
282 function toggleMark(id
) {
284 if (!xmlhttp_ready(xmlhttp_rpc
)) {
289 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
291 var mark_img
= document
.getElementById("FMARKPIC-" + id
);
292 var vfeedu
= document
.getElementById("FEEDU--1");
293 var crow
= document
.getElementById("RROW-" + id
);
295 if (mark_img
.alt
!= "Reset mark") {
296 mark_img
.src
= "images/mark_set.png";
297 mark_img
.alt
= "Reset mark";
298 query
= query
+ "&mark=1";
300 if (vfeedu
&& crow
.className
.match("Unread")) {
301 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
305 mark_img
.src
= "images/mark_unset.png";
306 mark_img
.alt
= "Set mark";
307 query
= query
+ "&mark=0";
309 if (vfeedu
&& crow
.className
.match("Unread")) {
310 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
315 var vfeedctr
= document
.getElementById("FEEDCTR--1");
316 var vfeedr
= document
.getElementById("FEEDR--1");
318 if (vfeedu
&& vfeedctr
) {
319 if ((+vfeedu
.innerHTML
) > 0) {
320 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
321 vfeedr
.className
= vfeedr
.className
+ "Unread";
322 vfeedctr
.className
= "odd";
325 vfeedctr
.className
= "invisible";
326 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
330 debug("toggle starred for aid " + id
);
332 new Ajax
.Request(query
);
336 function moveToPost(mode
) {
338 // check for combined mode
339 if (!document
.getElementById("headlinesList"))
342 var rows
= getVisibleHeadlineIds();
347 if (!document
.getElementById('RROW-' + active_post_id
)) {
348 active_post_id
= false;
351 if (active_post_id
== false) {
352 next_id
= getFirstVisibleHeadlineId();
353 prev_id
= getLastVisibleHeadlineId();
355 for (var i
= 0; i
< rows
.length
; i
++) {
356 if (rows
[i
] == active_post_id
) {
363 if (mode
== "next") {
364 if (next_id
!= undefined) {
365 view(next_id
, getActiveFeedId());
369 if (mode
== "prev") {
370 if ( prev_id
!= undefined) {
371 view(prev_id
, getActiveFeedId());
376 function toggleUnread(id
, cmode
) {
378 if (!xmlhttp_ready(xmlhttp_rpc
)) {
383 var row
= document
.getElementById("RROW-" + id
);
385 var nc
= row
.className
;
386 nc
= nc
.replace("Unread", "");
387 nc
= nc
.replace("Selected", "");
389 if (row
.className
.match("Unread")) {
392 row
.className
= nc
+ "Unread";
395 if (!cmode
) cmode
= 2;
397 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
398 param_escape(id
) + "&cmode=" + param_escape(cmode
);
400 notify_progress("Loading, please wait...");
402 xmlhttp_rpc
.open("GET", query
, true);
403 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
404 xmlhttp_rpc
.send(null);
410 exception_error("toggleUnread", e
);
414 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
416 if (!xmlhttp_ready(xmlhttp_rpc
)) {
424 rows
= cdmGetSelectedArticles();
426 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
429 if (rows
.length
== 0 && !no_error
) {
430 alert(__("No articles are selected."));
434 for (i
= 0; i
< rows
.length
; i
++) {
435 var row
= document
.getElementById("RROW-" + rows
[i
]);
437 var nc
= row
.className
;
438 nc
= nc
.replace("Unread", "");
439 nc
= nc
.replace("Selected", "");
441 if (row
.className
.match("Unread")) {
442 row
.className
= nc
+ "Selected";
444 row
.className
= nc
+ "UnreadSelected";
449 if (rows
.length
> 0) {
453 if (set_state
== undefined) {
455 } else if (set_state
== true) {
457 } else if (set_state
== false) {
461 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
462 param_escape(rows
.toString()) + "&cmode=" + cmode
;
464 _catchup_callback_func
= callback_func
;
466 notify_progress("Loading, please wait...");
468 xmlhttp_rpc
.open("GET", query
, true);
469 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
470 xmlhttp_rpc
.send(null);
475 exception_error("selectionToggleUnread", e
);
479 function selectionToggleMarked(cdm_mode
) {
481 if (!xmlhttp_ready(xmlhttp_rpc
)) {
489 rows
= cdmGetSelectedArticles();
491 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
494 if (rows
.length
== 0) {
495 alert(__("No articles are selected."));
499 for (i
= 0; i
< rows
.length
; i
++) {
500 var row
= document
.getElementById("RROW-" + rows
[i
]);
501 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
503 if (row
&& mark_img
) {
505 if (mark_img
.alt
== "Set mark") {
506 mark_img
.src
= "images/mark_set.png";
507 mark_img
.alt
= "Reset mark";
508 mark_img
.setAttribute('onclick',
509 'javascript:toggleMark('+rows
[i
]+', false)');
512 mark_img
.src
= "images/mark_unset.png";
513 mark_img
.alt
= "Set mark";
514 mark_img
.setAttribute('onclick',
515 'javascript:toggleMark('+rows
[i
]+', true)');
520 if (rows
.length
> 0) {
522 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
523 param_escape(rows
.toString()) + "&cmode=2";
525 xmlhttp_rpc
.open("GET", query
, true);
526 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
527 xmlhttp_rpc
.send(null);
532 exception_error("selectionToggleMarked", e
);
536 function cdmGetSelectedArticles() {
537 var sel_articles
= new Array();
538 var container
= document
.getElementById("headlinesInnerContainer");
540 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
541 var child
= container
.childNodes
[i
];
543 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
544 var c_id
= child
.id
.replace("RROW-", "");
545 sel_articles
.push(c_id
);
552 // mode = all,none,unread
553 function cdmSelectArticles(mode
) {
554 var container
= document
.getElementById("headlinesInnerContainer");
556 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
557 var child
= container
.childNodes
[i
];
559 if (child
.id
.match("RROW-")) {
560 var aid
= child
.id
.replace("RROW-", "");
562 var cb
= document
.getElementById("RCHK-" + aid
);
565 if (!child
.className
.match("Selected")) {
566 child
.className
= child
.className
+ "Selected";
569 } else if (mode
== "unread") {
570 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
571 child
.className
= child
.className
+ "Selected";
575 child
.className
= child
.className
.replace("Selected", "");
582 function catchupPage() {
584 if (document
.getElementById("headlinesList")) {
585 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
586 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
587 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
589 cdmSelectArticles('all');
590 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
591 cdmSelectArticles('none');
595 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
597 if (!xmlhttp_ready(xmlhttp_rpc
)) {
601 var title
= prompt("Please enter label title:", "");
605 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
606 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
607 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
608 "&title=" + param_escape(title
);
610 debug("LFS: " + query
);
612 xmlhttp_rpc
.open("GET", query
, true);
613 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
614 xmlhttp_rpc
.send(null);
619 function editArticleTags(id
, feed_id
, cdm_enabled
) {
620 _tag_active_post_id
= id
;
621 _tag_active_feed_id
= feed_id
;
622 _tag_active_cdm
= cdm_enabled
;
624 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
626 displayDlg('editArticleTags', id
);
630 function tag_saved_callback() {
631 if (xmlhttp_rpc
.readyState
== 4) {
633 debug("in tag_saved_callback");
638 if (tagsAreDisplayed()) {
639 _reload_feedlist_after_view
= true;
642 if (!_tag_active_cdm
) {
643 if (active_post_id
== _tag_active_post_id
) {
644 debug("reloading current article");
645 view(_tag_active_post_id
, _tag_active_feed_id
);
648 debug("reloading current feed");
653 exception_error("catchup_callback", e
);
658 function editTagsSave() {
660 if (!xmlhttp_ready(xmlhttp_rpc
)) {
664 notify_progress("Saving article tags...");
666 var form
= document
.forms
["tag_edit_form"];
668 var query
= Form
.serialize("tag_edit_form");
670 xmlhttp_rpc
.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query
, true);
671 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
672 xmlhttp_rpc
.send(null);
676 function editTagsInsert() {
679 var form
= document
.forms
["tag_edit_form"];
681 var found_tags
= form
.found_tags
;
682 var tags_str
= form
.tags_str
;
684 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
686 if (tags_str
.value
.length
> 0 &&
687 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
689 tags_str
.value
= tags_str
.value
+ ", ";
692 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
694 found_tags
.selectedIndex
= 0;
697 exception_error(e
, "editTagsInsert");
701 function cdmWatchdog() {
705 var ctr
= document
.getElementById("headlinesInnerContainer");
709 var ids
= new Array();
711 var e
= ctr
.firstChild
;
714 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
715 e
.id
.match("RROW-")) {
717 // article fits in viewport OR article is longer than viewport and
718 // its bottom is visible
720 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
721 ctr
.scrollTop
+ ctr
.offsetHeight
) {
723 // debug(e.id + " is visible " + e.offsetTop + "." +
724 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
725 // (ctr.scrollTop + ctr.offsetHeight));
727 ids
.push(e
.id
.replace("RROW-", ""));
729 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
730 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
731 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
733 ids
.push(e
.id
.replace("RROW-", ""));
737 // method 2: article bottom is visible and is in upper 1/2 of the viewport
739 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
740 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
742 ids.push(e.id.replace("RROW-", ""));
751 debug("cdmWatchdog, ids= " + ids
.toString());
753 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
755 for (var i
= 0; i
< ids
.length
; i
++) {
756 var e
= document
.getElementById("RROW-" + ids
[i
]);
758 e
.className
= e
.className
.replace("Unread", "");
762 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
763 param_escape(ids
.toString()) + "&cmode=0";
765 xmlhttp_rpc
.open("GET", query
, true);
766 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
767 xmlhttp_rpc
.send(null);
771 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
774 exception_error(e
, "cdmWatchdog");
780 function cache_inject(id
, article
) {
781 if (!cache_check(id
)) {
782 debug("cache_article: miss: " + id
);
784 var cache_obj
= new Array();
788 cache_obj
["id"] = id
;
789 cache_obj
["entered"] = d
.getTime() / 1000;
790 cache_obj
["data"] = article
;
791 cache_obj
["last_access"] = 0;
793 //article_cache[id] = cache_obj;
795 article_cache
.push(cache_obj
);
798 debug("cache_article: hit: " + id
);
802 function cache_find(id
) {
803 for (var i
= 0; i
< article_cache
.length
; i
++) {
804 if (article_cache
[i
]["id"] == id
) {
806 article_cache
[i
]["last_access"] = d
.getTime() / 1000;
807 return article_cache
[i
]["data"];
813 function cache_check(id
) {
814 for (var i
= 0; i
< article_cache
.length
; i
++) {
815 if (article_cache
[i
]["id"] == id
) {
822 function cache_expire() {
823 while (article_cache
.length
> 20) {
824 article_cache
.shift();