]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
cdm tweaks, misc fixes
[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;
0d51e25d 9var daemon_refresh_only = false;
15da5cc1 10var _qfd_deleted_feed = 0;
2bf6e0a8 11var firsttime_update = true;
c441662f 12var last_refetch = 0;
76b4eae1 13var cookie_lifetime = 0;
33d13e72 14var active_feed_id = 0;
0a6c4846 15var active_feed_is_cat = false;
9e397d0f 16var number_of_feeds = 0;
15da5cc1 17
a58069db 18var xmlhttp = Ajax.getTransport();
abbe0154 19var xmlhttp_ctr = Ajax.getTransport();
1cd17194 20
1035fcec 21var init_params = new Object();
3ac2b520 22
1dc8dba0
AD
23var op_history = new Array();
24
0b126ac2
AD
25function tagsAreDisplayed() {
26 return display_tags;
27}
28
8143ae1f
AD
29function toggleTags() {
30 display_tags = !display_tags;
31
32 var p = document.getElementById("dispSwitchPrompt");
33
34 if (display_tags) {
35 p.innerHTML = "display feeds";
36 } else {
37 p.innerHTML = "display tags";
38 }
39
807f452a
AD
40 notify("Loading, please wait...");
41
8143ae1f
AD
42 updateFeedList();
43}
44
6de5d056 45function dlg_frefresh_callback() {
abbe0154 46 if (xmlhttp.readyState == 4) {
a58d997c 47// notify(xmlhttp.responseText);
abbe0154
AD
48
49 if (getActiveFeedId() == _qfd_deleted_feed) {
50 var h = document.getElementById("headlines-frame");
51 if (h) {
52 h.innerHTML = "<div class='whiteBox'>No feed selected.</div>";
53 }
54 }
55
0e9dd1ba 56 setTimeout('updateFeedList(false, false)', 50);
eff4997c 57 closeInfoBox();
c0e5a40e 58 }
1cd17194 59}
e2f8f7b4 60
8158c57a 61function refetch_callback() {
abbe0154 62 if (xmlhttp_ctr.readyState == 4) {
7719618b 63 try {
310da49d 64
c441662f
AD
65 var date = new Date();
66
67 last_refetch = date.getTime() / 1000;
68
3ac2f3c7 69 parse_counters_reply(xmlhttp_ctr, true);
71ad883b 70
0ee1d1a0
AD
71 debug("refetch_callback: done");
72
0d51e25d 73 if (!daemon_enabled && !daemon_refresh_only) {
c6784aea 74 notify("All feeds updated.");
5a180505 75 updateTitle("");
4c059b7b
AD
76 } else {
77 notify("");
c6784aea 78 }
7719618b
AD
79 } catch (e) {
80 exception_error("refetch_callback", e);
e2cb4c6d 81 updateTitle("");
7719618b 82 }
090e250b
AD
83 }
84}
1a66d16e 85
295f9b42
AD
86function backend_sanity_check_callback() {
87
88 if (xmlhttp.readyState == 4) {
295f9b42 89
7719618b
AD
90 try {
91
92 if (!xmlhttp.responseXML) {
462a192b 93 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
7719618b
AD
94 return;
95 }
295f9b42 96
3ac2b520 97 var reply = xmlhttp.responseXML.firstChild.firstChild;
7719618b
AD
98
99 if (!reply) {
af106b0e 100 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
7719618b
AD
101 return;
102 }
103
104 var error_code = reply.getAttribute("error-code");
105
106 if (error_code && error_code != 0) {
af106b0e 107 return fatalError(error_code, reply.getAttribute("error-msg"));
7719618b
AD
108 }
109
0ee1d1a0
AD
110 debug("sanity check ok");
111
3ac2b520
AD
112 var params = reply.nextSibling;
113
114 if (params) {
115 debug('reading init-params...');
116 var param = params.firstChild;
117
118 while (param) {
119 var k = param.getAttribute("key");
120 var v = param.getAttribute("value");
121 debug(k + " => " + v);
122 init_params[k] = v;
123 param = param.nextSibling;
124 }
125 }
126
7719618b 127 init_second_stage();
295f9b42 128
7719618b
AD
129 } catch (e) {
130 exception_error("backend_sanity_check_callback", e);
131 }
295f9b42
AD
132 }
133}
134
cb246176 135function scheduleFeedUpdate(force) {
525116d4 136
0d51e25d 137 if (!daemon_enabled && !daemon_refresh_only) {
4d4200a8 138 notify("Updating feeds, please wait.", true);
c6784aea
AD
139 updateTitle("Updating");
140 }
55193822 141
cb246176
AD
142 var query_str = "backend.php?op=rpc&subop=";
143
144 if (force) {
c3b81db0 145 query_str = query_str + "forceUpdateAllFeeds";
cb246176 146 } else {
c3b81db0 147 query_str = query_str + "updateAllFeeds";
cb246176 148 }
525116d4 149
9826bd2e
AD
150 var omode;
151
b6104dee 152 if (firsttime_update && !navigator.userAgent.match("Opera")) {
2bf6e0a8
AD
153 firsttime_update = false;
154 omode = "T";
9826bd2e 155 } else {
2bf6e0a8 156 if (display_tags) {
cf4d339c 157 omode = "tl";
2bf6e0a8
AD
158 } else {
159 omode = "flc";
160 }
9826bd2e 161 }
2bf6e0a8 162
9826bd2e 163 query_str = query_str + "&omode=" + omode;
78ea1de0 164 query_str = query_str + "&uctr=" + global_unread;
9826bd2e 165
0ee1d1a0 166 debug("in scheduleFeedUpdate");
2bf6e0a8 167
c441662f
AD
168 var date = new Date();
169
abbe0154 170 if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
a7565293 171 debug("<b>xmlhttp seems to be stuck, aborting</b>");
abbe0154 172 xmlhttp_ctr.abort();
c441662f
AD
173 }
174
52db9978
AD
175 debug("REFETCH query: " + query_str);
176
abbe0154
AD
177 if (xmlhttp_ready(xmlhttp_ctr)) {
178 xmlhttp_ctr.open("GET", query_str, true);
179 xmlhttp_ctr.onreadystatechange=refetch_callback;
180 xmlhttp_ctr.send(null);
525116d4 181 } else {
abbe0154 182 debug("xmlhttp_ctr busy");
bed0f18f 183 //printLockingError();
c0e5a40e 184 }
525116d4 185}
1cd17194 186
525116d4 187function updateFeedList(silent, fetch) {
c0e5a40e 188
1a66d16e
AD
189// if (silent != true) {
190// notify("Loading feed list...");
191// }
82baad4a 192
cf4d339c
AD
193 debug("<b>updateFeedList</b>");
194
331900c6
AD
195 var query_str = "backend.php?op=feeds";
196
8143ae1f
AD
197 if (display_tags) {
198 query_str = query_str + "&tags=1";
199 }
200
5c365f60 201 if (getActiveFeedId() && !activeFeedIsCat()) {
86741347 202 query_str = query_str + "&actid=" + getActiveFeedId();
175847de
AD
203 }
204
59b8192f
AD
205 if (navigator.userAgent.match("Opera")) {
206 var date = new Date();
207 var timestamp = Math.round(date.getTime() / 1000);
208 query_str = query_str + "&ts=" + timestamp
209 }
210
1a66d16e 211 if (fetch) query_str = query_str + "&fetch=yes";
e1123aee 212
6b4163cb
AD
213// var feeds_frame = document.getElementById("feeds-frame");
214// feeds_frame.src = query_str;
215
0e9dd1ba 216 debug("updateFeedList Q=" + query_str);
6b4163cb
AD
217
218 if (xmlhttp_ready(xmlhttp)) {
219 xmlhttp.open("GET", query_str, true);
220 xmlhttp.onreadystatechange=feedlist_callback;
221 xmlhttp.send(null);
222 } else {
223 debug("xmlhttp busy");
224 //printLockingError();
225 }
e1123aee 226
1a66d16e 227}
175847de 228
476cac42 229function catchupAllFeeds() {
076682aa 230
476cac42
AD
231 var query_str = "backend.php?op=feeds&subop=catchupAll";
232
233 notify("Marking all feeds as read...");
234
ca2f46a7 235 debug("catchupAllFeeds Q=" + query_str);
1a66d16e 236
ca2f46a7
AD
237 if (xmlhttp_ready(xmlhttp)) {
238 xmlhttp.open("GET", query_str, true);
239 xmlhttp.onreadystatechange=feedlist_callback;
240 xmlhttp.send(null);
241 } else {
242 debug("xmlhttp busy");
243 //printLockingError();
244 }
476cac42 245
fc69e641 246 global_unread = 0;
21703604 247 updateTitle("");
fc69e641 248
476cac42 249}
1cd17194 250
767e2486 251function viewCurrentFeed(subop) {
1a66d16e 252
ec6e2fd3
AD
253// if (getActiveFeedId()) {
254 if (getActiveFeedId() != undefined) {
767e2486 255 viewfeed(getActiveFeedId(), subop);
033e47e0
AD
256 } else {
257 disableContainerChildren("headlinesToolbar", false, document);
ec6e2fd3 258// viewfeed(-1, subop); // FIXME
f0601b87 259 }
164f4738 260 return false; // block unneeded form submits
f0601b87
AD
261}
262
767e2486 263function viewfeed(feed, subop) {
db8d6f67 264 var f = window.frames["feeds-frame"];
767e2486 265 f.viewfeed(feed, subop);
9cfc649a
AD
266}
267
40d13c28 268function timeout() {
05732aa0 269 scheduleFeedUpdate(false);
f5de0d8d 270
3ac2b520 271 var refresh_time = getInitParam("feeds_frame_refresh");
f5de0d8d 272
3ac2b520 273 if (!refresh_time) refresh_time = 600;
f5de0d8d
AD
274
275 setTimeout("timeout()", refresh_time*1000);
ac53063a
AD
276}
277
c374a3fe 278function resetSearch() {
64c620ce
AD
279 var searchbox = document.getElementById("searchbox")
280
86741347 281 if (searchbox.value != "" && getActiveFeedId()) {
64c620ce 282 searchbox.value = "";
767e2486 283 viewfeed(getActiveFeedId(), "");
ac43eba1 284 }
c374a3fe 285}
ac53063a 286
86b682ce
AD
287function searchCancel() {
288 closeInfoBox(true);
289}
290
f0601b87 291function search() {
eff4997c 292 closeInfoBox();
4ce19859 293 viewCurrentFeed(0, "");
76798ff3 294}
1cd17194 295
13ad9102
AD
296function localPiggieFunction(enable) {
297 if (enable) {
298 var query_str = "backend.php?op=feeds&subop=piggie";
299
c0e5a40e 300 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
301
302 xmlhttp.open("GET", query_str, true);
303 xmlhttp.onreadystatechange=feedlist_callback;
304 xmlhttp.send(null);
305 }
306 }
307}
308
806a3d14
AD
309// if argument is undefined, current subtitle is not updated
310// use blank string to clear subtitle
fc69e641
AD
311function updateTitle(s) {
312 var tmp = "Tiny Tiny RSS";
21703604 313
5a494a0b 314 if (s != undefined) {
21703604
AD
315 current_subtitle = s;
316 }
317
fc69e641
AD
318 if (global_unread > 0) {
319 tmp = tmp + " (" + global_unread + ")";
320 }
321
5a494a0b 322 if (current_subtitle) {
21703604 323 tmp = tmp + " - " + current_subtitle;
fc69e641 324 }
21703604
AD
325
326 if (active_title_text.length > 0) {
327 tmp = tmp + " > " + active_title_text;
328 }
329
fc69e641
AD
330 document.title = tmp;
331}
332
22a93ad8 333function genericSanityCheck() {
ac43eba1 334
295f9b42
AD
335 if (!xmlhttp) fatalError(1);
336
337 setCookie("ttrss_vf_test", "TEST");
338
339 if (getCookie("ttrss_vf_test") != "TEST") {
340 fatalError(2);
341 }
342
22a93ad8
AD
343 return true;
344}
345
346function init() {
347
7719618b 348 try {
fe2f1970 349
97dcd654
AD
350 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
351
352 if (arguments.callee.done) return;
353 arguments.callee.done = true;
354
7719618b 355 disableContainerChildren("headlinesToolbar", true);
70830c87 356
86b682ce
AD
357 Form.disable("main_toolbar_form");
358
7719618b
AD
359 if (!genericSanityCheck())
360 return;
ac43eba1 361
0ee1d1a0
AD
362 if (getURLParam('debug')) {
363 document.getElementById('debug_output').style.display = 'block';
364 debug('debug mode activated');
365 }
366
4220d6b0
AD
367 var params = "&ua=" + param_escape(navigator.userAgent);
368
369 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck" + params, true);
7719618b
AD
370 xmlhttp.onreadystatechange=backend_sanity_check_callback;
371 xmlhttp.send(null);
47179952 372
7719618b
AD
373 } catch (e) {
374 exception_error("init", e);
a8d28f48 375 }
7719618b 376}
86741347 377
7719618b 378function init_second_stage() {
295f9b42 379
7719618b 380 try {
2f587484 381
76b4eae1
AD
382 cookie_lifetime = getCookie("ttrss_cltime");
383
384 delCookie("ttrss_vf_test");
7719618b 385
7719618b 386 document.onkeydown = hotkey_handler;
1b0809ae 387
f31673f7
AD
388 var tb = parent.document.forms["main_toolbar_form"];
389
9f10e152
AD
390// dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
391// dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
c6784aea 392
0d51e25d
AD
393 daemon_enabled = getInitParam("daemon_enabled") == 1;
394 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
5f136c9a 395
0e9dd1ba 396 setTimeout('updateFeedList(false, false)', 50);
0ee1d1a0
AD
397
398 debug("second stage ok");
7719618b 399
7719618b
AD
400 } catch (e) {
401 exception_error("init_second_stage", e);
2f587484 402 }
1cd17194 403}
ac43eba1 404
c09ec856 405function quickMenuChange() {
cbe45fa8 406 var chooser = document.getElementById("quickMenuChooser");
86b682ce 407 var opid = chooser[chooser.selectedIndex].value;
e2f8f7b4 408
c09ec856
AD
409 chooser.selectedIndex = 0;
410 quickMenuGo(opid);
411}
412
413function quickMenuGo(opid) {
bb3423cf 414 try {
c09ec856 415
bb3423cf
AD
416 if (opid == "qmcPrefs") {
417 gotoPreferences();
418 }
419
420 if (opid == "qmcSearch") {
0a6c4846 421 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
6de5d056
AD
422 return;
423 }
bb3423cf
AD
424
425 if (opid == "qmcAddFeed") {
426 displayDlg("quickAddFeed");
427 return;
69668465 428 }
7086277c
AD
429
430 if (opid == "qmcEditFeed") {
431 editFeedDlg(getActiveFeedId());
432 }
6de5d056 433
bb3423cf
AD
434 if (opid == "qmcRemoveFeed") {
435 var actid = getActiveFeedId();
436
5c365f60 437 if (!actid || activeFeedIsCat()) {
0530ddd8 438 alert("Please select some feed first.");
bb3423cf
AD
439 return;
440 }
64a2875d
AD
441
442 var fn = getFeedName(actid);
bb3423cf 443
64a2875d 444 if (confirm("Unsubscribe from " + fn + "?")) {
bb3423cf
AD
445 qfdDelete(actid);
446 }
447
448 return;
449 }
450
451 if (opid == "qmcUpdateFeeds") {
452 scheduleFeedUpdate(true);
453 return;
454 }
455
456 if (opid == "qmcCatchupAll") {
457 catchupAllFeeds();
458 return;
459 }
460
461 if (opid == "qmcShowOnlyUnread") {
462 toggleDispRead();
463 return;
464 }
465
466 if (opid == "qmcAddFilter") {
467 displayDlg("quickAddFilter", getActiveFeedId());
468 }
469 } catch (e) {
470 exception_error("quickMenuGo", e);
a24f525c 471 }
e2f8f7b4
AD
472}
473
6de5d056
AD
474function qfdDelete(feed_id) {
475
476 notify("Removing feed...");
477
7f123cda
AD
478 if (!xmlhttp_ready(xmlhttp)) {
479 printLockingError();
480 return
481 }
482
15da5cc1
AD
483 _qfd_deleted_feed = feed_id;
484
69668465 485 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
6de5d056
AD
486 xmlhttp.onreadystatechange=dlg_frefresh_callback;
487 xmlhttp.send(null);
7bc4f251
AD
488
489 return false;
6de5d056 490}
033e47e0 491
3745788e 492
21703604
AD
493function updateFeedTitle(t) {
494 active_title_text = t;
495 updateTitle();
496}
497
3745788e 498function toggleDispRead() {
7f123cda 499 try {
3745788e 500
7f123cda
AD
501 if (!xmlhttp_ready(xmlhttp)) {
502 printLockingError();
503 return
504 }
3745788e 505
e8bd0da9 506 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
3745788e 507
7f123cda 508 hide_read_feeds = !hide_read_feeds;
e8bd0da9
AD
509
510 debug("toggle_disp_read => " + hide_read_feeds);
511
512 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
513
7f123cda
AD
514 var query = "backend.php?op=rpc&subop=setpref" +
515 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
3745788e 516
e8bd0da9
AD
517 storeInitParam("hide_read_feeds", hide_read_feeds, true);
518
519 new Ajax.Request(query);
7f123cda
AD
520
521 } catch (e) {
522 exception_error("toggleDispRead", e);
3745788e 523 }
3745788e 524}
295f9b42 525
71ad883b 526function parse_runtime_info(elem) {
4724a093
AD
527 if (!elem) {
528 debug("parse_runtime_info: elem is null, aborting");
529 return;
530 }
531
71ad883b
AD
532 var param = elem.firstChild;
533
52db9978 534 debug("parse_runtime_info: " + param);
1cb7492d 535
71ad883b
AD
536 while (param) {
537 var k = param.getAttribute("key");
538 var v = param.getAttribute("value");
3ac2b520 539
1cb7492d
AD
540 debug("RI: " + k + " => " + v);
541
71ad883b
AD
542 var w = document.getElementById("noDaemonWarning");
543
544 if (w) {
545 if (k == "daemon_is_running" && v != 1) {
546 w.style.display = "block";
547 } else {
548 w.style.display = "none";
549 }
550 }
551 param = param.nextSibling;
552 }
553}
fce24838
AD
554
555function catchupCurrentFeed() {
556
234e467c 557 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
fce24838 558
234e467c
AD
559 var str = "Mark all articles in " + fn + " as read?";
560
561/* if (active_feed_is_cat) {
562 str = "Mark all articles in this category as read?";
563 } */
564
f6d6e22f 565 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
28de3732 566 return viewCurrentFeed('MarkAllRead')
fce24838
AD
567 }
568}
88040f57 569
461766f3
AD
570function userSwitch() {
571 var chooser = document.getElementById("userSwitch");
572 var user = chooser[chooser.selectedIndex].value;
573 window.location = "tt-rss.php?swu=" + user;
574}
6e6504bc 575
7086277c
AD
576function editFeedDlg(feed) {
577
729bafaa
AD
578 disableHotkeys();
579
7086277c
AD
580 if (!feed) {
581 alert("Please select some feed first.");
582 return;
583 }
584
92f3bcae
AD
585 if (feed <= 0 || active_feed_is_cat || tagsAreDisplayed()) {
586 alert("You can't edit this kind of feed.");
587 return;
588 }
589
7086277c
AD
590 if (xmlhttp_ready(xmlhttp)) {
591 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
592 param_escape(feed), true);
593 xmlhttp.onreadystatechange=infobox_callback;
594 xmlhttp.send(null);
595 } else {
596 printLockingError();
597 }
598}
599
600/* this functions duplicate those of prefs.js feed editor, with
601 some differences because there is no feedlist */
602
603function feedEditCancel() {
604 closeInfoBox();
605 return false;
606}
607
608function feedEditSave() {
609
610 try {
611
612 if (!xmlhttp_ready(xmlhttp)) {
613 printLockingError();
614 return
615 }
616
617 // FIXME: add parameter validation
618
619 var query = Form.serialize("edit_feed_form");
620
621 notify("Saving feed...");
622
623 xmlhttp.open("POST", "backend.php", true);
624 xmlhttp.onreadystatechange=dlg_frefresh_callback;
625 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
626 xmlhttp.send(query);
627
628 closeInfoBox();
629
630 return false;
631
632 } catch (e) {
633 exception_error("feedEditSave (main)", e);
634 }
635}
6e6504bc 636
1dc8dba0
AD
637function localHotkeyHandler(e) {
638
639 var keycode;
640
641 if (window.event) {
642 keycode = window.event.keyCode;
643 } else if (e) {
644 keycode = e.which;
645 }
646
647 var shift_key = false;
648
649 try {
650 shift_key = e.shiftKey;
651 } catch (e) { }
652
653 if (keycode == 66 && shift_key) { // shift-B
654
655 var op = history_pop();
656
657 if (op) {
658 var op_s = op.split(":");
659
660 var i;
661 for (i = 0; i < op_s.length; i++) {
662 if (op_s[i] == 'undefined') {
663 op_s[i] = undefined;
664 }
665
666 if (op_s[i] == 'false') {
667 op_s[i] = false;
668 }
669
670 if (op_s[i] == 'true') {
671 op_s[i] = true;
672 }
673
674 }
675
676 debug("history split: " + op_s);
677
678 if (op_s[0] == "ARTICLE") {
679 debug("history: reverting to article " + op_s[1] + "/" + op_s[2]);
680 view(op_s[1], op_s[2], true);
681 }
682
683 if (op_s[0] == "FEED") {
684 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
685 }
686
687 } else {
688 notify("No operation to undo");
689 }
690
691 return false;
692
693 }
694
695 debug("LKP=" + keycode);
696}
697
698function history_push(op) {
699 debug("history_push: " + op);
700 op_history.push(op);
701
702 while (op_history.length > 30) {
703 op_history.shift();
704 }
705}
706
707function history_pop() {
708 var op = op_history.pop();
709 debug("history_pop: " + op);
710 return op;
711}
712
713function history_clear() {
714 debug("history_clear");
715 op_history.clear();
716}