]> git.wh0rd.org - tt-rss.git/blobdiff - functions.js
debug window look tweaks
[tt-rss.git] / functions.js
index 627eb287a5d449e3138ebfdbc8eadc8dfd173ba4..93a36b587d24568fe653bdcd5685748ad358009b 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,6 +78,8 @@ 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");               
@@ -153,40 +153,89 @@ var seq = "";
 
 function hotkey_handler(e) {
 
-       var keycode;
-
-       if (!hotkeys_enabled) return;
+       try {
 
-       if (window.event) {
-               keycode = window.event.keyCode;
-       } else if (e) {
-               keycode = e.which;
-       }
+               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;
+               }
+       
+               var m_ctx = getMainContext();
+               var f_ctx = getFeedsContext();
+               var h_ctx = getHeadlinesContext();
+       
+               if (keycode == 82) { // r
+                       return m_ctx.scheduleFeedUpdate(true);
+               }
 
-       if (keycode == 13 || keycode == 27) {
-               seq = "";
-       } else {
-               seq = seq + "" + keycode;
-       }
+               if (keycode == 83) { // r
+                       return m_ctx.displayDlg("search", getActiveFeedId());
+               }
 
-       if (document.getElementById("piggie")) {
+               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 (seq.match("807371717369")) {
-                       seq = "";
-                       localPiggieFunction(true);
-               } else {
-                       localPiggieFunction(false);
+               if (keycode == 78 || keycode == 40) { // n, down
+                       return h_ctx.moveToPost('next');
                }
-       }
        
-       if (typeof localHotkeyHandler != 'undefined') {
-               try {
-                       localHotkeyHandler(keycode);
-               } catch (e) {
-                       exception_error("hotkey_handler", e);
+               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) {
@@ -362,6 +411,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];
 
@@ -397,36 +451,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") {
@@ -460,6 +514,7 @@ function parse_counters(reply, f_document, title_obj, scheduled_call) {
                        var feedr = f_document.getElementById("FEEDR-" + id);
                        var feed_img = f_document.getElementById("FIMG-" + id);
                        var feedlink = f_document.getElementById("FEEDL-" + id);
+                       var feedupd = f_document.getElementById("FLUPD-" + id);
 
                        if (updated && feedlink) {
                                if (error) {
@@ -469,6 +524,14 @@ function parse_counters(reply, f_document, title_obj, scheduled_call) {
                                }
                        }
 
+                       if (updated && feedupd) {
+                               if (error) {
+                                       feedupd.innerHTML = updated + " (Error)";
+                               } else {
+                                       feedupd.innerHTML = updated;
+                               }
+                       }
+
                        if (feedctr && feedu && feedr) {
 
                                if (feedu.innerHTML != ctr && id == getActiveFeedId() && scheduled_call) {
@@ -510,33 +573,109 @@ 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 {
                        if (!xmlhttp_rpc.responseXML || !xmlhttp_rpc.responseXML.firstChild) {
-                               notify("[all_counters_callback] backend did not return valid XML");
+                               debug("[all_counters_callback] backend did not return valid XML");
                                return;
                        }
 
-                       if (!parent.frames["feeds-frame"]) {
-                               notify("[all_counters_callback] no parent feeds-frame");
-                               return;
-                       }
+                       debug("in all_counters_callback");
 
                        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);
+
+                       var runtime = counters.nextSibling;
+
+                       if (runtime) {
+                               getMainContext().parse_runtime_info(runtime);
+                       }
+
+                       if (getInitParam("feeds_sort_by_unread") == 1) {
+                               resort_feedlist();              
+                       }       
+
+                       hideOrShowFeeds(document, getInitParam("hide_read_feeds") == 1);
+
                } catch (e) {
                        exception_error("all_counters_callback", e);
                }
        }
 }
 
+function get_feed_entry_unread(doc, elem) {
+
+       var id = elem.id.replace("FEEDR-", "");
+
+       if (id <= 0) {
+               return -1;
+       }
+
+       try {
+               return parseInt(doc.getElementById("FEEDU-" + id).innerHTML);   
+       } catch (e) {
+               return -1;
+       }
+}
+
+function resort_category(doc, node) {
+       debug("resort_category: " + node);
+
+       if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
+               for (i = 0; i < node.childNodes.length; i++) {
+                       if (node.childNodes[i].nodeName != "LI") { continue; }
+
+                       if (get_feed_entry_unread(doc, node.childNodes[i]) < 0) {
+                               continue;
+                       }
+
+                       for (j = i+1; j < node.childNodes.length; j++) {                        
+                               if (node.childNodes[j].nodeName != "LI") { continue; }  
+
+                               var tmp_val = get_feed_entry_unread(doc, node.childNodes[i]);
+                               var cur_val = get_feed_entry_unread(doc, node.childNodes[j]);
+
+                               if (cur_val > tmp_val) {
+                                       tempnode_i = node.childNodes[i].cloneNode(true);
+                                       tempnode_j = node.childNodes[j].cloneNode(true);
+                                       node.replaceChild(tempnode_i, node.childNodes[j]);
+                                       node.replaceChild(tempnode_j, node.childNodes[i]);
+                               }
+                       }
+
+               }
+       }
+
+}
+
+function resort_feedlist() {
+       debug("resort_feedlist");
+
+       var fd = getFeedsContext().document;
+
+       if (fd.getElementById("feedCatHolder")) {
+
+               var feeds = fd.getElementById("feedList");
+               var child = feeds.firstChild;
+
+               while (child) {
+
+                       if (child.id == "feedCatHolder") {
+                               resort_category(fd, child.firstChild);
+                       }
+       
+                       child = child.nextSibling;
+               }
+
+       } else {
+               resort_category(fd, fd.getElementById("feedList"));
+       }
+}
+
 function update_all_counters(feed) {
        if (xmlhttp_ready(xmlhttp_rpc)) {
                var query = "backend.php?op=rpc&subop=getAllCounters";
@@ -593,25 +732,67 @@ function popupHelp(tid) {
 
 function hideOrShowFeeds(doc, hide) {
 
-       if (!doc.styleSheets) return;
+       var fd = getFeedsContext().document;
+
+       var list = fd.getElementById("feedList");
+
+       if (fd.getElementById("feedCatHolder")) {
+
+               var feeds = fd.getElementById("feedList");
+               var child = feeds.firstChild;
+
+               while (child) {
+
+                       if (child.id == "feedCatHolder") {
+                               hideOrShowFeedsCategory(fd, child.firstChild, hide, child.previousSibling);
+                       }
+       
+                       child = child.nextSibling;
+               }
+
+       } else {
+               hideOrShowFeedsCategory(fd, fd.getElementById("feedList"), hide);
+       }
+}
+
+function hideOrShowFeedsCategory(doc, node, hide, cat_node) {
+
+//     debug("hideOrShowFeedsCategory: " + node + " (" + hide + ")");
 
-       var css_rules = doc.styleSheets[0].cssRules;
+       var cat_unread = 0;
 
-       if (!css_rules || !css_rules.length) return;
+       if (node.hasChildNodes() && node.firstChild.nextSibling != false) {  
+               for (i = 0; i < node.childNodes.length; i++) {
+                       if (node.childNodes[i].nodeName != "LI") { continue; }
 
-       for (i = 0; i < css_rules.length; i++) {
-               var rule = css_rules[i];
+                       var has_unread = (node.childNodes[i].className != "feed");
+
+//                     debug(node.childNodes[i].id + " --> " + has_unread);
+
+                       if (hide && !has_unread) {
+                               node.childNodes[i].style.display = "none";
+                       }
 
-               if (rule.selectorText == "ul.feedList li.feed") {
                        if (!hide) {
-                               rule.style.display = "block";
-                       } else {
-                               rule.style.display = "none";
+                               node.childNodes[i].style.display = "list-item";
                        }
+
+                       if (has_unread) {
+                               cat_unread++;
+                       }
+
                }
+       }
 
-       } 
+       if (cat_unread == 0) {
+               if (hide) {
+                       cat_node.style.display = "none";
+               } else {
+                       cat_node.style.display = "list-item";
+               }
+       }
 
+//     debug("unread for category: " + cat_unread);
 }
 
 function selectTableRow(r, do_select) {
@@ -788,7 +969,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-', '');                                          
@@ -803,7 +984,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;
                }
 
@@ -937,28 +1118,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');
 
@@ -968,7 +1128,10 @@ function closeInfoBox() {
                box.style.display = "none";
        }
 
+       if (cleanup) box.innerHTML = "&nbsp;";
+
        enableHotkeys();
+
 }
 
 
@@ -1032,6 +1195,7 @@ function qaddFilter() {
        xmlhttp.onreadystatechange=infobox_submit_callback;
        xmlhttp.send(null);
 
+       return true;
 }
 
 function toggleSubmitNotEmpty(e, submit_id) {
@@ -1045,3 +1209,155 @@ 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);
+       }
+}
+
+var debug_last_class = "even";
+
+function debug(msg) {
+       var ctx = getMainContext();
+
+       if (ctx.debug_last_class == "even") {
+               ctx.debug_last_class = "odd";
+       } else {
+               ctx.debug_last_class = "even";
+       }
+
+       var c = ctx.document.getElementById('debug_output');
+       if (c && c.style.display == "block") {
+               while (c.lastChild != 'undefined' && c.childNodes.length > 100) {
+                       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 class=\"" + ctx.debug_last_class + "\"><span class=\"debugTS\">[" + ts + "]</span> " + 
+                       msg + "</li>" + c.innerHTML;
+       }
+}
+
+function getInitParam(key) {
+       return getMainContext().init_params[key];
+}
+
+function storeInitParam(key, value, is_client) {
+       try {
+               if (!is_client) {
+                       if (getMainContext().init_params[key] != value) {
+                               debug("storeInitParam: " + key + " => " + value);
+                               new Ajax.Request("backend.php?op=rpc&subop=storeParam&key=" + 
+                                       param_escape(key) + "&value=" + param_escape(value));   
+                       }
+               }
+               getMainContext().init_params[key] = value;
+       } catch (e) {
+               exception_error("storeInitParam", e);
+       }
+}
+
+/*
+function storeInitParams(params, is_client) {
+       try {
+               var s = "";
+
+               for (k in params) {
+                       if (getMainContext().init_params[k] != params[k]) {
+                               s += k + "=" + params[k] + ";";
+                               getMainContext().init_params[k] = params[k];
+                       }
+               } 
+
+               debug("storeInitParams: " + s);
+       
+               if (!is_client) {
+                       new Ajax.Request("backend.php?op=rpc&subop=storeParams&str=" + s);
+               }
+       } catch (e) {
+               exception_error("storeInitParams", 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;
+       }
+}