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