]> git.wh0rd.org - tt-rss.git/blobdiff - functions.js
getMainContext() and related JS-stuff
[tt-rss.git] / functions.js
index 1d45b1cbf8b03c1f568e5c68719273193439d69f..384f34827c8b20e12c90f6db297ca1c400e6d79b 100644 (file)
@@ -1,5 +1,10 @@
 var hotkeys_enabled = true;
 
+function browser_has_opacity() {
+       return navigator.userAgent.match("Gecko") != null || 
+               navigator.userAgent.match("Opera") != null;
+}
+
 function exception_error(location, e) {
        var msg;
 
@@ -28,7 +33,6 @@ function xmlhttp_ready(obj) {
        return obj.readyState == 4 || obj.readyState == 0 || !obj.readyState;
 }
 
-
 function notify_callback() {
        var container = document.getElementById('notify');
        if (xmlhttp.readyState == 4) {
@@ -73,37 +77,75 @@ function delay(gap) {
        }
 }
 
-function p_notify(msg) {
+var notify_hide_timerid = false;
+var notify_last_doc = false;
 
-       var n = parent.document.getElementById("notify");
-       var nb = parent.document.getElementById("notify_body");
+var notify_effect = false;
 
-       if (!n || !nb) return;
-
-       if (msg == "") {
-               nb.innerHTML = " ";
-//             n.style.background = "#ffffff";
-       } else {
-               nb.innerHTML = msg;
-//             n.style.background = "#fffff0";
+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.1;
+                               n.style.opacity = notify_opacity;
+                               notify_hide_timerid = window.setTimeout("hide_notify()", 20);   
+                       } else {
+                               n.style.display = "none";
+                               n.style.opacity = 1;
+                       }
+               } else {
+                       n.style.display = "none";
+               }
        }
 }
 
-function notify(msg) {
+function notify_real(msg, doc, no_hide, is_err) {
 
-       var n = document.getElementById("notify");
-       var nb = document.getElementById("notify_body");
+       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 == "") {
-               nb.innerHTML = " ";
-//             n.style.background = "#ffffff";
+               if (n.style.display == "block") {
+                       notify_hide_timerid = window.setTimeout("hide_notify()", 0);
+               }
+               return;
        } else {
-               nb.innerHTML = msg;
-//             n.style.background = "#fffff0";
+               n.style.display = "block";
        }
 
+       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";
+       }
+
+       nb.innerHTML = msg;
+
+       if (!no_hide) {
+               notify_hide_timerid = window.setTimeout("hide_notify()", 3000);
+       }
+}
+
+function p_notify(msg, no_hide, is_err) {
+       notify_real(msg, parent.document, no_hide, is_err);
+}
+
+function notify(msg, no_hide, is_err) {
+       notify_real(msg, document, no_hide, is_err);
 }
 
 function printLockingError() {
@@ -164,16 +206,15 @@ function cleanSelectedList(element) {
        } else {
                for (i = 0; i < content.childNodes.length; i++) {
                        var child = content.childNodes[i];
-
                        if (child.id == "feedCatHolder") {
+                               parent.debug(child.id);
                                var fcat = child.lastChild;
                                for (j = 0; j < fcat.childNodes.length; j++) {
-                                       var feed = fcat.childNodes[j];                                  
+                                       var feed = fcat.childNodes[j];
                                        feed.className = feed.className.replace("Selected", "");
                                }               
                        }
-               }
-
+               } 
        }
 }
 
@@ -267,7 +308,20 @@ function getFeedIds() {
        return rows;
 }
 
