]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
updates to debug mode
[tt-rss.git] / tt-rss.js
CommitLineData
1cd17194 1var xmlhttp = false;
76798ff3 2var total_unread = 0;
525116d4 3var first_run = true;
8143ae1f 4var display_tags = false;
806a3d14 5var global_unread = -1;
21703604 6var active_title_text = "";
21703604 7var current_subtitle = "";
c6784aea 8var daemon_enabled = false;
15da5cc1 9var _qfd_deleted_feed = 0;
2bf6e0a8 10var firsttime_update = true;
15da5cc1 11
1cd17194
AD
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.
16try {
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
27if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
28 xmlhttp = new XMLHttpRequest();
29}
30
8143ae1f
AD
31function 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
6de5d056 45function dlg_frefresh_callback() {
1cd17194 46 if (xmlhttp.readyState == 4) {
a24f525c 47 notify(xmlhttp.responseText);
e2f8f7b4 48 updateFeedList(false, false);
15da5cc1
AD
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 }
f84a97a3 55 closeDlg();
c0e5a40e 56 }
1cd17194 57}
e2f8f7b4 58
a24f525c
AD
59function dlg_submit_callback() {
60 if (xmlhttp.readyState == 4) {
61 notify(xmlhttp.responseText);
62 closeDlg();
63 }
64}
65
66function dlg_display_callback() {
f84a97a3
AD
67 if (xmlhttp.readyState == 4) {
68 var dlg = document.getElementById("userDlg");
76332f3c 69 var dlg_s = document.getElementById("userDlgShadow");
f84a97a3
AD
70
71 dlg.innerHTML = xmlhttp.responseText;
76332f3c 72 dlg_s.style.display = "block";
f84a97a3
AD
73 }
74}
1a66d16e 75
7f123cda
AD
76function 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
8158c57a 101function refetch_callback() {
29fb8c70 102 if (xmlhttp.readyState == 4) {
7719618b 103 try {
310da49d 104
7719618b
AD
105 if (!xmlhttp.responseXML) {
106 notify("refetch_callback: backend did not return valid XML");
107 return;
108 }
109
110 var reply = xmlhttp.responseXML.firstChild;
262bd8ea 111
7719618b
AD
112 if (!reply) {
113 notify("refetch_callback: backend did not return expected XML object");
e2cb4c6d 114 updateTitle("");
7719618b
AD
115 return;
116 }
262bd8ea 117
7719618b
AD
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;
1a6a9555 125
3bdb368b 126 parse_counters(reply, f_document, window);
c6784aea 127
0ee1d1a0
AD
128 debug("refetch_callback: done");
129
c6784aea 130 if (!daemon_enabled) {
c6784aea 131 notify("All feeds updated.");
5a180505 132 updateTitle("");
4c059b7b
AD
133 } else {
134 notify("");
c6784aea 135 }
7719618b
AD
136 } catch (e) {
137 exception_error("refetch_callback", e);
e2cb4c6d 138 updateTitle("");
7719618b 139 }
090e250b
AD
140 }
141}
1a66d16e 142
295f9b42
AD
143function backend_sanity_check_callback() {
144
145 if (xmlhttp.readyState == 4) {
295f9b42 146
7719618b
AD
147 try {
148
149 if (!xmlhttp.responseXML) {
91d612b3 150 fatalError(3, "D001;" + xmlhttp.responseText);
7719618b
AD
151 return;
152 }
295f9b42 153
7719618b
AD
154 var reply = xmlhttp.responseXML.firstChild;
155
156 if (!reply) {
91d612b3 157 fatalError(3, "D002;" + xmlhttp.responseText);
7719618b
AD
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
0ee1d1a0
AD
167 debug("sanity check ok");
168
7719618b 169 init_second_stage();
295f9b42 170
7719618b
AD
171 } catch (e) {
172 exception_error("backend_sanity_check_callback", e);
173 }
295f9b42
AD
174 }
175}
176
cb246176 177function scheduleFeedUpdate(force) {
525116d4 178
c6784aea
AD
179 if (!daemon_enabled) {
180 notify("Updating feeds, please wait.");
181 updateTitle("Updating");
182 }
55193822 183
cb246176
AD
184 var query_str = "backend.php?op=rpc&subop=";
185
186 if (force) {
c3b81db0 187 query_str = query_str + "forceUpdateAllFeeds";
cb246176 188 } else {
c3b81db0 189 query_str = query_str + "updateAllFeeds";
cb246176 190 }
525116d4 191
9826bd2e
AD
192 var omode;
193
b6104dee 194 if (firsttime_update && !navigator.userAgent.match("Opera")) {
2bf6e0a8
AD
195 firsttime_update = false;
196 omode = "T";
9826bd2e 197 } else {
2bf6e0a8
AD
198 if (display_tags) {
199 omode = "t";
200 } else {
201 omode = "flc";
202 }
9826bd2e 203 }
2bf6e0a8 204
9826bd2e 205 query_str = query_str + "&omode=" + omode;
78ea1de0 206 query_str = query_str + "&uctr=" + global_unread;
9826bd2e 207
0ee1d1a0 208 debug("in scheduleFeedUpdate");
2bf6e0a8 209
29fb8c70
AD
210 if (xmlhttp_ready(xmlhttp)) {
211 xmlhttp.open("GET", query_str, true);
212 xmlhttp.onreadystatechange=refetch_callback;
213 xmlhttp.send(null);
525116d4
AD
214 } else {
215 printLockingError();
c0e5a40e 216 }
525116d4 217}
1cd17194 218
525116d4 219function updateFeedList(silent, fetch) {
c0e5a40e 220
1a66d16e
AD
221// if (silent != true) {
222// notify("Loading feed list...");
223// }
82baad4a 224
331900c6
AD
225 var query_str = "backend.php?op=feeds";
226
8143ae1f
AD
227 if (display_tags) {
228 query_str = query_str + "&tags=1";
229 }
230
86741347
AD
231 if (getActiveFeedId()) {
232 query_str = query_str + "&actid=" + getActiveFeedId();
175847de
AD
233 }
234
59b8192f
AD
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
1a66d16e 241 if (fetch) query_str = query_str + "&fetch=yes";
e1123aee 242
1a66d16e 243 var feeds_frame = document.getElementById("feeds-frame");
e1123aee 244
1a66d16e
AD
245 feeds_frame.src = query_str;
246}
175847de 247
476cac42 248function catchupAllFeeds() {
076682aa 249
476cac42
AD
250 var query_str = "backend.php?op=feeds&subop=catchupAll";
251
252 notify("Marking all feeds as read...");
253
1a66d16e
AD
254 var feeds_frame = document.getElementById("feeds-frame");
255
256 feeds_frame.src = query_str;
476cac42 257
fc69e641 258 global_unread = 0;
21703604 259 updateTitle("");
fc69e641 260
476cac42 261}
1cd17194 262
f0601b87 263function viewCurrentFeed(skip, subop) {
1a66d16e 264
86741347
AD
265 if (getActiveFeedId()) {
266 viewfeed(getActiveFeedId(), skip, subop);
033e47e0
AD
267 } else {
268 disableContainerChildren("headlinesToolbar", false, document);
269 viewfeed(-1, skip, subop); // FIXME
f0601b87
AD
270 }
271}
272
476cac42 273function viewfeed(feed, skip, subop) {
db8d6f67
AD
274 var f = window.frames["feeds-frame"];
275 f.viewfeed(feed, skip, subop);
9cfc649a
AD
276}
277
40d13c28 278function timeout() {
05732aa0 279 scheduleFeedUpdate(false);
f5de0d8d
AD
280
281 var refresh_time = getCookie('ttrss_vf_refresh');
282
283 if (!refresh_time) refresh_time = 600;
284
285 setTimeout("timeout()", refresh_time*1000);
ac53063a
AD
286}
287
c374a3fe 288function resetSearch() {
64c620ce
AD
289 var searchbox = document.getElementById("searchbox")
290
86741347 291 if (searchbox.value != "" && getActiveFeedId()) {
64c620ce 292 searchbox.value = "";
86741347 293 viewfeed(getActiveFeedId(), 0, "");
ac43eba1 294 }
c374a3fe 295}
ac53063a 296
f0601b87 297function search() {
49b7cbd3 298 closeDlg();
4ce19859 299 viewCurrentFeed(0, "");
76798ff3 300}
1cd17194 301
13ad9102
AD
302function localPiggieFunction(enable) {
303 if (enable) {
304 var query_str = "backend.php?op=feeds&subop=piggie";
305
c0e5a40e 306 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
307
308 xmlhttp.open("GET", query_str, true);
309 xmlhttp.onreadystatechange=feedlist_callback;
310 xmlhttp.send(null);
311 }
312 }
313}
314
9cfc649a
AD
315function localHotkeyHandler(keycode) {
316
b623b3ed 317 if (keycode == 82) { // r
c3a8d71a
AD
318 return scheduleFeedUpdate(true);
319 }
320
b623b3ed
AD
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();
c3a8d71a
AD
329 }
330
7b433d8c
AD
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
c3a8d71a
AD
346// notify("KC: " + keycode);
347
9cfc649a
AD
348}
349
806a3d14
AD
350// if argument is undefined, current subtitle is not updated
351// use blank string to clear subtitle
fc69e641
AD
352function updateTitle(s) {
353 var tmp = "Tiny Tiny RSS";
21703604 354
5a494a0b 355 if (s != undefined) {
21703604
AD
356 current_subtitle = s;
357 }
358
fc69e641
AD
359 if (global_unread > 0) {
360 tmp = tmp + " (" + global_unread + ")";
361 }
362
5a494a0b 363 if (current_subtitle) {
21703604 364 tmp = tmp + " - " + current_subtitle;
fc69e641 365 }
21703604
AD
366
367 if (active_title_text.length > 0) {
368 tmp = tmp + " > " + active_title_text;
369 }
370
fc69e641
AD
371 document.title = tmp;
372}
373
22a93ad8 374function genericSanityCheck() {
ac43eba1 375
295f9b42
AD
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
22a93ad8
AD
384 return true;
385}
386
387function init() {
388
7719618b 389 try {
fe2f1970 390
97dcd654
AD
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
7719618b 396 disableContainerChildren("headlinesToolbar", true);
70830c87 397
7719618b
AD
398 if (!genericSanityCheck())
399 return;
ac43eba1 400
0ee1d1a0
AD
401 if (getURLParam('debug')) {
402 document.getElementById('debug_output').style.display = 'block';
403 debug('debug mode activated');
404 }
405
7719618b
AD
406 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
407 xmlhttp.onreadystatechange=backend_sanity_check_callback;
408 xmlhttp.send(null);
47179952 409
7719618b
AD
410 } catch (e) {
411 exception_error("init", e);
a8d28f48 412 }
7719618b 413}
86741347 414
1de2b92c
AD
415function 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
7719618b 424function init_second_stage() {
295f9b42 425
7719618b 426 try {
2f587484 427
7719618b
AD
428 setCookie("ttrss_vf_actfeed", "");
429
430 updateFeedList(false, false);
431 document.onkeydown = hotkey_handler;
432
4bbd5aec
AD
433 var viewbox = document.getElementById("viewbox");
434 var limitbox = document.getElementById("limitbox");
1b0809ae
AD
435
436 dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
4bbd5aec 437 dropboxSelect(limitbox, getCookie("ttrss_vf_limit"));
c6784aea
AD
438
439 daemon_enabled = getCookie("ttrss_vf_daemon");
5f136c9a 440
1de2b92c
AD
441 // FIXME should be callled after window resize
442
5f136c9a 443 if (navigator.userAgent.match("Opera")) {
1de2b92c 444 resize_feeds_frame();
4cb6f375 445
77d51985 446/* // fix headlines frame height for Opera
4cb6f375
AD
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";
77d51985 452 c.style.height = c.scrollHeight - nh + "px"; */
4cb6f375 453
5f136c9a 454 }
0ee1d1a0
AD
455
456 debug("second stage ok");
7719618b 457
7719618b
AD
458 } catch (e) {
459 exception_error("init_second_stage", e);
2f587484 460 }
1cd17194 461}
ac43eba1 462
c09ec856 463function quickMenuChange() {
cbe45fa8
AD
464 var chooser = document.getElementById("quickMenuChooser");
465 var opid = chooser[chooser.selectedIndex].id;
e2f8f7b4 466
c09ec856
AD
467 chooser.selectedIndex = 0;
468 quickMenuGo(opid);
469}
470
471function quickMenuGo(opid) {
472
473
cbe45fa8 474 if (opid == "qmcPrefs") {
e2f8f7b4
AD
475 gotoPreferences();
476 }
477
27da8b1e 478 if (opid == "qmcSearch") {
49b7cbd3 479 displayDlg("search", getActiveFeedId());
033e47e0
AD
480 return;
481 }
482
cbe45fa8 483 if (opid == "qmcAddFeed") {
f84a97a3 484 displayDlg("quickAddFeed");
6de5d056
AD
485 return;
486 }
487
cbe45fa8 488 if (opid == "qmcRemoveFeed") {
6de5d056
AD
489 var actid = getActiveFeedId();
490
491 if (!actid) {
492 notify("Please select some feed first.");
493 return;
494 }
69668465
AD
495
496 if (confirm("Remove current feed?")) {
497 qfdDelete(actid);
498 }
6de5d056 499
6de5d056 500 return;
e2f8f7b4 501 }
7a991cac 502
cbe45fa8 503 if (opid == "qmcUpdateFeeds") {
7a991cac
AD
504 scheduleFeedUpdate(true);
505 return;
506 }
507
cbe45fa8 508 if (opid == "qmcCatchupAll") {
7a991cac
AD
509 catchupAllFeeds();
510 return;
511 }
512
cbe45fa8 513 if (opid == "qmcShowOnlyUnread") {
7a991cac
AD
514 toggleDispRead();
515 return;
516 }
517
a24f525c
AD
518 if (opid == "qmcAddFilter") {
519 displayDlg("quickAddFilter", getActiveFeedId());
520 }
521
e2f8f7b4
AD
522}
523
524function qafAdd() {
ac43eba1 525
e2f8f7b4
AD
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...");
15da5cc1
AD
537
538 var cat = document.getElementById("qafCat");
539 var cat_id = "";
e2f8f7b4 540
15da5cc1
AD
541 if (cat) {
542 cat_id = cat[cat.selectedIndex].id;
543 } else {
544 cat_id = 0;
545 }
546
e2f8f7b4
AD
547 var feeds_doc = window.frames["feeds-frame"].document;
548
549 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
550
a24f525c 551 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=add&link=" +
15da5cc1 552 param_escape(link.value) + "&cid=" + param_escape(cat_id), true);
6de5d056 553 xmlhttp.onreadystatechange=dlg_frefresh_callback;
e2f8f7b4
AD
554 xmlhttp.send(null);
555
556 link.value = "";
557
558 }
f84a97a3
AD
559}
560
a24f525c
AD
561function 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
6de5d056 595function displayDlg(id, param) {
e2f8f7b4 596
7f123cda
AD
597 if (!xmlhttp_ready(xmlhttp)) {
598 printLockingError();
599 return
600 }
601
40d601c5
AD
602 notify("");
603
f84a97a3 604 xmlhttp.open("GET", "backend.php?op=dlg&id=" +
6de5d056 605 param_escape(id) + "&param=" + param_escape(param), true);
a24f525c 606 xmlhttp.onreadystatechange=dlg_display_callback;
f84a97a3 607 xmlhttp.send(null);
e2f8f7b4 608
ccedfddd 609 disableHotkeys();
e2f8f7b4 610}
f84a97a3
AD
611
612function closeDlg() {
76332f3c 613 var dlg = document.getElementById("userDlgShadow");
f84a97a3 614 dlg.style.display = "none";
ccedfddd 615 enableHotkeys();
f84a97a3
AD
616}
617
6de5d056
AD
618function qfdDelete(feed_id) {
619
620 notify("Removing feed...");
621
7f123cda
AD
622 if (!xmlhttp_ready(xmlhttp)) {
623 printLockingError();
624 return
625 }
626
69668465
AD
627// var feeds_doc = window.frames["feeds-frame"].document;
628// feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
6de5d056 629
15da5cc1
AD
630 _qfd_deleted_feed = feed_id;
631
69668465 632 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
6de5d056
AD
633 xmlhttp.onreadystatechange=dlg_frefresh_callback;
634 xmlhttp.send(null);
6de5d056 635}
033e47e0 636
3745788e 637
c09ec856 638function allFeedsMenuChange() {
3745788e
AD
639 var chooser = document.getElementById("allFeedsChooser");
640
641 var opname = chooser[chooser.selectedIndex].text;
642
c09ec856
AD
643 chooser.selectedIndex = 0;
644
3745788e
AD
645 if (opname == "Update") {
646 scheduleFeedUpdate(true);
647 return;
648 }
649
650 if (opname == "Mark as read") {
651 catchupAllFeeds();
652 return;
653 }
654
bdf7f1bc 655 if (opname == "Show only unread") {
3745788e
AD
656 toggleDispRead();
657 return;
658 }
659
660}
661
21703604
AD
662function updateFeedTitle(t) {
663 active_title_text = t;
664 updateTitle();
665}
666
3745788e 667function toggleDispRead() {
7f123cda 668 try {
3745788e 669
7f123cda
AD
670 if (!xmlhttp_ready(xmlhttp)) {
671 printLockingError();
672 return
673 }
3745788e 674
7f123cda 675 var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
3745788e 676
7f123cda
AD
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);
3745788e 681
7f123cda
AD
682 xmlhttp.open("GET", query);
683 xmlhttp.onreadystatechange=hide_unread_callback;
684 xmlhttp.send(null);
685
686 } catch (e) {
687 exception_error("toggleDispRead", e);
3745788e 688 }
3745788e 689}
295f9b42 690
0ee1d1a0
AD
691function debug(msg) {
692 var c = document.getElementById('debug_output');
693 if (c && c.style.display == "block") {
694 while (c.firstChild != 'undefined' && c.childNodes.length > 15) {
695 c.removeChild(c.firstChild);
696 }
697
698 var d = new Date();
699 var ts = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
700 c.innerHTML = c.innerHTML + "<li>[" + ts + "] " + msg + "</li>";
701 }
702}