]> git.wh0rd.org Git - tt-rss.git/blob - feedlist.js
Merge branch 'master' of /home/fox/public_html/testbox/tt-rss
[tt-rss.git] / feedlist.js
1 var _feed_cur_page = 0;
2 var _infscroll_disable = 0;
3 var _infscroll_request_sent = 0;
4 var feed_under_pointer = undefined;
5
6 var mouse_is_down = false;
7 var mouse_y = 0;
8 var mouse_x = 0;
9
10 var resize_enabled = false;
11 var selection_disabled = false;
12 var counters_last_request = 0;
13
14 function toggle_sortable_feedlist(enabled) {
15         try {
16
17                 if (enabled) {
18                         Sortable.create('feedList', {onChange: feedlist_dragsorted, only: "feedCat"});
19                 } else {
20                         Sortable.destroy('feedList');
21                 }
22
23         } catch (e) {
24                 exception_error("toggle_sortable_feedlist", e);
25         }
26 }
27
28 function viewCategory(cat) {
29         viewfeed(cat, '', true);
30         return false;
31 }
32
33 function printFeedEntry(id, title, row_class, unread, icon) {
34
35         var tmp = "";
36         var fctr_class = "";
37         var feed_icon = "";
38
39         if (unread > 0) {
40                 row_class += "Unread";
41                 fctr_class = "feedCtrHasUnread";
42         } else {
43                 fctr_class = "feedCtrNoUnread";
44         }
45
46         if (icon) {
47                 feed_icon = "<img id='FIMG-"+id+"' src='" + icon + "'>";
48         } else {
49                 feed_icon = "<img id='FIMG-"+id+"' src='images/blank_icon.gif'>";
50         }
51
52         var link = "<a title=\"FIXME\" id=\"FEEDL-"+id+"\""+
53                 "href=\"javascript:viewfeed('"+id+"', '', false, '', false, 0);\">"+
54                 title + "</a>";
55
56         tmp += "<li id='FEEDR-"+id+"' class="+row_class+">" + feed_icon + 
57                 "<span id=\"FEEDN-"+id+"\">" + link + "</span>";
58
59         tmp += " <span class='"+fctr_class+"' id=\"FEEDCTR-"+id+"\">" +
60            "(<span id=\"FEEDU-"+id+"\">"+unread+"</span>)</span>";
61                         
62         tmp += "</li>";
63
64         return tmp;
65 }
66
67 function render_feedlist(data) {
68         try {
69
70                 var f = $("feeds-frame");
71                 f.innerHTML = data;
72 //              cache_invalidate("FEEDLIST");
73 //              cache_inject("FEEDLIST", data, getInitParam("num_feeds"));
74                 feedlist_init();
75
76         } catch (e) {
77                 exception_error("render_feedlist", e);
78         }
79 }
80
81 function feedlist_callback2(transport) {
82         try {
83                 debug("feedlist_callback2");
84                 if (!transport_error_check(transport)) return;
85                 render_feedlist(transport.responseText);
86         } catch (e) {
87                 exception_error("feedlist_callback2", e);
88         }
89 }
90
91 function viewNextFeedPage() {
92         try {
93                 //if (!getActiveFeedId()) return;
94
95                 debug("viewNextFeedPage: calling viewfeed(), p: " + parseInt(_feed_cur_page+1));
96
97                 viewfeed(getActiveFeedId(), undefined, activeFeedIsCat(), undefined,
98                         undefined, parseInt(_feed_cur_page+1));
99
100         } catch (e) {
101                 exception_error("viewNextFeedPage", e);
102         }
103 }
104
105
106 function viewfeed(feed, subop, is_cat, subop_param, skip_history, offset) {
107         try {
108
109                 if (offline_mode) return viewfeed_offline(feed, subop, is_cat, subop_param,
110                         skip_history, offset);
111
112 //              if (!offset) page_offset = 0;
113
114                 last_requested_article = 0;
115                 //counters_last_request = 0;
116
117                 if (feed == getActiveFeedId()) {
118                         cache_invalidate("F:" + feed);
119                 }
120
121 /*              if (getInitParam("theme") == "" || getInitParam("theme") == "compact") {
122                         if (getInitParam("hide_feedlist") == 1) {
123                                 Element.hide("feeds-holder");
124                         }               
125                 } */
126
127                 var force_nocache = false;
128
129                 var page_offset = 0;
130
131                 if (offset > 0) {
132                         page_offset = offset;
133                 } else {
134                         page_offset = 0;
135                         _feed_cur_page = 0;
136                         _infscroll_disable = 0;
137                 }
138
139                 if (getActiveFeedId() != feed) {
140                         _feed_cur_page = 0;
141                         active_post_id = 0;
142                         _infscroll_disable = 0;
143                 }
144
145                 if (page_offset != 0 && !subop) {
146                         var date = new Date();
147                         var timestamp = Math.round(date.getTime() / 1000);
148
149                         debug("<b>" + _infscroll_request_sent + " : " + timestamp + "</b>");
150
151                         if (_infscroll_request_sent && _infscroll_request_sent + 30 > timestamp) {
152                                 debug("infscroll request in progress, aborting");
153                                 return;
154                         }
155
156                         _infscroll_request_sent = timestamp;                    
157                 }
158
159                 enableHotkeys();
160
161                 closeInfoBox();
162
163                 Form.enable("main_toolbar_form");
164
165                 var toolbar_form = document.forms["main_toolbar_form"];
166                 var toolbar_query = Form.serialize("main_toolbar_form");
167
168                 if (toolbar_form.query) {
169                         if (toolbar_form.query.value != "") {
170                                 force_nocache = true;
171                         }
172                         toolbar_form.query.value = "";
173                 }
174
175                 var query = "backend.php?op=viewfeed&feed=" + feed + "&" +
176                         toolbar_query + "&subop=" + param_escape(subop);
177
178                 if ($("search_form")) {
179                         var search_query = Form.serialize("search_form");
180                         query = query + "&" + search_query;
181                         $("search_form").query.value = "";
182                         closeInfoBox(true);
183                         force_nocache = true;
184                 }
185
186 //              debug("IS_CAT_STORED: " + activeFeedIsCat() + ", IS_CAT: " + is_cat);
187
188                 if (subop == "MarkAllRead") {
189
190                         catchup_local_feed(feed, is_cat);
191
192                         var show_next_feed = getInitParam("on_catchup_show_next_feed") == "1";
193
194                         if (show_next_feed) {
195
196                                 if (!activeFeedIsCat()) {
197         
198                                         var feedlist = $('feedList');
199                                 
200                                         var next_unread_feed = getRelativeFeedId(feedlist,
201                                                         feed, "next", true);
202         
203                                         if (!next_unread_feed) {
204                                                 next_unread_feed = getRelativeFeedId(feedlist,
205                                                         -3, "next", true);
206                                         }
207                 
208                                         if (next_unread_feed) {
209                                                 query = query + "&nuf=" + param_escape(next_unread_feed);
210                                                 //setActiveFeedId(next_unread_feed);
211                                                 feed = next_unread_feed;
212                                         }
213                                 } else {
214         
215                                         var next_unread_feed = getNextUnreadCat(feed);
216
217                                         /* we don't need to specify that our next feed is actually
218                                         a category, because we're in the is_cat mode by definition
219                                         already */
220
221                                         if (next_unread_feed && show_next_feed) {
222                                                 query = query + "&nuf=" + param_escape(next_unread_feed);
223                                                 feed = next_unread_feed;
224                                         }
225
226                                 }
227                         }
228                 }
229
230                 if (is_cat) {
231                         query = query + "&cat=1";
232                 }
233
234                 if (page_offset != 0) {
235                         query = query + "&skip=" + page_offset;
236
237                         // to prevent duplicate feed titles when showing grouped vfeeds
238                         if (vgroup_last_feed) {
239                                 query = query + "&vgrlf=" + param_escape(vgroup_last_feed);
240                         }
241                 }
242
243                 var date = new Date();
244                 var timestamp = Math.round(date.getTime() / 1000);
245                 query = query + "&ts=" + timestamp
246                 
247                 disableContainerChildren("headlinesToolbar", false);
248                 Form.enable("main_toolbar_form");
249
250                 // for piggybacked counters
251
252                 if (tagsAreDisplayed()) {
253                         query = query + "&omode=lt";
254                 } else {
255                         query = query + "&omode=flc";
256                 }
257
258                 if (!async_counters_work) {
259                         query = query + "&csync=true";
260                 }
261
262                 debug(query);
263
264                 var container = $("headlinesInnerContainer");
265
266 /*              if (container && page_offset == 0 && !isCdmMode()) {
267                         new Effect.Fade(container, {duration: 1, to: 0.01,
268                                 queue: { position:'end', scope: 'FEEDL-' + feed, limit: 1 } } );
269                 } */
270
271                 var unread_ctr = -1;
272                 
273                 if (!is_cat) unread_ctr = get_feed_unread(feed);
274
275                 var cache_check = false;
276
277                 if (unread_ctr != -1 && !page_offset && !force_nocache && !subop) {
278
279                         var cache_prefix = "";
280                                 
281                         if (is_cat) {
282                                 cache_prefix = "C:";
283                         } else {
284                                 cache_prefix = "F:";
285                         }
286
287                         cache_check = cache_check_param(cache_prefix + feed, unread_ctr);
288                         debug("headline cache check: " + cache_check);
289                 }
290
291                 if (cache_check) {
292                         var f = $("headlines-frame");
293
294                         clean_feed_selections();
295
296                         setActiveFeedId(feed, is_cat);
297                 
298                         if (!is_cat) {
299                                 var feedr = $("FEEDR-" + feed);
300                                 if (feedr && !feedr.className.match("Selected")) {      
301                                         feedr.className = feedr.className + "Selected";
302                                 } 
303                         } else {
304                                 var feedr = $("FCAT-" + feed_id);
305                                 if (feedr && !feedr.className.match("Selected")) {      
306                                         feedr.className = feedr.className + "Selected";
307                                 } 
308                         }
309
310                         f.innerHTML = cache_find_param(cache_prefix + feed, unread_ctr);
311
312                         request_counters();
313                         remove_splash();
314
315                 } else {
316
317                         if (!page_offset) {
318                                 var feedr;
319
320                                 if (is_cat) {
321                                         feedr = $('FCAP-' + feed);
322                                 } else {
323                                         feedr = $('FEEDR-' + feed);
324                                 }
325
326                                 if (feedr && !$('FLL-' + feed)) {
327
328                                         var img = $('FIMG-' + feed);
329
330                                         if (!is_cat && img) {
331
332                                                 img.alt = img.src;
333                                                 img.src = 'images/indicator_white.gif';
334
335                                         } else {
336
337                                                 var ll = document.createElement('img');
338
339                                                 ll.src = 'images/indicator_tiny.gif';
340                                                 ll.className = 'hlLoading';
341                                                 ll.id = 'FLL-' + feed;
342         
343                                                 feedr.appendChild(ll);
344
345
346                                         }
347                                 }
348                         }
349
350                         new Ajax.Request(query, {
351                                 onComplete: function(transport) { 
352                                         headlines_callback2(transport, page_offset); 
353                                 } });
354                 }
355
356         } catch (e) {
357                 exception_error("viewfeed", e);
358         }               
359 }
360
361 function toggleCollapseCat_af(effect) {
362         //var caption = elem.id.replace("FCATLIST-", "");
363
364         try {
365
366                 var elem = effect.element;
367                 var cat = elem.id.replace("FCATLIST-", "");
368                 var cap = $("FCAP-" + cat);
369
370                 if (Element.visible(elem)) {
371                         cap.innerHTML = cap.innerHTML.replace("…", "");
372                 } else {
373                         if (cap.innerHTML.lastIndexOf("…") != cap.innerHTML.length-3) {
374                                 cap.innerHTML = cap.innerHTML + "…";
375                         }
376                 }
377
378         } catch (e) {
379                 exception_error("toggleCollapseCat_af", e);
380         }
381 }
382
383 function toggleCollapseCat(cat) {
384         try {
385         
386                 var cat_elem = $("FCAT-" + cat);
387                 var cat_list = $("FCATLIST-" + cat).parentNode;
388                 var caption = $("FCAP-" + cat);
389                 
390 /*              if (cat_list.className.match("invisible")) {
391                         cat_list.className = "";
392                         caption.innerHTML = caption.innerHTML.replace("...", "");
393                         if (cat == 0) {
394                                 setCookie("ttrss_vf_uclps", "0");
395                         }
396                 } else {
397                         cat_list.className = "invisible";
398                         caption.innerHTML = caption.innerHTML + "...";
399                         if (cat == 0) {
400                                 setCookie("ttrss_vf_uclps", "1");
401                         } 
402
403                 } */
404
405                 if (cat == 0) {
406                         if (Element.visible("FCATLIST-" + cat)) {
407                                 setCookie("ttrss_vf_uclps", "1");
408                         } else {
409                                 setCookie("ttrss_vf_uclps", "0");
410                         }
411                 } 
412
413                 if (cat == -2) {
414                         if (Element.visible("FCATLIST-" + cat)) {
415                                 setCookie("ttrss_vf_lclps", "1");
416                         } else {
417                                 setCookie("ttrss_vf_lclps", "0");
418                         }
419                 } 
420
421                 if (cat == -1) {
422                         if (Element.visible("FCATLIST-" + cat)) {
423                                 setCookie("ttrss_vf_vclps", "1");
424                         } else {
425                                 setCookie("ttrss_vf_vclps", "0");
426                         }
427                 } 
428
429                 Effect.toggle('FCATLIST-' + cat, 'blind', { duration: 0.5,
430                         afterFinish: toggleCollapseCat_af });
431
432                 new Ajax.Request("backend.php?op=feeds&subop=collapse&cid=" + 
433                         param_escape(cat));
434
435                 local_collapse_cat(cat);
436
437         } catch (e) {
438                 exception_error("toggleCollapseCat", e);
439         }
440 }
441
442 function feedlist_dragsorted(ctr) {
443         try {
444                 var elem = $("feedList");
445
446                 var cats = elem.getElementsByTagName("LI");
447                 var ordered_cats = new Array();
448
449                 for (var i = 0; i < cats.length; i++) {
450                         if (cats[i].id && cats[i].id.match("FCAT-")) {
451                                 ordered_cats.push(cats[i].id.replace("FCAT-", ""));
452                         }
453                 }
454
455                 if (ordered_cats.length > 0) {
456
457                         var query = "backend.php?op=feeds&subop=catsort&corder=" + 
458                                 param_escape(ordered_cats.toString());
459
460                         debug(query);
461
462                         new Ajax.Request(query);
463                 }
464
465         } catch (e) {
466                 exception_error("feedlist_dragsorted", e);
467         }
468 }
469
470 function feedlist_init() {
471         try {
472 //              if (arguments.callee.done) return;
473 //              arguments.callee.done = true;           
474                 
475                 loading_set_progress(90);
476
477                 debug("in feedlist init");
478                 
479                 hideOrShowFeeds(getInitParam("hide_read_feeds") == 1);
480                 document.onkeydown = hotkey_handler;
481                 document.onmousemove = mouse_move_handler;
482                 document.onmousedown = mouse_down_handler;
483                 document.onmouseup = mouse_up_handler;
484
485                 if (!offline_mode) setTimeout("timeout()", 1);
486
487                 setTimeout("hotkey_prefix_timeout()", 5*1000);
488
489                 if (typeof correctPNG != 'undefined') {
490                         correctPNG();
491                 }
492
493                 if (getActiveFeedId()) {
494                         //debug("some feed is open on feedlist refresh, reloading");
495                         //setTimeout("viewCurrentFeed()", 100);
496                 } else {
497                         if (getInitParam("cdm_auto_catchup") != 1 && get_feed_unread(-3) > 0) {
498                                 notify_silent_next();
499                                 setTimeout("viewfeed(-3)", 100);
500                         } else {
501                                 remove_splash();
502                         }
503                 }
504
505                 if (getInitParam("theme") == "") {
506                         setTimeout("hide_footer()", 5000);
507                 }
508
509                 init_collapsable_feedlist(getInitParam("theme"));
510
511                 toggle_sortable_feedlist(isFeedlistSortable());
512
513         } catch (e) {
514                 exception_error("feedlist/init", e);
515         }
516 }
517
518 function hide_footer_af(effect) {
519         try {
520                 var c = $("content-frame");
521
522                 if (c) {
523                         c.style.bottom = "0px";
524
525                         var ioa = $("inline_orig_article");
526
527                         if (ioa) {
528                                 ioa.height = c.offsetHeight;
529                         }
530
531                 } else {
532                         var h = $("headlines-frame");
533
534                         if (h) {
535                                 h.style.bottom = "0px";
536                         }
537                 }
538
539         } catch (e) {
540                 exception_error("hide_footer_af", e);
541         }
542 }
543
544 function hide_footer() {
545         try {
546                 if (Element.visible("footer")) {
547                         new Effect.Fade("footer", { afterFinish: hide_footer_af });
548                 }
549         } catch (e) {
550                 exception_error("hide_footer", e);
551         }
552 }
553
554 /*
555 function init_hidden_feedlist(theme) {
556         try {
557                 debug("init_hidden_feedlist");
558
559                 if (theme != "" && theme != "compact") return;
560
561                 var fl = $("feeds-holder");
562                 var fh = $("headlines-frame");
563                 var fc = $("content-frame");
564                 var ft = $("toolbar");
565                 var ff = $("footer");
566                 var fhdr = $("header");
567
568                 var fbtn = $("toggle_feeds_btn");
569
570                 if (fbtn) Element.show(fbtn);
571
572                 fl.style.top = fh.offsetTop + "px";
573                 fl.style.backgroundColor = "white"; //FIXME
574
575                 Element.hide(fl);
576                 
577                 fh.style.left = "0px";
578                 ft.style.left = "0px";
579                 if (fc) fc.style.left = "0px";
580                 if (ff) ff.style.left = "0px";
581
582                 if (theme == "compact") {
583                         fhdr.style.left = "10px";
584                         fl.style.top = (fh.offsetTop + 1) + "px";
585                 }
586
587         } catch (e) {
588                 exception_error("init_hidden_feedlist", e);
589         }
590 } */
591
592 function init_collapsable_feedlist(theme) {
593         try {
594                 debug("init_collapsable_feedlist");
595
596                 if (theme != "" && theme != "compact" && theme != "graycube" &&
597                                 theme != "compat") return;
598
599                 var fbtn = $("collapse_feeds_btn");
600
601                 if (fbtn) Element.show(fbtn);
602
603                 if (getCookie("ttrss_vf_fclps") == 1) {
604                         collapse_feedlist();
605                 }
606
607         } catch (e) {
608                 exception_error("init_hidden_feedlist", e);
609         }
610
611 }
612
613 function mouse_move_handler(e) {
614         try {
615                 var client_y;
616                 var client_x;
617
618                 if (window.event) {
619                         client_y = window.event.clientY;
620                         client_x = window.event.clientX;
621                 } else if (e) {
622                         client_x = e.screenX;
623                         client_y = e.screenY;
624                 }
625
626                 if (mouse_is_down) {
627
628                         if (mouse_y == 0) mouse_y = client_y;
629                         if (mouse_x == 0) mouse_x = client_x;
630
631                         resize_headlines(mouse_x - client_x, mouse_y - client_y);
632
633                         mouse_y = client_y;
634                         mouse_x = client_x;
635
636                         return false;
637                 }
638
639         } catch (e) {
640                 exception_error("mouse_move_handler", e);
641         }
642 }
643
644 function enable_selection(b) {
645         selection_disabled = !b;
646 }
647
648 function enable_resize(b) {
649         resize_enabled = b;
650 }
651
652 function mouse_down_handler(e) {
653         try {
654
655                 /* do not prevent right click */
656                 if (e && e.button && e.button == 2) return;
657
658                 if (resize_enabled) { 
659                         mouse_is_down = true;
660                         mouse_x = 0;
661                         mouse_y = 0;
662                         document.onselectstart = function() { return false; };
663                         return false;
664                 }
665
666                 if (selection_disabled) {
667                         document.onselectstart = function() { return false; };
668                         return false;
669                 }
670
671         } catch (e) {
672                 exception_error("mouse_down_handler", e);
673         }
674 }
675
676 function mouse_up_handler(e) {
677         try {
678                 mouse_is_down = false;
679
680                 if (!selection_disabled) {
681                         document.onselectstart = null;
682                         var e = $("headlineActionsBody");
683                         if (e) Element.hide(e);
684                         
685                         var e = $("offlineModeDrop");
686                         if (e) Element.hide(e);
687
688                 }
689
690         } catch (e) {
691                 exception_error("mouse_up_handler", e);
692         }
693 }
694
695 function request_counters_real() {
696
697         try {
698
699                 if (offline_mode) return;
700
701                 debug("requesting counters...");
702
703                 var query = "backend.php?op=rpc&subop=getAllCounters";
704
705                 if (tagsAreDisplayed()) {
706                         query = query + "&omode=tl";
707                 } else {
708                         query = query + "&omode=flc";
709                 }
710
711                 new Ajax.Request(query, {
712                         onComplete: function(transport) { 
713                                 try {
714                                         all_counters_callback2(transport, true);
715                                 } catch (e) {
716                                         exception_error("viewfeed/getcounters", e);
717                                 }
718                         } });
719
720         } catch (e) {
721                 exception_error("request_counters_real", e);
722         }
723 }
724
725
726 function request_counters() {
727
728         try {
729
730                 if (getInitParam("bw_limit") == "1") return;
731
732                 var date = new Date();
733                 var timestamp = Math.round(date.getTime() / 1000);
734
735 //              if (getInitParam("sync_counters") == "1" || 
736 //                              timestamp - counters_last_request > 10) {
737
738                 if (timestamp - counters_last_request > 15) {
739                         debug("scheduling request of counters...");
740                         window.setTimeout("request_counters_real()", 1000);
741                         counters_last_request = timestamp;
742                 } else {
743                         debug("request_counters: rate limit reached: " + (timestamp - counters_last_request));
744                 }
745
746         } catch (e) {
747                 exception_error("request_counters", e);
748         }
749 }
750
751