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