]> git.wh0rd.org - tt-rss.git/blob - viewfeed.js
invalidate article cache when editing tags
[tt-rss.git] / viewfeed.js
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;
5
6 var _tag_active_post_id = false;
7 var _tag_active_feed_id = false;
8 var _tag_active_cdm = false;
9
10 // FIXME: kludge, to restore scrollTop after tag editor terminates
11 var _tag_cdm_scroll = false;
12
13 // FIXME: kludges, needs proper implementation
14 var _reload_feedlist_after_view = false;
15
16 var _cdm_wd_timeout = false;
17 var _cdm_wd_vishist = new Array();
18
19 var article_cache = new Array();
20
21 function catchup_callback() {
22 if (xmlhttp_rpc.readyState == 4) {
23 try {
24 debug("catchup_callback");
25 if (_catchup_callback_func) {
26 setTimeout(_catchup_callback_func, 100);
27 }
28 notify("");
29 all_counters_callback();
30 } catch (e) {
31 exception_error("catchup_callback", e);
32 }
33 }
34 }
35
36 function headlines_callback() {
37 if (xmlhttp.readyState == 4) {
38 debug("headlines_callback");
39 var f = document.getElementById("headlines-frame");
40 try {
41 f.scrollTop = 0;
42 } catch (e) { };
43
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
49 f.innerHTML = headlines.firstChild.nodeValue;
50
51 if (articles) {
52 for (var i = 0; i < articles.length; i++) {
53 var a_id = articles[i].getAttribute("id");
54 debug("found id: " + a_id);
55 cache_inject(a_id, articles[i].firstChild.nodeValue);
56 }
57 }
58
59 if (counters) {
60 debug("parsing piggybacked counters: " + counters);
61 parse_counters(counters, false);
62 } else {
63 debug("counters container not found in reply");
64 }
65 } else {
66 debug("headlines_callback: returned no XML object");
67 f.innerHTML = "<div class='whiteBox'>" + __('Could not update headlines (missing XML object)') + "</div>";
68 }
69
70 if (typeof correctPNG != 'undefined') {
71 correctPNG();
72 }
73
74 if (_cdm_wd_timeout) window.clearTimeout(_cdm_wd_timeout);
75
76 if (!document.getElementById("headlinesList") &&
77 getInitParam("cdm_auto_catchup") == 1) {
78 debug("starting CDM watchdog");
79 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 5000);
80 _cdm_wd_vishist = new Array();
81 } else {
82 debug("not in CDM mode or watchdog disabled");
83 }
84
85 if (_tag_cdm_scroll) {
86 try {
87 document.getElementById("headlinesInnerContainer").scrollTop = _tag_cdm_scroll;
88 _tag_cdm_scroll = false;
89 } catch (e) { }
90 }
91
92 notify("");
93 }
94 }
95
96 function render_article(article) {
97 try {
98 var f = document.getElementById("content-frame");
99 try {
100 f.scrollTop = 0;
101 } catch (e) { };
102
103 f.innerHTML = article;
104
105 } catch (e) {
106 exception_error("render_article", e);
107 }
108 }
109
110 function article_callback() {
111 if (xmlhttp.readyState == 4) {
112 debug("article_callback");
113
114 try {
115 if (xmlhttp.responseXML) {
116 var reply = xmlhttp.responseXML.firstChild.firstChild;
117
118 var articles = xmlhttp.responseXML.getElementsByTagName("article");
119
120 for (var i = 0; i < articles.length; i++) {
121 var a_id = articles[i].getAttribute("id");
122
123 debug("found id: " + a_id);
124
125 if (a_id == active_post_id) {
126 debug("active article, rendering...");
127 render_article(articles[i].firstChild.nodeValue);
128 }
129
130 cache_inject(a_id, articles[i].firstChild.nodeValue);
131 }
132
133 } else {
134 debug("article_callback: returned no XML object");
135 f.innerHTML = "<div class='whiteBox'>" + __('Could not display article (missing XML object)') + "</div>";
136 }
137 } catch (e) {
138 exception_error("article_callback", e);
139 }
140
141 var date = new Date();
142 last_article_view = date.getTime() / 1000;
143
144 if (typeof correctPNG != 'undefined') {
145 correctPNG();
146 }
147
148 if (_reload_feedlist_after_view) {
149 setTimeout('updateFeedList(false, false)', 50);
150 _reload_feedlist_after_view = false;
151 } else {
152 var counters = xmlhttp.responseXML.getElementsByTagName("counters")[0];
153
154 if (counters) {
155 debug("parsing piggybacked counters: " + counters);
156 parse_counters(counters, false);
157 } else {
158 debug("counters container not found in reply");
159 }
160 }
161
162 notify("");
163 }
164 }
165
166 function view(id, feed_id, skip_history) {
167
168 try {
169 debug("loading article: " + id + "/" + feed_id);
170
171 active_real_feed_id = feed_id;
172
173 var cached_article = cache_find(id);
174
175 debug("cache check result: " + (cached_article != false));
176
177 /* if (!skip_history) {
178 history_push("ARTICLE:" + id + ":" + feed_id);
179 } */
180
181 enableHotkeys();
182
183 active_post_id = id;
184 //setActiveFeedId(feed_id);
185
186 var query = "backend.php?op=view&id=" + param_escape(id) +
187 "&feed=" + param_escape(feed_id);
188
189 var date = new Date();
190
191 if (!xmlhttp_ready(xmlhttp) && last_article_view < date.getTime() / 1000 - 15) {
192 debug("<b>xmlhttp seems to be stuck at view, aborting</b>");
193 xmlhttp.abort();
194 }
195
196 if (cached_article || xmlhttp_ready(xmlhttp)) {
197
198 cleanSelected("headlinesList");
199
200 var crow = document.getElementById("RROW-" + active_post_id);
201
202 var article_is_unread = crow.className.match("Unread");
203 debug("article is unread: " + article_is_unread);
204
205 crow.className = crow.className.replace("Unread", "");
206
207 var upd_img_pic = document.getElementById("FUPDPIC-" + active_post_id);
208
209 if (upd_img_pic) {
210 upd_img_pic.src = "images/blank_icon.gif";
211 }
212
213 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
214 markHeadline(active_post_id);
215
216 var neighbor_ids = getRelativePostIds(active_post_id);
217
218 /* only request uncached articles */
219
220 var cids_to_request = Array();
221
222 for (var i = 0; i < neighbor_ids.length; i++) {
223 if (!cache_check(neighbor_ids[i])) {
224 cids_to_request.push(neighbor_ids[i]);
225 }
226 }
227
228 debug("additional ids: " + cids_to_request.toString());
229
230 /* additional info for piggyback counters */
231
232 if (tagsAreDisplayed()) {
233 query = query + "&omode=lt";
234 } else {
235 query = query + "&omode=flc";
236 }
237
238 var date = new Date();
239 var timestamp = Math.round(date.getTime() / 1000);
240 query = query + "&ts=" + timestamp;
241
242 query = query + "&cids=" + cids_to_request.toString();
243
244 if (!cached_article) {
245
246 notify_progress("Loading, please wait...");
247
248 debug(query);
249
250 xmlhttp.open("GET", query, true);
251 xmlhttp.onreadystatechange=article_callback;
252 xmlhttp.send(null);
253 } else if (cached_article && article_is_unread) {
254
255 query = query + "&mode=prefetch";
256
257 debug(query);
258
259 xmlhttp.open("GET", query, true);
260 xmlhttp.onreadystatechange=article_callback;
261 xmlhttp.send(null);
262
263 render_article(cached_article);
264
265 } else if (cached_article) {
266
267 query = query + "&mode=prefetch_old";
268
269 debug(query);
270
271 xmlhttp.open("GET", query, true);
272 xmlhttp.onreadystatechange=article_callback;
273 xmlhttp.send(null);
274
275 render_article(cached_article);
276
277 }
278
279 cache_expire();
280
281 } else {
282 debug("xmlhttp busy (@view)");
283 printLockingError();
284 }
285
286 } catch (e) {
287 exception_error("view", e);
288 }
289 }
290
291 function tMark(id) {
292 return toggleMark(id);
293 }
294
295 function toggleMark(id) {
296
297 if (!xmlhttp_ready(xmlhttp_rpc)) {
298 printLockingError();
299 return;
300 }
301
302 var query = "backend.php?op=rpc&id=" + id + "&subop=mark";
303
304 var mark_img = document.getElementById("FMPIC-" + id);
305 var vfeedu = document.getElementById("FEEDU--1");
306 var crow = document.getElementById("RROW-" + id);
307
308 if (mark_img.alt != "Reset mark") {
309 mark_img.src = "images/mark_set.png";
310 mark_img.alt = "Reset mark";
311 query = query + "&mark=1";
312
313 if (vfeedu && crow.className.match("Unread")) {
314 vfeedu.innerHTML = (+vfeedu.innerHTML) + 1;
315 }
316
317 } else {
318 mark_img.src = "images/mark_unset.png";
319 mark_img.alt = "Set mark";
320 query = query + "&mark=0";
321
322 if (vfeedu && crow.className.match("Unread")) {
323 vfeedu.innerHTML = (+vfeedu.innerHTML) - 1;
324 }
325
326 }
327
328 var vfeedctr = document.getElementById("FEEDCTR--1");
329 var vfeedr = document.getElementById("FEEDR--1");
330
331 if (vfeedu && vfeedctr) {
332 if ((+vfeedu.innerHTML) > 0) {
333 if (crow.className.match("Unread") && !vfeedr.className.match("Unread")) {
334 vfeedr.className = vfeedr.className + "Unread";
335 vfeedctr.className = "odd";
336 }
337 } else {
338 vfeedctr.className = "invisible";
339 vfeedr.className = vfeedr.className.replace("Unread", "");
340 }
341 }
342
343 debug("toggle starred for aid " + id);
344
345 new Ajax.Request(query);
346
347 }
348
349 function moveToPost(mode) {
350
351 // check for combined mode
352 if (!document.getElementById("headlinesList"))
353 return;
354
355 var rows = getVisibleHeadlineIds();
356
357 var prev_id;
358 var next_id;
359
360 if (!document.getElementById('RROW-' + active_post_id)) {
361 active_post_id = false;
362 }
363
364 if (active_post_id == false) {
365 next_id = getFirstVisibleHeadlineId();
366 prev_id = getLastVisibleHeadlineId();
367 } else {
368 for (var i = 0; i < rows.length; i++) {
369 if (rows[i] == active_post_id) {
370 prev_id = rows[i-1];
371 next_id = rows[i+1];
372 }
373 }
374 }
375
376 if (mode == "next") {
377 if (next_id != undefined) {
378 view(next_id, getActiveFeedId());
379 }
380 }
381
382 if (mode == "prev") {
383 if ( prev_id != undefined) {
384 view(prev_id, getActiveFeedId());
385 }
386 }
387 }
388
389 function toggleUnread(id, cmode) {
390 try {
391 if (!xmlhttp_ready(xmlhttp_rpc)) {
392 printLockingError();
393 return;
394 }
395
396 var row = document.getElementById("RROW-" + id);
397 if (row) {
398 var nc = row.className;
399 nc = nc.replace("Unread", "");
400 nc = nc.replace("Selected", "");
401
402 if (row.className.match("Unread")) {
403 row.className = nc;
404 } else {
405 row.className = nc + "Unread";
406 }
407
408 if (!cmode) cmode = 2;
409
410 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
411 param_escape(id) + "&cmode=" + param_escape(cmode);
412
413 notify_progress("Loading, please wait...");
414
415 xmlhttp_rpc.open("GET", query, true);
416 xmlhttp_rpc.onreadystatechange=all_counters_callback;
417 xmlhttp_rpc.send(null);
418
419 }
420
421
422 } catch (e) {
423 exception_error("toggleUnread", e);
424 }
425 }
426
427 function selectionToggleUnread(cdm_mode, set_state, callback_func, no_error) {
428 try {
429 if (!xmlhttp_ready(xmlhttp_rpc)) {
430 printLockingError();
431 return;
432 }
433
434 var rows;
435
436 if (cdm_mode) {
437 rows = cdmGetSelectedArticles();
438 } else {
439 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
440 }
441
442 if (rows.length == 0 && !no_error) {
443 alert(__("No articles are selected."));
444 return;
445 }
446
447 for (i = 0; i < rows.length; i++) {
448 var row = document.getElementById("RROW-" + rows[i]);
449 if (row) {
450 var nc = row.className;
451 nc = nc.replace("Unread", "");
452 nc = nc.replace("Selected", "");
453
454 if (row.className.match("Unread")) {
455 row.className = nc + "Selected";
456 } else {
457 row.className = nc + "UnreadSelected";
458 }
459 }
460 }
461
462 if (rows.length > 0) {
463
464 var cmode = "";
465
466 if (set_state == undefined) {
467 cmode = "2";
468 } else if (set_state == true) {
469 cmode = "1";
470 } else if (set_state == false) {
471 cmode = "0";
472 }
473
474 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
475 param_escape(rows.toString()) + "&cmode=" + cmode;
476
477 _catchup_callback_func = callback_func;
478
479 notify_progress("Loading, please wait...");
480
481 xmlhttp_rpc.open("GET", query, true);
482 xmlhttp_rpc.onreadystatechange=catchup_callback;
483 xmlhttp_rpc.send(null);
484
485 }
486
487 } catch (e) {
488 exception_error("selectionToggleUnread", e);
489 }
490 }
491
492 function selectionToggleMarked(cdm_mode) {
493 try {
494 if (!xmlhttp_ready(xmlhttp_rpc)) {
495 printLockingError();
496 return;
497 }
498
499 var rows;
500
501 if (cdm_mode) {
502 rows = cdmGetSelectedArticles();
503 } else {
504 rows = getSelectedTableRowIds("headlinesList", "RROW", "RCHK");
505 }
506
507 if (rows.length == 0) {
508 alert(__("No articles are selected."));
509 return;
510 }
511
512 for (i = 0; i < rows.length; i++) {
513 var row = document.getElementById("RROW-" + rows[i]);
514 var mark_img = document.getElementById("FMARKPIC-" + rows[i]);
515
516 if (row && mark_img) {
517
518 if (mark_img.alt == "Set mark") {
519 mark_img.src = "images/mark_set.png";
520 mark_img.alt = "Reset mark";
521 mark_img.setAttribute('onclick',
522 'javascript:toggleMark('+rows[i]+', false)');
523
524 } else {
525 mark_img.src = "images/mark_unset.png";
526 mark_img.alt = "Set mark";
527 mark_img.setAttribute('onclick',
528 'javascript:toggleMark('+rows[i]+', true)');
529 }
530 }
531 }
532
533 if (rows.length > 0) {
534
535 var query = "backend.php?op=rpc&subop=markSelected&ids=" +
536 param_escape(rows.toString()) + "&cmode=2";
537
538 xmlhttp_rpc.open("GET", query, true);
539 xmlhttp_rpc.onreadystatechange=all_counters_callback;
540 xmlhttp_rpc.send(null);
541
542 }
543
544 } catch (e) {
545 exception_error("selectionToggleMarked", e);
546 }
547 }
548
549 function cdmGetSelectedArticles() {
550 var sel_articles = new Array();
551 var container = document.getElementById("headlinesInnerContainer");
552
553 for (i = 0; i < container.childNodes.length; i++) {
554 var child = container.childNodes[i];
555
556 if (child.id.match("RROW-") && child.className.match("Selected")) {
557 var c_id = child.id.replace("RROW-", "");
558 sel_articles.push(c_id);
559 }
560 }
561
562 return sel_articles;
563 }
564
565 // mode = all,none,unread
566 function cdmSelectArticles(mode) {
567 var container = document.getElementById("headlinesInnerContainer");
568
569 for (i = 0; i < container.childNodes.length; i++) {
570 var child = container.childNodes[i];
571
572 if (child.id.match("RROW-")) {
573 var aid = child.id.replace("RROW-", "");
574
575 var cb = document.getElementById("RCHK-" + aid);
576
577 if (mode == "all") {
578 if (!child.className.match("Selected")) {
579 child.className = child.className + "Selected";
580 cb.checked = true;
581 }
582 } else if (mode == "unread") {
583 if (child.className.match("Unread") && !child.className.match("Selected")) {
584 child.className = child.className + "Selected";
585 cb.checked = true;
586 }
587 } else {
588 child.className = child.className.replace("Selected", "");
589 cb.checked = false;
590 }
591 }
592 }
593 }
594
595 function catchupPage() {
596
597 if (document.getElementById("headlinesList")) {
598 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', true, 'Unread', true);
599 selectionToggleUnread(false, false, 'viewCurrentFeed()', true);
600 selectTableRowsByIdPrefix('headlinesList', 'RROW-', 'RCHK-', false);
601 } else {
602 cdmSelectArticles('all');
603 selectionToggleUnread(true, false, 'viewCurrentFeed()', true)
604 cdmSelectArticles('none');
605 }
606 }
607
608 function labelFromSearch(search, search_mode, match_on, feed_id, is_cat) {
609
610 if (!xmlhttp_ready(xmlhttp_rpc)) {
611 printLockingError();
612 }
613
614 var title = prompt("Please enter label title:", "");
615
616 if (title) {
617
618 var query = "backend.php?op=labelFromSearch&search=" + param_escape(search) +
619 "&smode=" + param_escape(search_mode) + "&match=" + param_escape(match_on) +
620 "&feed=" + param_escape(feed_id) + "&is_cat=" + param_escape(is_cat) +
621 "&title=" + param_escape(title);
622
623 debug("LFS: " + query);
624
625 xmlhttp_rpc.open("GET", query, true);
626 xmlhttp_rpc.onreadystatechange=dlg_frefresh_callback;
627 xmlhttp_rpc.send(null);
628 }
629
630 }
631
632 function editArticleTags(id, feed_id, cdm_enabled) {
633 _tag_active_post_id = id;
634 _tag_active_feed_id = feed_id;
635 _tag_active_cdm = cdm_enabled;
636
637 cache_invalidate(id);
638
639 try {
640 _tag_cdm_scroll = document.getElementById("headlinesInnerContainer").scrollTop;
641 } catch (e) { }
642 displayDlg('editArticleTags', id);
643 }
644
645
646 function tag_saved_callback() {
647 if (xmlhttp_rpc.readyState == 4) {
648 try {
649 debug("in tag_saved_callback");
650
651 closeInfoBox();
652 notify("");
653
654 if (tagsAreDisplayed()) {
655 _reload_feedlist_after_view = true;
656 }
657
658 if (!_tag_active_cdm) {
659 if (active_post_id == _tag_active_post_id) {
660 debug("reloading current article");
661 view(_tag_active_post_id, _tag_active_feed_id);
662 }
663 } else {
664 debug("reloading current feed");
665 viewCurrentFeed();
666 }
667
668 } catch (e) {
669 exception_error("catchup_callback", e);
670 }
671 }
672 }
673
674 function editTagsSave() {
675
676 if (!xmlhttp_ready(xmlhttp_rpc)) {
677 printLockingError();
678 }
679
680 notify_progress("Saving article tags...");
681
682 var form = document.forms["tag_edit_form"];
683
684 var query = Form.serialize("tag_edit_form");
685
686 xmlhttp_rpc.open("GET", "backend.php?op=rpc&subop=setArticleTags&" + query, true);
687 xmlhttp_rpc.onreadystatechange=tag_saved_callback;
688 xmlhttp_rpc.send(null);
689
690 }
691
692 function editTagsInsert() {
693 try {
694
695 var form = document.forms["tag_edit_form"];
696
697 var found_tags = form.found_tags;
698 var tags_str = form.tags_str;
699
700 var tag = found_tags[found_tags.selectedIndex].value;
701
702 if (tags_str.value.length > 0 &&
703 tags_str.value.lastIndexOf(", ") != tags_str.value.length - 2) {
704
705 tags_str.value = tags_str.value + ", ";
706 }
707
708 tags_str.value = tags_str.value + tag + ", ";
709
710 found_tags.selectedIndex = 0;
711
712 } catch (e) {
713 exception_error(e, "editTagsInsert");
714 }
715 }
716
717 function cdmWatchdog() {
718
719 try {
720
721 var ctr = document.getElementById("headlinesInnerContainer");
722
723 if (!ctr) return;
724
725 var ids = new Array();
726
727 var e = ctr.firstChild;
728
729 while (e) {
730 if (e.className && e.className == "cdmArticleUnread" && e.id &&
731 e.id.match("RROW-")) {
732
733 // article fits in viewport OR article is longer than viewport and
734 // its bottom is visible
735
736 if (ctr.scrollTop <= e.offsetTop && e.offsetTop + e.offsetHeight <=
737 ctr.scrollTop + ctr.offsetHeight) {
738
739 // debug(e.id + " is visible " + e.offsetTop + "." +
740 // (e.offsetTop + e.offsetHeight) + " vs " + ctr.scrollTop + "." +
741 // (ctr.scrollTop + ctr.offsetHeight));
742
743 ids.push(e.id.replace("RROW-", ""));
744
745 } else if (e.offsetHeight > ctr.offsetHeight &&
746 e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
747 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight) {
748
749 ids.push(e.id.replace("RROW-", ""));
750
751 }
752
753 // method 2: article bottom is visible and is in upper 1/2 of the viewport
754
755 /* if (e.offsetTop + e.offsetHeight >= ctr.scrollTop &&
756 e.offsetTop + e.offsetHeight <= ctr.scrollTop + ctr.offsetHeight/2) {
757
758 ids.push(e.id.replace("RROW-", ""));
759
760 } */
761
762 }
763
764 e = e.nextSibling;
765 }
766
767 debug("cdmWatchdog, ids= " + ids.toString());
768
769 if (ids.length > 0 && xmlhttp_ready(xmlhttp_rpc)) {
770
771 for (var i = 0; i < ids.length; i++) {
772 var e = document.getElementById("RROW-" + ids[i]);
773 if (e) {
774 e.className = e.className.replace("Unread", "");
775 }
776 }
777
778 var query = "backend.php?op=rpc&subop=catchupSelected&ids=" +
779 param_escape(ids.toString()) + "&cmode=0";
780
781 xmlhttp_rpc.open("GET", query, true);
782 xmlhttp_rpc.onreadystatechange=all_counters_callback;
783 xmlhttp_rpc.send(null);
784
785 }
786
787 _cdm_wd_timeout = window.setTimeout("cdmWatchdog()", 4000);
788
789 } catch (e) {
790 exception_error(e, "cdmWatchdog");
791 }
792
793 }
794
795
796 function cache_inject(id, article) {
797 if (!cache_check(id)) {
798 debug("cache_article: miss: " + id);
799
800 var cache_obj = new Array();
801
802 cache_obj["id"] = id;
803 cache_obj["data"] = article;
804
805 article_cache.push(cache_obj);
806
807 } else {
808 debug("cache_article: hit: " + id);
809 }
810 }
811
812 function cache_find(id) {
813 for (var i = 0; i < article_cache.length; i++) {
814 if (article_cache[i]["id"] == id) {
815 return article_cache[i]["data"];
816 }
817 }
818 return false;
819 }
820
821 function cache_check(id) {
822 for (var i = 0; i < article_cache.length; i++) {
823 if (article_cache[i]["id"] == id) {
824 return true;
825 }
826 }
827 return false;
828 }
829
830 function cache_expire() {
831 while (article_cache.length > 20) {
832 article_cache.shift();
833 }
834 }
835
836 function cache_invalidate(id) {
837 var i = 0
838
839 while (i < article_cache.length) {
840 if (article_cache[i]["id"] == id) {
841 article_cache.remove(i);
842 return true;
843 }
844 i++;
845 }
846 return false;
847 }