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);
55 debug("headlines_callback: returned no XML object");
56 f
.innerHTML
= xmlhttp
.responseText
;
57 update_all_counters();
60 if (typeof correctPNG
!= 'undefined') {
64 if (_cdm_wd_timeout
) window
.clearTimeout(_cdm_wd_timeout
);
66 if (!document
.getElementById("headlinesList") &&
67 getInitParam("cdm_auto_catchup") == 1) {
68 debug("starting CDM watchdog");
69 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 5000);
70 _cdm_wd_vishist
= new Array();
72 debug("not in CDM mode or watchdog disabled");
75 if (_tag_cdm_scroll
) {
77 document
.getElementById("headlinesInnerContainer").scrollTop
= _tag_cdm_scroll
;
78 _tag_cdm_scroll
= false;
86 function render_article(article
) {
88 var f
= document
.getElementById("content-frame");
93 f
.innerHTML
= article
;
96 exception_error("render_article", e
);
100 function article_callback() {
101 if (xmlhttp
.readyState
== 4) {
102 debug("article_callback");
105 if (xmlhttp
.responseXML
) {
106 var reply
= xmlhttp
.responseXML
.firstChild
.firstChild
;
108 var articles
= xmlhttp
.responseXML
.getElementsByTagName("article");
110 for (var i
= 0; i
< articles
.length
; i
++) {
111 var a_id
= articles
[i
].getAttribute("id");
113 debug("found id: " + a_id
);
115 if (a_id
== active_post_id
) {
116 debug("active article, rendering...");
117 render_article(articles
[i
].firstChild
.nodeValue
);
120 cache_inject(a_id
, articles
[i
].firstChild
.nodeValue
);
124 debug("article_callback: returned no XML object");
127 exception_error("article_callback", e
);
130 var date
= new Date();
131 last_article_view
= date
.getTime() / 1000;
133 if (typeof correctPNG
!= 'undefined') {
137 if (_reload_feedlist_after_view
) {
138 setTimeout('updateFeedList(false, false)', 50);
139 _reload_feedlist_after_view
= false;
141 var counters
= xmlhttp
.responseXML
.getElementsByTagName("counters")[0];
144 debug("parsing piggybacked counters: " + counters
);
145 parse_counters(counters
, false);
147 update_all_counters();
155 function view(id
, feed_id
, skip_history
) {
158 debug("loading article: " + id
+ "/" + feed_id
);
160 active_real_feed_id
= feed_id
;
162 var cached_article
= cache_find(id
);
164 debug("cache check result: " + (cached_article
!= false));
166 /* if (!skip_history) {
167 history_push("ARTICLE:" + id + ":" + feed_id);
173 //setActiveFeedId(feed_id);
175 var query
= "backend.php?op=view&id=" + param_escape(id
) +
176 "&feed=" + param_escape(feed_id
);
178 var date
= new Date();
180 if (!xmlhttp_ready(xmlhttp
) && last_article_view
< date
.getTime() / 1000 - 15) {
181 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
185 if (cached_article
|| xmlhttp_ready(xmlhttp
)) {
187 cleanSelected("headlinesList");
189 var crow
= document
.getElementById("RROW-" + active_post_id
);
191 var article_is_unread
= crow
.className
.match("Unread");
192 debug("article is unread: " + article_is_unread
);
194 crow
.className
= crow
.className
.replace("Unread", "");
196 var upd_img_pic
= document
.getElementById("FUPDPIC-" + active_post_id
);
199 upd_img_pic
.src
= "images/blank_icon.gif";
202 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
203 markHeadline(active_post_id
);
205 var neighbor_ids
= getRelativePostIds(active_post_id
);
207 /* only request uncached articles */
209 var cids_to_request
= Array();
211 for (var i
= 0; i
< neighbor_ids
.length
; i
++) {
212 if (!cache_check(neighbor_ids
[i
])) {
213 cids_to_request
.push(neighbor_ids
[i
]);
217 debug("additional ids: " + cids_to_request
.toString());
219 /* additional info for piggyback counters */
221 if (tagsAreDisplayed()) {
222 query
= query
+ "&omode=lt";
224 query
= query
+ "&omode=flc";
227 var date
= new Date();
228 var timestamp
= Math
.round(date
.getTime() / 1000);
229 query
= query
+ "&ts=" + timestamp
;
231 query
= query
+ "&cids=" + cids_to_request
.toString();
233 if (!cached_article
) {
235 notify_progress("Loading, please wait...");
239 xmlhttp
.open("GET", query
, true);
240 xmlhttp
.onreadystatechange
=article_callback
;
242 } else if (cached_article
&& article_is_unread
) {
244 query
= query
+ "&mode=prefetch";
248 xmlhttp
.open("GET", query
, true);
249 xmlhttp
.onreadystatechange
=article_callback
;
252 render_article(cached_article
);
254 } else if (cached_article
) {
256 render_article(cached_article
);
263 debug("xmlhttp busy (@view)");
268 exception_error("view", e
);
272 function toggleMark(id
) {
274 if (!xmlhttp_ready(xmlhttp_rpc
)) {
279 var query
= "backend.php?op=rpc&id=" + id
+ "&subop=mark";
281 var mark_img
= document
.getElementById("FMARKPIC-" + id
);
282 var vfeedu
= document
.getElementById("FEEDU--1");
283 var crow
= document
.getElementById("RROW-" + id
);
285 if (mark_img
.alt
!= "Reset mark") {
286 mark_img
.src
= "images/mark_set.png";
287 mark_img
.alt
= "Reset mark";
288 query
= query
+ "&mark=1";
290 if (vfeedu
&& crow
.className
.match("Unread")) {
291 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) + 1;
295 mark_img
.src
= "images/mark_unset.png";
296 mark_img
.alt
= "Set mark";
297 query
= query
+ "&mark=0";
299 if (vfeedu
&& crow
.className
.match("Unread")) {
300 vfeedu
.innerHTML
= (+vfeedu
.innerHTML
) - 1;
305 var vfeedctr
= document
.getElementById("FEEDCTR--1");
306 var vfeedr
= document
.getElementById("FEEDR--1");
308 if (vfeedu
&& vfeedctr
) {
309 if ((+vfeedu
.innerHTML
) > 0) {
310 if (crow
.className
.match("Unread") && !vfeedr
.className
.match("Unread")) {
311 vfeedr
.className
= vfeedr
.className
+ "Unread";
312 vfeedctr
.className
= "odd";
315 vfeedctr
.className
= "invisible";
316 vfeedr
.className
= vfeedr
.className
.replace("Unread", "");
320 debug("toggle starred for aid " + id
);
322 new Ajax
.Request(query
);
326 function moveToPost(mode
) {
328 // check for combined mode
329 if (!document
.getElementById("headlinesList"))
332 var rows
= getVisibleHeadlineIds();
337 if (!document
.getElementById('RROW-' + active_post_id
)) {
338 active_post_id
= false;
341 if (active_post_id
== false) {
342 next_id
= getFirstVisibleHeadlineId();
343 prev_id
= getLastVisibleHeadlineId();
345 for (var i
= 0; i
< rows
.length
; i
++) {
346 if (rows
[i
] == active_post_id
) {
353 if (mode
== "next") {
354 if (next_id
!= undefined) {
355 view(next_id
, getActiveFeedId());
359 if (mode
== "prev") {
360 if ( prev_id
!= undefined) {
361 view(prev_id
, getActiveFeedId());
366 function toggleUnread(id
, cmode
) {
368 if (!xmlhttp_ready(xmlhttp_rpc
)) {
373 var row
= document
.getElementById("RROW-" + id
);
375 var nc
= row
.className
;
376 nc
= nc
.replace("Unread", "");
377 nc
= nc
.replace("Selected", "");
379 if (row
.className
.match("Unread")) {
382 row
.className
= nc
+ "Unread";
385 if (!cmode
) cmode
= 2;
387 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
388 param_escape(id
) + "&cmode=" + param_escape(cmode
);
390 notify_progress("Loading, please wait...");
392 xmlhttp_rpc
.open("GET", query
, true);
393 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
394 xmlhttp_rpc
.send(null);
400 exception_error("toggleUnread", e
);
404 function selectionToggleUnread(cdm_mode
, set_state
, callback_func
, no_error
) {
406 if (!xmlhttp_ready(xmlhttp_rpc
)) {
414 rows
= cdmGetSelectedArticles();
416 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
419 if (rows
.length
== 0 && !no_error
) {
420 alert(__("No articles are selected."));
424 for (i
= 0; i
< rows
.length
; i
++) {
425 var row
= document
.getElementById("RROW-" + rows
[i
]);
427 var nc
= row
.className
;
428 nc
= nc
.replace("Unread", "");
429 nc
= nc
.replace("Selected", "");
431 if (row
.className
.match("Unread")) {
432 row
.className
= nc
+ "Selected";
434 row
.className
= nc
+ "UnreadSelected";
439 if (rows
.length
> 0) {
443 if (set_state
== undefined) {
445 } else if (set_state
== true) {
447 } else if (set_state
== false) {
451 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
452 param_escape(rows
.toString()) + "&cmode=" + cmode
;
454 _catchup_callback_func
= callback_func
;
456 notify_progress("Loading, please wait...");
458 xmlhttp_rpc
.open("GET", query
, true);
459 xmlhttp_rpc
.onreadystatechange
=catchup_callback
;
460 xmlhttp_rpc
.send(null);
465 exception_error("selectionToggleUnread", e
);
469 function selectionToggleMarked(cdm_mode
) {
471 if (!xmlhttp_ready(xmlhttp_rpc
)) {
479 rows
= cdmGetSelectedArticles();
481 rows
= getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
484 if (rows
.length
== 0) {
485 alert(__("No articles are selected."));
489 for (i
= 0; i
< rows
.length
; i
++) {
490 var row
= document
.getElementById("RROW-" + rows
[i
]);
491 var mark_img
= document
.getElementById("FMARKPIC-" + rows
[i
]);
493 if (row
&& mark_img
) {
495 if (mark_img
.alt
== "Set mark") {
496 mark_img
.src
= "images/mark_set.png";
497 mark_img
.alt
= "Reset mark";
498 mark_img
.setAttribute('onclick',
499 'javascript:toggleMark('+rows
[i
]+', false)');
502 mark_img
.src
= "images/mark_unset.png";
503 mark_img
.alt
= "Set mark";
504 mark_img
.setAttribute('onclick',
505 'javascript:toggleMark('+rows
[i
]+', true)');
510 if (rows
.length
> 0) {
512 var query
= "backend.php?op=rpc&subop=markSelected&ids=" +
513 param_escape(rows
.toString()) + "&cmode=2";
515 xmlhttp_rpc
.open("GET", query
, true);
516 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
517 xmlhttp_rpc
.send(null);
522 exception_error("selectionToggleMarked", e
);
526 function cdmGetSelectedArticles() {
527 var sel_articles
= new Array();
528 var container
= document
.getElementById("headlinesInnerContainer");
530 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
531 var child
= container
.childNodes
[i
];
533 if (child
.id
.match("RROW-") && child
.className
.match("Selected")) {
534 var c_id
= child
.id
.replace("RROW-", "");
535 sel_articles
.push(c_id
);
542 // mode = all,none,unread
543 function cdmSelectArticles(mode
) {
544 var container
= document
.getElementById("headlinesInnerContainer");
546 for (i
= 0; i
< container
.childNodes
.length
; i
++) {
547 var child
= container
.childNodes
[i
];
549 if (child
.id
.match("RROW-")) {
550 var aid
= child
.id
.replace("RROW-", "");
552 var cb
= document
.getElementById("RCHK-" + aid
);
555 if (!child
.className
.match("Selected")) {
556 child
.className
= child
.className
+ "Selected";
559 } else if (mode
== "unread") {
560 if (child
.className
.match("Unread") && !child
.className
.match("Selected")) {
561 child
.className
= child
.className
+ "Selected";
565 child
.className
= child
.className
.replace("Selected", "");
572 function catchupPage() {
574 if (document
.getElementById("headlinesList")) {
575 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
576 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
577 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
579 cdmSelectArticles('all');
580 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
581 cdmSelectArticles('none');
585 function labelFromSearch(search
, search_mode
, match_on
, feed_id
, is_cat
) {
587 if (!xmlhttp_ready(xmlhttp_rpc
)) {
591 var title
= prompt("Please enter label title:", "");
595 var query
= "backend.php?op=labelFromSearch&search=" + param_escape(search
) +
596 "&smode=" + param_escape(search_mode
) + "&match=" + param_escape(match_on
) +
597 "&feed=" + param_escape(feed_id
) + "&is_cat=" + param_escape(is_cat
) +
598 "&title=" + param_escape(title
);
600 debug("LFS: " + query
);
602 xmlhttp_rpc
.open("GET", query
, true);
603 xmlhttp_rpc
.onreadystatechange
=dlg_frefresh_callback
;
604 xmlhttp_rpc
.send(null);
609 function editArticleTags(id
, feed_id
, cdm_enabled
) {
610 _tag_active_post_id
= id
;
611 _tag_active_feed_id
= feed_id
;
612 _tag_active_cdm
= cdm_enabled
;
614 _tag_cdm_scroll
= document
.getElementById("headlinesInnerContainer").scrollTop
;
616 displayDlg('editArticleTags', id
);
620 function tag_saved_callback() {
621 if (xmlhttp_rpc
.readyState
== 4) {
623 debug("in tag_saved_callback");
628 if (tagsAreDisplayed()) {
629 _reload_feedlist_after_view
= true;
632 if (!_tag_active_cdm
) {
633 if (active_post_id
== _tag_active_post_id
) {
634 debug("reloading current article");
635 view(_tag_active_post_id
, _tag_active_feed_id
);
638 debug("reloading current feed");
643 exception_error("catchup_callback", e
);
648 function editTagsSave() {
650 if (!xmlhttp_ready(xmlhttp_rpc
)) {
654 notify_progress("Saving article tags...");
656 var form
= document
.forms
["tag_edit_form"];
658 var query
= Form
.serialize("tag_edit_form");
660 xmlhttp_rpc
.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query
, true);
661 xmlhttp_rpc
.onreadystatechange
=tag_saved_callback
;
662 xmlhttp_rpc
.send(null);
666 function editTagsInsert() {
669 var form
= document
.forms
["tag_edit_form"];
671 var found_tags
= form
.found_tags
;
672 var tags_str
= form
.tags_str
;
674 var tag
= found_tags
[found_tags
.selectedIndex
].value
;
676 if (tags_str
.value
.length
> 0 &&
677 tags_str
.value
.lastIndexOf(", ") != tags_str
.value
.length
- 2) {
679 tags_str
.value
= tags_str
.value
+ ", ";
682 tags_str
.value
= tags_str
.value
+ tag
+ ", ";
684 found_tags
.selectedIndex
= 0;
687 exception_error(e
, "editTagsInsert");
691 function cdmWatchdog() {
695 var ctr
= document
.getElementById("headlinesInnerContainer");
699 var ids
= new Array();
701 var e
= ctr
.firstChild
;
704 if (e
.className
&& e
.className
== "cdmArticleUnread" && e
.id
&&
705 e
.id
.match("RROW-")) {
707 // article fits in viewport OR article is longer than viewport and
708 // its bottom is visible
710 if (ctr
.scrollTop
<= e
.offsetTop
&& e
.offsetTop
+ e
.offsetHeight
<=
711 ctr
.scrollTop
+ ctr
.offsetHeight
) {
713 // debug(e.id + " is visible " + e.offsetTop + "." +
714 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
715 // (ctr.scrollTop + ctr.offsetHeight));
717 ids
.push(e
.id
.replace("RROW-", ""));
719 } else if (e
.offsetHeight
> ctr
.offsetHeight
&&
720 e
.offsetTop
+ e
.offsetHeight
>= ctr
.scrollTop
&&
721 e
.offsetTop
+ e
.offsetHeight
<= ctr
.scrollTop
+ ctr
.offsetHeight
) {
723 ids
.push(e
.id
.replace("RROW-", ""));
727 // method 2: article bottom is visible and is in upper 1/2 of the viewport
729 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
730 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
732 ids.push(e.id.replace("RROW-", ""));
741 debug("cdmWatchdog, ids= " + ids
.toString());
743 if (ids
.length
> 0 && xmlhttp_ready(xmlhttp_rpc
)) {
745 for (var i
= 0; i
< ids
.length
; i
++) {
746 var e
= document
.getElementById("RROW-" + ids
[i
]);
748 e
.className
= e
.className
.replace("Unread", "");
752 var query
= "backend.php?op=rpc&subop=catchupSelected&ids=" +
753 param_escape(ids
.toString()) + "&cmode=0";
755 xmlhttp_rpc
.open("GET", query
, true);
756 xmlhttp_rpc
.onreadystatechange
=all_counters_callback
;
757 xmlhttp_rpc
.send(null);
761 _cdm_wd_timeout
= window
.setTimeout("cdmWatchdog()", 4000);
764 exception_error(e
, "cdmWatchdog");
770 function cache_inject(id
, article
) {
771 if (!cache_check(id
)) {
772 debug("cache_article: miss: " + id
);
774 var cache_obj
= new Array();
778 cache_obj
["id"] = id
;
779 cache_obj
["entered"] = d
.getTime() / 1000;
780 cache_obj
["data"] = article
;
781 cache_obj
["last_access"] = 0;
783 //article_cache[id] = cache_obj;
785 article_cache
.push(cache_obj
);
788 debug("cache_article: hit: " + id
);
792 function cache_find(id
) {
793 for (var i
= 0; i
< article_cache
.length
; i
++) {
794 if (article_cache
[i
]["id"] == id
) {
796 article_cache
[i
]["last_access"] = d
.getTime() / 1000;
797 return article_cache
[i
]["data"];
803 function cache_check(id
) {
804 for (var i
= 0; i
< article_cache
.length
; i
++) {
805 if (article_cache
[i
]["id"] == id
) {
812 function cache_expire() {
813 while (article_cache
.length
> 20) {
814 article_cache
.shift();