]> git.wh0rd.org - tt-rss.git/blobdiff - tt-rss.js
disable pref toolbars when nothing is selected
[tt-rss.git] / tt-rss.js
index 85c4564dffe93f6393a26db5db9c5c9cc23f306f..82a52e9424eebd7f4e4f8b2d0da71fc59d7a3935 100644 (file)
--- a/tt-rss.js
+++ b/tt-rss.js
@@ -5,8 +5,11 @@ var display_tags = false;
 var global_unread = -1;
 var active_title_text = "";
 var current_subtitle = "";
-
+var daemon_enabled = false;
 var _qfd_deleted_feed = 0;
+var firsttime_update = true;
+var last_refetch = 0;
+var cookie_lifetime = 0;
 
 /*@cc_on @*/
 /*@if (@_jscript_version >= 5)
@@ -55,20 +58,28 @@ function dlg_frefresh_callback() {
        } 
 }
 
-function dlg_submit_callback() {
+function hide_unread_callback() {
        if (xmlhttp.readyState == 4) {
-               notify(xmlhttp.responseText);
-               closeDlg();
-       } 
-}
 
-function dlg_display_callback() {
-       if (xmlhttp.readyState == 4) {
-               var dlg = document.getElementById("userDlg");
-               var dlg_s = document.getElementById("userDlgShadow");
+               try {
+
+                       var reply = xmlhttp.responseXML.firstChild.firstChild;
+                       var value = reply.getAttribute("value");
+                       var hide_read_feeds = (value != "false")
+                       var feeds_doc = window.frames["feeds-frame"].document;
+       
+                       hideOrShowFeeds(feeds_doc, hide_read_feeds);
+       
+                       if (hide_read_feeds) {
+                               setCookie("ttrss_vf_hreadf", 1);
+                       } else {
+                               setCookie("ttrss_vf_hreadf", 0);
+                       } 
+
+               } catch (e) {
+                       exception_error("hide_unread_callback", e);
+               }
 
-               dlg.innerHTML = xmlhttp.responseText;
-               dlg_s.style.display = "block";
        } 
 }
 
@@ -76,6 +87,10 @@ function refetch_callback() {
        if (xmlhttp.readyState == 4) {
                try {
 
+                       var date = new Date();
+
+                       last_refetch = date.getTime() / 1000;
+
                        if (!xmlhttp.responseXML) {
                                notify("refetch_callback: backend did not return valid XML");
                                return;
@@ -85,23 +100,31 @@ function refetch_callback() {
        
                        if (!reply) {
                                notify("refetch_callback: backend did not return expected XML object");
+                               updateTitle("");
                                return;
                        } 
        
                        var error_code = reply.getAttribute("error-code");
                
                        if (error_code && error_code != 0) {
-                               return fatalError(error_code);
+                               return fatalError(error_code, reply.getAttribute("error-msg"));
                        }
        
                        var f_document = window.frames["feeds-frame"].document;
 
-                       parse_counters(reply, f_document, window);
-       
-                       updateTitle("");
-                       notify("All feeds updated.");
+                       parse_counters(reply, f_document, window, true);
+
+                       debug("refetch_callback: done");
+
+                       if (!daemon_enabled) {
+                               notify("All feeds updated.");
+                               updateTitle("");
+                       } else {
+                               notify("");
+                       }
                } catch (e) {
                        exception_error("refetch_callback", e);
+                       updateTitle("");
                }
        }
 }
@@ -113,23 +136,25 @@ function backend_sanity_check_callback() {
                try {
                
                        if (!xmlhttp.responseXML) {
-                               fatalError(3);
+                               fatalError(3, "[D001, Received reply is not XML]: " + xmlhttp.responseText);
                                return;
                        }
        
                        var reply = xmlhttp.responseXML.firstChild;
        
                        if (!reply) {
-                               fatalError(3);
+                               fatalError(3, "[D002, Invalid RPC reply]: " + xmlhttp.responseText);
                                return;
                        }
        
                        var error_code = reply.getAttribute("error-code");
                
                        if (error_code && error_code != 0) {
-                               return fatalError(error_code);
+                               return fatalError(error_code, reply.getAttribute("error-msg"));
                        }
        
+                       debug("sanity check ok");
+
                        init_second_stage();
 
                } catch (e) {
@@ -140,11 +165,10 @@ function backend_sanity_check_callback() {
 
 function scheduleFeedUpdate(force) {
 
-       notify("Updating feeds, please wait.");
-
-//     document.title = "Tiny Tiny RSS - Updating...";
-
-       updateTitle("Updating");
+       if (!daemon_enabled) {
+               notify("Updating feeds, please wait.");
+               updateTitle("Updating");
+       }
 
        var query_str = "backend.php?op=rpc&subop=";
 
@@ -156,19 +180,35 @@ function scheduleFeedUpdate(force) {
 
        var omode;
 
-       if (display_tags) {
-               omode = "t";
+       if (firsttime_update && !navigator.userAgent.match("Opera")) {
+               firsttime_update = false;
+               omode = "T";
        } else {
-               omode = "fl";
+               if (display_tags) {
+                       omode = "t";
+               } else {
+                       omode = "flc";
+               }
        }
-
+       
        query_str = query_str + "&omode=" + omode;
+       query_str = query_str + "&uctr=" + global_unread;
+
+       debug("in scheduleFeedUpdate");
+
+       var date = new Date();
+
+       if (!xmlhttp_ready(xmlhttp) && last_refetch < date.getTime() / 1000 - 60) {
+               debug("xmlhttp seems to be stuck, aborting");
+               xmlhttp.abort();
+       }
 
        if (xmlhttp_ready(xmlhttp)) {
                xmlhttp.open("GET", query_str, true);
                xmlhttp.onreadystatechange=refetch_callback;
                xmlhttp.send(null);
        } else {
+               debug("xmlhttp busy");
                printLockingError();
        }   
 }
@@ -189,6 +229,12 @@ function updateFeedList(silent, fetch) {
                query_str = query_str + "&actid=" + getActiveFeedId();
        }
 
+       if (navigator.userAgent.match("Opera")) {
+               var date = new Date();
+               var timestamp = Math.round(date.getTime() / 1000);
+               query_str = query_str + "&ts=" + timestamp
+       }
+       
        if (fetch) query_str = query_str + "&fetch=yes";
 
        var feeds_frame = document.getElementById("feeds-frame");
@@ -228,7 +274,12 @@ function viewfeed(feed, skip, subop) {
 
 function timeout() {
        scheduleFeedUpdate(false);
-       setTimeout("timeout()", 900*1000);
+
+       var refresh_time = getCookie('ttrss_vf_refresh');
+
+       if (!refresh_time) refresh_time = 600;
+
+       setTimeout("timeout()", refresh_time*1000);
 }
 
 function resetSearch() {
@@ -241,7 +292,7 @@ function resetSearch() {
 }
 
 function search() {
-       closeDlg();
+       closeDlg();     
        viewCurrentFeed(0, "");
 }
 
@@ -260,14 +311,6 @@ function localPiggieFunction(enable) {
 
 function localHotkeyHandler(keycode) {
 
-/*     if (keycode == 78) {
-               return moveToPost('next');
-       }
-
-       if (keycode == 80) {
-               return moveToPost('prev');
-       } */
-
        if (keycode == 82) { // r
                return scheduleFeedUpdate(true);
        }
