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