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