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