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