]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
misc interface cleanups, only allow daemon to start when ENABLE_UPDATE_DAEMON is...
[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
AD
9var _qfd_deleted_feed = 0;
10
1cd17194
AD
11/*@cc_on @*/
12/*@if (@_jscript_version >= 5)
13// JScript gives us Conditional compilation, we can cope with old IE versions.
14// and security blocked creation of the objects.
15try {
16 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
17} catch (e) {
18 try {
19 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
20 } catch (E) {
21 xmlhttp = false;
22 }
23}
24@end @*/
25
26if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
27 xmlhttp = new XMLHttpRequest();
28}
29
8143ae1f
AD
30function toggleTags() {
31 display_tags = !display_tags;
32
33 var p = document.getElementById("dispSwitchPrompt");
34
35 if (display_tags) {
36 p.innerHTML = "display feeds";
37 } else {
38 p.innerHTML = "display tags";
39 }
40
41 updateFeedList();
42}
43
6de5d056 44function dlg_frefresh_callback() {
1cd17194 45 if (xmlhttp.readyState == 4) {
a24f525c 46 notify(xmlhttp.responseText);
e2f8f7b4 47 updateFeedList(false, false);
15da5cc1
AD
48 if (_qfd_deleted_feed) {
49 var hframe = document.getElementById("headlines-frame");
50 if (hframe) {
51 hframe.src = "backend.php?op=error&msg=No%20feed%20selected.";
52 }
53 }
f84a97a3 54 closeDlg();
c0e5a40e 55 }
1cd17194 56}
e2f8f7b4 57
a24f525c
AD
58function dlg_submit_callback() {
59 if (xmlhttp.readyState == 4) {
60 notify(xmlhttp.responseText);
61 closeDlg();
62 }
63}
64
65function dlg_display_callback() {
f84a97a3
AD
66 if (xmlhttp.readyState == 4) {
67 var dlg = document.getElementById("userDlg");
76332f3c 68 var dlg_s = document.getElementById("userDlgShadow");
f84a97a3
AD
69
70 dlg.innerHTML = xmlhttp.responseText;
76332f3c 71 dlg_s.style.display = "block";
f84a97a3
AD
72 }
73}
1a66d16e 74
7f123cda
AD
75function hide_unread_callback() {
76 if (xmlhttp.readyState == 4) {
77
78 try {
79
80 var reply = xmlhttp.responseXML.firstChild.firstChild;
81 var value = reply.getAttribute("value");
82 var hide_read_feeds = (value != "false")
83 var feeds_doc = window.frames["feeds-frame"].document;
84
85 hideOrShowFeeds(feeds_doc, hide_read_feeds);
86
87 if (hide_read_feeds) {
88 setCookie("ttrss_vf_hreadf", 1);
89 } else {
90 setCookie("ttrss_vf_hreadf", 0);
91 }
92
93 } catch (e) {
94 exception_error("hide_unread_callback", e);
95 }
96
97 }
98}
99
8158c57a 100function refetch_callback() {
29fb8c70 101 if (xmlhttp.readyState == 4) {
7719618b 102 try {
310da49d 103
7719618b
AD
104 if (!xmlhttp.responseXML) {
105 notify("refetch_callback: backend did not return valid XML");
106 return;
107 }
108
109 var reply = xmlhttp.responseXML.firstChild;
262bd8ea 110
7719618b
AD
111 if (!reply) {
112 notify("refetch_callback: backend did not return expected XML object");
e2cb4c6d 113 updateTitle("");
7719618b
AD
114 return;
115 }
262bd8ea 116
7719618b
AD
117 var error_code = reply.getAttribute("error-code");
118
119 if (error_code && error_code != 0) {
120 return fatalError(error_code);
121 }
122
123 var f_document = window.frames["feeds-frame"].document;
1a6a9555 124
3bdb368b 125 parse_counters(reply, f_document, window);
c6784aea
AD
126
127 if (!daemon_enabled) {
128 updateTitle("");
129 notify("All feeds updated.");
130 }
7719618b
AD
131 } catch (e) {
132 exception_error("refetch_callback", e);
e2cb4c6d 133 updateTitle("");
7719618b 134 }
090e250b
AD
135 }
136}
1a66d16e 137
295f9b42
AD
138function backend_sanity_check_callback() {
139
140 if (xmlhttp.readyState == 4) {
295f9b42 141
7719618b
AD
142 try {
143
144 if (!xmlhttp.responseXML) {
145 fatalError(3);
146 return;
147 }
295f9b42 148
7719618b
AD
149 var reply = xmlhttp.responseXML.firstChild;
150
151 if (!reply) {
152 fatalError(3);
153 return;
154 }
155
156 var error_code = reply.getAttribute("error-code");
157
158 if (error_code && error_code != 0) {
159 return fatalError(error_code);
160 }
161
162 init_second_stage();
295f9b42 163
7719618b
AD
164 } catch (e) {
165 exception_error("backend_sanity_check_callback", e);
166 }
295f9b42
AD
167 }
168}
169
cb246176 170function scheduleFeedUpdate(force) {
525116d4 171
c6784aea
AD
172 if (!daemon_enabled) {
173 notify("Updating feeds, please wait.");
174 updateTitle("Updating");
175 }
55193822 176
cb246176
AD
177 var query_str = "backend.php?op=rpc&subop=";
178
179 if (force) {
c3b81db0 180 query_str = query_str + "forceUpdateAllFeeds";
cb246176 181 } else {
c3b81db0 182 query_str = query_str + "updateAllFeeds";
cb246176 183 }
525116d4 184
9826bd2e
AD
185 var omode;
186
187 if (display_tags) {
188 omode = "t";
189 } else {
190 omode = "fl";
191 }
192
193 query_str = query_str + "&omode=" + omode;
194
29fb8c70
AD
195 if (xmlhttp_ready(xmlhttp)) {
196 xmlhttp.open("GET", query_str, true);
197 xmlhttp.onreadystatechange=refetch_callback;
198 xmlhttp.send(null);
525116d4
AD
199 } else {
200 printLockingError();
c0e5a40e 201 }
525116d4 202}
1cd17194 203
525116d4 204function updateFeedList(silent, fetch) {
c0e5a40e 205
1a66d16e
AD
206// if (silent != true) {
207// notify("Loading feed list...");
208// }
82baad4a 209
331900c6
AD
210 var query_str = "backend.php?op=feeds";
211
8143ae1f
AD
212 if (display_tags) {
213 query_str = query_str + "&tags=1";
214 }
215
86741347
AD
216 if (getActiveFeedId()) {
217 query_str = query_str + "&actid=" + getActiveFeedId();
175847de
AD
218 }
219
1a66d16e 220 if (fetch) query_str = query_str + "&fetch=yes";
e1123aee 221
1a66d16e 222 var feeds_frame = document.getElementById("feeds-frame");
e1123aee 223
1a66d16e
AD
224 feeds_frame.src = query_str;
225}
175847de 226
476cac42 227function catchupAllFeeds() {
076682aa 228
476cac42
AD
229 var query_str = "backend.php?op=feeds&subop=catchupAll";
230
231 notify("Marking all feeds as read...");
232
1a66d16e
AD
233 var feeds_frame = document.getElementById("feeds-frame");
234
235 feeds_frame.src = query_str;
476cac42 236
fc69e641 237 global_unread = 0;
21703604 238 updateTitle("");
fc69e641 239
476cac42 240}
1cd17194 241
f0601b87 242function viewCurrentFeed(skip, subop) {
1a66d16e 243
86741347
AD
244 if (getActiveFeedId()) {
245 viewfeed(getActiveFeedId(), skip, subop);
033e47e0
AD
246 } else {
247 disableContainerChildren("headlinesToolbar", false, document);
248 viewfeed(-1, skip, subop); // FIXME
f0601b87
AD
249 }
250}
251
476cac42 252function viewfeed(feed, skip, subop) {
db8d6f67
AD
253 var f = window.frames["feeds-frame"];
254 f.viewfeed(feed, skip, subop);
9cfc649a
AD
255}
256
40d13c28 257function timeout() {
05732aa0 258 scheduleFeedUpdate(false);
f5de0d8d
AD
259
260 var refresh_time = getCookie('ttrss_vf_refresh');
261
262 if (!refresh_time) refresh_time = 600;
263
264 setTimeout("timeout()", refresh_time*1000);
ac53063a
AD
265}
266
c374a3fe 267function resetSearch() {
64c620ce
AD
268 var searchbox = document.getElementById("searchbox")
269
86741347 270 if (searchbox.value != "" && getActiveFeedId()) {
64c620ce 271 searchbox.value = "";
86741347 272 viewfeed(getActiveFeedId(), 0, "");
ac43eba1 273 }
c374a3fe 274}
ac53063a 275
f0601b87 276function search() {
49b7cbd3 277 closeDlg();
4ce19859 278 viewCurrentFeed(0, "");
76798ff3 279}
1cd17194 280
13ad9102
AD
281function localPiggieFunction(enable) {
282 if (enable) {
283 var query_str = "backend.php?op=feeds&subop=piggie";
284
c0e5a40e 285 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
286
287 xmlhttp.open("GET", query_str, true);
288 xmlhttp.onreadystatechange=feedlist_callback;
289 xmlhttp.send(null);
290 }
291 }
292}
293
9cfc649a
AD
294function localHotkeyHandler(keycode) {
295
f0601b87 296/* if (keycode == 78) {
c3a8d71a 297 return moveToPost('next');
9cfc649a
AD
298 }
299
300 if (keycode == 80) {
c3a8d71a 301 return moveToPost('prev');
f0601b87 302 } */
c3a8d71a 303
b623b3ed 304 if (keycode == 82) { // r
c3a8d71a
AD
305 return scheduleFeedUpdate(true);
306 }
307
b623b3ed
AD
308 if (keycode == 85) { // u
309 if (getActiveFeedId()) {
310 return viewfeed(getActiveFeedId(), 0, "ForceUpdate");
311 }
312 }
313
314 if (keycode == 65) { // a
315 return toggleDispRead();
c3a8d71a
AD
316 }
317
7b433d8c
AD
318 var f_doc = window.frames["feeds-frame"].document;
319 var feedlist = f_doc.getElementById('feedList');
320
321 if (keycode == 74) { // j
322 var feed = getActiveFeedId();
323 var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
324 if (new_feed) viewfeed(new_feed, 0, '');
325 }
326
327 if (keycode == 75) { // k
328 var feed = getActiveFeedId();
329 var new_feed = getRelativeFeedId(feedlist, feed, 'next');
330 if (new_feed) viewfeed(new_feed, 0, '');
331 }
332
c3a8d71a
AD
333// notify("KC: " + keycode);
334
9cfc649a
AD
335}
336
806a3d14
AD
337// if argument is undefined, current subtitle is not updated
338// use blank string to clear subtitle
fc69e641
AD
339function updateTitle(s) {
340 var tmp = "Tiny Tiny RSS";
21703604
AD
341
342 if (s && s.length > 0) {
343 current_subtitle = s;
344 }
345
fc69e641
AD
346 if (global_unread > 0) {
347 tmp = tmp + " (" + global_unread + ")";
348 }
349
350 if (s) {
21703604 351 tmp = tmp + " - " + current_subtitle;
fc69e641 352 }
21703604
AD
353
354 if (active_title_text.length > 0) {
355 tmp = tmp + " > " + active_title_text;
356 }
357
fc69e641
AD
358 document.title = tmp;
359}
360
22a93ad8 361function genericSanityCheck() {
ac43eba1 362
295f9b42
AD
363 if (!xmlhttp) fatalError(1);
364
365 setCookie("ttrss_vf_test", "TEST");
366
367 if (getCookie("ttrss_vf_test") != "TEST") {
368 fatalError(2);
369 }
370
22a93ad8
AD
371 return true;
372}
373
374function init() {
375
7719618b 376 try {
fe2f1970 377
7719618b 378 disableContainerChildren("headlinesToolbar", true);
70830c87 379
7719618b
AD
380 if (!genericSanityCheck())
381 return;
ac43eba1 382
7719618b
AD
383 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
384 xmlhttp.onreadystatechange=backend_sanity_check_callback;
385 xmlhttp.send(null);
47179952 386
7719618b
AD
387 } catch (e) {
388 exception_error("init", e);
a8d28f48 389 }
7719618b 390}
86741347 391
7719618b 392function init_second_stage() {
295f9b42 393
7719618b 394 try {
2f587484 395
7719618b
AD
396 setCookie("ttrss_vf_actfeed", "");
397
398 updateFeedList(false, false);
399 document.onkeydown = hotkey_handler;
400
4bbd5aec
AD
401 var viewbox = document.getElementById("viewbox");
402 var limitbox = document.getElementById("limitbox");
1b0809ae
AD
403
404 dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
4bbd5aec 405 dropboxSelect(limitbox, getCookie("ttrss_vf_limit"));
c6784aea
AD
406
407 daemon_enabled = getCookie("ttrss_vf_daemon");
7719618b 408
7719618b
AD
409 } catch (e) {
410 exception_error("init_second_stage", e);
2f587484 411 }
1cd17194 412}
ac43eba1 413
c09ec856 414function quickMenuChange() {
cbe45fa8
AD
415 var chooser = document.getElementById("quickMenuChooser");
416 var opid = chooser[chooser.selectedIndex].id;
e2f8f7b4 417
c09ec856
AD
418 chooser.selectedIndex = 0;
419 quickMenuGo(opid);
420}
421
422function quickMenuGo(opid) {
423
424
cbe45fa8 425 if (opid == "qmcPrefs") {
e2f8f7b4
AD
426 gotoPreferences();
427 }
428
27da8b1e 429 if (opid == "qmcSearch") {
49b7cbd3 430 displayDlg("search", getActiveFeedId());
033e47e0
AD
431 return;
432 }
433
cbe45fa8 434 if (opid == "qmcAddFeed") {
f84a97a3 435 displayDlg("quickAddFeed");
6de5d056
AD
436 return;
437 }
438
cbe45fa8 439 if (opid == "qmcRemoveFeed") {
6de5d056
AD
440 var actid = getActiveFeedId();
441
442 if (!actid) {
443 notify("Please select some feed first.");
444 return;
445 }
69668465
AD
446
447 if (confirm("Remove current feed?")) {
448 qfdDelete(actid);
449 }
6de5d056 450
6de5d056 451 return;
e2f8f7b4 452 }
7a991cac 453
cbe45fa8 454 if (opid == "qmcUpdateFeeds") {
7a991cac
AD
455 scheduleFeedUpdate(true);
456 return;
457 }
458
cbe45fa8 459 if (opid == "qmcCatchupAll") {
7a991cac
AD
460 catchupAllFeeds();
461 return;
462 }
463
cbe45fa8 464 if (opid == "qmcShowOnlyUnread") {
7a991cac
AD
465 toggleDispRead();
466 return;
467 }
468
a24f525c
AD
469 if (opid == "qmcAddFilter") {
470 displayDlg("quickAddFilter", getActiveFeedId());
471 }
472
e2f8f7b4
AD
473}
474
475function qafAdd() {
ac43eba1 476
e2f8f7b4
AD
477 if (!xmlhttp_ready(xmlhttp)) {
478 printLockingError();
479 return
480 }
481
482 var link = document.getElementById("qafInput");
483
484 if (link.value.length == 0) {
485 notify("Missing feed URL.");
486 } else {
487 notify("Adding feed...");
15da5cc1
AD
488
489 var cat = document.getElementById("qafCat");
490 var cat_id = "";
e2f8f7b4 491
15da5cc1
AD
492 if (cat) {
493 cat_id = cat[cat.selectedIndex].id;
494 } else {
495 cat_id = 0;
496 }
497
e2f8f7b4
AD
498 var feeds_doc = window.frames["feeds-frame"].document;
499
500 feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
501
a24f525c 502 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=add&link=" +
15da5cc1 503 param_escape(link.value) + "&cid=" + param_escape(cat_id), true);
6de5d056 504 xmlhttp.onreadystatechange=dlg_frefresh_callback;
e2f8f7b4
AD
505 xmlhttp.send(null);
506
507 link.value = "";
508
509 }
f84a97a3
AD
510}
511
a24f525c
AD
512function qaddFilter() {
513
514 if (!xmlhttp_ready(xmlhttp)) {
515 printLockingError();
516 return
517 }
518
519 var regexp = document.getElementById("fadd_regexp");
520 var match = document.getElementById("fadd_match");
521 var feed = document.getElementById("fadd_feed");
522 var action = document.getElementById("fadd_action");
523
524 if (regexp.value.length == 0) {
525 notify("Missing filter expression.");
526 } else {
527 notify("Adding filter...");
528
529 var v_match = match[match.selectedIndex].text;
530 var feed_id = feed[feed.selectedIndex].id;
531 var action_id = action[action.selectedIndex].id;
532
533 xmlhttp.open("GET", "backend.php?op=pref-filters&quiet=1&subop=add&regexp=" +
534 param_escape(regexp.value) + "&match=" + v_match +
535 "&fid=" + param_escape(feed_id) + "&aid=" + param_escape(action_id), true);
536
537 xmlhttp.onreadystatechange=dlg_submit_callback;
538 xmlhttp.send(null);
539
540 regexp.value = "";
541 }
542
543}
544
545
6de5d056 546function displayDlg(id, param) {
e2f8f7b4 547
7f123cda
AD
548 if (!xmlhttp_ready(xmlhttp)) {
549 printLockingError();
550 return
551 }
552
40d601c5
AD
553 notify("");
554
f84a97a3 555 xmlhttp.open("GET", "backend.php?op=dlg&id=" +
6de5d056 556 param_escape(id) + "&param=" + param_escape(param), true);
a24f525c 557 xmlhttp.onreadystatechange=dlg_display_callback;
f84a97a3 558 xmlhttp.send(null);
e2f8f7b4 559
ccedfddd 560 disableHotkeys();
e2f8f7b4 561}
f84a97a3
AD
562
563function closeDlg() {
76332f3c 564 var dlg = document.getElementById("userDlgShadow");
f84a97a3 565 dlg.style.display = "none";
ccedfddd 566 enableHotkeys();
f84a97a3
AD
567}
568
6de5d056
AD
569function qfdDelete(feed_id) {
570
571 notify("Removing feed...");
572
7f123cda
AD
573 if (!xmlhttp_ready(xmlhttp)) {
574 printLockingError();
575 return
576 }
577
69668465
AD
578// var feeds_doc = window.frames["feeds-frame"].document;
579// feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
6de5d056 580
15da5cc1
AD
581 _qfd_deleted_feed = feed_id;
582
69668465 583 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
6de5d056
AD
584 xmlhttp.onreadystatechange=dlg_frefresh_callback;
585 xmlhttp.send(null);
6de5d056 586}
033e47e0 587
3745788e 588
c09ec856 589function allFeedsMenuChange() {
3745788e
AD
590 var chooser = document.getElementById("allFeedsChooser");
591
592 var opname = chooser[chooser.selectedIndex].text;
593
c09ec856
AD
594 chooser.selectedIndex = 0;
595
3745788e
AD
596 if (opname == "Update") {
597 scheduleFeedUpdate(true);
598 return;
599 }
600
601 if (opname == "Mark as read") {
602 catchupAllFeeds();
603 return;
604 }
605
bdf7f1bc 606 if (opname == "Show only unread") {
3745788e
AD
607 toggleDispRead();
608 return;
609 }
610
611}
612
21703604
AD
613function updateFeedTitle(t) {
614 active_title_text = t;
615 updateTitle();
616}
617
3745788e 618function toggleDispRead() {
7f123cda 619 try {
3745788e 620
7f123cda
AD
621 if (!xmlhttp_ready(xmlhttp)) {
622 printLockingError();
623 return
624 }
3745788e 625
7f123cda 626 var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
3745788e 627
7f123cda
AD
628 hide_read_feeds = !hide_read_feeds;
629
630 var query = "backend.php?op=rpc&subop=setpref" +
631 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
3745788e 632
7f123cda
AD
633 xmlhttp.open("GET", query);
634 xmlhttp.onreadystatechange=hide_unread_callback;
635 xmlhttp.send(null);
636
637 } catch (e) {
638 exception_error("toggleDispRead", e);
3745788e 639 }
3745788e 640}
295f9b42 641