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