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