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