]> git.wh0rd.org - tt-rss.git/blobdiff - functions.js
add sanity check & debug mode to prefs; misc code cleanups
[tt-rss.git] / functions.js
index 817946c397737ca2153b90f5cd1b2ce823352258..35579364047fa149fc88c2f1c2d530bf49a32fa1 100644 (file)
@@ -1,5 +1,7 @@
 var hotkeys_enabled = true;
 
+var xmlhttp_rpc = Ajax.getTransport();
+
 function browser_has_opacity() {
        return navigator.userAgent.match("Gecko") != null || 
                navigator.userAgent.match("Opera") != null;
@@ -14,10 +16,13 @@ function exception_error(location, e) {
                msg = "Exception: " + e.name + ", " + e.message + 
                        "\nFunction: " + location + "()" +
                        "\nLocation: " + base_fname + ":" + e.lineNumber;
+               
        } else {
                msg = "Exception: " + e + "\nFunction: " + location + "()";
        }
 
+       debug("<b>EXCEPTION: " + msg + "</b>");
+
        alert(msg);
 }
 
@@ -47,13 +52,6 @@ function rpc_notify_callback() {
        }
 }
 
-function rpc_pnotify_callback() {
-       var container = parent.document.getElementById('notify');
-       if (xmlhttp_rpc.readyState == 4) {
-               container.innerHTML=xmlhttp_rpc.responseText;
-       }
-}
-
 function param_escape(arg) {
        if (typeof encodeURIComponent != 'undefined')
                return encodeURIComponent(arg); 
@@ -80,12 +78,14 @@ function delay(gap) {
 var notify_hide_timerid = false;
 var notify_last_doc = false;
 
+var notify_effect = false;
+
 function hide_notify() {
        if (notify_last_doc) {
                var n = notify_last_doc.getElementById("notify");               
                if (browser_has_opacity()) {
                        if (notify_opacity >= 0) {
-                               notify_opacity = notify_opacity - 0.2;
+                               notify_opacity = notify_opacity - 0.1;
                                n.style.opacity = notify_opacity;
                                notify_hide_timerid = window.setTimeout("hide_notify()", 20);   
                        } else {
@@ -105,8 +105,18 @@ function notify_real(msg, doc, no_hide, is_err) {
 
        if (!n || !nb) return;
 
+       if (notify_hide_timerid) {
+               window.clearTimeout(notify_hide_timerid);
+       }
+
+       notify_last_doc = doc;
+       notify_opacity = 1;
+
        if (msg == "") {
-               n.style.display = "none";
+               if (n.style.display == "block") {
+                       notify_hide_timerid = window.setTimeout("hide_notify()", 0);
+               }
+               return;
        } else {
                n.style.display = "block";
        }
@@ -123,13 +133,6 @@ function notify_real(msg, doc, no_hide, is_err) {
 
        nb.innerHTML = msg;
 
-       if (notify_hide_timerid) {
-               window.clearTimeout(notify_hide_timerid);
-       }
-
-       notify_last_doc = doc;
-       notify_opacity = 1;
-
        if (!no_hide) {
                notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
        }
@@ -150,40 +153,85 @@ var seq = "";
 
 function hotkey_handler(e) {
 
-       var keycode;
-
-       if (!hotkeys_enabled) return;
-
-       if (window.event) {
-               keycode = window.event.keyCode;
-       } else if (e) {
-               keycode = e.which;
-       }
-
-       if (keycode == 13 || keycode == 27) {
-               seq = "";
-       } else {
-               seq = seq + "" + keycode;
-       }
-
-       if (document.getElementById("piggie")) {
+       try {
 
-               if (seq.match("807371717369")) {
+               var keycode;
+       
+               if (!hotkeys_enabled) return;
+       
+               if (window.event) {
+                       keycode = window.event.keyCode;
+               } else if (e) {
+                       keycode = e.which;
+               }
+       
+               if (keycode == 13 || keycode == 27) {
                        seq = "";
-                       localPiggieFunction(true);
                } else {
-                       localPiggieFunction(false);
+                       seq = seq + "" + keycode;
                }
-       }
        
-       if (typeof localHotkeyHandler != 'undefined') {
-               try {
-                       localHotkeyHandler(keycode);
-               } catch (e) {
-                       exception_error("hotkey_handler", e);
+               var m_ctx = getMainContext();
+               var f_ctx = getFeedsContext();
+               var h_ctx = getHeadlinesContext();
+       
+               if (keycode == 82) { // r
+                       return m_ctx.scheduleFeedUpdate(true);
+               }
+       
+               if (keycode == 85) { // u
+                       if (getActiveFeedId()) {
+                               return f_ctx.viewfeed(getActiveFeedId(), 0, "ForceUpdate");
+                       }
+               }
+       
+               if (keycode == 65) { // a
+                       return m_ctx.toggleDispRead();
+               }
+       
+               var f_doc = m_ctx.frames["feeds-frame"].document;
+               var feedlist = f_doc.getElementById('feedList');
+       
+               if (keycode == 74) { // j
+                       var feed = getActiveFeedId();
+                       var new_feed = getRelativeFeedId(feedlist, feed, 'prev');
+                       if (new_feed) viewfeed(new_feed, 0, '');
+               }
+       
+               if (keycode == 75) { // k
+                       var feed = getActiveFeedId();
+                       var new_feed = getRelativeFeedId(feedlist, feed, 'next');
+                       if (new_feed) viewfeed(new_feed, 0, '');
                }
-       }
 
+               if (keycode == 78 || keycode == 40) { // n, down
+                       return h_ctx.moveToPost('next');
+               }
+       
+               if (keycode == 80 || keycode == 38) { // p, up
+                       return h_ctx.moveToPost('prev');
+               } 
+       
+               if (document.getElementById("piggie")) {
+       
+                       if (seq.match("807371717369")) {
+                               seq = "";
+                               localPiggieFunction(true);
+                       } else {
+                               localPiggieFunction(false);
+                       }
+               }
+               
+               if (typeof localHotkeyHandler != 'undefined') {
+                       try {
+                               localHotkeyHandler(keycode);
+                       } catch (e) {
+                               exception_error("hotkey_handler, local:", e);
+                       }
+               }
+       } catch (e) {
+               exception_error("hotkey_handler", e);
+       }
 }
 
 function cleanSelectedList(element) {
@@ -359,6 +407,11 @@ function disableContainerChildren(id, disable, doc) {
 
        var container = doc.getElementById(id);
 
+       if (!container) {
+               //alert("disableContainerChildren: element " + id + " not found");
+               return;
+       }
+
        for (var i = 0; i < container.childNodes.length; i++) {
                var child = container.childNodes[i];
 
@@ -394,36 +447,31 @@ function gotoExportOpml() {
 }
 
 function getActiveFeedId() {
-       return getCookie("ttrss_vf_actfeed");
+//     return getCookie("ttrss_vf_actfeed");
+       try {
+               debug("gAFID: " + getMainContext().active_feed_id);
+               return getMainContext().active_feed_id;
+       } catch (e) {
+               exception_error("getActiveFeedId", e);
+       }
 }
 
 function setActiveFeedId(id) {
-       return setCookie("ttrss_vf_actfeed", id);
-}
-
-var xmlhttp_rpc = false;
-
-/*@cc_on @*/
-/*@if (@_jscript_version >= 5)
-// JScript gives us Conditional compilation, we can cope with old IE versions.
-// and security blocked creation of the objects.
-try {
-       xmlhttp_rpc = new ActiveXObject("Msxml2.XMLHTTP");
-} catch (e) {
+//     return setCookie("ttrss_vf_actfeed", id);
        try {
-               xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
-       } catch (E) {
-               xmlhttp_rpc = false;
+               getMainContext().active_feed_id = id;
+       } catch (e) {
+               exception_error("setActiveFeedId", e);
        }
 }
-@end @*/
 
-if (!xmlhttp_rpc && typeof XMLHttpRequest!='undefined') {
-       xmlhttp_rpc = new XMLHttpRequest();
-}
-
-function parse_counters(reply, f_document, title_obj, scheduled_call) {
+function parse_counters(reply, scheduled_call) {
        try {
+               var f_document = getFeedsContext().document;
+               var title_obj = getMainContext();
+
+               debug("F_DOC: " + f_document + ", T_OBJ: " + title_obj);
+
                for (var l = 0; l < reply.childNodes.length; l++) {
                        if (!reply.childNodes[l] ||
                                typeof(reply.childNodes[l].getAttribute) == "undefined") {
@@ -507,9 +555,6 @@ function parse_counters(reply, f_document, title_obj, scheduled_call) {
        }
 }
 
-// this one is called from feedlist context
-// thus title_obj passed to parse_counters is parent (e.g. main ttrss window)
-
 function all_counters_callback() {
        if (xmlhttp_rpc.readyState == 4) {
                try {
@@ -518,15 +563,9 @@ function all_counters_callback() {
                                return;
                        }
 
-                       if (!parent.frames["feeds-frame"]) {
-                               notify("[all_counters_callback] no parent feeds-frame");
-                               return;
-                       }
-
                        var reply = xmlhttp_rpc.responseXML.firstChild;
-                       var f_document = parent.frames["feeds-frame"].document;
 
-                       parse_counters(reply, f_document, parent);
+                       parse_counters(reply);
        
                } catch (e) {
                        exception_error("all_counters_callback", e);
@@ -785,7 +824,7 @@ function getRelativeFeedId(list, id, direction, unread_only) {
                                        }
                                } else if (child.id.match("FEEDR-")) {
                                
-                                       if (getCookie("ttrss_vf_hreadf") == 1) {
+                                       if (getInitParam("hide_read_feeds") == 1) {
                                                if (child.className != "feed") {
                                                        alert(child.className);
                                                        return child.id.replace('FEEDR-', '');                                          
@@ -800,7 +839,7 @@ function getRelativeFeedId(list, id, direction, unread_only) {
        
                var feed = list.ownerDocument.getElementById("FEEDR-" + getActiveFeedId());
                
-               if (getCookie("ttrss_vf_hreadf") == 1) {
+               if (getInitParam("hide_read_feeds") == 1) {
                        unread_only = true;
                }
 
@@ -934,28 +973,7 @@ function leading_zero(p) {
        return s;
 }
 
-function center_element(e) {
-
-       try {
-               var c_width = document.body.clientWidth;
-               var c_height = document.body.clientHeight;
-       
-               var c_scroll = document.body.scrollTop;
-       
-               var e_width = e.clientWidth;
-               var e_height = e.clientHeight;
-       
-               var set_y = (c_height / 2) + c_scroll - (e_height / 2);
-               var set_x = (c_width / 2) - (e_width / 2);
-       
-               e.style.top = set_y + "px";
-               e.style.left = set_x + "px";
-       } catch (e) {
-               exception_error("center_element", e);
-       }
-}
-
-function closeInfoBox() {
+function closeInfoBox(cleanup) {
        var box = document.getElementById('infoBox');
        var shadow = document.getElementById('infoBoxShadow');
 
@@ -965,7 +983,10 @@ function closeInfoBox() {
                box.style.display = "none";
        }
 
+       if (cleanup) box.innerHTML = "&nbsp;";
+
        enableHotkeys();
+
 }
 
 
@@ -989,13 +1010,15 @@ function displayDlg(id, param) {
 
 function infobox_submit_callback() {
        if (xmlhttp.readyState == 4) {
-               notify(xmlhttp.responseText);
                closeInfoBox();
 
                // called from prefs, reload tab
                if (active_tab) {
                        selectTab(active_tab, false);
                }
+
+               notify(xmlhttp.responseText);
+
        } 
 }
 
@@ -1021,30 +1044,13 @@ function qaddFilter() {
                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) {
-               alert("Missing filter expression.");
-       } else {
-               notify("Adding filter...");
+       var query = Form.serialize("filter_add_form");
 
-               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=infobox_submit_callback;
-               xmlhttp.send(null);
-
-               regexp.value = "";
-       }
+       xmlhttp.open("GET", "backend.php?" + query, true);
+       xmlhttp.onreadystatechange=infobox_submit_callback;
+       xmlhttp.send(null);
 
+       return true;
 }
 
 function toggleSubmitNotEmpty(e, submit_id) {
@@ -1058,3 +1064,114 @@ function toggleSubmitNotEmpty(e, submit_id) {
 function isValidURL(s) {
        return s.match("http://") != null || s.match("https://") != null;
 }
+
+function qafAdd() {
+
+       if (!xmlhttp_ready(xmlhttp)) {
+               printLockingError();
+               return
+       }
+
+       notify("Adding feed...");
+
+       closeInfoBox();
+
+       var feeds_doc = getFeedsContext().document;
+
+       feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
+       
+       var query = Form.serialize("feed_add_form");
+       
+       xmlhttp.open("GET", "backend.php?" + query, true);
+       xmlhttp.onreadystatechange=dlg_frefresh_callback;
+       xmlhttp.send(null);
+}
+
+function filterCR(e)
+{
+     var key;
+
+     if(window.event)
+          key = window.event.keyCode;     //IE
+     else
+          key = e.which;     //firefox
+
+     if(key == 13)
+          return false;
+     else
+          return true;
+}
+
+function getMainContext() {
+       if (parent.window != window) {
+               return parent.window;
+       } else {
+               return this.window;
+       }
+}
+
+function getFeedsContext() {
+       try {
+               return getMainContext().frames["feeds-frame"];
+       } catch (e) {
+               exception_error("getFeedsContext", e);
+       }
+}
+
+
+function getHeadlinesContext() {
+       try {
+               return getMainContext().frames["headlines-frame"];
+       } catch (e) {
+               exception_error("getHeadlinesContext", e);
+       }
+}
+
+function debug(msg) {
+       var ctx = getMainContext();
+
+       var c = ctx.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 getInitParam(key) {
+       return getMainContext().init_params[key];
+}
+
+// TODO: batch mode
+function storeInitParam(key, value, is_client) {
+       try {
+               getMainContext().init_params[key] = value;
+               if (!is_client) {
+                       new Ajax.Request("backend.php?op=rpc&subop=storeParam&key=" + 
+                               param_escape(key) + "&value=" + param_escape(value));   
+               }
+       } catch (e) {
+               exception_error("storeInitParam", e);
+       }
+}
+
+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);
+       }
+}
+
+