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");
26 all_counters_callback();
27 if (_catchup_callback_func) {
28 setTimeout(_catchup_callback_func, 10);
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");
48 var runtime_info = xmlhttp.responseXML.getElementsByTagName("runtime-info");
51 f.innerHTML = headlines.firstChild.nodeValue;
53 debug("headlines_callback: returned no data");
54 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
59 for (var i = 0; i < articles.length; i++) {
60 var a_id = articles[i].getAttribute("id");
61 debug("found id: " + a_id);
62 cache_inject(a_id, articles[i].firstChild.nodeValue);
65 debug("no cached articles received");
69 debug("parsing piggybacked counters: " + counters);
70 parse_counters(counters, false);
72 debug("counters container not found in reply");
76 debug("parsing runtime info: " + runtime_info[0]);
77 parse_runtime_info(runtime_info[0]);
79 debug("counters container not found in reply");
83 debug("headlines_callback: returned no XML object");
84 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
87 if (typeof correctPNG != 'undefined') {
91 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
93 if (!document.getElementById("headlinesList") &&
94 getInitParam("cdm_auto_catchup") == 1) {
95 debug("starting CDM watchdog");
96 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
97 _cdm_wd_vishist = new Array();
99 debug("not in CDM mode or watchdog disabled");
102 if (_tag_cdm_scroll) {
104 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
105 _tag_cdm_scroll = false;
113 function render_article(article) {
115 var f = document.getElementById("content-frame");
120 f.innerHTML = article;
123 exception_error("render_article", e);
127 function article_callback() {
128 if (xmlhttp.readyState == 4) {
129 debug("article_callback");
132 if (xmlhttp.responseXML) {
133 var reply = xmlhttp.responseXML.firstChild.firstChild;
135 var articles = xmlhttp.responseXML.getElementsByTagName("article");
137 for (var i = 0; i < articles.length; i++) {
138 var a_id = articles[i].getAttribute("id");
140 debug("found id: " + a_id);
142 if (a_id == active_post_id) {
143 debug("active article, rendering...");
144 render_article(articles[i].firstChild.nodeValue);
147 cache_inject(a_id, articles[i].firstChild.nodeValue);
151 debug("article_callback: returned no XML object");
152 var f = document.getElementById("content-frame");
153 f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
156 exception_error("article_callback", e);
159 var date = new Date();
160 last_article_view = date.getTime() / 1000;
162 if (typeof correctPNG != 'undefined') {
166 if (_reload_feedlist_after_view) {
167 setTimeout('updateFeedList(false, false)', 50);
168 _reload_feedlist_after_view = false;
170 var counters = xmlhttp.responseXML.getElementsByTagName("counters")[0];
173 debug("parsing piggybacked counters: " + counters);
174 parse_counters(counters, false);
176 debug("counters container not found in reply");
184 function view(id, feed_id, skip_history) {
187 debug("loading article: " + id + "/" + feed_id);
189 active_real_feed_id = feed_id;
191 var cached_article = cache_find(id);
193 debug("cache check result: " + (cached_article != false));
197 //setActiveFeedId(feed_id);
199 var query = "backend.php?op=view&id=" + param_escape(id) +
200 "&feed=" + param_escape(feed_id);
202 var date = new Date();
204 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
205 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
208 debug("trying alternative reset method for Safari");
209 xmlhttp = Ajax.getTransport();
213 if (xmlhttp_ready(xmlhttp)) {
217 cleanSelected("headlinesList");
219 var crow = document.getElementById("RROW-" + active_post_id);
221 var article_is_unread = crow.className.match("Unread");
222 debug("article is unread: " + article_is_unread);
224 crow.className = crow.className.replace("Unread", "");
226 var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
229 upd_img_pic.src = "images/blank_icon.gif";
232 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
233 markHeadline(active_post_id);
235 var neighbor_ids = getRelativePostIds(active_post_id);
237 /* only request uncached articles */
239 var cids_to_request = Array();
241 for (var i = 0; i < neighbor_ids.length; i++) {
242 if (!cache_check(neighbor_ids[i])) {
243 cids_to_request.push(neighbor_ids[i]);
247 debug("additional ids: " + cids_to_request.toString());
249 /* additional info for piggyback counters */
251 if (tagsAreDisplayed()) {
252 query = query + "&omode=lt";
254 query = query + "&omode=flc";
257 var date = new Date();
258 var timestamp = Math.round(date.getTime() / 1000);
259 query = query + "&ts=" + timestamp;
261 query = query + "&cids=" + cids_to_request.toString();
263 if (!cached_article) {
265 notify_progress("Loading, please wait...");
269 xmlhttp.open("GET", query, true);
270 xmlhttp.onreadystatechange=article_callback;
272 } else if (cached_article && article_is_unread) {
274 query = query + "&mode=prefetch";
278 xmlhttp.open("GET", query, true);
279 xmlhttp.onreadystatechange=article_callback;
282 render_article(cached_article);
284 } else if (cached_article) {
286 query = query + "&mode=prefetch_old";
290 xmlhttp.open("GET", query, true);
291 xmlhttp.onreadystatechange=article_callback;
294 render_article(cached_article);
301 debug("xmlhttp busy (@view)");
306 exception_error("view", e);
311 return toggleMark(id);
314 function toggleMark(id) {
316 if (!xmlhttp_ready(xmlhttp_rpc)) {
321 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
323 var mark_img = document.getElementById("FMPIC-" + id);
324 var vfeedu = document.getElementById("FEEDU--1");
325 var crow = document.getElementById("RROW-" + id);
327 if (mark_img.alt != "Reset mark") {
328 mark_img.src = "images/mark_set.png";
329 mark_img.alt = "Reset mark";
330 query = query + "&mark=1";
332 if (vfeedu && crow.className.match("Unread")) {
333 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
337 mark_img.src = "images/mark_unset.png";
338 mark_img.alt = "Set mark";
339 query = query + "&mark=0";
341 if (vfeedu && crow.className.match("Unread")) {
342 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
347 var vfeedctr = document.getElementById("FEEDCTR--1");
348 var vfeedr = document.getElementById("FEEDR--1");
350 if (vfeedu && vfeedctr) {
351 if ((+vfeedu.innerHTML) > 0) {
352 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
353 vfeedr.className = vfeedr.className + "Unread";
354 vfeedctr.className = "odd";
357 vfeedctr.className = "invisible";
358 vfeedr.className = vfeedr.className.replace("Unread", "");
362 debug("toggle starred for aid " + id);
364 new Ajax.Request(query);
368 function correctHeadlinesOffset(id) {
372 var hlist = document.getElementById("headlinesList");
373 var container = document.getElementById("headlinesInnerContainer");
374 var row = document.getElementById("RROW-" + id);
376 var viewport = container.offsetHeight;
378 var rel_offset_top = row.offsetTop - container.scrollTop;
379 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
381 debug("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
382 debug("Vport: " + viewport);
384 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
385 container.scrollTop = row.offsetTop;
386 } else if (rel_offset_bottom > viewport) {
388 /* doesn't properly work with Opera in some cases because
389 Opera fucks up element scrolling */
391 container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
395 exception_error("correctHeadlinesOffset", e);
400 function moveToPost(mode) {
402 // check for combined mode
403 if (!document.getElementById("headlinesList"))
406 var rows = getVisibleHeadlineIds();
411 if (!document.getElementById('RROW-' + active_post_id)) {
412 active_post_id = false;
415 if (active_post_id == false) {
416 next_id = getFirstVisibleHeadlineId();
417 prev_id = getLastVisibleHeadlineId();
419 for (var i = 0; i < rows.length; i++) {
420 if (rows[i] == active_post_id) {
427 if (mode == "next") {
429 correctHeadlinesOffset(next_id);
430 view(next_id, getActiveFeedId());
434 if (mode == "prev") {
436 correctHeadlinesOffset(prev_id);
437 view(prev_id, getActiveFeedId());
442 function toggleUnread(id, cmode) {
444 if (!xmlhttp_ready(xmlhttp_rpc)) {
449 var row = document.getElementById("RROW-" + id);
451 var nc = row.className;
452 nc = nc.replace("Unread", "");
453 nc = nc.replace("Selected", "");
455 if (row.className.match("Unread")) {
458 row.className = nc + "Unread";
461 if (!cmode) cmode = 2;
463 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
464 param_escape(id) + "&cmode=" + param_escape(cmode);
466 notify_progress("Loading, please wait...");
468 xmlhttp_rpc.open("GET", query, true);
469 xmlhttp_rpc.onreadystatechange=all_counters_callback;
470 xmlhttp_rpc.send(null);
476 exception_error("toggleUnread", e);
480 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
482 if (!xmlhttp_ready(xmlhttp_rpc)) {
490 rows = cdmGetSelectedArticles();
492 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
495 if (rows.length == 0 && !no_error) {
496 alert(__("No articles are selected."));
500 for (i = 0; i < rows.length; i++) {
501 var row = document.getElementById("RROW-" + rows[i]);
503 var nc = row.className;
504 nc = nc.replace("Unread", "");
505 nc = nc.replace("Selected", "");
507 if (row.className.match("Unread")) {
508 row.className = nc + "Selected";
510 row.className = nc + "UnreadSelected";
515 if (rows.length > 0) {
519 if (set_state == undefined) {
521 } else if (set_state == true) {
523 } else if (set_state == false) {
527 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
528 param_escape(rows.toString()) + "&cmode=" + cmode;
530 _catchup_callback_func = callback_func;
532 notify_progress("Loading, please wait...");
534 xmlhttp_rpc.open("GET", query, true);
535 xmlhttp_rpc.onreadystatechange=catchup_callback;
536 xmlhttp_rpc.send(null);
541 exception_error("selectionToggleUnread", e);
545 function selectionToggleMarked(cdm_mode) {
547 if (!xmlhttp_ready(xmlhttp_rpc)) {
555 rows = cdmGetSelectedArticles();
557 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
560 if (rows.length == 0) {
561 alert(__("No articles are selected."));
565 for (i = 0; i < rows.length; i++) {
566 var row = document.getElementById("RROW-" + rows[i]);
567 var mark_img = document.getElementById("FMARKPIC-" + rows[i]);
569 if (row && mark_img) {
571 if (mark_img.alt == "Set mark") {
572 mark_img.src = "images/mark_set.png";
573 mark_img.alt = "Reset mark";
574 mark_img.setAttribute('onclick',
575 'javascript:toggleMark('+rows[i]+', false)');
578 mark_img.src = "images/mark_unset.png";
579 mark_img.alt = "Set mark";
580 mark_img.setAttribute('onclick',
581 'javascript:toggleMark('+rows[i]+', true)');
586 if (rows.length > 0) {
588 var query = "backend.php?op=rpc&subop=markSelected&ids=" +
589 param_escape(rows.toString()) + "&cmode=2";
591 xmlhttp_rpc.open("GET", query, true);
592 xmlhttp_rpc.onreadystatechange=all_counters_callback;
593 xmlhttp_rpc.send(null);
598 exception_error("selectionToggleMarked", e);
602 function cdmGetSelectedArticles() {
603 var sel_articles = new Array();
604 var container = document.getElementById("headlinesInnerContainer");
606 for (i = 0; i < container.childNodes.length; i++) {
607 var child = container.childNodes[i];
609 if (child.id.match("RROW-") && child.className.match("Selected")) {
610 var c_id = child.id.replace("RROW-", "");
611 sel_articles.push(c_id);
618 // mode = all,none,unread
619 function cdmSelectArticles(mode) {
620 var container = document.getElementById("headlinesInnerContainer");
622 for (i = 0; i < container.childNodes.length; i++) {
623 var child = container.childNodes[i];
625 if (child.id.match("RROW-")) {
626 var aid = child.id.replace("RROW-", "");
628 var cb = document.getElementById("RCHK-" + aid);
631 if (!child.className.match("Selected")) {
632 child.className = child.className + "Selected";
635 } else if (mode == "unread") {
636 if (child.className.match("Unread") && !child.className.match("Selected")) {
637 child.className = child.className + "Selected";
641 child.className = child.className.replace("Selected", "");
648 function catchupPage() {
650 if (document.getElementById("headlinesList")) {
651 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
652 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
653 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
655 cdmSelectArticles('all');
656 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
657 cdmSelectArticles('none');
661 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
663 if (!xmlhttp_ready(xmlhttp_rpc)) {
667 var title = prompt("Please enter label title:", "");
671 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
672 "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
673 "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) +
674 "&title=" + param_escape(title);
676 debug("LFS: " + query);
678 xmlhttp_rpc.open("GET", query, true);
679 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
680 xmlhttp_rpc.send(null);
685 function editArticleTags(id, feed_id, cdm_enabled) {
686 _tag_active_post_id = id;
687 _tag_active_feed_id = feed_id;
688 _tag_active_cdm = cdm_enabled;
690 cache_invalidate(id);
693 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
695 displayDlg('editArticleTags', id);
699 function tag_saved_callback() {
700 if (xmlhttp_rpc.readyState == 4) {
702 debug("in tag_saved_callback");
707 if (tagsAreDisplayed()) {
708 _reload_feedlist_after_view = true;
711 if (!_tag_active_cdm) {
712 if (active_post_id == _tag_active_post_id) {
713 debug("reloading current article");
714 view(_tag_active_post_id, _tag_active_feed_id);
717 debug("reloading current feed");
722 exception_error("catchup_callback", e);
727 function editTagsSave() {
729 if (!xmlhttp_ready(xmlhttp_rpc)) {
733 notify_progress("Saving article tags...");
735 var form = document.forms["tag_edit_form"];
737 var query = Form.serialize("tag_edit_form");
739 query = "backend.php?op=rpc&subop=setArticleTags&" + query;
743 xmlhttp_rpc.open("GET", query, true);
744 xmlhttp_rpc.onreadystatechange=tag_saved_callback;
745 xmlhttp_rpc.send(null);
749 function editTagsInsert() {
752 var form = document.forms["tag_edit_form"];
754 var found_tags = form.found_tags;
755 var tags_str = form.tags_str;
757 var tag = found_tags[found_tags.selectedIndex].value;
759 if (tags_str.value.length > 0 &&
760 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
762 tags_str.value = tags_str.value + ", ";
765 tags_str.value = tags_str.value + tag + ", ";
767 found_tags.selectedIndex = 0;
770 exception_error(e, "editTagsInsert");
774 function cdmWatchdog() {
778 var ctr = document.getElementById("headlinesInnerContainer");
782 var ids = new Array();
784 var e = ctr.firstChild;
787 if (e.className && e.className == "cdmArticleUnread" && e.id &&
788 e.id.match("RROW-")) {
790 // article fits in viewport OR article is longer than viewport and
791 // its bottom is visible
793 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
794 ctr.scrollTop + ctr.offsetHeight) {
796 // debug(e.id + " is visible " + e.offsetTop + "." +
797 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
798 // (ctr.scrollTop + ctr.offsetHeight));
800 ids.push(e.id.replace("RROW-", ""));
802 } else if (e.offsetHeight > ctr.offsetHeight &&
803 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
804 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
806 ids.push(e.id.replace("RROW-", ""));
810 // method 2: article bottom is visible and is in upper 1/2 of the viewport
812 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
813 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
815 ids.push(e.id.replace("RROW-", ""));
824 debug("cdmWatchdog, ids= " + ids.toString());
826 if (ids.length > 0 && xmlhttp_ready(xmlhttp_rpc)) {
828 for (var i = 0; i < ids.length; i++) {
829 var e = document.getElementById("RROW-" + ids[i]);
831 e.className = e.className.replace("Unread", "");
835 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
836 param_escape(ids.toString()) + "&cmode=0";
838 xmlhttp_rpc.open("GET", query, true);
839 xmlhttp_rpc.onreadystatechange=all_counters_callback;
840 xmlhttp_rpc.send(null);
844 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
847 exception_error(e, "cdmWatchdog");
853 function cache_inject(id, article) {
854 if (!cache_check(id)) {
855 debug("cache_article: miss: " + id);
857 var cache_obj = new Array();
859 cache_obj["id"] = id;
860 cache_obj["data"] = article;
862 article_cache.push(cache_obj);
865 debug("cache_article: hit: " + id);
869 function cache_find(id) {
870 for (var i = 0; i < article_cache.length; i++) {
871 if (article_cache[i]["id"] == id) {
872 return article_cache[i]["data"];
878 function cache_check(id) {
879 for (var i = 0; i < article_cache.length; i++) {
880 if (article_cache[i]["id"] == id) {
887 function cache_expire() {
888 while (article_cache.length > 20) {
889 article_cache.shift();
893 function cache_invalidate(id) {
898 while (i < article_cache.length) {
899 if (article_cache[i]["id"] == id) {
900 debug("cache_invalidate: removed id " + id);
901 article_cache.splice(i, 1);
906 debug("cache_invalidate: id not found: " + id);
909 exception_error("cache_invalidate", e);