]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
tag cloud shows top 100 instead of top 50
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var daemon_refresh_only = false;
10 var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var last_refetch = 0;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
17 var sanity_check_done = false;
18
19 var xmlhttp = Ajax.getTransport();
20 var xmlhttp_ctr = Ajax.getTransport();
21
22 var init_params = new Object();
23
24 //var op_history = new Array();
25
26 function tagsAreDisplayed() {
27         return display_tags;
28 }
29
30 function toggleTags(show_all) {
31
32         var p = document.getElementById("dispSwitchPrompt");
33
34         if (!show_all && !display_tags) {
35                 displayDlg("printTagCloud");
36         } else if (show_all) {
37                 closeInfoBox();
38                 display_tags = true;
39                 p.innerHTML = __("display feeds");
40                 notify_progress("Loading, please wait...");
41                 updateFeedList();
42         } else if (display_tags) {
43                 display_tags = false;
44                 p.innerHTML = __("tag cloud");
45                 notify_progress("Loading, please wait...");
46                 updateFeedList();
47         }
48
49         return false;
50 }
51
52 function dlg_frefresh_callback() {
53         if (xmlhttp.readyState == 4) {          
54 //              notify(xmlhttp.responseText);
55
56                 if (getActiveFeedId() == _qfd_deleted_feed) {
57                         var h = document.getElementById("headlines-frame");
58                         if (h) {
59                                 h.innerHTML = "<div class='whiteBox'>" + __('No feed selected.') + "</div>";
60                         }
61                 }
62
63                 setTimeout('updateFeedList(false, false)', 50);
64                 closeInfoBox();
65         } 
66 }
67
68 function refetch_callback() {
69         if (xmlhttp_ctr.readyState == 4) {
70                 try {
71
72                         var date = new Date();
73
74                         last_refetch = date.getTime() / 1000;
75
76                         parse_counters_reply(xmlhttp_ctr, true);
77
78                         debug("refetch_callback: done");
79
80                         if (!daemon_enabled && !daemon_refresh_only) {
81                                 notify_info("All feeds updated.");
82                                 updateTitle("");
83                         } else {
84                                 //notify("");
85                         }
86                 } catch (e) {
87                         exception_error("refetch_callback", e);
88                         updateTitle("");
89                 }
90         }
91 }
92
93 function backend_sanity_check_callback() {
94
95         if (xmlhttp.readyState == 4) {
96
97                 try {
98         
99                         if (sanity_check_done) {
100                                 fatalError(11, "Sanity check request received twice. This can indicate "+
101                               "presence of Firebug or some other disrupting extension. "+
102                                         "Please disable it and try again.");
103                                 return;
104                         }
105
106                         if (!xmlhttp.responseXML) {
107                                 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
108                                 return;
109                         }
110         
111                         var reply = xmlhttp.responseXML.firstChild.firstChild;
112         
113                         if (!reply) {
114                                 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
115                                 return;
116                         }
117         
118                         var error_code = reply.getAttribute("error-code");
119                 
120                         if (error_code && error_code != 0) {
121                                 return fatalError(error_code, reply.getAttribute("error-msg"));
122                         }
123         
124                         debug("sanity check ok");
125
126                         var params = reply.nextSibling;
127
128                         if (params) {
129                                 debug('reading init-params...');
130                                 var param = params.firstChild;
131
132                                 while (param) {
133                                         var k = param.getAttribute("key");
134                                         var v = param.getAttribute("value");
135                                         debug(k + " => " + v);
136                                         init_params[k] = v;                                     
137                                         param = param.nextSibling;
138                                 }
139                         }
140
141                         sanity_check_done = true;
142
143                         init_second_stage();
144
145                 } catch (e) {
146                         exception_error("backend_sanity_check_callback", e);
147                 }
148         } 
149 }
150
151 function scheduleFeedUpdate(force) {
152
153         if (!daemon_enabled && !daemon_refresh_only) {
154                 notify_progress("Updating feeds, please wait.", true);
155                 updateTitle("Updating");
156         }
157
158         var query_str = "backend.php?op=rpc&subop=";
159
160         if (force) {
161                 query_str = query_str + "forceUpdateAllFeeds";
162         } else {
163                 query_str = query_str + "updateAllFeeds";
164         }
165
166         var omode;
167
168         if (firsttime_update && !navigator.userAgent.match("Opera")) {
169                 firsttime_update = false;
170                 omode = "T";
171         } else {
172                 if (display_tags) {
173                         omode = "tl";
174                 } else {
175                         omode = "flc";
176                 }
177         }
178         
179         query_str = query_str + "&omode=" + omode;
180         query_str = query_str + "&uctr=" + global_unread;
181
182         debug("in scheduleFeedUpdate");
183
184         var date = new Date();
185
186         var timestamp = Math.round(date.getTime() / 1000);
187         query_str = query_str + "&ts=" + timestamp
188
189         if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
190                 debug("<b>xmlhttp seems to be stuck, aborting</b>");
191                 xmlhttp_ctr.abort();
192         }
193
194         debug("REFETCH query: " + query_str);
195
196         if (xmlhttp_ready(xmlhttp_ctr)) {
197                 xmlhttp_ctr.open("GET", query_str, true);
198                 xmlhttp_ctr.onreadystatechange=refetch_callback;
199                 xmlhttp_ctr.send(null);
200         } else {
201                 debug("xmlhttp_ctr busy");
202                 //printLockingError();
203         }   
204 }
205
206 function updateFeedList(silent, fetch) {
207
208 //      if (silent != true) {
209 //              notify("Loading feed list...");
210 //      }
211
212         debug("<b>updateFeedList</b>");
213
214         var query_str = "backend.php?op=feeds";
215
216         if (display_tags) {
217                 query_str = query_str + "&tags=1";
218         }
219
220         if (getActiveFeedId() && !activeFeedIsCat()) {
221                 query_str = query_str + "&actid=" + getActiveFeedId();
222         }
223
224         var date = new Date();
225         var timestamp = Math.round(date.getTime() / 1000);
226         query_str = query_str + "&ts=" + timestamp
227         
228         if (fetch) query_str = query_str + "&fetch=yes";
229
230 //      var feeds_frame = document.getElementById("feeds-frame");
231 //      feeds_frame.src = query_str;
232
233         debug("updateFeedList Q=" + query_str);
234
235         if (xmlhttp_ready(xmlhttp)) {
236                 xmlhttp.open("GET", query_str, true);
237                 xmlhttp.onreadystatechange=feedlist_callback;
238                 xmlhttp.send(null);
239         } else {
240                 debug("xmlhttp busy");
241                 //printLockingError();
242         }   
243
244 }
245
246 function catchupAllFeeds() {
247
248         var query_str = "backend.php?op=feeds&subop=catchupAll";
249
250         notify_progress("Marking all feeds as read...");
251
252         debug("catchupAllFeeds Q=" + query_str);
253
254         if (xmlhttp_ready(xmlhttp)) {
255                 xmlhttp.open("GET", query_str, true);
256                 xmlhttp.onreadystatechange=feedlist_callback;
257                 xmlhttp.send(null);
258         } else {
259                 debug("xmlhttp busy");
260                 //printLockingError();
261         }   
262
263         global_unread = 0;
264         updateTitle("");
265
266 }
267
268 function viewCurrentFeed(subop) {
269
270 //      if (getActiveFeedId()) {
271         if (getActiveFeedId() != undefined) {
272                 viewfeed(getActiveFeedId(), subop);
273         } else {
274                 disableContainerChildren("headlinesToolbar", false, document);
275 //              viewfeed(-1, subop); // FIXME
276         }
277         return false; // block unneeded form submits
278 }
279
280 function viewfeed(feed, subop) {
281         var f = window.frames["feeds-frame"];
282         f.viewfeed(feed, subop);
283 }
284
285 function timeout() {
286         scheduleFeedUpdate(false);
287
288         var refresh_time = getInitParam("feeds_frame_refresh");
289
290         if (!refresh_time) refresh_time = 600; 
291
292         setTimeout("timeout()", refresh_time*1000);
293 }
294
295 function resetSearch() {
296         var searchbox = document.getElementById("searchbox")
297
298         if (searchbox.value != "" && getActiveFeedId()) {       
299                 searchbox.value = "";
300                 viewfeed(getActiveFeedId(), "");
301         }
302 }
303
304 function searchCancel() {
305         closeInfoBox(true);
306 }
307
308 function search() {
309         closeInfoBox(); 
310         viewCurrentFeed(0, "");
311 }
312
313 function localPiggieFunction(enable) {
314         if (enable) {
315                 var query_str = "backend.php?op=feeds&subop=piggie";
316
317                 if (xmlhttp_ready(xmlhttp)) {
318
319                         xmlhttp.open("GET", query_str, true);
320                         xmlhttp.onreadystatechange=feedlist_callback;
321                         xmlhttp.send(null);
322                 }
323         }
324 }
325
326 // if argument is undefined, current subtitle is not updated
327 // use blank string to clear subtitle
328 function updateTitle(s) {
329         var tmp = "Tiny Tiny RSS";
330
331         if (s != undefined) {
332                 current_subtitle = s;
333         }
334
335         if (global_unread > 0) {
336                 tmp = tmp + " (" + global_unread + ")";
337         }
338
339         if (current_subtitle) {
340                 tmp = tmp + " - " + current_subtitle;
341         }
342
343         if (active_title_text.length > 0) {
344                 tmp = tmp + " > " + active_title_text;
345         }
346
347         document.title = tmp;
348 }
349
350 function genericSanityCheck() {
351
352         if (!xmlhttp) fatalError(1);
353
354         setCookie("ttrss_vf_test", "TEST");
355         
356         if (getCookie("ttrss_vf_test") != "TEST") {
357                 fatalError(2);
358         }
359
360         return true;
361 }
362
363 function init() {
364
365         try {
366
367                 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
368
369                 if (arguments.callee.done) return;
370                 arguments.callee.done = true;           
371
372                 disableContainerChildren("headlinesToolbar", true);
373
374                 Form.disable("main_toolbar_form");
375
376                 if (!genericSanityCheck()) 
377                         return;
378
379                 if (getURLParam('debug')) {
380                         document.getElementById('debug_output').style.display = 'block';
381                         debug('debug mode activated');
382                 }
383
384                 var params = "&ua=" + param_escape(navigator.userAgent);
385
386                 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck" + params, true);
387                 xmlhttp.onreadystatechange=backend_sanity_check_callback;
388                 xmlhttp.send(null);
389
390         } catch (e) {
391                 exception_error("init", e);
392         }
393 }
394
395 function resize_headlines() {
396
397         var h_frame = document.getElementById("headlines-frame");
398         var c_frame = document.getElementById("content-frame");
399         var f_frame = document.getElementById("footer");
400
401         if (!c_frame || !h_frame) return;
402
403         debug("resize_headlines");
404
405         if (!is_msie()) {
406                 h_frame.style.height = 30 + "%";
407                 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
408                 h_frame.style.height = h_frame.offsetHeight + "px";
409         } else {
410                 h_frame.style.height = document.documentElement.clientHeight * 0.3 + "px";
411                 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
412
413                 var c_bottom = document.documentElement.clientHeight;
414
415                 if (f_frame) {
416                         c_bottom = f_frame.offsetTop;
417                 }
418
419                 c_frame.style.height = c_bottom - (h_frame.offsetTop + 
420                         h_frame.offsetHeight + 1) + "px";
421                 h_frame.style.height = h_frame.offsetHeight + "px";
422
423         }
424 }
425
426 function init_second_stage() {
427
428         try {
429
430                 cookie_lifetime = getCookie("ttrss_cltime");
431
432                 delCookie("ttrss_vf_test");
433
434 //              document.onresize = resize_headlines;
435                 resize_headlines();
436
437                 var toolbar = document.forms["main_toolbar_form"];
438
439                 dropboxSelect(toolbar.view_mode, getInitParam("default_view_mode"));
440                 dropboxSelect(toolbar.limit, getInitParam("default_view_limit"));
441
442                 daemon_enabled = getInitParam("daemon_enabled") == 1;
443                 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
444
445                 setTimeout('updateFeedList(false, false)', 50);
446
447                 debug("second stage ok");
448         
449         } catch (e) {
450                 exception_error("init_second_stage", e);
451         }
452 }
453
454 function quickMenuChange() {
455         var chooser = document.getElementById("quickMenuChooser");
456         var opid = chooser[chooser.selectedIndex].value;
457
458         chooser.selectedIndex = 0;
459         quickMenuGo(opid);
460 }
461
462 function quickMenuGo(opid) {
463         try {
464
465                 if (opid == "qmcPrefs") {
466                         gotoPreferences();
467                 }
468         
469                 if (opid == "qmcSearch") {
470                         displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
471                         return;
472                 }
473         
474                 if (opid == "qmcAddFeed") {
475                         displayDlg("quickAddFeed");
476                         return;
477                 }
478
479                 if (opid == "qmcEditFeed") {
480                         editFeedDlg(getActiveFeedId());
481                 }
482         
483                 if (opid == "qmcRemoveFeed") {
484                         var actid = getActiveFeedId();
485
486                         if (activeFeedIsCat()) {
487                                 alert("You can't unsubscribe from the category.");
488                                 return;
489                         }       
490
491                         if (!actid) {
492                                 alert("Please select some feed first.");
493                                 return;
494                         }
495
496                         var fn = getFeedName(actid);
497         
498                         if (confirm("Unsubscribe from " + fn + "?")) {
499                                 qfdDelete(actid);
500                         }
501                 
502                         return;
503                 }
504         
505                 if (opid == "qmcUpdateFeeds") {
506                         scheduleFeedUpdate(true);
507                         return;
508                 }
509         
510                 if (opid == "qmcCatchupAll") {
511                         catchupAllFeeds();
512                         return;
513                 }
514         
515                 if (opid == "qmcShowOnlyUnread") {
516                         toggleDispRead();
517                         return;
518                 }
519         
520                 if (opid == "qmcAddFilter") {
521                         displayDlg("quickAddFilter", getActiveFeedId());
522                 }
523
524         } catch (e) {
525                 exception_error("quickMenuGo", e);
526         }
527 }
528
529 function qfdDelete(feed_id) {
530
531         notify_progress("Removing feed...");
532
533         if (!xmlhttp_ready(xmlhttp)) {
534                 printLockingError();
535                 return
536         }
537
538         _qfd_deleted_feed = feed_id;
539
540         xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
541         xmlhttp.onreadystatechange=dlg_frefresh_callback;
542         xmlhttp.send(null);
543
544         return false;
545 }
546
547
548 function updateFeedTitle(t) {
549         active_title_text = t;
550         updateTitle();
551 }
552
553 function toggleDispRead() {
554         try {
555
556                 if (!xmlhttp_ready(xmlhttp)) {
557                         printLockingError();
558                         return
559                 } 
560
561                 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
562
563                 hide_read_feeds = !hide_read_feeds;
564
565                 debug("toggle_disp_read => " + hide_read_feeds);
566
567                 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
568
569                 storeInitParam("hide_read_feeds", hide_read_feeds, true);
570
571 /*              var query = "backend.php?op=rpc&subop=setpref" +
572                         "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
573
574                 new Ajax.Request(query); */
575                                 
576         } catch (e) {
577                 exception_error("toggleDispRead", e);
578         }
579 }
580
581 function parse_runtime_info(elem) {
582         if (!elem) {
583                 debug("parse_runtime_info: elem is null, aborting");
584                 return;
585         }
586
587         var param = elem.firstChild;
588
589         debug("parse_runtime_info: " + param);
590
591         while (param) {
592                 var k = param.getAttribute("key");
593                 var v = param.getAttribute("value");
594
595                 debug("RI: " + k + " => " + v);
596
597                 if (k == "new_version_available") {
598                         var icon = document.getElementById("newVersionIcon");
599                         if (icon) {
600                                 if (v == "1") {
601                                         icon.style.display = "inline";
602                                 } else {
603                                         icon.style.display = "none";
604                                 }
605                         }
606                 }
607
608                 if (k == "daemon_is_running" && v != 1) {
609                         notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not runing.</span>");
610                 }
611
612 /*              var w = document.getElementById("noDaemonWarning");
613                 
614                 if (w) {
615                         if (k == "daemon_is_running" && v != 1) {
616                                 w.style.display = "block";
617                         } else {
618                                 w.style.display = "none";
619                         }
620                 } */
621                 param = param.nextSibling;
622         }
623 }
624
625 function catchupCurrentFeed() {
626
627         var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
628         
629         var str = "Mark all articles in " + fn + " as read?";
630
631 /*      if (active_feed_is_cat) {
632                 str = "Mark all articles in this category as read?";
633         } */
634
635         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
636                 return viewCurrentFeed('MarkAllRead')
637         }
638 }
639
640 function userSwitch() {
641         var chooser = document.getElementById("userSwitch");
642         var user = chooser[chooser.selectedIndex].value;
643         window.location = "tt-rss.php?swu=" + user;
644 }
645
646 function editFeedDlg(feed) {
647
648         disableHotkeys();
649
650         if (!feed) {
651                 alert("Please select some feed first.");
652                 return;
653         }
654
655         if (feed <= 0 || activeFeedIsCat() || tagsAreDisplayed()) {
656                 alert("You can't edit this kind of feed.");
657                 return;
658         }
659
660         if (xmlhttp_ready(xmlhttp)) {
661                 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
662                         param_escape(feed), true);
663                 xmlhttp.onreadystatechange=infobox_callback;
664                 xmlhttp.send(null);
665         } else {
666                 printLockingError();
667         }
668 }
669
670 /* this functions duplicate those of prefs.js feed editor, with
671         some differences because there is no feedlist */
672
673 function feedEditCancel() {
674         closeInfoBox();
675         return false;
676 }
677
678 function feedEditSave() {
679
680         try {
681         
682                 if (!xmlhttp_ready(xmlhttp)) {
683                         printLockingError();
684                         return
685                 }
686
687                 // FIXME: add parameter validation
688
689                 var query = Form.serialize("edit_feed_form");
690
691                 notify_progress("Saving feed...");
692
693                 xmlhttp.open("POST", "backend.php", true);
694                 xmlhttp.onreadystatechange=dlg_frefresh_callback;
695                 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
696                 xmlhttp.send(query);
697
698                 closeInfoBox();
699
700                 return false;
701
702         } catch (e) {
703                 exception_error("feedEditSave (main)", e);
704         } 
705 }
706 /*
707 function localHotkeyHandler(e) {
708
709         var keycode;
710
711         if (window.event) {
712                 keycode = window.event.keyCode;
713         } else if (e) {
714                 keycode = e.which;
715         }
716
717         var shift_key = false;
718
719         try {
720                 shift_key = e.shiftKey;
721         } catch (e) { }
722
723         if (keycode == 66 && shift_key) { // shift-B
724
725                 var op = history_pop();
726
727                 if (op) {
728                         var op_s = op.split(":");
729
730                         var i;
731                         for (i = 0; i < op_s.length; i++) {
732                                 if (op_s[i] == 'undefined') {
733                                         op_s[i] = undefined;
734                                 }
735
736                                 if (op_s[i] == 'false') {
737                                         op_s[i] = false;
738                                 }
739
740                                 if (op_s[i] == 'true') {
741                                         op_s[i] = true;
742                                 }
743                                 
744                         }
745
746                         debug("history split: " + op_s);
747
748                         if (op_s[0] == "ARTICLE") {
749                                 debug("history: reverting to article " + op_s[1] + "/" + op_s[2]);
750                                 view(op_s[1], op_s[2], true);
751                         }
752
753                         if (op_s[0] == "FEED") {
754                                 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
755                         }
756
757                 } else {
758                         notify_error("No operation to undo");
759                 }
760
761                 return false;
762
763         }       
764
765         debug("LKP=" + keycode);
766
767
768 function history_push(op) {
769         debug("history_push: " + op);
770         op_history.push(op);
771
772         while (op_history.length > 30) {
773                 op_history.shift();
774         }
775 }
776
777 function history_pop() {
778         var op = op_history.pop();
779         debug("history_pop: " + op);
780         return op;
781 }
782
783 function history_clear() {
784         debug("history_clear");
785         op_history.clear();
786 } */