]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
TAG 1.2.11
[tt-rss.git] / tt-rss.js
1 var xmlhttp = false;
2 var total_unread = 0;
3 var first_run = true;
4 var display_tags = false;
5 var global_unread = -1;
6 var active_title_text = "";
7 var current_subtitle = "";
8 var daemon_enabled = false;
9 var daemon_refresh_only = false;
10 var _qfd_deleted_feed = 0;
11 var firsttime_update = true;
12 var last_refetch = 0;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
17 var sanity_check_done = false;
18
19 var xmlhttp = Ajax.getTransport();
20 var xmlhttp_ctr = Ajax.getTransport();
21
22 var init_params = new Object();
23
24 //var op_history = new Array();
25
26 function tagsAreDisplayed() {
27 return display_tags;
28 }
29
30 function toggleTags(show_all) {
31
32 var p = document.getElementById("dispSwitchPrompt");
33
34 if (!show_all && !display_tags) {
35 displayDlg("printTagCloud");
36 } else if (show_all) {
37 closeInfoBox();
38 display_tags = true;
39 p.innerHTML = __("display feeds");
40 notify_progress("Loading, please wait...", true);
41 updateFeedList();
42 } else if (display_tags) {
43 display_tags = false;
44 p.innerHTML = __("tag cloud");
45 notify_progress("Loading, please wait...", true);
46 updateFeedList();
47 }
48 }
49
50 function dlg_frefresh_callback() {
51 if (xmlhttp.readyState == 4) {
52 // notify(xmlhttp.responseText);
53
54 if (getActiveFeedId() == _qfd_deleted_feed) {
55 var h = document.getElementById("headlines-frame");
56 if (h) {
57 h.innerHTML = "<div class='whiteBox'>" + __('No feed selected.') + "</div>";
58 }
59 }
60
61 setTimeout('updateFeedList(false, false)', 50);
62 closeInfoBox();
63 }
64 }
65
66 function refetch_callback() {
67 if (xmlhttp_ctr.readyState == 4) {
68 try {
69
70 var date = new Date();
71
72 last_refetch = date.getTime() / 1000;
73
74 parse_counters_reply(xmlhttp_ctr, true);
75
76 debug("refetch_callback: done");
77
78 if (!daemon_enabled && !daemon_refresh_only) {
79 notify_info("All feeds updated.");
80 updateTitle("");
81 } else {
82 //notify("");
83 }
84 } catch (e) {
85 exception_error("refetch_callback", e);
86 updateTitle("");
87 }
88 }
89 }
90
91 function backend_sanity_check_callback() {
92
93 if (xmlhttp.readyState == 4) {
94
95 try {
96
97 if (sanity_check_done) {
98 fatalError(11, "Sanity check request received twice. This can indicate "+
99 "presence of Firebug or some other disrupting extension. "+
100 "Please disable it and try again.");
101 return;
102 }
103
104 if (!xmlhttp.responseXML) {
105 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
106 return;
107 }
108
109 var reply = xmlhttp.responseXML.firstChild.firstChild;
110
111 if (!reply) {
112 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
113 return;
114 }
115
116 var error_code = reply.getAttribute("error-code");
117
118 if (error_code && error_code != 0) {
119 return fatalError(error_code, reply.getAttribute("error-msg"));
120 }
121
122 debug("sanity check ok");
123
124 var params = reply.nextSibling;
125
126 if (params) {
127 debug('reading init-params...');
128 var param = params.firstChild;
129
130 while (param) {
131 var k = param.getAttribute("key");
132 var v = param.getAttribute("value");
133 debug(k + " => " + v);
134 init_params[k] = v;
135 param = param.nextSibling;
136 }
137 }
138
139 sanity_check_done = true;
140
141 init_second_stage();
142
143 } catch (e) {
144 exception_error("backend_sanity_check_callback", e);
145 }
146 }
147 }
148
149 function scheduleFeedUpdate(force) {
150
151 if (!daemon_enabled && !daemon_refresh_only) {
152 notify_progress("Updating feeds, please wait.", true);
153 updateTitle("Updating");
154 }
155
156 var query_str = "backend.php?op=rpc&subop=";
157
158 if (force) {
159 query_str = query_str + "forceUpdateAllFeeds";
160 } else {
161 query_str = query_str + "updateAllFeeds";
162 }
163
164 var omode;
165
166 if (firsttime_update && !navigator.userAgent.match("Opera")) {
167 firsttime_update = false;
168 omode = "T";
169 } else {
170 if (display_tags) {
171 omode = "tl";
172 } else {
173 omode = "flc";
174 }
175 }
176
177 query_str = query_str + "&omode=" + omode;
178 query_str = query_str + "&uctr=" + global_unread;
179
180 debug("in scheduleFeedUpdate");
181
182 var date = new Date();
183
184 var timestamp = Math.round(date.getTime() / 1000);
185 query_str = query_str + "&ts=" + timestamp
186
187 if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
188 debug("<b>xmlhttp seems to be stuck, aborting</b>");
189 xmlhttp_ctr.abort();
190 }
191
192 debug("REFETCH query: " + query_str);
193
194 if (xmlhttp_ready(xmlhttp_ctr)) {
195 xmlhttp_ctr.open("GET", query_str, true);
196 xmlhttp_ctr.onreadystatechange=refetch_callback;
197 xmlhttp_ctr.send(null);
198 } else {
199 debug("xmlhttp_ctr busy");
200 //printLockingError();
201 }
202 }
203
204 function updateFeedList(silent, fetch) {
205
206 // if (silent != true) {
207 // notify("Loading feed list...");
208 // }
209
210 debug("<b>updateFeedList</b>");
211
212 var query_str = "backend.php?op=feeds";
213
214 if (display_tags) {
215 query_str = query_str + "&tags=1";
216 }
217
218 if (getActiveFeedId() && !activeFeedIsCat()) {
219 query_str = query_str + "&actid=" + getActiveFeedId();
220 }
221
222 var date = new Date();
223 var timestamp = Math.round(date.getTime() / 1000);
224 query_str = query_str + "&ts=" + timestamp
225
226 if (fetch) query_str = query_str + "&fetch=yes";
227
228 // var feeds_frame = document.getElementById("feeds-frame");
229 // feeds_frame.src = query_str;
230
231 debug("updateFeedList Q=" + query_str);
232
233 if (xmlhttp_ready(xmlhttp)) {
234 xmlhttp.open("GET", query_str, true);
235 xmlhttp.onreadystatechange=feedlist_callback;
236 xmlhttp.send(null);
237 } else {
238 debug("xmlhttp busy");
239 //printLockingError();
240 }
241
242 }
243
244 function catchupAllFeeds() {
245
246 var query_str = "backend.php?op=feeds&subop=catchupAll";
247
248 notify_progress("Marking all feeds as read...");
249
250 debug("catchupAllFeeds Q=" + query_str);
251
252 if (xmlhttp_ready(xmlhttp)) {
253 xmlhttp.open("GET", query_str, true);
254 xmlhttp.onreadystatechange=feedlist_callback;
255 xmlhttp.send(null);
256 } else {
257 debug("xmlhttp busy");
258 //printLockingError();
259 }
260
261 global_unread = 0;
262 updateTitle("");
263
264 }
265
266 function viewCurrentFeed(subop) {
267
268 // if (getActiveFeedId()) {
269 if (getActiveFeedId() != undefined) {
270 viewfeed(getActiveFeedId(), subop);
271 } else {
272 disableContainerChildren("headlinesToolbar", false, document);
273 // viewfeed(-1, subop); // FIXME
274 }
275 return false; // block unneeded form submits
276 }
277
278 function viewfeed(feed, subop) {
279 var f = window.frames["feeds-frame"];
280 f.viewfeed(feed, subop);
281 }
282
283 function timeout() {
284 scheduleFeedUpdate(false);
285
286 var refresh_time = getInitParam("feeds_frame_refresh");
287
288 if (!refresh_time) refresh_time = 600;
289
290 setTimeout("timeout()", refresh_time*1000);
291 }
292
293 function resetSearch() {
294 var searchbox = document.getElementById("searchbox")
295
296 if (searchbox.value != "" && getActiveFeedId()) {
297 searchbox.value = "";
298 viewfeed(getActiveFeedId(), "");
299 }
300 }
301
302 function searchCancel() {
303 closeInfoBox(true);
304 }
305
306 function search() {
307 closeInfoBox();
308 viewCurrentFeed(0, "");
309 }
310
311 function localPiggieFunction(enable) {
312 if (enable) {
313 var query_str = "backend.php?op=feeds&subop=piggie";
314
315 if (xmlhttp_ready(xmlhttp)) {
316
317 xmlhttp.open("GET", query_str, true);
318 xmlhttp.onreadystatechange=feedlist_callback;
319 xmlhttp.send(null);
320 }
321 }
322 }
323
324 // if argument is undefined, current subtitle is not updated
325 // use blank string to clear subtitle
326 function updateTitle(s) {
327 var tmp = "Tiny Tiny RSS";
328
329 if (s != undefined) {
330 current_subtitle = s;
331 }
332
333 if (global_unread > 0) {
334 tmp = tmp + " (" + global_unread + ")";
335 }
336
337 if (current_subtitle) {
338 tmp = tmp + " - " + current_subtitle;
339 }
340
341 if (active_title_text.length > 0) {
342 tmp = tmp + " > " + active_title_text;
343 }
344
345 document.title = tmp;
346 }
347
348 function genericSanityCheck() {
349
350 if (!xmlhttp) fatalError(1);
351
352 setCookie("ttrss_vf_test", "TEST");
353
354 if (getCookie("ttrss_vf_test") != "TEST") {
355 fatalError(2);
356 }
357
358 return true;
359 }
360
361 function init() {
362
363 try {
364
365 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
366
367 if (arguments.callee.done) return;
368 arguments.callee.done = true;
369
370 disableContainerChildren("headlinesToolbar", true);
371
372 Form.disable("main_toolbar_form");
373
374 if (!genericSanityCheck())
375 return;
376
377 if (getURLParam('debug')) {
378 document.getElementById('debug_output').style.display = 'block';
379 debug('debug mode activated');
380 }
381
382 var params = "&ua=" + param_escape(navigator.userAgent);
383
384 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck" + params, true);
385 xmlhttp.onreadystatechange=backend_sanity_check_callback;
386 xmlhttp.send(null);
387
388 } catch (e) {
389 exception_error("init", e);
390 }
391 }
392
393 function resize_headlines() {
394
395 var h_frame = document.getElementById("headlines-frame");
396 var c_frame = document.getElementById("content-frame");
397 var f_frame = document.getElementById("footer");
398
399 if (!c_frame || !h_frame) return;
400
401 debug("resize_headlines");
402
403 if (!is_msie()) {
404 h_frame.style.height = 30 + "%";
405 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
406 h_frame.style.height = h_frame.offsetHeight + "px";
407 } else {
408 h_frame.style.height = document.documentElement.clientHeight * 0.3 + "px";
409 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
410
411 var c_bottom = document.documentElement.clientHeight;
412
413 if (f_frame) {
414 c_bottom = f_frame.offsetTop;
415 }
416
417 c_frame.style.height = c_bottom - (h_frame.offsetTop +
418 h_frame.offsetHeight + 1) + "px";
419 h_frame.style.height = h_frame.offsetHeight + "px";
420
421 }
422 }
423
424 function init_second_stage() {
425
426 try {
427
428 cookie_lifetime = getCookie("ttrss_cltime");
429
430 delCookie("ttrss_vf_test");
431
432 // document.onresize = resize_headlines;
433 resize_headlines();
434
435 var toolbar = document.forms["main_toolbar_form"];
436
437 dropboxSelect(toolbar.view_mode, getInitParam("default_view_mode"));
438 dropboxSelect(toolbar.limit, getInitParam("default_view_limit"));
439
440 daemon_enabled = getInitParam("daemon_enabled") == 1;
441 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
442
443 setTimeout('updateFeedList(false, false)', 50);
444
445 debug("second stage ok");
446
447 } catch (e) {
448 exception_error("init_second_stage", e);
449 }
450 }
451
452 function quickMenuChange() {
453 var chooser = document.getElementById("quickMenuChooser");
454 var opid = chooser[chooser.selectedIndex].value;
455
456 chooser.selectedIndex = 0;
457 quickMenuGo(opid);
458 }
459
460 function quickMenuGo(opid) {
461 try {
462
463 if (opid == "qmcPrefs") {
464 gotoPreferences();
465 }
466
467 if (opid == "qmcSearch") {
468 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
469 return;
470 }
471
472 if (opid == "qmcAddFeed") {
473 displayDlg("quickAddFeed");
474 return;
475 }
476
477 if (opid == "qmcEditFeed") {
478 editFeedDlg(getActiveFeedId());
479 }
480
481 if (opid == "qmcRemoveFeed") {
482 var actid = getActiveFeedId();
483
484 if (activeFeedIsCat()) {
485 alert("You can't unsubscribe from the category.");
486 return;
487 }
488
489 if (!actid) {
490 alert("Please select some feed first.");
491 return;
492 }
493
494 var fn = getFeedName(actid);
495
496 if (confirm("Unsubscribe from " + fn + "?")) {
497 qfdDelete(actid);
498 }
499
500 return;
501 }
502
503 if (opid == "qmcUpdateFeeds") {
504 scheduleFeedUpdate(true);
505 return;
506 }
507
508 if (opid == "qmcCatchupAll") {
509 catchupAllFeeds();
510 return;
511 }
512
513 if (opid == "qmcShowOnlyUnread") {
514 toggleDispRead();
515 return;
516 }
517
518 if (opid == "qmcAddFilter") {
519 displayDlg("quickAddFilter", getActiveFeedId());
520 }
521
522 } catch (e) {
523 exception_error("quickMenuGo", e);
524 }
525 }
526
527 function qfdDelete(feed_id) {
528
529 notify_progress("Removing feed...");
530
531 if (!xmlhttp_ready(xmlhttp)) {
532 printLockingError();
533 return
534 }
535
536 _qfd_deleted_feed = feed_id;
537
538 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
539 xmlhttp.onreadystatechange=dlg_frefresh_callback;
540 xmlhttp.send(null);
541
542 return false;
543 }
544
545
546 function updateFeedTitle(t) {
547 active_title_text = t;
548 updateTitle();
549 }
550
551 function toggleDispRead() {
552 try {
553
554 if (!xmlhttp_ready(xmlhttp)) {
555 printLockingError();
556 return
557 }
558
559 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
560
561 hide_read_feeds = !hide_read_feeds;
562
563 debug("toggle_disp_read => " + hide_read_feeds);
564
565 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
566
567 storeInitParam("hide_read_feeds", hide_read_feeds, true);
568
569 /* var query = "backend.php?op=rpc&subop=setpref" +
570 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
571
572 new Ajax.Request(query); */
573
574 } catch (e) {
575 exception_error("toggleDispRead", e);
576 }
577 }
578
579 function parse_runtime_info(elem) {
580 if (!elem) {
581 debug("parse_runtime_info: elem is null, aborting");
582 return;
583 }
584
585 var param = elem.firstChild;
586
587 debug("parse_runtime_info: " + param);
588
589 while (param) {
590 var k = param.getAttribute("key");
591 var v = param.getAttribute("value");
592
593 debug("RI: " + k + " => " + v);
594
595 if (k == "new_version_available") {
596 var icon = document.getElementById("newVersionIcon");
597 if (icon) {
598 if (v == "1") {
599 icon.style.display = "inline";
600 } else {
601 icon.style.display = "none";
602 }
603 }
604 }
605
606 if (k == "daemon_is_running" && v != 1) {
607 notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not running.</span>");
608 } else {
609 notify('');
610 }
611
612 /* var w = document.getElementById("noDaemonWarning");
613
614 if (w) {
615 if (k == "daemon_is_running" && v != 1) {
616 w.style.display = "block";
617 } else {
618 w.style.display = "none";
619 }
620 } */
621 param = param.nextSibling;
622 }
623 }
624
625 function catchupCurrentFeed() {
626
627 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
628
629 var str = "Mark all articles in " + fn + " as read?";
630
631 /* if (active_feed_is_cat) {
632 str = "Mark all articles in this category as read?";
633 } */
634
635 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
636 return viewCurrentFeed('MarkAllRead')
637 }
638 }
639
640 function userSwitch() {
641 var chooser = document.getElementById("userSwitch");
642 var user = chooser[chooser.selectedIndex].value;
643 window.location = "tt-rss.php?swu=" + user;
644 }
645
646 function editFeedDlg(feed) {
647
648 disableHotkeys();
649
650 if (!feed) {
651 alert("Please select some feed first.");
652 return;
653 }
654
655 if (feed <= 0 || activeFeedIsCat() || tagsAreDisplayed()) {
656 alert("You can't edit this kind of feed.");
657 return;
658 }
659
660 if (xmlhttp_ready(xmlhttp)) {
661 xmlhttp.open("GET", "backend.php?op=pref-feeds&subop=editfeed&id=" +
662 param_escape(feed), true);
663 xmlhttp.onreadystatechange=infobox_callback;
664 xmlhttp.send(null);
665 } else {
666 printLockingError();
667 }
668 }
669
670 /* this functions duplicate those of prefs.js feed editor, with
671 some differences because there is no feedlist */
672
673 function feedEditCancel() {
674 closeInfoBox();
675 return false;
676 }
677
678 function feedEditSave() {
679
680 try {
681
682 if (!xmlhttp_ready(xmlhttp)) {
683 printLockingError();
684 return
685 }
686
687 // FIXME: add parameter validation
688
689 var query = Form.serialize("edit_feed_form");
690
691 notify_progress("Saving feed...");
692
693 xmlhttp.open("POST", "backend.php", true);
694 xmlhttp.onreadystatechange=dlg_frefresh_callback;
695 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
696 xmlhttp.send(query);
697
698 closeInfoBox();
699
700 return false;
701
702 } catch (e) {
703 exception_error("feedEditSave (main)", e);
704 }
705 }
706 /*
707 function localHotkeyHandler(e) {
708
709 var keycode;
710
711 if (window.event) {
712 keycode = window.event.keyCode;
713 } else if (e) {
714 keycode = e.which;
715 }
716
717 var shift_key = false;
718
719 try {
720 shift_key = e.shiftKey;
721 } catch (e) { }
722
723 if (keycode == 66 && shift_key) { // shift-B
724
725 var op = history_pop();
726
727 if (op) {
728 var op_s = op.split(":");
729
730 var i;
731 for (i = 0; i < op_s.length; i++) {
732 if (op_s[i] == 'undefined') {
733 op_s[i] = undefined;
734 }
735
736 if (op_s[i] == 'false') {
737 op_s[i] = false;
738 }
739
740 if (op_s[i] == 'true') {
741 op_s[i] = true;
742 }
743
744 }
745
746 debug("history split: " + op_s);
747
748 if (op_s[0] == "ARTICLE") {
749 debug("history: reverting to article " + op_s[1] + "/" + op_s[2]);
750 view(op_s[1], op_s[2], true);
751 }
752
753 if (op_s[0] == "FEED") {
754 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
755 }
756
757 } else {
758 notify_error("No operation to undo");
759 }
760
761 return false;
762
763 }
764
765 debug("LKP=" + keycode);
766 }
767
768 function history_push(op) {
769 debug("history_push: " + op);
770 op_history.push(op);
771
772 while (op_history.length > 30) {
773 op_history.shift();
774 }
775 }
776
777 function history_pop() {
778 var op = op_history.pop();
779 debug("history_pop: " + op);
780 return op;
781 }
782
783 function history_clear() {
784 debug("history_clear");
785 op_history.clear();
786 } */