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