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");
43 f.innerHTML = xmlhttp.responseText;
44 update_all_counters();
45 if (typeof correctPNG != 'undefined') {
49 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
51 if (!document.getElementById("headlinesList") &&
52 getInitParam("cdm_auto_catchup") == 1) {
53 debug("starting CDM watchdog");
54 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
55 _cdm_wd_vishist = new Array();
57 debug("not in CDM mode or watchdog disabled");
60 if (_tag_cdm_scroll) {
62 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
63 _tag_cdm_scroll = false;
71 function render_article(article) {
73 var f = document.getElementById("content-frame");
78 f.innerHTML = article;
81 exception_error("render_article", e);
85 function article_callback() {
86 if (xmlhttp.readyState == 4) {
87 debug("article_callback");
90 if (xmlhttp.responseXML) {
91 var reply = xmlhttp.responseXML.firstChild.firstChild;
93 var articles = xmlhttp.responseXML.getElementsByTagName("article");
95 for (var i = 0; i < articles.length; i++) {
96 var a_id = articles[i].getAttribute("id");
98 debug("found id: " + a_id);
100 if (a_id == active_post_id) {
101 debug("active article, rendering...");
102 render_article(articles[i].firstChild.nodeValue);
105 cache_inject(a_id, articles[i].firstChild.nodeValue);
109 debug("article_callback: returned no XML object");
112 exception_error("article_callback", e);
115 var date = new Date();
116 last_article_view = date.getTime() / 1000;
118 if (typeof correctPNG != 'undefined') {
122 if (_reload_feedlist_after_view) {
123 setTimeout('updateFeedList(false, false)', 50);
124 _reload_feedlist_after_view = false;
126 update_all_counters();
133 function view(id, feed_id, skip_history) {
136 debug("loading article: " + id + "/" + feed_id);
138 active_real_feed_id = feed_id;
140 var cached_article = cache_find(id);
142 debug("cache check result: " + (cached_article != false));
144 /* if (!skip_history) {
145 history_push("ARTICLE:" + id + ":" + feed_id);
151 //setActiveFeedId(feed_id);
153 var query = "backend.php?op=view&id=" + param_escape(id) +
154 "&feed=" + param_escape(feed_id);
156 var date = new Date();
158 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
159 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
163 if (cached_article || xmlhttp_ready(xmlhttp)) {
165 cleanSelected("headlinesList");
167 var crow = document.getElementById("RROW-" + active_post_id);
169 var article_is_unread = crow.className.match("Unread");
170 debug("article is unread: " + article_is_unread);
172 crow.className = crow.className.replace("Unread", "");
174 var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
177 upd_img_pic.src = "images/blank_icon.gif";
180 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
181 markHeadline(active_post_id);
183 var neighbor_ids = getRelativePostIds(active_post_id);
185 /* only request uncached articles */
187 var cids_to_request = Array();
189 for (var i = 0; i < neighbor_ids.length; i++) {
190 if (!cache_check(neighbor_ids[i])) {
191 cids_to_request.push(neighbor_ids[i]);
195 debug("additional ids: " + cids_to_request.toString());
197 var date = new Date();
198 var timestamp = Math.round(date.getTime() / 1000);
199 query = query + "&ts=" + timestamp;
201 query = query + "&cids=" + cids_to_request.toString();
203 if (!cached_article) {
205 notify_progress("Loading, please wait...");
209 xmlhttp.open("GET", query, true);
210 xmlhttp.onreadystatechange=article_callback;
212 } else if (cached_article && article_is_unread) {
214 query = query + "&mode=prefetch";
218 xmlhttp.open("GET", query, true);
219 xmlhttp.onreadystatechange=article_callback;
222 render_article(cached_article);
224 } else if (cached_article) {
226 render_article(cached_article);
233 debug("xmlhttp busy (@view)");
238 exception_error("view", e);
242 function toggleMark(id) {
244 if (!xmlhttp_ready(xmlhttp_rpc)) {
249 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
251 var mark_img = document.getElementById("FMARKPIC-" + id);
252 var vfeedu = document.getElementById("FEEDU--1");
253 var crow = document.getElementById("RROW-" + id);
255 if (mark_img.alt != "Reset mark") {
256 mark_img.src = "images/mark_set.png";
257 mark_img.alt = "Reset mark";
258 query = query + "&mark=1";
260 if (vfeedu && crow.className.match("Unread")) {
261 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
265 mark_img.src = "images/mark_unset.png";
266 mark_img.alt = "Set mark";
267 query = query + "&mark=0";
269 if (vfeedu && crow.className.match("Unread")) {
270 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
275 var vfeedctr = document.getElementById("FEEDCTR--1");
276 var vfeedr = document.getElementById("FEEDR--1");
278 if (vfeedu && vfeedctr) {
279 if ((+vfeedu.innerHTML) > 0) {
280 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
281 vfeedr.className = vfeedr.className + "Unread";
282 vfeedctr.className = "odd";
285 vfeedctr.className = "invisible";
286 vfeedr.className = vfeedr.className.replace("Unread", "");
290 debug("toggle starred for aid " + id);
292 new Ajax.Request(query);
296 function moveToPost(mode) {
298 // check for combined mode
299 if (!document.getElementById("headlinesList"))
302 var rows = getVisibleHeadlineIds();
307 if (!document.getElementById('RROW-' + active_post_id)) {
308 active_post_id = false;
311 if (active_post_id == false) {
312 next_id = getFirstVisibleHeadlineId();
313 prev_id = getLastVisibleHeadlineId();
315 for (var i = 0; i < rows.length; i++) {
316 if (rows[i] == active_post_id) {
323 if (mode == "next") {
324 if (next_id != undefined) {
325 view(next_id, getActiveFeedId());
329 if (mode == "prev") {
330 if ( prev_id != undefined) {
331 view(prev_id, getActiveFeedId());
336 function toggleUnread(id, cmode) {
338 if (!xmlhttp_ready(xmlhttp_rpc)) {
343 var row = document.getElementById("RROW-" + id);
345 var nc = row.className;
346 nc = nc.replace("Unread", "");
347 nc = nc.replace("Selected", "");
349 if (row.className.match("Unread")) {
352 row.className = nc + "Unread";
355 if (!cmode) cmode = 2;
357 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
358 param_escape(id) + "&cmode=" + param_escape(cmode);
360 notify_progress("Loading, please wait...");
362 xmlhttp_rpc.open("GET", query, true);
363 xmlhttp_rpc.onreadystatechange=all_counters_callback;
364 xmlhttp_rpc.send(null);
370 exception_error("toggleUnread", e);
374 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
376 if (!xmlhttp_ready(xmlhttp_rpc)) {
384 rows = cdmGetSelectedArticles();
386 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
389 if (rows.length == 0 && !no_error) {
390 alert(__("No articles are selected."));
394 for (i = 0; i < rows.length; i++) {
395 var row = document.getElementById("RROW-" + rows[i]);
397 var nc = row.className;
398 nc = nc.replace("Unread", "");
399 nc = nc.replace("Selected", "");
401 if (row.className.match("Unread")) {
402 row.className = nc + "Selected";
404 row.className = nc + "UnreadSelected";
409 if (rows.length > 0) {
413 if (set_state == undefined) {
415 } else if (set_state == true) {
417 } else if (set_state == false) {
421 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
422 param_escape(rows.toString()) + "&cmode=" + cmode;
424 _catchup_callback_func = callback_func;
426 notify_progress("Loading, please wait...");
428 xmlhttp_rpc.open("GET", query, true);
429 xmlhttp_rpc.onreadystatechange=catchup_callback;
430 xmlhttp_rpc.send(null);
435 exception_error("selectionToggleUnread", e);
439 function selectionToggleMarked(cdm_mode) {
441 if (!xmlhttp_ready(xmlhttp_rpc)) {
449 rows = cdmGetSelectedArticles();
451 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
454 if (rows.length == 0) {
455 alert(__("No articles are selected."));
459 for (i = 0; i < rows.length; i++) {
460 var row = document.getElementById("RROW-" + rows[i]);
461 var mark_img = document.getElementById("FMARKPIC-" + rows[i]);
463 if (row && mark_img) {
465 if (mark_img.alt == "Set mark") {
466 mark_img.src = "images/mark_set.png";
467 mark_img.alt = "Reset mark";
468 mark_img.setAttribute('onclick',
469 'javascript:toggleMark('+rows[i]+', false)');
472 mark_img.src = "images/mark_unset.png";
473 mark_img.alt = "Set mark";
474 mark_img.setAttribute('onclick',
475 'javascript:toggleMark('+rows[i]+', true)');
480 if (rows.length > 0) {
482 var query = "backend.php?op=rpc&subop=markSelected&ids=" +
483 param_escape(rows.toString()) + "&cmode=2";
485 xmlhttp_rpc.open("GET", query, true);
486 xmlhttp_rpc.onreadystatechange=all_counters_callback;
487 xmlhttp_rpc.send(null);
492 exception_error("selectionToggleMarked", e);
496 function cdmGetSelectedArticles() {
497 var sel_articles = new Array();
498 var container = document.getElementById("headlinesInnerContainer");
500 for (i = 0; i < container.childNodes.length; i++) {
501 var child = container.childNodes[i];
503 if (child.id.match("RROW-") && child.className.match("Selected")) {
504 var c_id = child.id.replace("RROW-", "");
505 sel_articles.push(c_id);
512 // mode = all,none,unread
513 function cdmSelectArticles(mode) {
514 var container = document.getElementById("headlinesInnerContainer");
516 for (i = 0; i < container.childNodes.length; i++) {
517 var child = container.childNodes[i];
519 if (child.id.match("RROW-")) {
520 var aid = child.id.replace("RROW-", "");
522 var cb = document.getElementById("RCHK-" + aid);
525 if (!child.className.match("Selected")) {
526 child.className = child.className + "Selected";
529 } else if (mode == "unread") {
530 if (child.className.match("Unread") && !child.className.match("Selected")) {
531 child.className = child.className + "Selected";
535 child.className = child.className.replace("Selected", "");
542 function catchupPage() {
544 if (document.getElementById("headlinesList")) {
545 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
546 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
547 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
549 cdmSelectArticles('all');
550 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
551 cdmSelectArticles('none');
555 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
557 if (!xmlhttp_ready(xmlhttp_rpc)) {
561 var title = prompt("Please enter label title:", "");
565 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
566 "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
567 "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) +
568 "&title=" + param_escape(title);
570 debug("LFS: " + query);
572 xmlhttp_rpc.open("GET", query, true);
573 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
574 xmlhttp_rpc.send(null);
579 function editArticleTags(id, feed_id, cdm_enabled) {
580 _tag_active_post_id = id;
581 _tag_active_feed_id = feed_id;
582 _tag_active_cdm = cdm_enabled;
584 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
586 displayDlg('editArticleTags', id);
590 function tag_saved_callback() {
591 if (xmlhttp_rpc.readyState == 4) {
593 debug("in tag_saved_callback");
598 if (tagsAreDisplayed()) {
599 _reload_feedlist_after_view = true;
602 if (!_tag_active_cdm) {
603 if (active_post_id == _tag_active_post_id) {
604 debug("reloading current article");
605 view(_tag_active_post_id, _tag_active_feed_id);
608 debug("reloading current feed");
613 exception_error("catchup_callback", e);
618 function editTagsSave() {
620 if (!xmlhttp_ready(xmlhttp_rpc)) {
624 notify_progress("Saving article tags...");
626 var form = document.forms["tag_edit_form"];
628 var query = Form.serialize("tag_edit_form");
630 xmlhttp_rpc.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query, true);
631 xmlhttp_rpc.onreadystatechange=tag_saved_callback;
632 xmlhttp_rpc.send(null);
636 function editTagsInsert() {
639 var form = document.forms["tag_edit_form"];
641 var found_tags = form.found_tags;
642 var tags_str = form.tags_str;
644 var tag = found_tags[found_tags.selectedIndex].value;
646 if (tags_str.value.length > 0 &&
647 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
649 tags_str.value = tags_str.value + ", ";
652 tags_str.value = tags_str.value + tag + ", ";
654 found_tags.selectedIndex = 0;
657 exception_error(e, "editTagsInsert");
661 function cdmWatchdog() {
665 var ctr = document.getElementById("headlinesInnerContainer");
669 var ids = new Array();
671 var e = ctr.firstChild;
674 if (e.className && e.className == "cdmArticleUnread" && e.id &&
675 e.id.match("RROW-")) {
677 // article fits in viewport OR article is longer than viewport and
678 // its bottom is visible
680 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
681 ctr.scrollTop + ctr.offsetHeight) {
683 // debug(e.id + " is visible " + e.offsetTop + "." +
684 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
685 // (ctr.scrollTop + ctr.offsetHeight));
687 ids.push(e.id.replace("RROW-", ""));
689 } else if (e.offsetHeight > ctr.offsetHeight &&
690 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
691 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
693 ids.push(e.id.replace("RROW-", ""));
697 // method 2: article bottom is visible and is in upper 1/2 of the viewport
699 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
700 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
702 ids.push(e.id.replace("RROW-", ""));
711 debug("cdmWatchdog, ids= " + ids.toString());
713 if (ids.length > 0 && xmlhttp_ready(xmlhttp_rpc)) {
715 for (var i = 0; i < ids.length; i++) {
716 var e = document.getElementById("RROW-" + ids[i]);
718 e.className = e.className.replace("Unread", "");
722 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
723 param_escape(ids.toString()) + "&cmode=0";
725 xmlhttp_rpc.open("GET", query, true);
726 xmlhttp_rpc.onreadystatechange=all_counters_callback;
727 xmlhttp_rpc.send(null);
731 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
734 exception_error(e, "cdmWatchdog");
740 function cache_inject(id, article) {
741 if (!cache_check(id)) {
742 debug("cache_article: miss: " + id);
744 var cache_obj = new Array();
748 cache_obj["id"] = id;
749 cache_obj["entered"] = d.getTime() / 1000;
750 cache_obj["data"] = article;
751 cache_obj["last_access"] = 0;
753 //article_cache[id] = cache_obj;
755 article_cache.push(cache_obj);
758 debug("cache_article: hit: " + id);
762 function cache_find(id) {
763 for (var i = 0; i < article_cache.length; i++) {
764 if (article_cache[i]["id"] == id) {
766 article_cache[i]["last_access"] = d.getTime() / 1000;
767 return article_cache[i]["data"];
773 function cache_check(id) {
774 for (var i = 0; i < article_cache.length; i++) {
775 if (article_cache[i]["id"] == id) {
782 function cache_expire() {
783 while (article_cache.length > 30) {
784 article_cache.shift();