]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
fix infobox overlay override for gecko and opera
[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 function toggleTags() {
24         display_tags = !display_tags;
25
26         var p = document.getElementById("dispSwitchPrompt");
27
28         if (display_tags) {
29                 p.innerHTML = "display feeds";
30         } else {
31                 p.innerHTML = "display tags";
32         }
33         
34         updateFeedList();
35 }
36
37 function dlg_frefresh_callback() {
38         if (xmlhttp.readyState == 4) {          
39 //              notify(xmlhttp.responseText);
40
41                 if (getActiveFeedId() == _qfd_deleted_feed) {
42                         var h = document.getElementById("headlines-frame");
43                         if (h) {
44                                 h.innerHTML = "<div class='whiteBox'>No feed selected.</div>";
45                         }
46                 }
47
48                 setTimeout('updateFeedList(false, false)', 50);
49                 closeInfoBox();
50         } 
51 }
52
53 function refetch_callback() {
54         if (xmlhttp_ctr.readyState == 4) {
55                 try {
56
57                         var date = new Date();
58
59                         last_refetch = date.getTime() / 1000;
60
61                         if (!xmlhttp_ctr.responseXML) {
62                                 notify("refetch_callback: backend did not return valid XML", true, true);
63                                 return;
64                         }
65                 
66                         var reply = xmlhttp_ctr.responseXML.firstChild;
67         
68                         if (!reply) {
69                                 notify("refetch_callback: backend did not return expected XML object", true, true);
70                                 updateTitle("");
71                                 return;
72                         } 
73         
74                         var error_code = reply.getAttribute("error-code");
75                 
76                         if (error_code && error_code != 0) {
77                                 return fatalError(error_code, reply.getAttribute("error-msg"));
78                         }
79
80                         var counters = reply.firstChild;
81         
82                         parse_counters(counters, true);
83
84                         var runtime_info = counters.nextSibling;
85
86                         parse_runtime_info(runtime_info);
87
88                         debug("refetch_callback: done");
89
90                         if (!daemon_enabled && !daemon_refresh_only) {
91                                 notify("All feeds updated.");
92                                 updateTitle("");
93                         } else {
94                                 notify("");
95                         }
96                 } catch (e) {
97                         exception_error("refetch_callback", e);
98                         updateTitle("");
99                 }
100         }
101 }
102
103 function backend_sanity_check_callback() {
104
105         if (xmlhttp.readyState == 4) {
106
107                 try {
108                 
109                         if (!xmlhttp.responseXML) {
110                                 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
111                                 return;
112                         }
113         
114                         var reply = xmlhttp.responseXML.firstChild.firstChild;
115         
116                         if (!reply) {
117                                 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
118                                 return;
119                         }
120         
121                         var error_code = reply.getAttribute("error-code");
122                 
123                         if (error_code && error_code != 0) {
124                                 return fatalError(error_code, reply.getAttribute("error-msg"));
125                         }
126         
127                         debug("sanity check ok");
128
129                         var params = reply.nextSibling;
130
131                         if (params) {
132                                 debug('reading init-params...');
133                                 var param = params.firstChild;
134
135                                 while (param) {
136                                         var k = param.getAttribute("key");
137                                         var v = param.getAttribute("value");
138                                         debug(k + " => " + v);
139                                         init_params[k] = v;                                     
140                                         param = param.nextSibling;
141                                 }
142                         }
143
144                         init_second_stage();
145
146                 } catch (e) {
147                         exception_error("backend_sanity_check_callback", e);
148                 }
149         } 
150 }
151
152 function scheduleFeedUpdate(force) {
153
154         if (!daemon_enabled && !daemon_refresh_only) {
155                 notify("Updating feeds, please wait.", true);
156                 updateTitle("Updating");
157         }
158
159         var query_str = "backend.php?op=rpc&subop=";
160
161         if (force) {
162                 query_str = query_str + "forceUpdateAllFeeds";
163         } else {
164                 query_str = query_str + "updateAllFeeds";
165         }
166
167         var omode;
168
169         if (firsttime_update && !navigator.userAgent.match("Opera")) {
170                 firsttime_update = false;
171                 omode = "T";
172         } else {
173                 if (display_tags) {
174                         omode = "t";
175                 } else {
176                         omode = "flc";
177                 }
178         }
179         
180         query_str = query_str + "&omode=" + omode;
181         query_str = query_str + "&uctr=" + global_unread;
182
183         debug("in scheduleFeedUpdate");
184
185         var date = new Date();
186
187         if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
188                 debug("<b>xmlhttp seems to be stuck, aborting</b>");
189                 xmlhttp_ctr.abort();
190         }
191
192         debug("REFETCH query: " + query_str);
193
194         if (xmlhttp_ready(xmlhttp_ctr)) {
195                 xmlhttp_ctr.open("GET", query_str, true);
196                 xmlhttp_ctr.onreadystatechange=refetch_callback;
197                 xmlhttp_ctr.send(null);
198         } else {
199                 debug("xmlhttp_ctr busy");
200                 //printLockingError();
201         }   
202 }
203
204 function updateFeedList(silent, fetch) {
205
206 //      if (silent != true) {
207 //              notify("Loading feed list...");
208 //      }
209
210         var query_str = "backend.php?op=feeds";
211
212         if (display_tags) {
213                 query_str = query_str + "&tags=1";
214         }
215
216         if (getActiveFeedId() && !activeFeedIsCat()) {
217                 query_str = query_str + "&actid=" + getActiveFeedId();
218         }
219
220         if (navigator.userAgent.match("Opera")) {
221                 var date = new Date();
222                 var timestamp = Math.round(date.getTime() / 1000);
223                 query_str = query_str + "&ts=" + timestamp
224         }
225         
226         if (fetch) query_str = query_str + "&fetch=yes";
227
228 //      var feeds_frame = document.getElementById("feeds-frame");
229 //      feeds_frame.src = query_str;
230
231         debug("updateFeedList Q=" + query_str);
232
233         if (xmlhttp_ready(xmlhttp)) {
234                 xmlhttp.open("GET", query_str, true);
235                 xmlhttp.onreadystatechange=feedlist_callback;
236                 xmlhttp.send(null);
237         } else {
238                 debug("xmlhttp busy");
239                 //printLockingError();
240         }   
241
242 }
243
244 function catchupAllFeeds() {
245
246         var query_str = "backend.php?op=feeds&subop=catchupAll";
247
248         notify("Marking all feeds as read...");
249
250         var feeds_frame = document.getElementById("feeds-frame");
251
252         feeds_frame.src = query_str;
253
254         global_unread = 0;
255         updateTitle("");
256
257 }
258
259 function viewCurrentFeed(subop) {
260
261 //      if (getActiveFeedId()) {
262         if (getActiveFeedId() != undefined) {
263                 viewfeed(getActiveFeedId(), subop);
264         } else {
265                 disableContainerChildren("headlinesToolbar", false, document);
266 //              viewfeed(-1, subop); // FIXME
267         }
268         return false; // block unneeded form submits
269 }
270
271 function viewfeed(feed, subop) {
272         var f = window.frames["feeds-frame"];
273         f.viewfeed(feed, subop);
274 }
275
276 function timeout() {
277         scheduleFeedUpdate(false);
278
279         var refresh_time = getInitParam("feeds_frame_refresh");
280
281         if (!refresh_time) refresh_time = 600; 
282
283         setTimeout("timeout()", refresh_time*1000);
284 }
285
286 function resetSearch() {
287         var searchbox = document.getElementById("searchbox")
288
289         if (searchbox.value != "" && getActiveFeedId()) {       
290                 searchbox.value = "";
291                 viewfeed(getActiveFeedId(), "");
292         }
293 }
294
295 function searchCancel() {
296         closeInfoBox(true);
297 }
298
299 function search() {
300         closeInfoBox(); 
301         viewCurrentFeed(0, "");
302 }
303
304 function localPiggieFunction(enable) {
305         if (enable) {
306                 var query_str = "backend.php?op=feeds&subop=piggie";
307
308                 if (xmlhttp_ready(xmlhttp)) {
309
310                         xmlhttp.open("GET", query_str, true);
311                         xmlhttp.onreadystatechange=feedlist_callback;
312                         xmlhttp.send(null);
313                 }
314         }
315 }
316
317 // if argument is undefined, current subtitle is not updated
318 // use blank string to clear subtitle
319 function updateTitle(s) {
320         var tmp = "Tiny Tiny RSS";
321
322         if (s != undefined) {
323                 current_subtitle = s;
324         }
325
326         if (global_unread > 0) {
327                 tmp = tmp + " (" + global_unread + ")";
328         }
329
330         if (current_subtitle) {
331                 tmp = tmp + " - " + current_subtitle;
332         }
333
334         if (active_title_text.length > 0) {
335                 tmp = tmp + " > " + active_title_text;
336         }
337
338         document.title = tmp;
339 }
340
341 function genericSanityCheck() {
342
343         if (!xmlhttp) fatalError(1);
344
345         setCookie("ttrss_vf_test", "TEST");
346         
347         if (getCookie("ttrss_vf_test") != "TEST") {
348                 fatalError(2);
349         }
350
351         return true;
352 }
353
354 function init() {
355
356         try {
357
358                 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
359
360                 if (arguments.callee.done) return;
361                 arguments.callee.done = true;           
362
363                 disableContainerChildren("headlinesToolbar", true);
364
365                 Form.disable("main_toolbar_form");
366
367                 if (!genericSanityCheck()) 
368                         return;
369
370                 if (getURLParam('debug')) {
371                         document.getElementById('debug_output').style.display = 'block';
372                         debug('debug mode activated');
373                 }
374
375                 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
376                 xmlhttp.onreadystatechange=backend_sanity_check_callback;
377                 xmlhttp.send(null);
378
379         } catch (e) {
380                 exception_error("init", e);
381         }
382 }
383
384 function init_second_stage() {
385
386         try {
387
388                 cookie_lifetime = getCookie("ttrss_cltime");
389
390                 delCookie("ttrss_vf_test");
391         
392                 document.onkeydown = hotkey_handler;
393
394                 var tb = parent.document.forms["main_toolbar_form"];
395
396 //              dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
397 //              dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
398
399                 daemon_enabled = getInitParam("daemon_enabled") == 1;
400                 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
401
402                 setTimeout('updateFeedList(false, false)', 50);
403
404                 debug("second stage ok");
405         
406         } catch (e) {
407                 exception_error("init_second_stage", e);
408         }
409 }
410
411 function quickMenuChange() {
412         var chooser = document.getElementById("quickMenuChooser");
413         var opid = chooser[chooser.selectedIndex].value;
414
415         chooser.selectedIndex = 0;
416         quickMenuGo(opid);
417 }
418
419 function quickMenuGo(opid) {
420         try {
421
422                 if (opid == "qmcPrefs") {
423                         gotoPreferences();
424                 }
425         
426                 if (opid == "qmcSearch") {
427                         displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
428                         return;
429                 }
430         
431                 if (opid == "qmcAddFeed") {
432                         displayDlg("quickAddFeed");
433                         return;
434                 }
435         
436                 if (opid == "qmcRemoveFeed") {
437                         var actid = getActiveFeedId();
438         
439                         if (!actid || activeFeedIsCat()) {
440                                 alert("Please select some feed first.");
441                                 return;
442                         }
443
444                         var fn = getFeedName(actid);
445         
446                         if (confirm("Unsubscribe from " + fn + "?")) {
447                                 qfdDelete(actid);
448                         }
449                 
450                         return;
451                 }
452         
453                 if (opid == "qmcUpdateFeeds") {
454                         scheduleFeedUpdate(true);
455                         return;
456                 }
457         
458                 if (opid == "qmcCatchupAll") {
459                         catchupAllFeeds();
460                         return;
461                 }
462         
463                 if (opid == "qmcShowOnlyUnread") {
464                         toggleDispRead();
465                         return;
466                 }
467         
468                 if (opid == "qmcAddFilter") {
469                         displayDlg("quickAddFilter", getActiveFeedId());
470                 }
471         } catch (e) {
472                 exception_error("quickMenuGo", e);
473         }
474 }
475
476 function qfdDelete(feed_id) {
477
478         notify("Removing feed...");
479
480         if (!xmlhttp_ready(xmlhttp)) {
481                 printLockingError();
482                 return
483         }
484
485         _qfd_deleted_feed = feed_id;
486
487         xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
488         xmlhttp.onreadystatechange=dlg_frefresh_callback;
489         xmlhttp.send(null);
490
491         return false;
492 }
493
494
495 function updateFeedTitle(t) {
496         active_title_text = t;
497         updateTitle();
498 }
499
500 function toggleDispRead() {
501         try {
502
503                 if (!xmlhttp_ready(xmlhttp)) {
504                         printLockingError();
505                         return
506                 }
507
508                 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
509
510                 hide_read_feeds = !hide_read_feeds;
511
512                 debug("toggle_disp_read => " + hide_read_feeds);
513
514                 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
515
516                 var query = "backend.php?op=rpc&subop=setpref" +
517                         "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
518
519                 storeInitParam("hide_read_feeds", hide_read_feeds, true);
520
521                 new Ajax.Request(query);
522                 
523         } catch (e) {
524                 exception_error("toggleDispRead", e);
525         }
526 }
527
528 function parse_runtime_info(elem) {
529         var param = elem.firstChild;
530
531         debug("parse_runtime_info: " + param);
532
533         while (param) {
534                 var k = param.getAttribute("key");
535                 var v = param.getAttribute("value");
536
537                 debug("RI: " + k + " => " + v);
538
539                 var w = document.getElementById("noDaemonWarning");
540                 
541                 if (w) {
542                         if (k == "daemon_is_running" && v != 1) {
543                                 w.style.display = "block";
544                         } else {
545                                 w.style.display = "none";
546                         }
547                 }
548                 param = param.nextSibling;
549         }
550 }
551
552 function catchupCurrentFeed() {
553
554         var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
555         
556         var str = "Mark all articles in " + fn + " as read?";
557
558 /*      if (active_feed_is_cat) {
559                 str = "Mark all articles in this category as read?";
560         } */
561
562         if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
563                 return viewCurrentFeed('MarkAllRead')
564         }
565 }
566
567 function userSwitch() {
568         var chooser = document.getElementById("userSwitch");
569         var user = chooser[chooser.selectedIndex].value;
570         window.location = "tt-rss.php?swu=" + user;
571 }
572
573