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