@@ -306,7 +349,7 @@ function localHotkeyHandler(keycode) {
 function updateTitle(s) {
        var tmp = "Tiny Tiny RSS";
 
-       if (s && s.length > 0) {
+       if (s != undefined) {
                current_subtitle = s;
        }
 
@@ -314,7 +357,7 @@ function updateTitle(s) {
                tmp = tmp + " (" + global_unread + ")";
        }
 
-       if (s) {
+       if (current_subtitle) {
                tmp = tmp + " - " + current_subtitle;
        }
 
@@ -342,11 +385,21 @@ function init() {
 
        try {
 
+               // this whole shebang is based on http://www.birnamdesigns.com/misc/busted2.html
+
+               if (arguments.callee.done) return;
+               arguments.callee.done = true;           
+
                disableContainerChildren("headlinesToolbar", true);
 
                if (!genericSanityCheck()) 
                        return;
 
+               if (getURLParam('debug')) {
+                       document.getElementById('debug_output').style.display = 'block';
+                       debug('debug mode activated');
+               }
+
                xmlhttp.open("GET", "backend.php?op=rpc&subop=sanityCheck", true);
                xmlhttp.onreadystatechange=backend_sanity_check_callback;
                xmlhttp.send(null);
@@ -356,10 +409,23 @@ function init() {
        }
 }
 
+function resize_feeds_frame() {
+       var f = document.getElementById("feeds-frame");
+       var tf = document.getElementById("mainFooter");
+       var th = document.getElementById("mainHeader");
+                
+       f.style.height = document.body.scrollHeight - tf.scrollHeight - 
+               th.scrollHeight - 50 + "px";
+}
+
 function init_second_stage() {
 
        try {
 
+               cookie_lifetime = getCookie("ttrss_cltime");
+
+               delCookie("ttrss_vf_test");
+
                setCookie("ttrss_vf_actfeed", "");
        
                updateFeedList(false, false);
@@ -370,65 +436,92 @@ function init_second_stage() {
 
                dropboxSelect(viewbox, getCookie("ttrss_vf_vmode"));
                dropboxSelect(limitbox, getCookie("ttrss_vf_limit"));
+
+               daemon_enabled = getCookie("ttrss_vf_daemon");
+
+               // FIXME should be callled after window resize
+
+               if (navigator.userAgent.match("Opera")) {
+                       resize_feeds_frame();
+
+/*                     // fix headlines frame height for Opera
+                       var h = document.getElementById("headlines");
+                       var c = document.getElementById("content");
+                       var nh = document.body.scrollHeight * 0.25;
+       
+                       h.style.height = nh + "px";
+                       c.style.height = c.scrollHeight - nh + "px"; */
+                       
+               }
+
+               debug("second stage ok");
        
        } catch (e) {
                exception_error("init_second_stage", e);
        }
 }
 
-function quickMenuGo() {
-
+function quickMenuChange() {
        var chooser = document.getElementById("quickMenuChooser");
        var opid = chooser[chooser.selectedIndex].id;
 
-       if (opid == "qmcPrefs") {
-               gotoPreferences();
-       }
-
-       if (opid == "qmcSearch") {
-               displayDlg("search");
-               return;
-       }
-
-       if (opid == "qmcAddFeed") {
-               displayDlg("quickAddFeed");
-               return;
-       }
+       chooser.selectedIndex = 0;
+       quickMenuGo(opid);
+}
 
-       if (opid == "qmcRemoveFeed") {
-               var actid = getActiveFeedId();
+function quickMenuGo(opid) {
+       try {
 
-               if (!actid) {
-                       notify("Please select some feed first.");
+               if (opid == "qmcPrefs") {
+                       gotoPreferences();
+               }
+       
+               if (opid == "qmcSearch") {
+                       displayDlg("search", getActiveFeedId());
                        return;
                }
-
-               if (confirm("Remove current feed?")) {
-                       qfdDelete(actid);
+       
+               if (opid == "qmcAddFeed") {
+                       displayDlg("quickAddFeed");
+                       return;
                }
        
-               return;
-       }
-
-       if (opid == "qmcUpdateFeeds") {
-               scheduleFeedUpdate(true);
-               return;
-       }
-
-       if (opid == "qmcCatchupAll") {
-               catchupAllFeeds();
-               return;
-       }
-
-       if (opid == "qmcShowOnlyUnread") {
-               toggleDispRead();
-               return;
-       }
-
-       if (opid == "qmcAddFilter") {
-               displayDlg("quickAddFilter", getActiveFeedId());
+               if (opid == "qmcRemoveFeed") {
+                       var actid = getActiveFeedId();
+       
+                       if (!actid) {
+                               notify("Please select some feed first.");
+                               return;
+                       }
+       
+                       if (confirm("Remove current feed?")) {
+                               qfdDelete(actid);
+                       }
+               
+                       return;
+               }
+       
+               if (opid == "qmcUpdateFeeds") {
+                       scheduleFeedUpdate(true);
+                       return;
+               }
+       
+               if (opid == "qmcCatchupAll") {
+                       catchupAllFeeds();
+                       return;
+               }
+       
+               if (opid == "qmcShowOnlyUnread") {
+                       toggleDispRead();
+                       return;
+               }
+       
+               if (opid == "qmcAddFilter") {
+                       displayDlg("quickAddFilter", getActiveFeedId());
+               }
+       } catch (e) {
+               exception_error("quickMenuGo", e);
        }
-
 }
 
 function qafAdd() {
@@ -441,10 +534,15 @@ function qafAdd() {
        var link = document.getElementById("qafInput");
 
        if (link.value.length == 0) {
-               notify("Missing feed URL.");
+               alert("Error: No feed URL given.");
+       } else if (link.value.match("http://") == null && 
+                       link.value.match("https://") == null) {
+               alert("Error: Invalid feed URL.");
        } else {
                notify("Adding feed...");
-       
+
+               closeInfoBox();
+
                var cat = document.getElementById("qafCat");
                var cat_id = "";
                
@@ -468,62 +566,15 @@ function qafAdd() {
        }
 }
 
-function qaddFilter() {
+function qfdDelete(feed_id) {
+
+       notify("Removing feed...");
 
        if (!xmlhttp_ready(xmlhttp)) {
                printLockingError();
                return
        }
 
-       var regexp = document.getElementById("fadd_regexp");
-       var match = document.getElementById("fadd_match");
-       var feed = document.getElementById("fadd_feed");
-       var action = document.getElementById("fadd_action");
-
-       if (regexp.value.length == 0) {
-               notify("Missing filter expression.");
-       } else {
-               notify("Adding filter...");
-
-               var v_match = match[match.selectedIndex].text;
-               var feed_id = feed[feed.selectedIndex].id;
-               var action_id = action[action.selectedIndex].id;
-
-               xmlhttp.open("GET", "backend.php?op=pref-filters&quiet=1&subop=add&regexp=" +
-                       param_escape(regexp.value) + "&match=" + v_match +
-                       "&fid=" + param_escape(feed_id) + "&aid=" + param_escape(action_id), true);
-                       
-               xmlhttp.onreadystatechange=dlg_submit_callback;
-               xmlhttp.send(null);
-
-               regexp.value = "";
-       }
-
-}
-
-
-function displayDlg(id, param) {
-
-       notify("");
-
-       xmlhttp.open("GET", "backend.php?op=dlg&id=" +
-               param_escape(id) + "&param=" + param_escape(param), true);
-       xmlhttp.onreadystatechange=dlg_display_callback;
-       xmlhttp.send(null);
-
-       disableHotkeys();
-}
-
-function closeDlg() {
-       var dlg = document.getElementById("userDlgShadow");
-       dlg.style.display = "none";
-       enableHotkeys();
-}
-
-function qfdDelete(feed_id) {
-
-       notify("Removing feed...");
-
 //     var feeds_doc = window.frames["feeds-frame"].document;
 //     feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
 
@@ -535,47 +586,59 @@ function qfdDelete(feed_id) {
 }
 
 
-function allFeedsMenuGo() {
-       var chooser = document.getElementById("allFeedsChooser");
-
-       var opname = chooser[chooser.selectedIndex].text;
-
-       if (opname == "Update") {
-               scheduleFeedUpdate(true);
-               return;
-       }
-
-       if (opname == "Mark as read") {
-               catchupAllFeeds();
-               return;
-       }
-
-       if (opname == "Show only unread") {
-               toggleDispRead();
-               return;
-       }
-
-}
-
 function updateFeedTitle(t) {
        active_title_text = t;
        updateTitle();
 }
 
 function toggleDispRead() {
-       var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
+       try {
 
-       hide_read_feeds = !hide_read_feeds;
+               if (!xmlhttp_ready(xmlhttp)) {
+                       printLockingError();
+                       return
+               }
 
-       var feeds_doc = window.frames["feeds-frame"].document;
+               var hide_read_feeds = (getCookie("ttrss_vf_hreadf") == 1);
 
-       hideOrShowFeeds(feeds_doc, hide_read_feeds);
+               hide_read_feeds = !hide_read_feeds;
+       
+               var query = "backend.php?op=rpc&subop=setpref" +
+                       "&key=HIDE_READ_FEEDS&value=" + param_escape(hide_read_feeds);
 
-       if (hide_read_feeds) {
-               setCookie("ttrss_vf_hreadf", 1);
-       } else {
-               setCookie("ttrss_vf_hreadf", 0);
+               xmlhttp.open("GET", query);
+               xmlhttp.onreadystatechange=hide_unread_callback;
+               xmlhttp.send(null);
+               
+       } catch (e) {
+               exception_error("toggleDispRead", e);
        }
+}
 
+function debug(msg) {
+       var c = document.getElementById('debug_output');
+       if (c && c.style.display == "block") {
+               while (c.lastChild != 'undefined' && c.childNodes.length > 20) {
+                       c.removeChild(c.lastChild);
+               }
+       
+               var d = new Date();
+               var ts = leading_zero(d.getHours()) + ":" + leading_zero(d.getMinutes()) +
+                       ":" + leading_zero(d.getSeconds());
+               c.innerHTML = "<li>[" + ts + "] " + msg + "</li>" + c.innerHTML;
+       }
 }
 
+function fatalError(code, message) {
+       try {   
+               var fe = document.getElementById("fatal_error");
+               var fc = document.getElementById("fatal_error_msg");
+
+               fc.innerHTML = "Code " + code + ": " + message;
+
+               fe.style.display = "block";
+
+       } catch (e) {
+               exception_error("fatalError", e);
+       }
+}