]> git.wh0rd.org - tt-rss.git/blame - tt-rss.js
tag editor
[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
AD
156 if (display_tags) {
157 omode = "t";
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
331900c6
AD
193 var query_str = "backend.php?op=feeds";
194
8143ae1f
AD
195 if (display_tags) {
196 query_str = query_str + "&tags=1";
197 }
198
5c365f60 199 if (getActiveFeedId() && !activeFeedIsCat()) {
86741347 200 query_str = query_str + "&actid=" + getActiveFeedId();
175847de
AD
201 }
202
59b8192f
AD
203 if (navigator.userAgent.match("Opera")) {
204 var date = new Date();
205 var timestamp = Math.round(date.getTime() / 1000);
206 query_str = query_str + "&ts=" + timestamp
207 }
208
1a66d16e 209 if (fetch) query_str = query_str + "&fetch=yes";
e1123aee 210
6b4163cb
AD
211// var feeds_frame = document.getElementById("feeds-frame");
212// feeds_frame.src = query_str;
213
0e9dd1ba 214 debug("updateFeedList Q=" + query_str);
6b4163cb
AD
215
216 if (xmlhttp_ready(xmlhttp)) {
217 xmlhttp.open("GET", query_str, true);
218 xmlhttp.onreadystatechange=feedlist_callback;
219 xmlhttp.send(null);
220 } else {
221 debug("xmlhttp busy");
222 //printLockingError();
223 }
e1123aee 224
1a66d16e 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
ca2f46a7 233 debug("catchupAllFeeds Q=" + query_str);
1a66d16e 234
ca2f46a7
AD
235 if (xmlhttp_ready(xmlhttp)) {
236 xmlhttp.open("GET", query_str, true);
237 xmlhttp.onreadystatechange=feedlist_callback;
238 xmlhttp.send(null);
239 } else {
240 debug("xmlhttp busy");
241 //printLockingError();
242 }
476cac42 243
fc69e641 244 global_unread = 0;
21703604 245 updateTitle("");
fc69e641 246
476cac42 247}
1cd17194 248
767e2486 249function viewCurrentFeed(subop) {
1a66d16e 250
ec6e2fd3
AD
251// if (getActiveFeedId()) {
252 if (getActiveFeedId() != undefined) {
767e2486 253 viewfeed(getActiveFeedId(), subop);
033e47e0
AD
254 } else {
255 disableContainerChildren("headlinesToolbar", false, document);
ec6e2fd3 256// viewfeed(-1, subop); // FIXME
f0601b87 257 }
164f4738 258 return false; // block unneeded form submits
f0601b87
AD
259}
260
767e2486 261function viewfeed(feed, subop) {
db8d6f67 262 var f = window.frames["feeds-frame"];
767e2486 263 f.viewfeed(feed, subop);
9cfc649a
AD
264}
265
40d13c28 266function timeout() {
05732aa0 267 scheduleFeedUpdate(false);
f5de0d8d 268
3ac2b520 269 var refresh_time = getInitParam("feeds_frame_refresh");
f5de0d8d 270
3ac2b520 271 if (!refresh_time) refresh_time = 600;
f5de0d8d
AD
272
273 setTimeout("timeout()", refresh_time*1000);
ac53063a
AD
274}
275
c374a3fe 276function resetSearch() {
64c620ce
AD
277 var searchbox = document.getElementById("searchbox")
278
86741347 279 if (searchbox.value != "" && getActiveFeedId()) {
64c620ce 280 searchbox.value = "";
767e2486 281 viewfeed(getActiveFeedId(), "");
ac43eba1 282 }
c374a3fe 283}
ac53063a 284
86b682ce
AD
285function searchCancel() {
286 closeInfoBox(true);
287}
288
f0601b87 289function search() {
eff4997c 290 closeInfoBox();
4ce19859 291 viewCurrentFeed(0, "");
76798ff3 292}
1cd17194 293
13ad9102
AD
294function localPiggieFunction(enable) {
295 if (enable) {
296 var query_str = "backend.php?op=feeds&subop=piggie";
297
c0e5a40e 298 if (xmlhttp_ready(xmlhttp)) {
13ad9102
AD
299
300 xmlhttp.open("GET", query_str, true);
301 xmlhttp.onreadystatechange=feedlist_callback;
302 xmlhttp.send(null);
303 }
304 }
305}
306
806a3d14
AD
307// if argument is undefined, current subtitle is not updated
308// use blank string to clear subtitle
fc69e641
AD
309function updateTitle(s) {
310 var tmp = "Tiny Tiny RSS";
21703604 311
5a494a0b 312 if (s != undefined) {
21703604
AD
313 current_subtitle = s;
314 }
315
fc69e641
AD
316 if (global_unread > 0) {
317 tmp = tmp + " (" + global_unread + ")";
318 }
319
5a494a0b 320 if (current_subtitle) {
21703604 321 tmp = tmp + " - " + current_subtitle;
fc69e641 322 }
21703604
AD
323
324 if (active_title_text.length > 0) {
325 tmp = tmp + " > " + active_title_text;
326 }
327
fc69e641
AD
328 document.title = tmp;
329}
330
22a93ad8 331function genericSanityCheck() {
ac43eba1 332
295f9b42
AD
333 if (!xmlhttp) fatalError(1);
334
335 setCookie("ttrss_vf_test", "TEST");
336
337 if (getCookie("ttrss_vf_test") != "TEST") {
338 fatalError(2);
339 }
340
22a93ad8
AD
341 return true;
342}
343
344function init() {
345
7719618b 346 try {
fe2f1970 347
97dcd654
AD
348 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
349
350 if (arguments.callee.done) return;
351 arguments.callee.done = true;
352
7719618b 353 disableContainerChildren("headlinesToolbar", true);
70830c87 354
86b682ce
AD
355 Form.disable("main_toolbar_form");
356
7719618b
AD
357 if (!genericSanityCheck())
358 return;
ac43eba1 359
0ee1d1a0
AD
360 if (getURLParam('debug')) {
361 document.getElementById('debug_output').style.display = 'block';
362 debug('debug mode activated');
363 }
364
7719618b
AD
365 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
366 xmlhttp.onreadystatechange=backend_sanity_check_callback;
367 xmlhttp.send(null);
47179952 368
7719618b
AD
369 } catch (e) {
370 exception_error("init", e);
a8d28f48 371 }
7719618b 372}
86741347 373
7719618b 374function init_second_stage() {
295f9b42 375
7719618b 376 try {
2f587484 377
76b4eae1
AD
378 cookie_lifetime = getCookie("ttrss_cltime");
379
380 delCookie("ttrss_vf_test");
7719618b 381
7719618b 382 document.onkeydown = hotkey_handler;
1b0809ae 383
f31673f7
AD
384 var tb = parent.document.forms["main_toolbar_form"];
385
9f10e152
AD
386// dropboxSelect(tb.view_mode, getInitParam("toolbar_view_mode"));
387// dropboxSelect(tb.limit, getInitParam("toolbar_limit"));
c6784aea 388
0d51e25d
AD
389 daemon_enabled = getInitParam("daemon_enabled") == 1;
390 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
5f136c9a 391
0e9dd1ba 392 setTimeout('updateFeedList(false, false)', 50);
0ee1d1a0
AD
393
394 debug("second stage ok");
7719618b 395
7719618b
AD
396 } catch (e) {
397 exception_error("init_second_stage", e);
2f587484 398 }
1cd17194 399}
ac43eba1 400
c09ec856 401function quickMenuChange() {
cbe45fa8 402 var chooser = document.getElementById("quickMenuChooser");
86b682ce 403 var opid = chooser[chooser.selectedIndex].value;
e2f8f7b4 404
c09ec856
AD
405 chooser.selectedIndex = 0;
406 quickMenuGo(opid);
407}
408
409function quickMenuGo(opid) {
bb3423cf 410 try {
c09ec856 411
bb3423cf
AD
412 if (opid == "qmcPrefs") {
413 gotoPreferences();
414 }
415
416 if (opid == "qmcSearch") {
0a6c4846 417 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
6de5d056
AD
418 return;
419 }
bb3423cf
AD
420
421 if (opid == "qmcAddFeed") {
422 displayDlg("quickAddFeed");
423 return;
69668465 424 }
7086277c
AD
425
426 if (opid == "qmcEditFeed") {
427 editFeedDlg(getActiveFeedId());
428 }
6de5d056 429
bb3423cf
AD
430 if (opid == "qmcRemoveFeed") {
431 var actid = getActiveFeedId();
432
5c365f60 433 if (!actid || activeFeedIsCat()) {
0530ddd8 434 alert("Please select some feed first.");
bb3423cf
AD
435 return;
436 }
64a2875d
AD
437
438 var fn = getFeedName(actid);
bb3423cf 439
64a2875d 440 if (confirm("Unsubscribe from " + fn + "?")) {
bb3423cf
AD
441 qfdDelete(actid);
442 }
443
444 return;
445 }
446
447 if (opid == "qmcUpdateFeeds") {
448 scheduleFeedUpdate(true);
449 return;
450 }
451
452 if (opid == "qmcCatchupAll") {
453 catchupAllFeeds();
454 return;
455 }
456
457 if (opid == "qmcShowOnlyUnread") {
458 toggleDispRead();
459 return;
460 }
461
462 if (opid == "qmcAddFilter") {
463 displayDlg("quickAddFilter", getActiveFeedId());
464 }
465 } catch (e) {
466 exception_error("quickMenuGo", e);
a24f525c 467 }
e2f8f7b4
AD
468}
469
6de5d056
AD
470function qfdDelete(feed_id) {
471
472 notify("Removing feed...");
473
7f123cda
AD
474 if (!xmlhttp_ready(xmlhttp)) {
475 printLockingError();
476 return
477 }
478
15da5cc1
AD
479 _qfd_deleted_feed = feed_id;
480
69668465 481 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
6de5d056
AD
482 xmlhttp.onreadystatechange=dlg_frefresh_callback;
483 xmlhttp.send(null);
7bc4f251
AD
484
485 return false;
6de5d056 486}
033e47e0 487
3745788e 488
21703604
AD
489function updateFeedTitle(t) {
490 active_title_text = t;
491 updateTitle();
492}
493
3745788e 494function toggleDispRead() {
7f123cda 495 try {
3745788e 496
7f123cda
AD
497 if (!xmlhttp_ready(xmlhttp)) {
498 printLockingError();
499 return
500 }
3745788e 501
e8bd0da9 502 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
3745788e 503
7f123cda 504 hide_read_feeds = !hide_read_feeds;
e8bd0da9
AD
505
506 debug("toggle_disp_read => " + hide_read_feeds);
507
508 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
509
7f123cda
AD
510 var query = "backend.php?op=rpc&subop=setpref" +
511 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
3745788e 512
e8bd0da9
AD
513 storeInitParam("hide_read_feeds", hide_read_feeds, true);
514
515 new Ajax.Request(query);
7f123cda
AD
516
517 } catch (e) {
518 exception_error("toggleDispRead", e);
3745788e 519 }
3745788e 520}
295f9b42 521
71ad883b 522function parse_runtime_info(elem) {
4724a093
AD
523 if (!elem) {
524 debug("parse_runtime_info: elem is null, aborting");
525 return;
526 }
527
71ad883b
AD
528 var param = elem.firstChild;
529
52db9978 530 debug("parse_runtime_info: " + param);
1cb7492d 531
71ad883b
AD
532 while (param) {
533 var k = param.getAttribute("key");
534 var v = param.getAttribute("value");
3ac2b520 535
1cb7492d
AD
536 debug("RI: " + k + " => " + v);
537
71ad883b
AD
538 var w = document.getElementById("noDaemonWarning");
539
540 if (w) {
541 if (k == "daemon_is_running" && v != 1) {
542 w.style.display = "block";
543 } else {
544 w.style.display = "none";
545 }
546 }
547 param = param.nextSibling;
548 }
549}
fce24838
AD
550
551function catchupCurrentFeed() {
552
234e467c 553 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
fce24838 554
234e467c
AD
555 var str = "Mark all articles in " + fn + " as read?";
556
557/* if (active_feed_is_cat) {
558 str = "Mark all articles in this category as read?";
559 } */
560
f6d6e22f 561 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
28de3732 562 return viewCurrentFeed('MarkAllRead')
fce24838
AD
563 }
564}
88040f57 565
461766f3
AD
566function userSwitch() {
567 var chooser = document.getElementById("userSwitch");
568 var user = chooser[chooser.selectedIndex].value;
569 window.location = "tt-rss.php?swu=" + user;
570}
6e6504bc 571
7086277c
AD
572function editFeedDlg(feed) {
573
729bafaa
AD
574 disableHotkeys();
575
7086277c
AD
576 if (!feed) {
577 alert("Please select some feed first.");
578 return;
579 }
580
581 if (xmlhttp_ready(xmlhttp)) {
582 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
583 param_escape(feed), true);
584 xmlhttp.onreadystatechange=infobox_callback;
585 xmlhttp.send(null);
586 } else {
587 printLockingError();
588 }
589}
590
591/* this functions duplicate those of prefs.js feed editor, with
592 some differences because there is no feedlist */
593
594function feedEditCancel() {
595 closeInfoBox();
596 return false;
597}
598
599function feedEditSave() {
600
601 try {
602
603 if (!xmlhttp_ready(xmlhttp)) {
604 printLockingError();
605 return
606 }
607
608 // FIXME: add parameter validation
609
610 var query = Form.serialize("edit_feed_form");
611
612 notify("Saving feed...");
613
614 xmlhttp.open("POST", "backend.php", true);
615 xmlhttp.onreadystatechange=dlg_frefresh_callback;
616 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
617 xmlhttp.send(query);
618
619 closeInfoBox();
620
621 return false;
622
623 } catch (e) {
624 exception_error("feedEditSave (main)", e);
625 }
626}
6e6504bc 627
1dc8dba0
AD
628function localHotkeyHandler(e) {
629
630 var keycode;
631
632 if (window.event) {
633 keycode = window.event.keyCode;
634 } else if (e) {
635 keycode = e.which;
636 }
637
638 var shift_key = false;
639
640 try {
641 shift_key = e.shiftKey;
642 } catch (e) { }
643
644 if (keycode == 66 && shift_key) { // shift-B
645
646 var op = history_pop();
647
648 if (op) {
649 var op_s = op.split(":");
650
651 var i;
652 for (i = 0; i < op_s.length; i++) {
653 if (op_s[i] == 'undefined') {
654 op_s[i] = undefined;
655 }
656
657 if (op_s[i] == 'false') {
658 op_s[i] = false;
659 }
660
661 if (op_s[i] == 'true') {
662 op_s[i] = true;
663 }
664
665 }
666
667 debug("history split: " + op_s);
668
669 if (op_s[0] == "ARTICLE") {
670 debug("history: reverting to article " + op_s[1] + "/" + op_s[2]);
671 view(op_s[1], op_s[2], true);
672 }
673
674 if (op_s[0] == "FEED") {
675 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
676 }
677
678 } else {
679 notify("No operation to undo");
680 }
681
682 return false;
683
684 }
685
686 debug("LKP=" + keycode);
687}
688
689function history_push(op) {
690 debug("history_push: " + op);
691 op_history.push(op);
692
693 while (op_history.length > 30) {
694 op_history.shift();
695 }
696}
697
698function history_pop() {
699 var op = op_history.pop();
700 debug("history_pop: " + op);
701 return op;
702}
703
704function history_clear() {
705 debug("history_clear");
706 op_history.clear();
707}