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;
13 var cookie_lifetime = 0;
14 var active_feed_id = 0;
15 var active_feed_is_cat = false;
16 var number_of_feeds = 0;
18 var xmlhttp = Ajax.getTransport();
19 var xmlhttp_ctr = Ajax.getTransport();
21 var init_params = new Object();
23 var op_history = new Array();
25 function tagsAreDisplayed() {
29 function toggleTags() {
30 display_tags = !display_tags;
32 var p = document.getElementById("dispSwitchPrompt");
35 p.innerHTML = __("display feeds");
37 p.innerHTML = __("display tags");
40 notify_progress("Loading, please wait...");
45 function dlg_frefresh_callback() {
46 if (xmlhttp.readyState == 4) {
47 // notify(xmlhttp.responseText);
49 if (getActiveFeedId() == _qfd_deleted_feed) {
50 var h = document.getElementById("headlines-frame");
52 h.innerHTML = "<div class='whiteBox'>No feed selected.</div>";
56 setTimeout('updateFeedList(false, false)', 50);
61 function refetch_callback() {
62 if (xmlhttp_ctr.readyState == 4) {
65 var date = new Date();
67 last_refetch = date.getTime() / 1000;
69 parse_counters_reply(xmlhttp_ctr, true);
71 debug("refetch_callback: done");
73 if (!daemon_enabled && !daemon_refresh_only) {
74 notify_info("All feeds updated.");
80 exception_error("refetch_callback", e);
86 function backend_sanity_check_callback() {
88 if (xmlhttp.readyState == 4) {
92 if (!xmlhttp.responseXML) {
93 fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
97 var reply = xmlhttp.responseXML.firstChild.firstChild;
100 fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
104 var error_code = reply.getAttribute("error-code");
106 if (error_code && error_code != 0) {
107 return fatalError(error_code, reply.getAttribute("error-msg"));
110 debug("sanity check ok");
112 var params = reply.nextSibling;
115 debug('reading init-params...');
116 var param = params.firstChild;
119 var k = param.getAttribute("key");
120 var v = param.getAttribute("value");
121 debug(k + " => " + v);
123 param = param.nextSibling;
130 exception_error("backend_sanity_check_callback", e);
135 function scheduleFeedUpdate(force) {
137 if (!daemon_enabled && !daemon_refresh_only) {
138 notify_progress("Updating feeds, please wait.", true);
139 updateTitle("Updating");
142 var query_str = "backend.php?op=rpc&subop=";
145 query_str = query_str + "forceUpdateAllFeeds";
147 query_str = query_str + "updateAllFeeds";
152 if (firsttime_update && !navigator.userAgent.match("Opera")) {
153 firsttime_update = false;
163 query_str = query_str + "&omode=" + omode;
164 query_str = query_str + "&uctr=" + global_unread;
166 debug("in scheduleFeedUpdate");
168 var date = new Date();
170 var timestamp = Math.round(date.getTime() / 1000);
171 query_str = query_str + "&ts=" + timestamp
173 if (!xmlhttp_ready(xmlhttp_ctr) && last_refetch < date.getTime() / 1000 - 60) {
174 debug("<b>xmlhttp seems to be stuck, aborting</b>");
178 debug("REFETCH query: " + query_str);
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);
185 debug("xmlhttp_ctr busy");
186 //printLockingError();
190 function updateFeedList(silent, fetch) {
192 // if (silent != true) {
193 // notify("Loading feed list...");
196 debug("<b>updateFeedList</b>");
198 var query_str = "backend.php?op=feeds";
201 query_str = query_str + "&tags=1";
204 if (getActiveFeedId() && !activeFeedIsCat()) {
205 query_str = query_str + "&actid=" + getActiveFeedId();
208 var date = new Date();
209 var timestamp = Math.round(date.getTime() / 1000);
210 query_str = query_str + "&ts=" + timestamp
212 if (fetch) query_str = query_str + "&fetch=yes";
214 // var feeds_frame = document.getElementById("feeds-frame");
215 // feeds_frame.src = query_str;
217 debug("updateFeedList Q=" + query_str);
219 if (xmlhttp_ready(xmlhttp)) {
220 xmlhttp.open("GET", query_str, true);
221 xmlhttp.onreadystatechange=feedlist_callback;
224 debug("xmlhttp busy");
225 //printLockingError();
230 function catchupAllFeeds() {
232 var query_str = "backend.php?op=feeds&subop=catchupAll";
234 notify_progress("Marking all feeds as read...");
236 debug("catchupAllFeeds Q=" + query_str);
238 if (xmlhttp_ready(xmlhttp)) {
239 xmlhttp.open("GET", query_str, true);
240 xmlhttp.onreadystatechange=feedlist_callback;
243 debug("xmlhttp busy");
244 //printLockingError();
252 function viewCurrentFeed(subop) {
254 // if (getActiveFeedId()) {
255 if (getActiveFeedId() != undefined) {
256 viewfeed(getActiveFeedId(), subop);
258 disableContainerChildren("headlinesToolbar", false, document);
259 // viewfeed(-1, subop); // FIXME
261 return false; // block unneeded form submits
264 function viewfeed(feed, subop) {
265 var f = window.frames["feeds-frame"];
266 f.viewfeed(feed, subop);
270 scheduleFeedUpdate(false);
272 var refresh_time = getInitParam("feeds_frame_refresh");
274 if (!refresh_time) refresh_time = 600;
276 setTimeout("timeout()", refresh_time*1000);
279 function resetSearch() {
280 var searchbox = document.getElementById("searchbox")
282 if (searchbox.value != "" && getActiveFeedId()) {
283 searchbox.value = "";
284 viewfeed(getActiveFeedId(), "");
288 function searchCancel() {
294 viewCurrentFeed(0, "");
297 function localPiggieFunction(enable) {
299 var query_str = "backend.php?op=feeds&subop=piggie";
301 if (xmlhttp_ready(xmlhttp)) {
303 xmlhttp.open("GET", query_str, true);
304 xmlhttp.onreadystatechange=feedlist_callback;
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";
315 if (s != undefined) {
316 current_subtitle = s;
319 if (global_unread > 0) {
320 tmp = tmp + " (" + global_unread + ")";
323 if (current_subtitle) {
324 tmp = tmp + " - " + current_subtitle;
327 if (active_title_text.length > 0) {
328 tmp = tmp + " > " + active_title_text;
331 document.title = tmp;
334 function genericSanityCheck() {
336 if (!xmlhttp) fatalError(1);
338 setCookie("ttrss_vf_test", "TEST");
340 if (getCookie("ttrss_vf_test") != "TEST") {
351 // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
353 if (arguments.callee.done) return;
354 arguments.callee.done = true;
356 disableContainerChildren("headlinesToolbar", true);
358 Form.disable("main_toolbar_form");
360 if (!genericSanityCheck())
363 if (getURLParam('debug')) {
364 document.getElementById('debug_output').style.display = 'block';
365 debug('debug mode activated');
368 var params = "&ua=" + param_escape(navigator.userAgent);
370 xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck" + params, true);
371 xmlhttp.onreadystatechange=backend_sanity_check_callback;
375 exception_error("init", e);
379 function resize_headlines() {
380 var h_frame = document.getElementById("headlines-frame");
381 var c_frame = document.getElementById("content-frame");
383 debug("resize_headlines");
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";
392 function init_second_stage() {
396 cookie_lifetime = getCookie("ttrss_cltime");
398 delCookie("ttrss_vf_test");
401 document.onresize = resize_headlines;
405 var toolbar = document.forms["main_toolbar_form"];
407 dropboxSelect(toolbar.view_mode, getInitParam("default_view_mode"));
408 dropboxSelect(toolbar.limit, getInitParam("default_view_limit"));
410 daemon_enabled = getInitParam("daemon_enabled") == 1;
411 daemon_refresh_only = getInitParam("daemon_refresh_only") == 1;
413 setTimeout('updateFeedList(false, false)', 50);
415 debug("second stage ok");
418 exception_error("init_second_stage", e);
422 function quickMenuChange() {
423 var chooser = document.getElementById("quickMenuChooser");
424 var opid = chooser[chooser.selectedIndex].value;
426 chooser.selectedIndex = 0;
430 function quickMenuGo(opid) {
433 if (opid == "qmcPrefs") {
437 if (opid == "qmcSearch") {
438 displayDlg("search", getActiveFeedId() + ":" + activeFeedIsCat());
442 if (opid == "qmcAddFeed") {
443 displayDlg("quickAddFeed");
447 if (opid == "qmcEditFeed") {
448 editFeedDlg(getActiveFeedId());
451 if (opid == "qmcRemoveFeed") {
452 var actid = getActiveFeedId();
454 if (!actid || activeFeedIsCat()) {
455 alert("Please select some feed first.");
459 var fn = getFeedName(actid);
461 if (confirm("Unsubscribe from " + fn + "?")) {
468 if (opid == "qmcUpdateFeeds") {
469 scheduleFeedUpdate(true);
473 if (opid == "qmcCatchupAll") {
478 if (opid == "qmcShowOnlyUnread") {
483 if (opid == "qmcAddFilter") {
484 displayDlg("quickAddFilter", getActiveFeedId());
487 exception_error("quickMenuGo", e);
491 function qfdDelete(feed_id) {
493 notify_progress("Removing feed...");
495 if (!xmlhttp_ready(xmlhttp)) {
500 _qfd_deleted_feed = feed_id;
502 xmlhttp.open("GET", "backend.php?op=pref-feeds&quiet=1&subop=remove&ids=" + feed_id);
503 xmlhttp.onreadystatechange=dlg_frefresh_callback;
510 function updateFeedTitle(t) {
511 active_title_text = t;
515 function toggleDispRead() {
518 if (!xmlhttp_ready(xmlhttp)) {
523 var hide_read_feeds = (getInitParam("hide_read_feeds") == "1");
525 hide_read_feeds = !hide_read_feeds;
527 debug("toggle_disp_read => " + hide_read_feeds);
529 hideOrShowFeeds(getFeedsContext().document, hide_read_feeds);
531 var query = "backend.php?op=rpc&subop=setpref" +
532 "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
534 storeInitParam("hide_read_feeds", hide_read_feeds, true);
536 new Ajax.Request(query);
539 exception_error("toggleDispRead", e);
543 function parse_runtime_info(elem) {
545 debug("parse_runtime_info: elem is null, aborting");
549 var param = elem.firstChild;
551 debug("parse_runtime_info: " + param);
554 var k = param.getAttribute("key");
555 var v = param.getAttribute("value");
557 debug("RI: " + k + " => " + v);
559 if (k == "new_version_available") {
560 var icon = document.getElementById("newVersionIcon");
563 icon.style.display = "inline";
565 icon.style.display = "none";
570 if (k == "daemon_is_running" && v != 1) {
571 notify_error("<span onclick=\"javascript:explainError(1)\">Update daemon is not runing.</span>");
574 /* var w = document.getElementById("noDaemonWarning");
577 if (k == "daemon_is_running" && v != 1) {
578 w.style.display = "block";
580 w.style.display = "none";
583 param = param.nextSibling;
587 function catchupCurrentFeed() {
589 var fn = getFeedName(getActiveFeedId(), active_feed_is_cat);
591 var str = "Mark all articles in " + fn + " as read?";
593 /* if (active_feed_is_cat) {
594 str = "Mark all articles in this category as read?";
597 if (getInitParam("confirm_feed_catchup") != 1 || confirm(str)) {
598 return viewCurrentFeed('MarkAllRead')
602 function userSwitch() {
603 var chooser = document.getElementById("userSwitch");
604 var user = chooser[chooser.selectedIndex].value;
605 window.location = "tt-rss.php?swu=" + user;
608 function editFeedDlg(feed) {
613 alert("Please select some feed first.");
617 if (feed <= 0 || active_feed_is_cat || tagsAreDisplayed()) {
618 alert("You can't edit this kind of feed.");
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;
632 /* this functions duplicate those of prefs.js feed editor, with
633 some differences because there is no feedlist */
635 function feedEditCancel() {
640 function feedEditSave() {
644 if (!xmlhttp_ready(xmlhttp)) {
649 // FIXME: add parameter validation
651 var query = Form.serialize("edit_feed_form");
653 notify_progress("Saving feed...");
655 xmlhttp.open("POST", "backend.php", true);
656 xmlhttp.onreadystatechange=dlg_frefresh_callback;
657 xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
665 exception_error("feedEditSave (main)", e);
669 function localHotkeyHandler(e) {
674 keycode = window.event.keyCode;
679 var shift_key = false;
682 shift_key = e.shiftKey;
685 if (keycode == 66 && shift_key) { // shift-B
687 var op = history_pop();
690 var op_s = op.split(":");
693 for (i = 0; i < op_s.length; i++) {
694 if (op_s[i] == 'undefined') {
698 if (op_s[i] == 'false') {
702 if (op_s[i] == 'true') {
708 debug("history split: " + op_s);
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);
715 if (op_s[0] == "FEED") {
716 viewfeed(op_s[1], op_s[2], op_s[3], op_s[4], true);
720 notify_error("No operation to undo");
727 debug("LKP=" + keycode);
730 function history_push(op) {
731 debug("history_push: " + op);
734 while (op_history.length > 30) {
739 function history_pop() {
740 var op = op_history.pop();
741 debug("history_pop: " + op);
745 function history_clear() {
746 debug("history_clear");