]> git.wh0rd.org Git - tt-rss.git/blob - tt-rss.js
display current feed in title (only in daemon mode for now)
[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 _qfd_deleted_feed = 0;
10
11 /*@cc_on @*/
12 /*@if (@_jscript_version >= 5)
13 // JScript gives us Conditional compilation, we can cope with old IE versions.
14 // and security blocked creation of the objects.
15 try {
16         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
17 } catch (e) {
18         try {
19                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
20         } catch (E) {
21                 xmlhttp = false;
22         }
23 }
24 @end @*/
25
26 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
27         xmlhttp = new XMLHttpRequest();
28 }
29
30 function toggleTags() {
31         display_tags = !display_tags;
32
33         var p = document.getElementById("dispSwitchPrompt");
34
35         if (display_tags) {
36                 p.innerHTML = "display feeds";
37         } else {
38                 p.innerHTML = "display tags";
39         }
40         
41         updateFeedList();
42 }
43
44 function dlg_frefresh_callback() {
45         if (xmlhttp.readyState == 4) {
46                 notify(xmlhttp.responseText);
47                 updateFeedList(false, false);
48                 if (_qfd_deleted_feed) {
49                         var hframe = document.getElementById("headlines-frame");
50                         if (hframe) {
51                                 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
52                         }
53                 }
54                 closeDlg();
55         } 
56 }
57
58 function dlg_submit_callback() {
59         if (xmlhttp.readyState == 4) {
60                 notify(xmlhttp.responseText);
61                 closeDlg();
62         } 
63 }
64
65 function dlg_display_callback() {
66         if (xmlhttp.readyState == 4) {
67                 var dlg = document.getElementById("userDlg");
68                 var dlg_s = document.getElementById("userDlgShadow");
69
70                 dlg.innerHTML = xmlhttp.responseText;
71                 dlg_s.style.display = "block";
72         } 
73 }
74
75 function hide_unread_callback() {
76         if (xmlhttp.readyState == 4) {
77
78                 try {
79
80                         var reply = xmlhttp.responseXML.firstChild.firstChild;
81                         var value = reply.getAttribute("value");
82                         var hide_read_feeds = (value != "false")
83                         var feeds_doc = window.frames["feeds-frame"].document;
84         
85                         hideOrShowFeeds(feeds_doc, hide_read_feeds);
86         
87                         if (hide_read_feeds) {
88                                 setCookie("ttrss_vf_hreadf", 1);
89                         } else {
90                                 setCookie("ttrss_vf_hreadf", 0);
91                         } 
92
93                 } catch (e) {
94                         exception_error("hide_unread_callback", e);
95                 }
96
97         } 
98 }
99
100 function refetch_callback() {
101         if (xmlhttp.readyState == 4) {
102                 try {
103
104                         if (!xmlhttp.responseXML) {
105                                 notify("refetch_callback: backend did not return valid XML");
106                                 return;
107                         }
108                 
109                         var reply = xmlhttp.responseXML.firstChild;
110         
111                         if (!reply) {
112                                 notify("refetch_callback: backend did not return expected XML object");
113                                 updateTitle("");
114                                 return;
115                         } 
116         
117                         var error_code = reply.getAttribute("error-code");
118                 
119                         if (error_code && error_code != 0) {
120                                 return fatalError(error_code);
121                         }
122         
123                         var f_document = window.frames["feeds-frame"].document;
124
125                         parse_counters(reply, f_document, window);
126
127                         if (!daemon_enabled) {
128                                 notify("All feeds updated.");
129                         } else {
130                                 global_unread = 0;
131                         }
132                         updateTitle("");
133                 } catch (e) {
134                         exception_error("refetch_callback", e);
135                         updateTitle("");
136                 }
137         }
138 }
139
140 function backend_sanity_check_callback() {
141
142         if (xmlhttp.readyState == 4) {
143
144                 try {
145                 
146                         if (!xmlhttp.responseXML) {
147                                 fatalError(3);
148                                 return;
149                         }
150         
151                         var reply = xmlhttp.responseXML.firstChild;
152         
153                         if (!reply) {
154                                 fatalError(3);
155                                 return;
156                         }
157         
158                         var error_code = reply.getAttribute("error-code");
159                 
160                         if (error_code && error_code != 0) {
161                                 return fatalError(error_code);
162                         }
163         
164                         init_second_stage();
165
166                 } catch (e) {
167                         exception_error("backend_sanity_check_callback", e);
168                 }
169         } 
170 }
171
172 function scheduleFeedUpdate(force) {
173
174         if (!daemon_enabled) {
175                 notify("Updating feeds, please wait.");
176                 updateTitle("Updating");
177         }
178
179         var query_str = "backend.php?op=rpc&subop=";
180
181         if (force) {
182                 query_str = query_str + "forceUpdateAllFeeds";
183         } else {
184                 query_str = query_str + "updateAllFeeds";
185         }
186
187         var omode;
188
189         if (display_tags) {
190                 omode = "t";
191         } else {
192                 omode = "fl";
193         }
194
195         query_str = query_str + "&omode=" + omode;
196         query_str = query_str + "&uctr=" + global_unread;
197
198         if (xmlhttp_ready(xmlhttp)) {
199                 xmlhttp.open("GET", query_str, true);
200                 xmlhttp.onreadystatechange=refetch_callback;
201                 xmlhttp.send(null);
202         } else {
203                 printLockingError();
204         }   
205 }
206
207 function updateFeedList(silent, fetch) {
208
209 //      if (silent != true) {
210 //              notify("Loading feed list...");
211 //      }
212
213         var query_str = "backend.php?op=feeds";
214
215         if (display_tags) {
216                 query_str = query_str + "&tags=1";
217         }
218
219         if (getActiveFeedId()) {
220                 query_str = query_str + "&actid=" + getActiveFeedId();
221         }
222
223         if (fetch) query_str = query_str + "&fetch=yes";
224
225         var feeds_frame = document.getElementById("feeds-frame");
226
227         feeds_frame.src = query_str;
228 }
229
230 function catchupAllFeeds() {
231
232         var query_str = "backend.php?op=feeds&subop=catchupAll";
233
234         notify("Marking all feeds as read...");
235
236         var feeds_frame = document.getElementById("feeds-frame");
237
238         feeds_frame.src = query_str;
239
240         global_unread = 0;
241         updateTitle("");
242
243 }
244
245 function viewCurrentFeed(skip, subop) {
246
247         if (getActiveFeedId()) {
248                 viewfeed(getActiveFeedId(), skip, subop);
249         } else {
250                 disableContainerChildren("headlinesToolbar", false, document);
251                 viewfeed(-1, skip, subop); // FIXME
252         }
253 }
254
255 function viewfeed(feed, skip, subop) {
256         var f = window.frames["feeds-frame"];
257         f.viewfeed(feed, skip, subop);
258 }
259
260 function timeout() {
261         scheduleFeedUpdate(false);
262
263         var refresh_time = getCookie('ttrss_vf_refresh');
264
265         if (!refresh_time) refresh_time = 600;
266
267         setTimeout("timeout()", refresh_time*1000);
268 }
269
270 function resetSearch() {
271         var searchbox = document.getElementById("searchbox")
272
273         if (searchbox.value != "" && getActiveFeedId()) {       
274                 searchbox.value = "";
275                 viewfeed(getActiveFeedId(), 0, "");
276         }
277 }
278
279 function search() {
280         closeDlg();     
281         viewCurrentFeed(0, "");
282 }
283
284 function localPiggieFunction(enable) {
285         if (enable) {
286                 var query_str = "backend.php?op=feeds&subop=piggie";
287
288                 if (xmlhttp_ready(xmlhttp)) {
289
290                         xmlhttp.open("GET", query_str, true);
291                         xmlhttp.onreadystatechange=feedlist_callback;
292                         xmlhttp.send(null);
293                 }
294         }
295 }
296
297 function localHotkeyHandler(keycode) {
298
299 /*      if (keycode == 78) {
300                 return moveToPost('next');
301         }
302
303         if (keycode == 80) {
304                 return moveToPost('prev');
305         } */
306
307         if (keycode == 82) { // r
308                 return scheduleFeedUpdate(true);
309         }
310
311         if (keycode == 85) { // u
312                 if (getActiveFeedId()) {
313                         return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
314                 }
315         }
316
317         if (keycode == 65) { // a
318                 return toggleDispRead();
319         }
320
321         var f_doc = window.frames["feeds-frame"].document;
322         var feedlist = f_doc.getElementById('feedList');
323
324         if (keycode == 74) { // j
325                 var feed = getActiveFeedId();
326                 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
327                 if (new_feed) viewfeed(new_feed, 0, '');
328         }
329
330         if (keycode == 75) { // k
331                 var feed = getActiveFeedId();
332                 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
333                 if (new_feed) viewfeed(new_feed, 0, '');
334         }
335
336 //      notify("KC: " + keycode);
337
338 }
339
340 // if argument is undefined, current subtitle is not updated
341 // use blank string to clear subtitle
342 function updateTitle(s) {
343         var tmp = "Tiny Tiny RSS";
344
345         if (s != undefined) {
346                 current_subtitle = s;
347         }
348
349         if (global_unread > 0) {
350                 tmp = tmp + " (" + global_unread + ")";
351         }
352
353         if (current_subtitle) {
354                 tmp = tmp + " - " + current_subtitle;
355         }
356
357         if (active_title_text.length > 0) {
358                 tmp = tmp + " > " + active_title_text;
359         }
360
361         document.title = tmp;
362 }
363
364 function genericSanityCheck() {
365
366         if (!xmlhttp) fatalError(1);
367
368         setCookie("ttrss_vf_test", "TEST");
369         
370         if (getCookie("ttrss_vf_test") != "TEST") {
371                 fatalError(2);
372         }
373
374         return true;
375 }
376
377 function init() {
378
379         try {
380
381                 disableContainerChildren("headlinesToolbar", true);
382
383                 if (!genericSanityCheck()) 
384                         return;
385
386                 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
387                 xmlhttp.onreadystatechange=backend_sanity_check_callback;
388                 xmlhttp.send(null);
389
390         } catch (e) {
391                 exception_error("init", e);
392         }
393 }
394
395 function init_second_stage() {
396
397         try {
398
399                 setCookie("ttrss_vf_actfeed", "");
400         
401                 updateFeedList(false, false);
402                 document.onkeydown = hotkey_handler;
403         
404                 var viewbox = document.getElementById("viewbox");                       
405                 var limitbox = document.getElementById("limitbox");
406
407                 dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
408                 dropboxSelect(limitbox, getCookie("ttrss_vf_limit"));
409
410                 daemon_enabled = getCookie("ttrss_vf_daemon");
411         
412         } catch (e) {
413                 exception_error("init_second_stage", e);
414         }
415 }
416
417 function quickMenuChange() {
418         var chooser = document.getElementById("quickMenuChooser");
419         var opid = chooser[chooser.selectedIndex].id;
420
421         chooser.selectedIndex = 0;
422         quickMenuGo(opid);
423 }
424
425 function quickMenuGo(opid) {
426
427
428         if (opid == "qmcPrefs") {
429                 gotoPreferences();
430         }
431
432         if (opid == "qmcSearch") {
433                 displayDlg("search", getActiveFeedId());
434                 return;
435         }
436
437         if (opid == "qmcAddFeed") {
438                 displayDlg("quickAddFeed");
439                 return;
440         }
441
442         if (opid == "qmcRemoveFeed") {
443                 var actid = getActiveFeedId();
444
445                 if (!actid) {
446                         notify("Please select some feed first.");
447                         return;
448                 }
449
450                 if (confirm("Remove current feed?")) {
451                         qfdDelete(actid);
452                 }
453         
454                 return;
455         }
456
457         if (opid == "qmcUpdateFeeds") {
458                 scheduleFeedUpdate(true);
459                 return;
460         }
461
462         if (opid == "qmcCatchupAll") {
463                 catchupAllFeeds();
464                 return;
465         }
466
467         if (opid == "qmcShowOnlyUnread") {
468                 toggleDispRead();
469                 return;
470         }
471
472         if (opid == "qmcAddFilter") {
473                 displayDlg("quickAddFilter", getActiveFeedId());
474         }
475
476 }
477
478 function qafAdd() {
479
480         if (!xmlhttp_ready(xmlhttp)) {
481                 printLockingError();
482                 return
483         }
484
485         var link = document.getElementById("qafInput");
486
487         if (link.value.length == 0) {
488                 notify("Missing feed URL.");
489         } else {
490                 notify("Adding feed...");
491         
492                 var cat = document.getElementById("qafCat");
493                 var cat_id = "";
494                 
495                 if (cat) {
496                         cat_id = cat[cat.selectedIndex].id;
497                 } else {
498                         cat_id = 0;
499                 }
500
501                 var feeds_doc = window.frames["feeds-frame"].document;
502
503                 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
504
505                 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=add&link=" +
506                         param_escape(link.value) + "&cid=" + param_escape(cat_id), true);
507                 xmlhttp.onreadystatechange=dlg_frefresh_callback;
508                 xmlhttp.send(null);
509
510                 link.value = "";
511
512         }
513 }
514
515 function qaddFilter() {
516
517         if (!xmlhttp_ready(xmlhttp)) {
518                 printLockingError();
519                 return
520         }
521
522         var regexp = document.getElementById("fadd_regexp");
523         var match = document.getElementById("fadd_match");
524         var feed = document.getElementById("fadd_feed");
525         var action = document.getElementById("fadd_action");
526
527         if (regexp.value.length == 0) {
528                 notify("Missing filter expression.");
529         } else {
530                 notify("Adding filter...");
531
532                 var v_match = match[match.selectedIndex].text;
533                 var feed_id = feed[feed.selectedIndex].id;
534                 var action_id = action[action.selectedIndex].id;
535
536                 xmlhttp.open("GET", "backend.php?op=pref-filters&quiet=1&subop=add&regexp=" +
537                         param_escape(regexp.value) + "&match=" + v_match +
538                         "&fid=" + param_escape(feed_id) + "&aid=" + param_escape(action_id), true);
539                         
540                 xmlhttp.onreadystatechange=dlg_submit_callback;
541                 xmlhttp.send(null);
542
543                 regexp.value = "";
544         }
545
546 }
547
548
549 function displayDlg(id, param) {
550
551         if (!xmlhttp_ready(xmlhttp)) {
552                 printLockingError();
553                 return
554         }
555
556         notify("");
557
558         xmlhttp.open("GET", "backend.php?op=dlg&id=" +
559                 param_escape(id) + "&param=" + param_escape(param), true);
560         xmlhttp.onreadystatechange=dlg_display_callback;
561         xmlhttp.send(null);
562
563         disableHotkeys();
564 }
565
566 function closeDlg() {
567         var dlg = document.getElementById("userDlgShadow");
568         dlg.style.display = "none";
569         enableHotkeys();
570 }
571
572 function qfdDelete(feed_id) {
573
574         notify("Removing feed...");
575
576         if (!xmlhttp_ready(xmlhttp)) {
577                 printLockingError();
578                 return
579         }
580
581 //      var feeds_doc = window.frames["feeds-frame"].document;
582 //      feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
583
584         _qfd_deleted_feed = feed_id;
585
586         xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
587         xmlhttp.onreadystatechange=dlg_frefresh_callback;
588         xmlhttp.send(null);
589 }
590
591
592 function allFeedsMenuChange() {
593         var chooser = document.getElementById("allFeedsChooser");
594
595         var opname = chooser[chooser.selectedIndex].text;
596
597         chooser.selectedIndex = 0;
598
599         if (opname == "Update") {
600                 scheduleFeedUpdate(true);
601                 return;
602         }
603
604         if (opname == "Mark as read") {
605                 catchupAllFeeds();
606                 return;
607         }
608
609         if (opname == "Show only unread") {
610                 toggleDispRead();
611                 return;
612         }
613
614 }
615
616 function updateFeedTitle(t) {
617         active_title_text = t;
618         updateTitle();
619 }
620
621 function toggleDispRead() {
622         try {
623
624                 if (!xmlhttp_ready(xmlhttp)) {
625                         printLockingError();
626                         return
627                 }
628
629                 var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
630
631                 hide_read_feeds = !hide_read_feeds;
632         
633                 var query = "backend.php?op=rpc&subop=setpref" +
634                         "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
635
636                 xmlhttp.open("GET", query);
637                 xmlhttp.onreadystatechange=hide_unread_callback;
638                 xmlhttp.send(null);
639                 
640         } catch (e) {
641                 exception_error("toggleDispRead", e);
642         }
643 }
644