1 var active_post_id = false;
2 var last_article_view = false;
3 var active_real_feed_id = false;
5 // FIXME: kludges, needs proper implementation
6 var _reload_feedlist_after_view = false;
8 var _cdm_wd_timeout = false;
9 var _cdm_wd_vishist = new Array();
11 var article_cache = new Array();
13 var vgroup_last_feed = false;
14 var post_under_pointer = false;
16 var last_requested_article = false;
18 var preload_id_batch = [];
19 var preload_timeout_id = false;
23 function catchup_callback2(transport, callback) {
25 console.log("catchup_callback2 " + transport + ", " + callback);
27 handle_rpc_reply(transport);
29 setTimeout(callback, 10);
32 exception_error("catchup_callback2", e, transport);
36 function headlines_callback2(transport, feed_cur_page) {
39 if (!handle_rpc_reply(transport)) return;
41 loading_set_progress(100);
43 console.log("headlines_callback2 [page=" + feed_cur_page + "]");
45 if (!transport_error_check(transport)) return;
47 clean_feed_selections();
52 if (transport.responseXML) {
53 var headlines = transport.responseXML.getElementsByTagName("headlines")[0];
55 is_cat = headlines.getAttribute("is_cat");
56 feed_id = headlines.getAttribute("id");
57 setActiveFeedId(feed_id, is_cat);
61 var ll = $('FLL-' + feed_id);
63 if (ll && ll.parentNode)
64 ll.parentNode.removeChild(ll);
67 var feedr = $("FEEDR-" + feed_id);
68 if (feedr && !feedr.className.match("Selected")) {
69 feedr.className = feedr.className + "Selected";
72 var feedr = $("FCAT-" + feed_id);
73 if (feedr && !feedr.className.match("Selected")) {
74 feedr.className = feedr.className + "Selected";
78 var img = $('FIMG-' + feed_id);
84 var f = $("headlines-frame");
86 if (feed_cur_page == 0) {
87 //console.log("resetting headlines scrollTop");
92 if (transport.responseXML) {
93 var response = transport.responseXML;
95 var headlines = response.getElementsByTagName("headlines")[0];
96 var headlines_info = response.getElementsByTagName("headlines-info")[0];
99 headlines_info = JSON.parse(headlines_info.firstChild.nodeValue);
101 console.error("didn't find headlines-info object in response");
105 var headlines_count = headlines_info.count;
106 var headlines_unread = headlines_info.unread;
107 var disable_cache = headlines_info.disable_cache;
109 vgroup_last_feed = headlines_info.vgroup_last_feed;
111 if (headlines_count == 0) {
112 _infscroll_disable = 1;
114 _infscroll_disable = 0;
117 var counters = response.getElementsByTagName("counters")[0];
118 var articles = response.getElementsByTagName("article");
119 var runtime_info = response.getElementsByTagName("runtime-info");
121 if (feed_cur_page == 0) {
123 f.innerHTML = headlines.firstChild.nodeValue;
125 var cache_prefix = "";
133 cache_invalidate(cache_prefix + feed_id);
135 if (!disable_cache) {
136 cache_inject(cache_prefix + feed_id,
137 headlines.firstChild.nodeValue, headlines_unread);
141 console.warn("headlines_callback: returned no data");
142 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML data)') + "</div>";
147 if (headlines_count > 0) {
148 console.log("adding some more headlines...");
150 var c = $("headlinesList");
153 c = $("headlinesInnerContainer");
156 var ids = getSelectedArticleIds2();
158 c.innerHTML = c.innerHTML + headlines.firstChild.nodeValue;
160 console.log("restore selected ids: " + ids);
162 for (var i = 0; i < ids.length; i++) {
163 markHeadline(ids[i]);
167 console.log("no new headlines received");
170 console.warn("headlines_callback: returned no data");
171 notify_error("Error while trying to load more headlines");
177 for (var i = 0; i < articles.length; i++) {
178 var a_id = articles[i].getAttribute("id");
179 //console.log("found id: " + a_id);
180 cache_inject(a_id, articles[i].firstChild.nodeValue);
183 console.log("no cached articles received");
187 parse_counters(counters);
192 parse_runtime_info(runtime_info[0]);
196 console.warn("headlines_callback: returned no XML object");
197 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
201 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
204 getActiveFeedId() != -3 &&
205 getInitParam("cdm_auto_catchup") == 1) {
206 console.log("starting CDM watchdog");
207 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
208 _cdm_wd_vishist = new Array();
210 console.log("not in CDM mode or watchdog disabled");
213 _feed_cur_page = feed_cur_page;
214 _infscroll_request_sent = 0;
221 exception_error("headlines_callback2", e, transport);
225 function render_article(article) {
227 var f = $("content-frame");
232 var fi = $("content-insert");
238 fi.innerHTML = article;
240 // article.evalScripts();
243 exception_error("render_article", e);
247 function showArticleInHeadlines(id) {
251 cleanSelected("headlinesList");
253 var crow = $("RROW-" + id);
257 var article_is_unread = crow.className.match("Unread");
259 crow.className = crow.className.replace("Unread", "");
261 selectArticles('none');
263 var upd_img_pic = $("FUPDPIC-" + id);
265 var cache_prefix = "";
267 if (activeFeedIsCat()) {
273 var view_mode = false;
276 view_mode = document.forms['main_toolbar_form'].view_mode;
277 view_mode = view_mode[view_mode.selectedIndex].value;
282 if (upd_img_pic && (upd_img_pic.src.match("updated.png") ||
283 upd_img_pic.src.match("fresh_sign.png"))) {
285 upd_img_pic.src = "images/blank_icon.gif";
287 cache_invalidate(cache_prefix + getActiveFeedId());
289 cache_inject(cache_prefix + getActiveFeedId(),
290 $("headlines-frame").innerHTML,
291 get_feed_unread(getActiveFeedId()));
293 } else if (article_is_unread && view_mode == "all_articles") {
295 cache_invalidate(cache_prefix + getActiveFeedId());
297 cache_inject(cache_prefix + getActiveFeedId(),
298 $("headlines-frame").innerHTML,
299 get_feed_unread(getActiveFeedId())-1);
301 } else if (article_is_unread) {
302 cache_invalidate(cache_prefix + getActiveFeedId());
307 if (article_is_unread)
308 _force_scheduled_update = true;
311 exception_error("showArticleInHeadlines", e);
315 function article_callback2(transport, id) {
317 console.log("article_callback2 " + id);
319 if (!handle_rpc_reply(transport)) return;
321 if (transport.responseXML) {
323 if (!transport_error_check(transport)) return;
325 /* var ll = $('LL-' + id);
326 var content = $('HLC-' + id);
328 if (ll && content) content.removeChild(ll); */
330 var upic = $('FUPDPIC-' + id);
333 upic.src = 'images/blank_icon.gif';
336 if (id != last_requested_article) {
337 console.log("requested article id is out of sequence, aborting");
343 //console.log("looking for articles to cache...");
345 var articles = transport.responseXML.getElementsByTagName("article");
347 for (var i = 0; i < articles.length; i++) {
348 var a_id = articles[i].getAttribute("id");
350 //console.log("found id: " + a_id);
352 if (a_id == active_post_id) {
353 //console.log("active article, rendering...");
354 render_article(articles[i].firstChild.nodeValue);
357 cache_inject(a_id, articles[i].firstChild.nodeValue);
361 showArticleInHeadlines(id);
364 db.execute("UPDATE articles SET unread = 0 WHERE id = ?", [id]);
367 var reply = transport.responseXML.firstChild.firstChild;
370 console.warn("article_callback: returned no XML object");
371 //var f = $("content-frame");
372 //f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
375 var date = new Date();
376 last_article_view = date.getTime() / 1000;
378 if (_reload_feedlist_after_view) {
379 setTimeout('updateFeedList(false, false)', 50);
380 _reload_feedlist_after_view = false;
387 exception_error("article_callback2", e, transport);
393 console.log("loading article: " + id);
395 if (offline_mode) return view_offline(id);
397 var cached_article = cache_find(id);
399 console.log("cache check result: " + (cached_article != false));
404 var query = "?op=view&id=" + param_escape(id);
406 var neighbor_ids = getRelativePostIds(active_post_id);
408 /* only request uncached articles */
410 var cids_to_request = Array();
412 for (var i = 0; i < neighbor_ids.length; i++) {
413 if (!cache_check(neighbor_ids[i])) {
414 cids_to_request.push(neighbor_ids[i]);
418 console.log("additional ids: " + cids_to_request.toString());
420 /* additional info for piggyback counters */
422 if (tagsAreDisplayed()) {
423 query = query + "&omode=lt";
425 query = query + "&omode=flc";
428 query = query + "&cids=" + cids_to_request.toString();
430 var crow = $("RROW-" + id);
431 var article_is_unread = crow.className.match("Unread");
433 showArticleInHeadlines(id);
435 if (!cached_article) {
437 var upic = $('FUPDPIC-' + id);
440 upic.src = getInitParam("sign_progress");
443 } else if (cached_article && article_is_unread) {
445 query = query + "&mode=prefetch";
447 render_article(cached_article);
449 } else if (cached_article) {
451 query = query + "&mode=prefetch_old";
452 render_article(cached_article);
458 last_requested_article = id;
460 new Ajax.Request("backend.php", {
462 onComplete: function(transport) {
463 article_callback2(transport, id);
469 exception_error("view", e);
474 return toggleMark(id);
478 return togglePub(id);
481 function tMark_afh_off(effect) {
483 var elem = effect.effects[0].element;
485 //console.log("tMark_afh_off : " + elem.id);
488 elem.src = elem.src.replace("mark_set", "mark_unset");
489 elem.alt = __("Star article");
494 exception_error("tMark_afh_off", e);
498 function tPub_afh_off(effect) {
500 var elem = effect.effects[0].element;
502 //console.log("tPub_afh_off : " + elem.id);
505 elem.src = elem.src.replace("pub_set", "pub_unset");
506 elem.alt = __("Publish article");
511 exception_error("tPub_afh_off", e);
515 function toggleMark(id, client_only, no_effects) {
519 var query = "?op=rpc&id=" + id + "&subop=mark";
521 query = query + "&afid=" + getActiveFeedId();
523 if (tagsAreDisplayed()) {
524 query = query + "&omode=tl";
526 query = query + "&omode=flc";
529 var mark_img = $("FMPIC-" + id);
531 if (!mark_img) return;
533 var vfeedu = $("FEEDU--1");
534 var crow = $("RROW-" + id);
536 if (mark_img.src.match("mark_unset")) {
537 mark_img.src = mark_img.src.replace("mark_unset", "mark_set");
538 mark_img.alt = __("Unstar article");
539 query = query + "&mark=1";
542 db.execute("UPDATE articles SET marked = 1 WHERE id = ?", [id]);
546 mark_img.alt = __("Please wait...");
547 query = query + "&mark=0";
549 if (!isCdmMode() && !no_effects) {
550 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tMark_afh_off});
552 mark_img.src = mark_img.src.replace("mark_set", "mark_unset");
553 mark_img.alt = __("Star article");
557 db.execute("UPDATE articles SET marked = 0 WHERE id = ?", [id]);
562 if (!no_effects) update_local_feedlist_counters();
565 //console.log(query);
567 new Ajax.Request("backend.php", {
569 onComplete: function(transport) {
570 handle_rpc_reply(transport);
576 exception_error("toggleMark", e);
580 function togglePub(id, client_only, no_effects, note) {
584 var query = "?op=rpc&id=" + id + "&subop=publ";
586 query = query + "&afid=" + getActiveFeedId();
588 if (note != undefined) {
589 query = query + "¬e=" + param_escape(note);
591 query = query + "¬e=undefined";
594 if (tagsAreDisplayed()) {
595 query = query + "&omode=tl";
597 query = query + "&omode=flc";
600 var mark_img = $("FPPIC-" + id);
602 if (!mark_img) return;
604 var vfeedu = $("FEEDU--2");
605 var crow = $("RROW-" + id);
607 if (mark_img.src.match("pub_unset") || note != undefined) {
608 mark_img.src = mark_img.src.replace("pub_unset", "pub_set");
609 mark_img.alt = __("Unpublish article");
610 query = query + "&pub=1";
613 mark_img.alt = __("Please wait...");
614 query = query + "&pub=0";
616 if (!isCdmMode() && !no_effects) {
617 Effect.Puff(mark_img, {duration : 0.25, afterFinish: tPub_afh_off});
619 mark_img.src = mark_img.src.replace("pub_set", "pub_unset");
620 mark_img.alt = __("Publish article");
625 new Ajax.Request("backend.php", {
627 onComplete: function(transport) {
628 handle_rpc_reply(transport);
630 var note = transport.responseXML.getElementsByTagName("note")[0];
633 var note_id = note.getAttribute("id");
634 var note_size = note.getAttribute("size");
635 var note_content = note.firstChild.nodeValue;
637 var container = $('POSTNOTE-' + note_id);
639 cache_invalidate(note_id);
642 if (note_size == "0") {
643 Element.hide(container);
645 container.innerHTML = note_content;
646 Element.show(container);
655 exception_error("togglePub", e);
659 function correctHeadlinesOffset(id) {
663 var hlist = $("headlinesList");
664 var container = $("headlinesInnerContainer");
665 var row = $("RROW-" + id);
667 var viewport = container.offsetHeight;
669 var rel_offset_top = row.offsetTop - container.scrollTop;
670 var rel_offset_bottom = row.offsetTop + row.offsetHeight - container.scrollTop;
672 console.log("Rtop: " + rel_offset_top + " Rbtm: " + rel_offset_bottom);
673 console.log("Vport: " + viewport);
675 if (rel_offset_top <= 0 || rel_offset_top > viewport) {
676 container.scrollTop = row.offsetTop;
677 } else if (rel_offset_bottom > viewport) {
679 /* doesn't properly work with Opera in some cases because
680 Opera fucks up element scrolling */
682 container.scrollTop = row.offsetTop + row.offsetHeight - viewport;
686 exception_error("correctHeadlinesOffset", e);
691 function moveToPost(mode) {
695 var rows = getVisibleArticleIds();
700 if (!$('RROW-' + active_post_id)) {
701 active_post_id = false;
704 if (active_post_id == false) {
705 next_id = getFirstVisibleHeadlineId();
706 prev_id = getLastVisibleHeadlineId();
708 for (var i = 0; i < rows.length; i++) {
709 if (rows[i] == active_post_id) {
716 if (mode == "next") {
720 cdmExpandArticle(next_id);
721 cdmScrollToArticleId(next_id);
724 correctHeadlinesOffset(next_id);
725 view(next_id, getActiveFeedId());
730 if (mode == "prev") {
733 cdmExpandArticle(prev_id);
734 cdmScrollToArticleId(prev_id);
736 correctHeadlinesOffset(prev_id);
737 view(prev_id, getActiveFeedId());
743 exception_error("moveToPost", e);
747 function toggleSelected(id) {
750 var cb = $("RCHK-" + id);
752 var row = $("RROW-" + id);
754 var nc = row.className;
756 if (!nc.match("Selected")) {
757 nc = nc + "Selected";
762 // In CDM basically last selected article == active article
763 if (isCdmMode()) active_post_id = id;
765 nc = nc.replace("Selected", "");
775 exception_error("toggleSelected", e);
779 function toggleUnread_afh(effect) {
782 var elem = effect.element;
783 elem.style.backgroundColor = "";
786 exception_error("toggleUnread_afh", e);
790 function toggleUnread(id, cmode, effect) {
793 var row = $("RROW-" + id);
795 var nc = row.className;
796 var is_selected = row.className.match("Selected");
797 nc = nc.replace("Unread", "");
798 nc = nc.replace("Selected", "");
800 // since we are removing selection from the object, uncheck
801 // corresponding checkbox
803 var cb = $("RCHK-" + id);
808 // NOTE: I'm not sure that resetting selection here is a feature -fox
810 if (cmode == undefined || cmode == 2) {
811 if (row.className.match("Unread")) {
815 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
816 afterFinish: toggleUnread_afh,
817 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
821 row.className = nc + "Unread";
825 db.execute("UPDATE articles SET unread = not unread "+
826 "WHERE id = ?", [id]);
829 } else if (cmode == 0) {
833 new Effect.Highlight(row, {duration: 1, startcolor: "#fff7d5",
834 afterFinish: toggleUnread_afh,
835 queue: { position:'end', scope: 'TMRQ-' + id, limit: 1 } } );
839 db.execute("UPDATE articles SET unread = 0 "+
840 "WHERE id = ?", [id]);
843 } else if (cmode == 1) {
844 row.className = nc + "Unread";
847 db.execute("UPDATE articles SET unread = 1 "+
848 "WHERE id = ?", [id]);
853 update_local_feedlist_counters();
855 // Disable unmarking as selected for the time being (16.05.08) -fox
856 if (is_selected) row.className = row.className + "Selected";
858 if (cmode == undefined) cmode = 2;
860 var query = "?op=rpc&subop=catchupSelected" +
861 "&cmode=" + param_escape(cmode) + "&ids=" + param_escape(id);
863 // notify_progress("Loading, please wait...");
865 new Ajax.Request("backend.php", {
867 onComplete: function(transport) {
868 handle_rpc_reply(transport);
874 exception_error("toggleUnread", e);
878 function selectionRemoveLabel(id) {
881 var ids = getSelectedArticleIds2();
883 if (ids.length == 0) {
884 alert(__("No articles are selected."));
888 // var ok = confirm(__("Remove selected articles from label?"));
892 var query = "?op=rpc&subop=removeFromLabel&ids=" +
893 param_escape(ids.toString()) + "&lid=" + param_escape(id);
897 // notify_progress("Loading, please wait...");
899 cache_invalidate("F:" + (-11 - id));
901 new Ajax.Request("backend.php", {
903 onComplete: function(transport) {
904 show_labels_in_headlines(transport);
905 handle_rpc_reply(transport);
911 exception_error("selectionAssignLabel", e);
916 function selectionAssignLabel(id) {
919 var ids = getSelectedArticleIds2();
921 if (ids.length == 0) {
922 alert(__("No articles are selected."));
926 // var ok = confirm(__("Assign selected articles to label?"));
930 cache_invalidate("F:" + (-11 - id));
932 var query = "?op=rpc&subop=assignToLabel&ids=" +
933 param_escape(ids.toString()) + "&lid=" + param_escape(id);
937 // notify_progress("Loading, please wait...");
939 new Ajax.Request("backend.php", {
941 onComplete: function(transport) {
942 show_labels_in_headlines(transport);
943 handle_rpc_reply(transport);
949 exception_error("selectionAssignLabel", e);
954 function selectionToggleUnread(set_state, callback_func, no_error) {
956 var rows = getSelectedArticleIds2();
958 if (rows.length == 0 && !no_error) {
959 alert(__("No articles are selected."));
963 for (i = 0; i < rows.length; i++) {
964 var row = $("RROW-" + rows[i]);
966 var nc = row.className;
967 nc = nc.replace("Unread", "");
968 nc = nc.replace("Selected", "");
970 if (set_state == undefined) {
971 if (row.className.match("Unread")) {
972 row.className = nc + "Selected";
974 row.className = nc + "UnreadSelected";
977 db.execute("UPDATE articles SET unread = NOT unread WHERE id = ?",
982 if (set_state == false) {
983 row.className = nc + "Selected";
985 db.execute("UPDATE articles SET unread = 0 WHERE id = ?",
990 if (set_state == true) {
991 row.className = nc + "UnreadSelected";
993 db.execute("UPDATE articles SET unread = 1 WHERE id = ?",
1000 if (rows.length > 0) {
1002 update_local_feedlist_counters();
1006 if (set_state == undefined) {
1008 } else if (set_state == true) {
1010 } else if (set_state == false) {
1014 var query = "?op=rpc&subop=catchupSelected" +
1015 "&cmode=" + cmode + "&ids=" + param_escape(rows.toString());
1017 notify_progress("Loading, please wait...");
1019 new Ajax.Request("backend.php", {
1021 onComplete: function(transport) {
1022 catchup_callback2(transport, callback_func);
1028 exception_error("selectionToggleUnread", e);
1032 function selectionToggleMarked() {
1035 var rows = getSelectedArticleIds2();
1037 if (rows.length == 0) {
1038 alert(__("No articles are selected."));
1042 for (i = 0; i < rows.length; i++) {
1043 toggleMark(rows[i], true, true);
1046 update_local_feedlist_counters();
1048 if (rows.length > 0) {
1050 var query = "?op=rpc&subop=markSelected&ids=" +
1051 param_escape(rows.toString()) + "&cmode=2";
1053 query = query + "&afid=" + getActiveFeedId();
1055 query = query + "&omode=lc";
1057 new Ajax.Request("backend.php", {
1059 onComplete: function(transport) {
1060 handle_rpc_reply(transport);
1066 exception_error("selectionToggleMarked", e);
1070 function selectionTogglePublished() {
1073 var rows = getSelectedArticleIds2();
1075 if (rows.length == 0) {
1076 alert(__("No articles are selected."));
1080 for (i = 0; i < rows.length; i++) {
1081 togglePub(rows[i], true, true);
1084 if (rows.length > 0) {
1086 var query = "?op=rpc&subop=publishSelected&ids=" +
1087 param_escape(rows.toString()) + "&cmode=2";
1089 query = query + "&afid=" + getActiveFeedId();
1091 query = query + "&omode=lc";
1093 new Ajax.Request("backend.php", {
1095 onComplete: function(transport) {
1096 handle_rpc_reply(transport);
1102 exception_error("selectionToggleMarked", e);
1106 function getSelectedArticleIds2() {
1107 var sel_articles = new Array();
1112 var children = $("headlinesInnerContainer").childNodes;
1114 var children = $("headlinesList").rows;
1116 for (i = 0; i < children.length; i++) {
1117 var child = children[i];
1119 if (child.id && child.id.match("RROW-") && child.className.match("Selected")) {
1120 var c_id = child.id.replace("RROW-", "");
1121 sel_articles.push(c_id);
1125 return sel_articles;
1128 function getLoadedArticleIds() {
1129 var sel_articles = new Array();
1132 var children = $("headlinesInnerContainer").childNodes;
1134 var children = $("headlinesList").rows;
1136 if (!children) return sel_articles;
1138 for (i = 0; i < children.length; i++) {
1139 var child = children[i];
1141 if (child.id && child.id.match("RROW-")) {
1142 var c_id = child.id.replace("RROW-", "");
1143 sel_articles.push(c_id);
1147 return sel_articles;
1150 // mode = all,none,unread,invert
1151 function selectArticles(mode) {
1157 var children = $("headlinesInnerContainer").childNodes;
1159 var children = $("headlinesList").rows;
1161 for (i = 0; i < children.length; i++) {
1162 var child = children[i];
1164 if (child.id && child.id.match("RROW-")) {
1165 var aid = child.id.replace("RROW-", "");
1167 var cb = $("RCHK-" + aid);
1169 if (mode == "all") {
1170 if (!child.className.match("Selected")) {
1171 child.className = child.className + "Selected";
1174 } else if (mode == "unread") {
1175 if (child.className.match("Unread") && !child.className.match("Selected")) {
1176 child.className = child.className + "Selected";
1179 } else if (mode == "invert") {
1180 if (child.className.match("Selected")) {
1181 child.className = child.className.replace("Selected", "");
1184 child.className = child.className + "Selected";
1189 child.className = child.className.replace("Selected", "");
1196 exception_error("selectArticles", e);
1200 function catchupPage() {
1202 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1204 var str = __("Mark all visible articles in %s as read?");
1206 str = str.replace("%s", fn);
1208 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1212 selectArticles('all');
1213 selectionToggleUnread(false, 'viewCurrentFeed()', true)
1214 selectArticles('none');
1217 function deleteSelection() {
1221 var rows = getSelectedArticleIds2();
1223 if (rows.length == 0) {
1224 alert(__("No articles are selected."));
1228 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1232 if (getActiveFeedId() != 0) {
1233 str = __("Delete %d selected articles in %s?");
1235 str = __("Delete %d selected articles?");
1238 str = str.replace("%d", rows.length);
1239 str = str.replace("%s", fn);
1241 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1245 query = "?op=rpc&subop=delete&ids=" + param_escape(rows);
1249 new Ajax.Request("backend.php", {
1251 onComplete: function(transport) {
1256 exception_error("deleteSelection", e);
1260 function archiveSelection() {
1264 var rows = getSelectedArticleIds2();
1266 if (rows.length == 0) {
1267 alert(__("No articles are selected."));
1271 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1275 if (getActiveFeedId() != 0) {
1276 str = __("Archive %d selected articles in %s?");
1279 str = __("Move %d archived articles back?");
1283 str = str.replace("%d", rows.length);
1284 str = str.replace("%s", fn);
1286 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1290 query = "?op=rpc&subop="+op+"&ids=" + param_escape(rows);
1294 for (var i = 0; i < rows.length; i++) {
1295 cache_invalidate(rows[i]);
1298 new Ajax.Request("backend.php", {
1300 onComplete: function(transport) {
1305 exception_error("archiveSelection", e);
1309 function catchupSelection() {
1313 var rows = getSelectedArticleIds2();
1315 if (rows.length == 0) {
1316 alert(__("No articles are selected."));
1320 var fn = getFeedName(getActiveFeedId(), activeFeedIsCat());
1322 var str = __("Mark %d selected articles in %s as read?");
1324 str = str.replace("%d", rows.length);
1325 str = str.replace("%s", fn);
1327 if (getInitParam("confirm_feed_catchup") == 1 && !confirm(str)) {
1331 if ($("headlinesList")) {
1332 selectionToggleUnread(false, 'viewCurrentFeed()', true);
1334 selectionToggleUnread(false, 'viewCurrentFeed()', true)
1338 exception_error("catchupSelection", e);
1342 function editArticleTags(id, feed_id, cdm_enabled) {
1343 displayDlg('editArticleTags', id,
1345 $("tags_str").focus();
1347 new Ajax.Autocompleter('tags_str', 'tags_choices',
1348 "backend.php?op=rpc&subop=completeTags",
1349 { tokens: ',', paramName: "search" });
1353 function editTagsSave() {
1355 notify_progress("Saving article tags...");
1357 var form = document.forms["tag_edit_form"];
1359 var query = Form.serialize("tag_edit_form");
1361 query = "?op=rpc&subop=setArticleTags&" + query;
1363 //console.log(query);
1365 new Ajax.Request("backend.php", {
1367 onComplete: function(transport) {
1369 //console.log("tags saved...");
1374 if (tagsAreDisplayed()) {
1375 _reload_feedlist_after_view = true;
1378 if (transport.responseXML) {
1379 var tags_str = transport.responseXML.getElementsByTagName("tags-str")[0];
1382 var id = tags_str.getAttribute("id");
1385 var tags = $("ATSTR-" + id);
1387 tags.innerHTML = tags_str.firstChild.nodeValue;
1390 cache_invalidate(id);
1396 exception_error("editTagsSave", e);
1401 function editTagsInsert() {
1404 var form = document.forms["tag_edit_form"];
1406 var found_tags = form.found_tags;
1407 var tags_str = form.tags_str;
1409 var tag = found_tags[found_tags.selectedIndex].value;
1411 if (tags_str.value.length > 0 &&
1412 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
1414 tags_str.value = tags_str.value + ", ";
1417 tags_str.value = tags_str.value + tag + ", ";
1419 found_tags.selectedIndex = 0;
1422 exception_error("editTagsInsert", e);
1426 function cdmScrollToArticleId(id) {
1428 var ctr = $("headlinesInnerContainer");
1429 var e = $("RROW-" + id);
1431 if (!e || !ctr) return;
1433 ctr.scrollTop = e.offsetTop;
1436 exception_error("cdmScrollToArticleId", e);
1440 function cdmWatchdog() {
1444 var ctr = $("headlinesInnerContainer");
1448 var ids = new Array();
1450 var e = ctr.firstChild;
1453 if (e.className && e.className == "cdmArticleUnread" && e.id &&
1454 e.id.match("RROW-")) {
1456 // article fits in viewport OR article is longer than viewport and
1457 // its bottom is visible
1459 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
1460 ctr.scrollTop + ctr.offsetHeight) {
1462 // console.log(e.id + " is visible " + e.offsetTop + "." +
1463 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
1464 // (ctr.scrollTop + ctr.offsetHeight));
1466 ids.push(e.id.replace("RROW-", ""));
1468 } else if (e.offsetHeight > ctr.offsetHeight &&
1469 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1470 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
1472 ids.push(e.id.replace("RROW-", ""));
1476 // method 2: article bottom is visible and is in upper 1/2 of the viewport
1478 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
1479 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
1481 ids.push(e.id.replace("RROW-", ""));
1490 console.log("cdmWatchdog, ids= " + ids.toString());
1492 if (ids.length > 0) {
1494 for (var i = 0; i < ids.length; i++) {
1495 var e = $("RROW-" + ids[i]);
1497 e.className = e.className.replace("Unread", "");
1501 var query = "?op=rpc&subop=catchupSelected" +
1502 "&cmode=0" + "&ids=" + param_escape(ids.toString());
1504 new Ajax.Request("backend.php", {
1506 onComplete: function(transport) {
1507 handle_rpc_reply(transport);
1512 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
1515 exception_error("cdmWatchdog", e);
1521 function cache_inject(id, article, param) {
1524 if (!cache_check_param(id, param)) {
1525 //console.log("cache_article: miss: " + id + " [p=" + param + "]");
1527 var date = new Date();
1528 var ts = Math.round(date.getTime() / 1000);
1532 db.execute("INSERT INTO cache (id, article, param, added) VALUES (?, ?, ?, ?)",
1533 [id, article, param, ts]);
1538 cache_obj["id"] = id;
1539 cache_obj["data"] = article;
1540 cache_obj["param"] = param;
1542 if (param) id = id + ":" + param;
1544 cache_added["TS:" + id] = ts;
1546 if (has_local_storage())
1547 localStorage.setItem(id, JSON.stringify(cache_obj));
1549 article_cache.push(cache_obj);
1553 //console.log("cache_article: hit: " + id + " [p=" + param + "]");
1556 exception_error("cache_inject", e);
1560 function cache_find(id) {
1563 var rs = db.execute("SELECT article FROM cache WHERE id = ?", [id]);
1566 if (rs.isValidRow()) {
1567 var a = rs.field(0);
1576 if (has_local_storage()) {
1577 var cache_obj = localStorage.getItem(id);
1580 cache_obj = JSON.parse(cache_obj);
1583 return cache_obj['data'];
1587 for (var i = 0; i < article_cache.length; i++) {
1588 if (article_cache[i]["id"] == id) {
1589 return article_cache[i]["data"];
1597 function cache_find_param(id, param) {
1600 var rs = db.execute("SELECT article FROM cache WHERE id = ? AND param = ?",
1604 if (rs.isValidRow()) {
1614 if (has_local_storage()) {
1616 if (param) id = id + ":" + param;
1618 var cache_obj = localStorage.getItem(id);
1621 cache_obj = JSON.parse(cache_obj);
1624 return cache_obj['data'];
1628 for (var i = 0; i < article_cache.length; i++) {
1629 if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1630 return article_cache[i]["data"];
1638 function cache_check(id) {
1641 var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ?",
1645 if (rs.isValidRow()) {
1646 a = rs.field(0) != "0";
1654 if (has_local_storage()) {
1655 if (localStorage.getItem(id))
1658 for (var i = 0; i < article_cache.length; i++) {
1659 if (article_cache[i]["id"] == id) {
1668 function cache_check_param(id, param) {
1671 var rs = db.execute("SELECT COUNT(*) AS c FROM cache WHERE id = ? AND param = ?",
1675 if (rs.isValidRow()) {
1676 a = rs.field(0) != "0";
1685 if (has_local_storage()) {
1687 if (param) id = id + ":" + param;
1689 if (localStorage.getItem(id))
1693 for (var i = 0; i < article_cache.length; i++) {
1694 if (article_cache[i]["id"] == id && article_cache[i]["param"] == param) {
1703 function cache_expire() {
1705 var date = new Date();
1706 var ts = Math.round(date.getTime() / 1000);
1708 db.execute("DELETE FROM cache WHERE added < ? - 1800 AND id LIKE 'FEEDLIST'", [ts]);
1709 db.execute("DELETE FROM cache WHERE added < ? - 600 AND (id LIKE 'F:%' OR id LIKE 'C:%')", [ts]);
1710 db.execute("DELETE FROM cache WHERE added < ? - 86400", [ts]);
1714 if (has_local_storage()) {
1716 var date = new Date();
1717 var timestamp = Math.round(date.getTime() / 1000);
1719 for (var i = 0; i < localStorage.length; i++) {
1721 var id = localStorage.key(i);
1723 if (timestamp - cache_added["TS:" + id] > 180) {
1724 localStorage.removeItem(id);
1729 while (article_cache.length > 25) {
1730 article_cache.shift();
1736 function cache_flush() {
1738 db.execute("DELETE FROM cache");
1739 } else if (has_local_storage()) {
1740 localStorage.clear();
1742 article_cache = new Array();
1746 function cache_invalidate(id) {
1750 rs = db.execute("DELETE FROM cache WHERE id = ?", [id]);
1751 return rs.rowsAffected != 0;
1754 if (has_local_storage()) {
1758 for (var i = 0; i < localStorage.length; i++) {
1759 var key = localStorage.key(i);
1761 // console.warn("cache_invalidate: " + key_id + " cmp " + id);
1763 if (key == id || key.indexOf(id + ":") == 0) {
1764 localStorage.removeItem(key);
1775 while (i < article_cache.length) {
1776 if (article_cache[i]["id"] == id) {
1777 //console.log("cache_invalidate: removed id " + id);
1778 article_cache.splice(i, 1);
1786 //console.log("cache_invalidate: id not found: " + id);
1789 exception_error("cache_invalidate", e);
1793 function getActiveArticleId() {
1794 return active_post_id;
1797 function preloadBatchedArticles() {
1800 var query = "?op=rpc&subop=getArticles&ids=" +
1801 preload_id_batch.toString();
1803 new Ajax.Request("backend.php", {
1805 onComplete: function(transport) {
1807 preload_id_batch = [];
1809 var articles = transport.responseXML.getElementsByTagName("article");
1811 for (var i = 0; i < articles.length; i++) {
1812 var id = articles[i].getAttribute("id");
1813 if (!cache_check(id)) {
1814 cache_inject(id, articles[i].firstChild.nodeValue);
1815 console.log("preloaded article: " + id);
1821 exception_error("preloadBatchedArticles", e);
1825 function preloadArticleUnderPointer(id) {
1827 if (getInitParam("bw_limit") == "1") return;
1829 if (post_under_pointer == id && !cache_check(id)) {
1831 console.log("trying to preload article " + id);
1833 var neighbor_ids = getRelativePostIds(id, 1);
1835 /* only request uncached articles */
1837 if (preload_id_batch.indexOf(id) == -1) {
1838 for (var i = 0; i < neighbor_ids.length; i++) {
1839 if (!cache_check(neighbor_ids[i])) {
1840 preload_id_batch.push(neighbor_ids[i]);
1845 if (preload_id_batch.indexOf(id) == -1)
1846 preload_id_batch.push(id);
1848 //console.log("preload ids batch: " + preload_id_batch.toString());
1850 window.clearTimeout(preload_timeout_id);
1851 preload_batch_timeout_id = window.setTimeout('preloadBatchedArticles()', 1000);
1855 exception_error("preloadArticleUnderPointer", e);
1859 function postMouseIn(id) {
1861 if (post_under_pointer != id) {
1862 post_under_pointer = id;
1864 window.setTimeout("preloadArticleUnderPointer(" + id + ")", 250);
1869 exception_error("postMouseIn", e);
1873 function postMouseOut(id) {
1875 post_under_pointer = false;
1877 exception_error("postMouseOut", e);
1881 function headlines_scroll_handler() {
1884 var e = $("headlinesInnerContainer");
1886 var toolbar_form = document.forms["main_toolbar_form"];
1888 // console.log((e.scrollTop + e.offsetHeight) + " vs " + e.scrollHeight + " dis? " +
1889 // _infscroll_disable);
1891 if (e.scrollTop + e.offsetHeight > e.scrollHeight - 100) {
1892 if (!_infscroll_disable) {
1898 exception_error("headlines_scroll_handler", e);
1902 function catchupRelativeToArticle(below) {
1907 if (!getActiveArticleId()) {
1908 alert(__("No article is selected."));
1912 var visible_ids = getVisibleArticleIds();
1914 var ids_to_mark = new Array();
1917 for (var i = 0; i < visible_ids.length; i++) {
1918 if (visible_ids[i] != getActiveArticleId()) {
1919 var e = $("RROW-" + visible_ids[i]);
1921 if (e && e.className.match("Unread")) {
1922 ids_to_mark.push(visible_ids[i]);
1929 for (var i = visible_ids.length-1; i >= 0; i--) {
1930 if (visible_ids[i] != getActiveArticleId()) {
1931 var e = $("RROW-" + visible_ids[i]);
1933 if (e && e.className.match("Unread")) {
1934 ids_to_mark.push(visible_ids[i]);
1942 if (ids_to_mark.length == 0) {
1943 alert(__("No articles found to mark"));
1945 var msg = __("Mark %d article(s) as read?").replace("%d", ids_to_mark.length);
1947 if (getInitParam("confirm_feed_catchup") != 1 || confirm(msg)) {
1949 for (var i = 0; i < ids_to_mark.length; i++) {
1950 var e = $("RROW-" + ids_to_mark[i]);
1951 e.className = e.className.replace("Unread", "");
1954 var query = "?op=rpc&subop=catchupSelected" +
1955 "&cmode=0" + "&ids=" + param_escape(ids_to_mark.toString());
1957 new Ajax.Request("backend.php", {
1959 onComplete: function(transport) {
1960 catchup_callback2(transport);
1967 exception_error("catchupRelativeToArticle", e);
1971 function cdmExpandArticle(id) {
1976 var elem = $("CICD-" + active_post_id);
1978 if (id == active_post_id && Element.visible(elem))
1981 selectArticles("none");
1983 var old_offset = $("RROW-" + id).offsetTop;
1985 if (active_post_id && elem && !getInitParam("cdm_expanded")) {
1987 Element.show("CEXC-" + active_post_id);
1990 active_post_id = id;
1992 elem = $("CICD-" + id);
1994 if (!Element.visible(elem)) {
1996 Element.hide("CEXC-" + id);
1999 var new_offset = $("RROW-" + id).offsetTop;
2001 $("headlinesInnerContainer").scrollTop += (new_offset-old_offset);
2003 if ($("RROW-" + id).offsetTop != old_offset)
2004 $("headlinesInnerContainer").scrollTop = new_offset;
2006 toggleUnread(id, 0, true);
2010 exception_error("cdmExpandArticle", e);
2016 function fixHeadlinesOrder(ids) {
2018 for (var i = 0; i < ids.length; i++) {
2019 var e = $("RROW-" + ids[i]);
2023 e.className = e.className.replace("even", "odd");
2025 e.className = e.className.replace("odd", "even");
2030 exception_error("fixHeadlinesOrder", e);
2034 function invertHeadlineSelection() {
2036 var rows = new Array();
2040 r = document.getElementsByTagName("TR");
2042 r = document.getElementsByTagName("DIV");
2045 for (var i = 0; i < r.length; i++) {
2046 if (r[i].id && r[i].id.match("RROW-")) {
2051 for (var i = 0; i < rows.length; i++) {
2052 var nc = rows[i].className;
2053 var id = rows[i].id.replace("RROW-", "");
2054 var cb = $("RCHK-" + id);
2056 if (!rows[i].className.match("Selected")) {
2057 nc = nc + "Selected";
2060 nc = nc.replace("Selected", "");
2064 rows[i].className = nc;
2069 exception_error("invertHeadlineSelection", e);
2073 function getArticleUnderPointer() {
2074 return post_under_pointer;
2077 function zoomToArticle(id) {
2079 var w = window.open("backend.php?op=view&mode=zoom&id=" + param_escape(id),
2081 "status=0,toolbar=0,location=0,width=450,height=300,scrollbars=1,menubar=0");
2084 exception_error("zoomToArticle", e);
2088 function scrollArticle(offset) {
2091 var ci = $("content-insert");
2093 ci.scrollTop += offset;
2096 var hi = $("headlinesInnerContainer");
2098 hi.scrollTop += offset;
2103 exception_error("scrollArticle", e);
2107 function show_labels_in_headlines(transport) {
2109 if (transport.responseXML) {
2110 var info = transport.responseXML.getElementsByTagName("info-for-headlines")[0];
2112 var elems = info.getElementsByTagName("entry");
2114 for (var l = 0; l < elems.length; l++) {
2115 var e_id = elems[l].getAttribute("id");
2119 var ctr = $("HLLCTR-" + e_id);
2122 ctr.innerHTML = elems[l].firstChild.nodeValue;
2130 exception_error("show_labels_in_headlines", e);
2135 function toggleHeadlineActions() {
2137 var e = $("headlineActionsBody");
2138 var p = $("headlineActionsDrop");
2140 if (!Element.visible(e)) {
2147 e.style.left = (p.offsetLeft + 1) + "px";
2148 e.style.top = (p.offsetTop + p.offsetHeight + 2) + "px";
2151 exception_error("toggleHeadlineActions", e);
2155 function publishWithNote(id, def_note) {
2157 if (!def_note) def_note = '';
2159 var note = prompt(__("Please enter a note for this article:"), def_note);
2161 if (note != undefined) {
2162 togglePub(id, false, false, note);
2166 exception_error("publishWithNote", e);
2170 function emailArticle(id) {
2173 var ids = getSelectedArticleIds2();
2175 if (ids.length == 0) {
2176 alert(__("No articles are selected."));
2180 id = ids.toString();
2183 displayDlg('emailArticle', id,
2185 document.forms['article_email_form'].destination.focus();
2187 new Ajax.Autocompleter('destination', 'destination_choices',
2188 "backend.php?op=rpc&subop=completeEmails",
2189 { tokens: '', paramName: "search" });
2194 exception_error("emailArticle", e);
2198 function emailArticleDo() {
2200 var f = document.forms['article_email_form'];
2202 if (f.destination.value == "") {
2203 alert("Please fill in the destination email.");
2207 if (f.subject.value == "") {
2208 alert("Please fill in the subject.");
2212 var query = Form.serialize("article_email_form");
2214 // console.log(query);
2216 new Ajax.Request("backend.php", {
2218 onComplete: function(transport) {
2221 var error = transport.responseXML.getElementsByTagName('error')[0];
2224 alert(__('Error sending email:') + ' ' + error.firstChild.nodeValue);
2226 notify_info('Your message has been sent.');
2231 exception_error("sendEmailDo", e);
2237 exception_error("emailArticleDo", e);
2241 function dismissArticle(id) {
2243 var elem = $("RROW-" + id);
2245 toggleUnread(id, 0, true);
2247 new Effect.Fade(elem, {duration : 0.5});
2250 exception_error("dismissArticle", e);
2254 function dismissSelectedArticles() {
2257 var ids = getVisibleArticleIds();
2261 for (var i = 0; i < ids.length; i++) {
2262 var elem = $("RROW-" + ids[i]);
2264 if (elem.className && elem.className.match("Selected")) {
2265 new Effect.Fade(elem, {duration : 0.5});
2273 selectionToggleUnread(false);
2275 fixHeadlinesOrder(tmp);
2278 exception_error("dismissSelectedArticles", e);
2282 function dismissReadArticles() {
2285 var ids = getVisibleArticleIds();
2288 for (var i = 0; i < ids.length; i++) {
2289 var elem = $("RROW-" + ids[i]);
2291 if (elem.className && !elem.className.match("Unread") &&
2292 !elem.className.match("Selected")) {
2294 new Effect.Fade(elem, {duration : 0.5});
2300 fixHeadlinesOrder(tmp);
2303 exception_error("dismissSelectedArticles", e);
2307 function getVisibleArticleIds() {
2311 var tmp = getLoadedArticleIds();
2313 for (var i = 0; i < tmp.length; i++) {
2314 var elem = $("RROW-" + tmp[i]);
2315 if (elem && Element.visible(elem))
2320 exception_error("getVisibleArticleIds", e);
2326 function cdmClicked(event, id) {
2328 var shift_key = event.shiftKey;
2332 if (!event.ctrlKey) {
2333 selectArticles("none");
2336 var elem = $("RROW-" + id);
2339 elem.className = elem.className.replace("Unread", "");
2341 var query = "?op=rpc&subop=catchupSelected" +
2342 "&cmode=0&ids=" + param_escape(id);
2344 new Ajax.Request("backend.php", {
2346 onComplete: function(transport) {
2347 handle_rpc_reply(transport);
2356 exception_error("cdmClicked");
2362 function hlClicked(event, id) {
2364 var shift_key = event.shiftKey;
2366 if (!event.ctrlKey) {
2375 exception_error("hlClicked");
2381 function getFirstVisibleHeadlineId() {
2382 var rows = getVisibleArticleIds();
2387 function getLastVisibleHeadlineId() {
2388 var rows = getVisibleArticleIds();
2389 return rows[rows.length-1];
2392 // this only searches loaded headlines list, not in CDM
2393 function getRelativePostIds(id, limit) {
2395 if (!limit) limit = 3;
2397 //console.log("getRelativePostIds: " + id + " limit=" + limit);
2399 var ids = new Array();
2400 var container = $("headlinesList");
2403 var rows = container.rows;
2405 for (var i = 0; i < rows.length; i++) {
2406 var r_id = rows[i].id.replace("RROW-", "");
2409 for (var k = 1; k <= limit; k++) {
2412 if (i > k-1) var nid = rows[i-k].id.replace("RROW-", "");
2413 if (nid) ids.push(nid);
2415 if (i < rows.length-k) nid = rows[i+k].id.replace("RROW-", "");
2416 if (nid) ids.push(nid);
2427 function openArticleInNewWindow(id) {
2429 console.log("openArticleInNewWindow: " + id);
2431 var query = "?op=rpc&subop=getArticleLink&id=" + id;
2432 var wname = "ttrss_article_" + id;
2434 console.log(query + " " + wname);
2436 var w = window.open("", wname);
2438 if (!w) notify_error("Failed to open window for the article");
2440 new Ajax.Request("backend.php", {
2442 onComplete: function(transport) {
2444 var link = transport.responseXML.getElementsByTagName("link")[0];
2445 var id = transport.responseXML.getElementsByTagName("id")[0];
2447 console.log("open_article received link: " + link);
2451 var wname = "ttrss_article_" + id.firstChild.nodeValue;
2453 console.log("link url: " + link.firstChild.nodeValue + ", wname " + wname);
2455 var w = window.open(link.firstChild.nodeValue, wname);
2457 if (!w) { notify_error("Failed to load article in new window"); }
2460 id = id.firstChild.nodeValue;
2461 if (!$("headlinesList")) {
2462 window.setTimeout("toggleUnread(" + id + ", 0)", 100);
2466 notify_error("Can't open article: received invalid article link");
2471 exception_error("openArticleInNewWindow", e);
2475 function isCdmMode() {
2476 return !$("headlinesList");
2479 function markHeadline(id) {
2480 var row = $("RROW-" + id);
2482 var is_active = false;
2484 if (row.className.match("Active")) {
2487 row.className = row.className.replace("Selected", "");
2488 row.className = row.className.replace("Active", "");
2489 row.className = row.className.replace("Insensitive", "");
2492 row.className = row.className = "Active";
2495 var check = $("RCHK-" + id);
2498 check.checked = true;
2501 row.className = row.className + "Selected";