]> git.wh0rd.org - tt-rss.git/blobdiff - functions.js
quick unsubscribe prompt shows feed name
[tt-rss.git] / functions.js
index bcbc93b36591b83c0f77abf927efdd0651819468..f7e29888847cf84292fce92404c86b78b8fe2156 100644 (file)
@@ -1,5 +1,12 @@
 var hotkeys_enabled = true;
 
+var xmlhttp_rpc = Ajax.getTransport();
+
+function browser_has_opacity() {
+       return navigator.userAgent.match("Gecko") != null || 
+               navigator.userAgent.match("Opera") != null;
+}
+
 function exception_error(location, e) {
        var msg;
 
@@ -9,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);
 }
 
@@ -42,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); 
@@ -75,17 +78,19 @@ 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 (navigator.userAgent.match("Firefox")) {
+               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);                               
+                               notify_hide_timerid = window.setTimeout("hide_notify()", 20);   
                        } else {
                                n.style.display = "none";
-                               n.style.opacity = 1;                            
+                               n.style.opacity = 1;
                        }
                } else {
                        n.style.display = "none";
@@ -93,37 +98,52 @@ function hide_notify() {
        }
 }
 
-function notify_real(msg, doc) {
+function notify_real(msg, doc, no_hide, is_err) {
 
        var n = doc.getElementById("notify");
        var nb = doc.getElementById("notify_body");
 
        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";
        }
 
-       nb.innerHTML = msg;
-
-       if (notify_hide_timerid) {
-               window.clearTimeout(notify_hide_timerid);
+       if (is_err) {
+               n.style.backgroundColor = "#ffcccc";
+               n.style.color = "black";
+               n.style.borderColor = "#ff0000";
+       } else {
+               n.style.backgroundColor = "#fff7d5";
+               n.style.borderColor = "#d7c47a";
+               n.style.color = "black";
        }
 
-       notify_last_doc = doc;
-       notify_opacity = 1;
+       nb.innerHTML = msg;
 
-       notify_hide_timerid = window.setTimeout(hide_notify, 3000);
+       if (!no_hide) {
+               notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
+       }
 }
 
-function p_notify(msg) {
-       notify_real(msg, parent.document);
+function p_notify(msg, no_hide, is_err) {
+       notify_real(msg, parent.document, no_hide, is_err);
 }
 
-function notify(msg) {
-       notify_real(msg, document);
+function notify(msg, no_hide, is_err) {
+       notify_real(msg, document, no_hide, is_err);
 }
 
 function printLockingError() {
@@ -133,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) {
@@ -342,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];
 
@@ -377,36 +447,36 @@ 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();
+
+               if (reply.firstChild && reply.firstChild.firstChild) {
+                       debug("<b>wrong element passed to parse_counters, adjusting.</b>");
+                       reply = reply.firstChild;
+               }
+
+               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") {
@@ -490,9 +560,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 {
@@ -501,15 +568,11 @@ 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);
+                       var counters = reply.firstChild;
+
+                       parse_counters(counters);
        
                } catch (e) {
                        exception_error("all_counters_callback", e);
@@ -768,7 +831,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-', '');                                          
@@ -783,7 +846,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;
                }
 
@@ -917,28 +980,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');
 
@@ -948,7 +990,10 @@ function closeInfoBox() {
                box.style.display = "none";
        }
 
+       if (cleanup) box.innerHTML = "&nbsp;";
+
        enableHotkeys();
+
 }
 
 
@@ -972,13 +1017,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);
+
        } 
 }
 
@@ -1004,36 +1051,142 @@ 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");
+       var query = Form.serialize("filter_add_form");
+
+       xmlhttp.open("GET", "backend.php?" + query, true);
+       xmlhttp.onreadystatechange=infobox_submit_callback;
+       xmlhttp.send(null);
+
+       return true;
+}
+
+function toggleSubmitNotEmpty(e, submit_id) {
+       try {
+               document.getElementById(submit_id).disabled = (e.value == "")
+       } catch (e) {
+               exception_error("toggleSubmitNotEmpty", e);
+       }
+}
+
+function isValidURL(s) {
+       return s.match("http://") != null || s.match("https://") != null;
+}
+
+function qafAdd() {
+
+       if (!xmlhttp_ready(xmlhttp)) {
+               printLockingError();
+               return
+       }
+
+       notify("Adding feed...");
 
-       if (regexp.value.length == 0) {
-               alert("Missing filter expression.");
+       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 {
-               notify("Adding filter...");
+               return this.window;
+       }
+}
+
+function getFeedsContext() {
+       try {
+               return getMainContext().frames["feeds-frame"];
+       } catch (e) {
+               exception_error("getFeedsContext", e);
+       }
+}
 
-               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);
+function getHeadlinesContext() {
+       try {
+               return getMainContext().frames["headlines-frame"];
+       } catch (e) {
+               exception_error("getHeadlinesContext", e);
+       }
+}
+
+function debug(msg) {
+       var ctx = getMainContext();
 
-               regexp.value = "";
+       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];
 }
 
-function toggleSubmitNotEmpty(e, submit_id) {
+// TODO: batch mode
+function storeInitParam(key, value, is_client) {
        try {
-               document.getElementById(submit_id).disabled = (e.value == "")
+               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("toggleSubmitNotEmpty", 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);
+       }
+}
+
+function getFeedName(id) {
+       var d = getFeedsContext().document;
+       var e = d.getElementById("FEEDN-" + id);
+       if (e) {
+               return e.innerHTML.stripTags();
+       } else {
+               return null;
        }
 }