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