-function setCookie(name, value, expires, path, domain, secure) {
+function setCookie(name, value, lifetime, path, domain, secure) {
+       
+       var d = false;
+       
+       if (lifetime) {
+               d = new Date();
+               d.setTime(lifetime * 1000);
+       }
+       
+       int_setCookie(name, value, d, path, domain, secure);
+
+}
+
+function int_setCookie(name, value, expires, path, domain, secure) {
        document.cookie= name + "=" + escape(value) +
                ((expires) ? "; expires=" + expires.toGMTString() : "") +
                ((path) ? "; path=" + path : "") +
@@ -275,6 +329,16 @@ function setCookie(name, value, expires, path, domain, secure) {
                ((secure) ? "; secure" : "");
 }
 
+function delCookie(name, path, domain) {
+       if (getCookie(name)) {
+               document.cookie = name + "=" +
+               ((path) ? ";path=" + path : "") +
+               ((domain) ? ";domain=" + domain : "" ) +
+               ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
+       }
+}
+               
+
 function getCookie(name) {
 
        var dc = document.cookie;
@@ -300,6 +364,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];
 
@@ -342,29 +411,15 @@ function setActiveFeedId(id) {
        return setCookie("ttrss_vf_actfeed", id);
 }
 
-var xmlhttp_rpc = false;
+var xmlhttp_rpc = Ajax.getTransport();
 
-/*@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) {
+function parse_counters(reply, scheduled_call) {
        try {
-               xmlhttp_rpc = new ActiveXObject("Microsoft.XMLHTTP");
-       } catch (E) {
-               xmlhttp_rpc = false;
-       }
-}
-@end @*/
+               var f_document = getMainContext().frames["feeds-frame"].document;
+               var title_obj = getMainContext();
 
-if (!xmlhttp_rpc && typeof XMLHttpRequest!='undefined') {
-       xmlhttp_rpc = new XMLHttpRequest();
-}
+               debug("F_DOC: " + f_document + ", T_OBJ: " + title_obj);
 
-function parse_counters(reply, f_document, title_obj, scheduled_call) {
-       try {
                for (var l = 0; l < reply.childNodes.length; l++) {
                        if (!reply.childNodes[l] ||
                                typeof(reply.childNodes[l].getAttribute) == "undefined") {
@@ -377,6 +432,7 @@ function parse_counters(reply, f_document, title_obj, scheduled_call) {
                        var ctr = reply.childNodes[l].getAttribute("counter");
                        var error = reply.childNodes[l].getAttribute("error");
                        var has_img = reply.childNodes[l].getAttribute("hi");
+                       var updated = reply.childNodes[l].getAttribute("updated");
        
                        if (id == "global-unread") {
                                title_obj.global_unread = ctr;
@@ -396,6 +452,15 @@ function parse_counters(reply, f_document, title_obj, scheduled_call) {
                        var feedu = f_document.getElementById("FEEDU-" + id);
                        var feedr = f_document.getElementById("FEEDR-" + id);
                        var feed_img = f_document.getElementById("FIMG-" + id);
+                       var feedlink = f_document.getElementById("FEEDL-" + id);
+
+                       if (updated && feedlink) {
+                               if (error) {
+                                       feedlink.title = "Error: " + error + " (" + updated + ")";
+                               } else {
+                                       feedlink.title = "Updated: " + updated;
+                               }
+                       }
 
                        if (feedctr && feedu && feedr) {
 
@@ -438,9 +503,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 {
@@ -448,11 +510,16 @@ function all_counters_callback() {
                                notify("[all_counters_callback] backend did not return valid XML");
                                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;
+//                     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);
@@ -537,15 +604,6 @@ function hideOrShowFeeds(doc, hide) {
 
 }
 
-function fatalError(code, params) {
-       if (!params) {
-               window.location = "error.php?c=" + param_escape(code);
-       } else {
-               window.location = "error.php?c=" + param_escape(code) + 
-                       "&p=" + param_escape(params);
-       }
-}
-
 function selectTableRow(r, do_select) {
        r.className = r.className.replace("Selected", "");
        
@@ -692,18 +750,17 @@ function openExternalUrl(url) {
        var w = window.open(url);
 }
 
-
-function getRelativeFeedId(list, id, direction) {      
+function getRelativeFeedId(list, id, direction, unread_only) { 
        if (!id) {
                if (direction == "next") {
                        for (i = 0; i < list.childNodes.length; i++) {
                                var child = list.childNodes[i];
-                               if (child.id == "feedCatHolder") {
+                               if (child.id && child.id == "feedCatHolder") {
                                        if (child.lastChild) {
                                                var cr = getRelativeFeedId(child.firstChild, id, direction);
                                                if (cr) return cr;                                      
                                        }
-                               } else if (child.id.match("FEEDR-")) {
+                               } else if (child.id && child.id.match("FEEDR-")) {
                                        return child.id.replace('FEEDR-', '');
                                }                               
                        }
@@ -736,82 +793,83 @@ function getRelativeFeedId(list, id, direction) {
        
                var feed = list.ownerDocument.getElementById("FEEDR-" + getActiveFeedId());
                
+               if (getCookie("ttrss_vf_hreadf") == 1) {
+                       unread_only = true;
+               }
+
                if (direction == "next") {
 
-                       if (feed.nextSibling) {
+                       var e = feed;
 
-                               var next_feed = feed.nextSibling;
+                       while (e) {
 
-                               while (!next_feed.id && next_feed.nextSibling) {
-                                       next_feed = next_feed.nextSibling;
-                               }
+                               if (e.nextSibling) {
+                               
+                                       e = e.nextSibling;
+                                       
+                               } else if (e.parentNode.parentNode.nextSibling) {
 
-                               if (getCookie("ttrss_vf_hreadf") == 1) {
-                                               while (next_feed && next_feed.className == "feed") {
-                                                       next_feed = next_feed.nextSibling;
-                                               }
-                                       }
+                                       var this_cat = e.parentNode.parentNode;
 
-                               if (next_feed && next_feed.id.match("FEEDR-")) {
-                                       return next_feed.id.replace("FEEDR-", "");
-                               }                               
-                       }
+                                       e = false;
 
-                       var this_cat = feed.parentNode.parentNode;
-                       
-                       if (this_cat && this_cat.nextSibling) {
-                               while (this_cat = this_cat.nextSibling) {
-                                       if (this_cat.firstChild && this_cat.firstChild.firstChild) {
-                                               var next_feed = this_cat.firstChild.firstChild;
-                                                       if (getCookie("ttrss_vf_hreadf") == 1) {
-                                                       while (next_feed && next_feed.className == "feed") {
-                                                               next_feed = next_feed.nextSibling;
+                                       if (this_cat && this_cat.nextSibling) {
+                                               while (!e && this_cat.nextSibling) {
+                                                       this_cat = this_cat.nextSibling;
+                                                       if (this_cat.id == "feedCatHolder") {
+                                                               e = this_cat.firstChild.firstChild;
                                                        }
                                                }
-                                               if (next_feed && next_feed.id.match("FEEDR-")) {
-                                                       return next_feed.id.replace("FEEDR-", "");
-                                               }
                                        }
-                               }                               
+
+                               } else {
+                                       e = false;
+                          }
+
+                               if (e) {
+                                       if (!unread_only || (unread_only && e.className != "feed" && 
+                                                       e.className != "error"))        {
+                                               return e.id.replace("FEEDR-", "");
+                                       }
+                               }
                        }
+                       
                } else if (direction == "prev") {
 
-                       if (feed.previousSibling) {
-                       
-                               var prev_feed = feed.previousSibling;
+                       var e = feed;
 
-                               if (getCookie("ttrss_vf_hreadf") == 1) {
-                                               while (prev_feed && prev_feed.className == "feed") {
-                                                       prev_feed = prev_feed.previousSibling;
-                                               }
-                                       }
+                       while (e) {
 
-                               while (!prev_feed.id && prev_feed.previousSibling) {
-                                       prev_feed = prev_feed.previousSibling;
-                               }
+                               if (e.previousSibling) {
+                               
+                                       e = e.previousSibling;
+                                       
+                               } else if (e.parentNode.parentNode.previousSibling) {
 
-                               if (prev_feed && prev_feed.id.match("FEEDR-")) {
-                                       return prev_feed.id.replace("FEEDR-", "");
-                               }                               
-                       }
+                                       var this_cat = e.parentNode.parentNode;
 
-                       var this_cat = feed.parentNode.parentNode;
-                       
-                       if (this_cat && this_cat.previousSibling) {
-                               while (this_cat = this_cat.previousSibling) {
-                                       if (this_cat.lastChild && this_cat.firstChild.lastChild) {
-                                               var prev_feed = this_cat.firstChild.lastChild;
-                                                       if (getCookie("ttrss_vf_hreadf") == 1) {
-                                                       while (prev_feed && prev_feed.className == "feed") {
-                                                               prev_feed = prev_feed.previousSibling;
+                                       e = false;
+
+                                       if (this_cat && this_cat.previousSibling) {
+                                               while (!e && this_cat.previousSibling) {
+                                                       this_cat = this_cat.previousSibling;
+                                                       if (this_cat.id == "feedCatHolder") {
+                                                               e = this_cat.firstChild.lastChild;
                                                        }
                                                }
-                                               if (prev_feed && prev_feed.id.match("FEEDR-")) {
-                                                       return prev_feed.id.replace("FEEDR-", "");
-                                               }
                                        }
-                               }                               
-                       }       
+
+                               } else {
+                                       e = false;
+                          }
+
+                               if (e) {
+                                       if (!unread_only || (unread_only && e.className != "feed" && 
+                                                       e.className != "error"))        {
+                                               return e.id.replace("FEEDR-", "");
+                                       }
+                               }
+                       }
                }
        }
 }
@@ -869,23 +927,157 @@ function leading_zero(p) {
        return s;
 }
 
-function center_element(e) {
+function closeInfoBox(cleanup) {
+       var box = document.getElementById('infoBox');
+       var shadow = document.getElementById('infoBoxShadow');
+
+       if (shadow) {
+               shadow.style.display = "none";
+       } else if (box) {
+               box.style.display = "none";
+       }
+
+       if (cleanup) box.innerHTML = "&nbsp;";
+
+       enableHotkeys();
+
+}
+
+
+function displayDlg(id, param) {
+
+       if (!xmlhttp_ready(xmlhttp)) {
+               printLockingError();
+               return
+       }
+
+       notify("");
+
+       xmlhttp.open("GET", "backend.php?op=dlg&id=" +
+               param_escape(id) + "&param=" + param_escape(param), true);
+       xmlhttp.onreadystatechange=infobox_callback;
+       xmlhttp.send(null);
+
+       disableHotkeys();
+
+}
+
+function infobox_submit_callback() {
+       if (xmlhttp.readyState == 4) {
+               closeInfoBox();
+
+               // called from prefs, reload tab
+               if (active_tab) {
+                       selectTab(active_tab, false);
+               }
+
+               notify(xmlhttp.responseText);
+
+       } 
+}
+
+function infobox_callback() {
+       if (xmlhttp.readyState == 4) {
+               var box = document.getElementById('infoBox');
+               var shadow = document.getElementById('infoBoxShadow');
+               if (box) {                      
+                       box.innerHTML=xmlhttp.responseText;                     
+                       if (shadow) {
+                               shadow.style.display = "block";
+                       } else {
+                               box.style.display = "block";                            
+                       }
+               }
+       }
+}
+
+function qaddFilter() {
+
+       if (!xmlhttp_ready(xmlhttp)) {
+               printLockingError();
+               return
+       }
+
+       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 {
-               var c_width = document.body.clientWidth;
-               var c_height = document.body.clientHeight;
-       
-               var c_scroll = document.body.scrollTop;
+               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...");
+
+       closeInfoBox();
+
+       var feeds_doc = window.frames["feeds-frame"].document;
+
+       feeds_doc.location.href = "backend.php?op=error&msg=Loading,%20please wait...";
        
-               var e_width = e.clientWidth;
-               var e_height = e.clientHeight;
+       var query = Form.serialize("feed_add_form");
        
-               var set_y = (c_height / 2) + c_scroll - (e_height / 2);
-               var set_x = (c_width / 2) - (e_width / 2);
+       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 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);
+               }
        
-               e.style.top = set_y + "px";
-               e.style.left = set_x + "px";
-       } catch (e) {
-               exception_error("center_element", e);
+               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;
        }
 }
+
+