]> git.wh0rd.org - tt-rss.git/blob - tt-rss.js
code cleanups, make feedlist_callback async
[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 cookie_lifetime = 0;
13 var active_feed_id = 0;
14 var active_feed_is_cat = false;
15 var number_of_feeds = 0;
16 var sanity_check_done = false;
17
18 var xmlhttp = Ajax.getTransport();
19
20 var init_params = new Object();
21
22 function tagsAreDisplayed() {
23 return display_tags;
24 }
25
26 function toggleTags(show_all) {
27
28 try {
29
30 debug("toggleTags: " + show_all + "; " + display_tags);
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 } catch (e) {
50 exception_error("toggleTags", e);
51 }
52 }
53
54 function dlg_frefresh_callback() {
55 if (xmlhttp.readyState == 4) {
56 // notify(xmlhttp.responseText);
57
58 if (getActiveFeedId() == _qfd_deleted_feed) {
59 var h = document.getElementById("headlines-frame");
60 if (h) {
61 h.innerHTML = "<div class='whiteBox'>" + __('No feed selected.') + "</div>";
62 }
63 }
64
65 setTimeout('updateFeedList(false, false)', 50);
66 closeInfoBox();
67 }
68 }
69
70 function refetch_callback2(transport) {
71 try {
72
73 var date = new Date();
74
75 parse_counters_reply(transport, true);
76
77 debug("refetch_callback2: done");
78
79 if (!daemon_enabled && !daemon_refresh_only) {
80 notify_info("All feeds updated.");
81 updateTitle("");
82 } else {
83 //notify("");
84 }
85 } catch (e) {
86 exception_error("refetch_callback", e);
87 updateTitle("");
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 debug("in scheduleFeedUpdate");
152
153 if (!daemon_enabled && !daemon_refresh_only) {
154 notify_progress("Updating feeds...", true);
155 }
156
157 var query_str = "backend.php?op=rpc&subop=";
158
159 if (force) {
160 query_str = query_str + "forceUpdateAllFeeds";
161 } else {
162 query_str = query_str + "updateAllFeeds";
163 }
164
165 var omode;
166
167 if (firsttime_update && !navigator.userAgent.match("Opera")) {
168 firsttime_update = false;
169 omode = "T";
170 } else {
171 if (display_tags) {
172 omode = "tl";
173 } else {
174 omode = "flc";
175 }
176 }
177
178 query_str = query_str + "&omode=" + omode;
179 query_str = query_str + "&uctr=" + global_unread;
180
181 var date = new Date();
182 var timestamp = Math.round(date.getTime() / 1000);
183 query_str = query_str + "&ts=" + timestamp
184
185 debug("REFETCH query: " + query_str);
186
187 new Ajax.Request(query_str, {
188 onComplete: function(transport) {
189 refetch_callback2(transport);
190 } });
191 }
192
193 function updateFeedList(silent, fetch) {
194
195 // if (silent != true) {
196 // notify("Loading feed list...");
197 // }
198
199 debug("<b>updateFeedList</b>");
200
201 var query_str = "backend.php?op=feeds";
202
203 if (display_tags) {
204 query_str = query_str + "&tags=1";
205 }
206
207 if (getActiveFeedId() && !activeFeedIsCat()) {
208 query_str = query_str + "&actid=" + getActiveFeedId();
209 }
210
211 var date = new Date();
212 var timestamp = Math.round(date.getTime() / 1000);
213 query_str = query_str + "&ts=" + timestamp
214
215 if (fetch) query_str = query_str + "&fetch=yes";
216
217 // var feeds_frame = document.getElementById("feeds-frame");
218 // feeds_frame.src = query_str;
219
220 debug("updateFeedList Q=" + query_str);
221
222 new Ajax.Request(query_str, {
223 onComplete: function(transport) {
224 feedlist_callback2(transport);
225 } });
226
227 }
228
229 function catchupAllFeeds() {
230
231 var query_str = "backend.php?op=feeds&subop=catchupAll";
232
233 notify_progress("Marking all feeds as read...");
234
235 debug("catchupAllFeeds Q=" + query_str);
236
237 new Ajax.Request(query_str, {
238 onComplete: function(transport) {
239 feedlist_callback2(transport);
240 } });
241
242 global_unread = 0;
243 updateTitle("");
244
245 }
246
247 function viewCurrentFeed(subop) {
248
249 // if (getActiveFeedId()) {
250 if (getActiveFeedId() != undefined) {
251 viewfeed(getActiveFeedId(), subop);
252 } else {
253 disableContainerChildren("headlinesToolbar", false, document);
254 // viewfeed(-1, subop); // FIXME
255 }
256 return false; // block unneeded form submits
257 }
258
259 function viewfeed(feed, subop) {
260 var f = window.frames["feeds-frame"];
261 f.viewfeed(feed, subop);
262 }
263
264 function timeout() {
265 scheduleFeedUpdate(false);
266
267 var refresh_time = getInitParam("feeds_frame_refresh");
268
269 if (!refresh_time) refresh_time = 600;
270
271 setTimeout("timeout()", refresh_time*1000);
272 }
273
274 function resetSearch() {
275 var searchbox = document.getElementById("searchbox")
276
277 if (searchbox.value != "" && getActiveFeedId()) {
278 searchbox.value = "";
279 viewfeed(getActiveFeedId(), "");
280 }
281 }
282
283 function searchCancel() {
284 closeInfoBox(true);
285 }
286
287 function search() {
288 closeInfoBox();
289 viewCurrentFeed(0, "");
290 }
291
292 // if argument is undefined, current subtitle is not updated
293 // use blank string to clear subtitle
294 function updateTitle(s) {
295 var tmp = "Tiny Tiny RSS";
296
297 if (s != undefined) {
298 current_subtitle = s;
299 }
300
301 if (global_unread > 0) {
302 tmp = tmp + " (" + global_unread + ")";
303 }
304
305 if (current_subtitle) {
306 tmp = tmp + " - " + current_subtitle;
307 }
308
309 if (active_title_text.length > 0) {
310 tmp = tmp + " > " + active_title_text;
311 }
312
313 document.title = tmp;
314 }
315
316 function genericSanityCheck() {
317
318 if (!xmlhttp) fatalError(1);
319
320 setCookie("ttrss_vf_test", "TEST");
321
322 if (getCookie("ttrss_vf_test") != "TEST") {
323 fatalError(2);
324 }
325
326 return true;
327 }
328
329 function init() {
330
331 try {
332
333 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
334
335 if (arguments.callee.done) return;
336 arguments.callee.done = true;
337
338 disableContainerChildren("headlinesToolbar", true);
339
340 Form.disable("main_toolbar_form");
341
342 if (!genericSanityCheck())
343 return;
344
345 if (getURLParam('debug')) {
346 document.getElementById('debug_output').style.display = 'block';
347 debug('debug mode activated');
348 }
349
350 var params = "&ua=" + param_escape(navigator.userAgent);
351
352 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck" + params, true);
353 xmlhttp.onreadystatechange=backend_sanity_check_callback;
354 xmlhttp.send(null);
355
356 } catch (e) {
357 exception_error("init", e);
358 }
359 }
360
361 function resize_headlines() {
362
363 var h_frame = document.getElementById("headlines-frame");
364 var c_frame = document.getElementById("content-frame");
365 var f_frame = document.getElementById("footer");
366
367 if (!c_frame || !h_frame) return;
368
369 debug("resize_headlines");
370
371 if (!is_msie()) {
372 h_frame.style.height = 30 + "%";
373 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
374 h_frame.style.height = h_frame.offsetHeight + "px";
375 } else {
376 h_frame.style.height = document.documentElement.clientHeight * 0.3 + "px";
377 c_frame.style.top = h_frame.offsetTop + h_frame.offsetHeight + 1 + "px";
378
379 var c_bottom = document.documentElement.clientHeight;
380
381 if (f_frame) {
382 c_bottom = f_frame.offsetTop;
383 }
384
385 c_frame.style.height = c_bottom - (h_frame.offsetTop +
386 h_frame.offsetHeight + 1) + "px";
387 h_frame.style.height = h_frame.offsetHeight + "px";
388
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 // document.onresize = resize_headlines;
401 resize_headlines();
402
403 var toolbar = document.forms["main_toolbar_form"];
404
405 dropboxSelect(toolbar.view_mode, getInitParam("default_view_mode"));
406 dropboxSelect(toolbar.limit, getInitParam("default_view_limit"));
407
408 daemon_enabled = getInitParam("daemon_enabled") == 1;
409 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
410
411 setTimeout('updateFeedList(false, false)', 50);
412
413 debug("second stage ok");
414
415 } catch (e) {
416 exception_error("init_second_stage", e);
417 }
418 }
419
420 function quickMenuChange() {
421 var chooser = document.getElementById("quickMenuChooser");
422 var opid = chooser[chooser.selectedIndex].value;
423
424 chooser.selectedIndex = 0;
425 quickMenuGo(opid);
426 }
427
428 function quickMenuGo(opid) {
429 try {
430
431 if (opid == "qmcPrefs") {
432 gotoPreferences();
433 }
434
435 if (opid == "qmcSearch") {
436 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
437 return;
438 }
439
440 if (opid == "qmcAddFeed") {
441 displayDlg("quickAddFeed");
442 return;
443 }
444
445 if (opid == "qmcEditFeed") {
446 editFeedDlg(getActiveFeedId());
447 }
448
449 if (opid == "qmcRemoveFeed") {
450 var actid = getActiveFeedId();
451
452 if (activeFeedIsCat()) {
453 alert(__("You can't unsubscribe from the category."));
454 return;
455 }
456
457 if (!actid) {
458 alert(__("Please select some feed first."));
459 return;
460 }
461
462 var fn = getFeedName(actid);
463
464 var pr = __("Unsubscribe from %s?").replace("%s", fn);
465
466 if (confirm(pr)) {
467 qfdDelete(actid);
468 }
469
470 return;
471 }
472
473 if (opid == "qmcUpdateFeeds") {
474 scheduleFeedUpdate(true);
475 return;
476 }
477
478 if (opid == "qmcCatchupAll") {
479 catchupAllFeeds();
480 return;
481 }
482
483 if (opid == "qmcShowOnlyUnread") {
484 toggleDispRead();
485 return;
486 }
487
488 if (opid == "qmcAddFilter") {
489 displayDlg("quickAddFilter", getActiveFeedId());
490 }
491
492 } catch (e) {
493 exception_error("quickMenuGo", e);
494 }
495 }
496
497 function qfdDelete(feed_id) {
498
499 notify_progress("Removing feed...");
500
501 if (!xmlhttp_ready(xmlhttp)) {
502 printLockingError();
503 return
504 }
505
506 _qfd_deleted_feed = feed_id;
507
508 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
509 xmlhttp.onreadystatechange=dlg_frefresh_callback;
510 xmlhttp.send(null);
511
512 return false;
513 }
514
515
516 function updateFeedTitle(t) {
517 active_title_text = t;
518 updateTitle();
519 }
520
521 function toggleDispRead() {
522 try {
523
524 if (!xmlhttp_ready(xmlhttp)) {
525 printLockingError();
526 return
527 }
528
529 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
530
531 hide_read_feeds = !hide_read_feeds;
532
533 debug("toggle_disp_read => " + hide_read_feeds);
534
535 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
536
537 storeInitParam("hide_read_feeds", hide_read_feeds, true);
538
539 /* var query = "backend.php?op=rpc&subop=setpref" +
540 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
541
542 new Ajax.Request(query); */
543
544 } catch (e) {
545 exception_error("toggleDispRead", e);
546 }
547 }
548
549 function parse_runtime_info(elem) {
550 if (!elem) {
551 debug("parse_runtime_info: elem is null, aborting");
552 return;
553 }
554
555 var param = elem.firstChild;
556
557 debug("parse_runtime_info: " + param);
558
559 while (param) {
560 var k = param.getAttribute("key");
561 var v = param.getAttribute("value");
562
563 debug("RI: " + k + " => " + v);
564
565 if (k == "new_version_available") {
566 var icon = document.getElementById("newVersionIcon");
567 if (icon) {
568 if (v == "1") {
569 icon.style.display = "inline";
570 } else {
571 icon.style.display = "none";
572 }
573 }
574 }
575
576 var error_flag;
577
578 if (k == "daemon_is_running" && v != 1) {
579 notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not running.</span>", true);
580 error_flag = true;
581 }
582
583 if (k == "daemon_stamp_ok" && v != 1) {
584 notify_error("<span onclick=\"javascript:explainError(3)\">Update daemon is not updating feeds.</span>", true);
585 error_flag = true;
586 }
587
588 if (!error_flag) {
589 notify('');
590 }
591
592 /* var w = document.getElementById("noDaemonWarning");
593
594 if (w) {
595 if (k == "daemon_is_running" && v != 1) {
596 w.style.display = "block";
597 } else {
598 w.style.display = "none";
599 }
600 } */
601 param = param.nextSibling;
602 }
603 }
604
605 function catchupCurrentFeed() {
606
607 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
608
609 var str = "Mark all articles in " + fn + " as read?";
610
611 /* if (active_feed_is_cat) {
612 str = "Mark all articles in this category as read?";
613 } */
614
615 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
616 return viewCurrentFeed('MarkAllRead')
617 }
618 }
619
620 function editFeedDlg(feed) {
621 try {
622
623 disableHotkeys();
624
625 if (!feed) {
626 alert(__("Please select some feed first."));
627 return;
628 }
629
630 if ((feed <= 0 && feed > -10) || activeFeedIsCat() || tagsAreDisplayed()) {
631 alert(__("You can't edit this kind of feed."));
632 return;
633 }
634
635 var query = "";
636
637 if (feed > 0) {
638 query = "backend.php?op=pref-feeds&subop=editfeed&id=" + param_escape(feed);
639 } else {
640 query = "backend.php?op=pref-labels&subop=edit&id=" + param_escape(-feed-11);
641 }
642
643 new Ajax.Request(query, {
644 onComplete: function(transport) {
645 infobox_callback2(transport);
646 } });
647
648 } catch (e) {
649 exception_error("editFeedDlg", e);
650 }
651 }
652
653 /* this functions duplicate those of prefs.js feed editor, with
654 some differences because there is no feedlist */
655
656 function feedEditCancel() {
657 closeInfoBox();
658 return false;
659 }
660
661 function feedEditSave() {
662
663 try {
664
665 if (!xmlhttp_ready(xmlhttp)) {
666 printLockingError();
667 return
668 }
669
670 // FIXME: add parameter validation
671
672 var query = Form.serialize("edit_feed_form");
673
674 notify_progress("Saving feed...");
675
676 xmlhttp.open("POST", "backend.php", true);
677 xmlhttp.onreadystatechange=dlg_frefresh_callback;
678 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
679 xmlhttp.send(query);
680
681 closeInfoBox();
682
683 return false;
684
685 } catch (e) {
686 exception_error("feedEditSave (main)", e);
687 }
688 }
689
690 function labelEditCancel() {
691 closeInfoBox();
692 return false;
693 }
694
695 function labelEditSave() {
696
697 try {
698
699 if (!xmlhttp_ready(xmlhttp)) {
700 printLockingError();
701 return
702 }
703
704 closeInfoBox();
705
706 notify_progress("Saving label...");
707
708 query = Form.serialize("label_edit_form");
709
710 xmlhttp.open("GET", "backend.php?" + query, true);
711 xmlhttp.onreadystatechange=dlg_frefresh_callback;
712 xmlhttp.send(null);
713
714 return false;
715
716 } catch (e) {
717 exception_error("feedEditSave (main)", e);
718 }
719
720 }
721