]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
make mark as read subtoolbar dropdown clickable
[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
7719618b
AD
367 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
368 xmlhttp.onreadystatechange=backend_sanity_check_callback;
369 xmlhttp.send(null);
47179952 370
7719618b
AD
371 } catch (e) {
372 exception_error("init", e);
a8d28f48 373 }
7719618b 374}
86741347 375
7719618b 376function init_second_stage() {
295f9b42 377
7719618b 378 try {
2f587484 379
76b4eae1
AD
380 cookie_lifetime = getCookie("ttrss_cltime");
381
382 delCookie("ttrss_vf_test");
7719618b 383
7719618b 384 document.onkeydown = hotkey_handler;
1b0809ae 385
f31673f7
AD
386 var tb = parent.document.forms["main_toolbar_form"];
387
9f10e152
AD
388// dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
389// dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
c6784aea 390
0d51e25d
AD
391 daemon_enabled = getInitParam("daemon_enabled") == 1;
392 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
5f136c9a 393
0e9dd1ba 394 setTimeout('updateFeedList(false, false)', 50);
0ee1d1a0
AD
395
396 debug("second stage ok");
7719618b 397
7719618b
AD
398 } catch (e) {
399 exception_error("init_second_stage", e);
2f587484 400 }
1cd17194 401}
ac43eba1 402
c09ec856 403function quickMenuChange() {
cbe45fa8 404 var chooser = document.getElementById("quickMenuChooser");
86b682ce 405 var opid = chooser[chooser.selectedIndex].value;
e2f8f7b4 406
c09ec856
AD
407 chooser.selectedIndex = 0;
408 quickMenuGo(opid);
409}
410
411function quickMenuGo(opid) {
bb3423cf 412 try {
c09ec856 413
bb3423cf
AD
414 if (opid == "qmcPrefs") {
415 gotoPreferences();
416 }
417
418 if (opid == "qmcSearch") {
0a6c4846 419 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
6de5d056
AD
420 return;
421 }
bb3423cf
AD
422
423 if (opid == "qmcAddFeed") {
424 displayDlg("quickAddFeed");
425 return;
69668465 426 }
7086277c
AD
427
428 if (opid == "qmcEditFeed") {
429 editFeedDlg(getActiveFeedId());
430 }
6de5d056 431
bb3423cf
AD
432 if (opid == "qmcRemoveFeed") {
433 var actid = getActiveFeedId();
434
5c365f60 435 if (!actid || activeFeedIsCat()) {
0530ddd8 436 alert("Please select some feed first.");
bb3423cf
AD
437 return;
438 }
64a2875d
AD
439
440 var fn = getFeedName(actid);
bb3423cf 441
64a2875d 442 if (confirm("Unsubscribe from " + fn + "?")) {
bb3423cf
AD
443 qfdDelete(actid);
444 }
445
446 return;
447 }
448
449 if (opid == "qmcUpdateFeeds") {
450 scheduleFeedUpdate(true);
451 return;
452 }
453
454 if (opid == "qmcCatchupAll") {
455 catchupAllFeeds();
456 return;
457 }
458
459 if (opid == "qmcShowOnlyUnread") {
460 toggleDispRead();
461 return;
462 }
463
464 if (opid == "qmcAddFilter") {
465 displayDlg("quickAddFilter", getActiveFeedId());
466 }
467 } catch (e) {
468 exception_error("quickMenuGo", e);
a24f525c 469 }
e2f8f7b4
AD
470}
471
6de5d056
AD
472function qfdDelete(feed_id) {
473
474 notify("Removing feed...");
475
7f123cda
AD
476 if (!xmlhttp_ready(xmlhttp)) {
477 printLockingError();
478 return
479 }
480
15da5cc1
AD
481 _qfd_deleted_feed = feed_id;
482
69668465 483 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
6de5d056
AD
484 xmlhttp.onreadystatechange=dlg_frefresh_callback;
485 xmlhttp.send(null);
7bc4f251
AD
486
487 return false;
6de5d056 488}
033e47e0 489
3745788e 490
21703604
AD
491function updateFeedTitle(t) {
492 active_title_text = t;
493 updateTitle();
494}
495
3745788e 496function toggleDispRead() {
7f123cda 497 try {
3745788e 498
7f123cda
AD
499 if (!xmlhttp_ready(xmlhttp)) {
500 printLockingError();
501 return
502 }
3745788e 503
e8bd0da9 504 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
3745788e 505
7f123cda 506 hide_read_feeds = !hide_read_feeds;
e8bd0da9
AD
507
508 debug("toggle_disp_read => " + hide_read_feeds);
509
510 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
511
7f123cda
AD
512 var query = "backend.php?op=rpc&subop=setpref" +
513 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
3745788e 514
e8bd0da9
AD
515 storeInitParam("hide_read_feeds", hide_read_feeds, true);
516
517 new Ajax.Request(query);
7f123cda
AD
518
519 } catch (e) {
520 exception_error("toggleDispRead", e);
3745788e 521 }
3745788e 522}
295f9b42 523
71ad883b 524function parse_runtime_info(elem) {
4724a093
AD
525 if (!elem) {
526 debug("parse_runtime_info: elem is null, aborting");
527 return;
528 }
529
71ad883b
AD
530 var param = elem.firstChild;
531
52db9978 532 debug("parse_runtime_info: " + param);
1cb7492d 533
71ad883b
AD
534 while (param) {
535 var k = param.getAttribute("key");
536 var v = param.getAttribute("value");
3ac2b520 537
1cb7492d
AD
538 debug("RI: " + k + " => " + v);
539
71ad883b
AD
540 var w = document.getElementById("noDaemonWarning");
541
542 if (w) {
543 if (k == "daemon_is_running" && v != 1) {
544 w.style.display = "block";
545 } else {
546 w.style.display = "none";
547 }
548 }
549 param = param.nextSibling;
550 }
551}
fce24838
AD
552
553function catchupCurrentFeed() {
554
234e467c 555 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
fce24838 556
234e467c
AD
557 var str = "Mark all articles in " + fn + " as read?";
558
559/* if (active_feed_is_cat) {
560 str = "Mark all articles in this category as read?";
561 } */
562
f6d6e22f 563 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
28de3732 564 return viewCurrentFeed('MarkAllRead')
fce24838
AD
565 }
566}
88040f57 567
461766f3
AD
568function userSwitch() {
569 var chooser = document.getElementById("userSwitch");
570 var user = chooser[chooser.selectedIndex].value;
571 window.location = "tt-rss.php?swu=" + user;
572}
6e6504bc 573
7086277c
AD
574function editFeedDlg(feed) {
575
729bafaa
AD
576 disableHotkeys();
577
7086277c
AD
578 if (!feed) {
579 alert("Please select some feed first.");
580 return;
581 }
582
92f3bcae
AD
583 if (feed <= 0 || active_feed_is_cat || tagsAreDisplayed()) {
584 alert("You can't edit this kind of feed.");
585 return;
586 }
587
7086277c
AD
588 if (xmlhttp_ready(xmlhttp)) {
589 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
590 param_escape(feed), true);
591 xmlhttp.onreadystatechange=infobox_callback;
592 xmlhttp.send(null);
593 } else {
594 printLockingError();
595 }
596}
597
598/* this functions duplicate those of prefs.js feed editor, with
599 some differences because there is no feedlist */
600
601function feedEditCancel() {
602 closeInfoBox();
603 return false;
604}
605
606function feedEditSave() {
607
608 try {
609
610 if (!xmlhttp_ready(xmlhttp)) {
611 printLockingError();
612 return
613 }
614
615 // FIXME: add parameter validation
616
617 var query = Form.serialize("edit_feed_form");
618
619 notify("Saving feed...");
620
621 xmlhttp.open("POST", "backend.php", true);
622 xmlhttp.onreadystatechange=dlg_frefresh_callback;
623 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
624 xmlhttp.send(query);
625
626 closeInfoBox();
627
628 return false;
629
630 } catch (e) {
631 exception_error("feedEditSave (main)", e);
632 }
633}
6e6504bc 634
1dc8dba0
AD
635function localHotkeyHandler(e) {
636
637 var keycode;
638
639 if (window.event) {
640 keycode = window.event.keyCode;
641 } else if (e) {
642 keycode = e.which;
643 }
644
645 var shift_key = false;
646
647 try {
648 shift_key = e.shiftKey;
649 } catch (e) { }
650
651 if (keycode == 66 && shift_key) { // shift-B
652
653 var op = history_pop();
654
655 if (op) {
656 var op_s = op.split(":");
657
658 var i;
659 for (i = 0; i < op_s.length; i++) {
660 if (op_s[i] == 'undefined') {
661 op_s[i] = undefined;
662 }
663
664 if (op_s[i] == 'false') {
665 op_s[i] = false;
666 }
667
668 if (op_s[i] == 'true') {
669 op_s[i] = true;
670 }
671
672 }
673
674 debug("history split: " + op_s);
675
676 if (op_s[0] == "ARTICLE") {
677 debug("history: reverting to article " + op_s[1] + "/" + op_s[2]);
678 view(op_s[1], op_s[2], true);
679 }
680
681 if (op_s[0] == "FEED") {
682 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
683 }
684
685 } else {
686 notify("No operation to undo");
687 }
688
689 return false;
690
691 }
692
693 debug("LKP=" + keycode);
694}
695
696function history_push(op) {
697 debug("history_push: " + op);
698 op_history.push(op);
699
700 while (op_history.length > 30) {
701 op_history.shift();
702 }
703}
704
705function history_pop() {
706 var op = op_history.pop();
707 debug("history_pop: " + op);
708 return op;
709}
710
711function history_clear() {
712 debug("history_clear");
713 op_history.clear();
714}