]> git.wh0rd.org - tt-rss.git/blobdiff - js/functions.js
Replace all setTimeout strings with functions
[tt-rss.git] / js / functions.js
old mode 100644 (file)
new mode 100755 (executable)
index fdd7915..63ff412
@@ -44,11 +44,8 @@ function exception_error(location, e, ext_info) {
 
        try {
 
-               if (ext_info) {
-                       if (ext_info.responseText) {
-                               ext_info = ext_info.responseText;
-                       }
-               }
+               if (ext_info)
+                       ext_info = JSON.stringify(ext_info);
 
                try {
                        new Ajax.Request("backend.php", {
@@ -104,13 +101,15 @@ function exception_error(location, e, ext_info) {
                        title: "Unhandled exception",
                        style: "width: 600px",
                        report: function() {
-                               if (confirm(__("Are you sure to report this exception to tt-rss.org? The report will include your browser information. Your IP would be saved in the database."))) {
+                               if (confirm(__("Are you sure to report this exception to tt-rss.org? The report will include information about your web browser and tt-rss configuration. Your IP will be saved in the database."))) {
 
                                        document.forms['exceptionForm'].params.value = $H({
                                                browserName: navigator.appName,
                                                browserVersion: navigator.appVersion,
                                                browserPlatform: navigator.platform,
                                                browserCookies: navigator.cookieEnabled,
+                                               ttrssVersion: __ttrss_version,
+                                               initParams: JSON.stringify(init_params),
                                        }).toQueryString();
 
                                        document.forms['exceptionForm'].submit();
@@ -183,11 +182,6 @@ function param_unescape(arg) {
                return unescape(arg);
 }
 
-
-function hide_notify() {
-       Element.hide('notify');
-}
-
 function notify_real(msg, no_hide, n_type) {
 
        var n = $("notify");
@@ -199,12 +193,11 @@ function notify_real(msg, no_hide, n_type) {
        }
 
        if (msg == "") {
-               if (Element.visible(n)) {
-                       notify_hide_timerid = window.setTimeout("hide_notify()", 0);
+               if (n.hasClassName("visible")) {
+                       notify_hide_timerid = window.setTimeout(function() {
+                               n.removeClassName("visible") }, 0);
                }
                return;
-       } else {
-               Element.show(n);
        }
 
        /* types:
@@ -218,30 +211,40 @@ function notify_real(msg, no_hide, n_type) {
 
        msg = "<span class=\"msg\"> " + __(msg) + "</span>";
 
-       if (n_type == 1) {
-               n.className = "notify";
-       } else if (n_type == 2) {
-               n.className = "notify progress";
+       if (n_type == 2) {
                msg = "<span><img src='images/indicator_white.gif'></span>" + msg;
                no_hide = true;
        } else if (n_type == 3) {
-               n.className = "notify error";
                msg = "<span><img src='images/alert.png'></span>" + msg;
        } else if (n_type == 4) {
-               n.className = "notify info";
                msg = "<span><img src='images/information.png'></span>" + msg;
        }
 
        msg += " <span><img src=\"images/cross.png\" class=\"close\" title=\"" +
                __("Click to close") + "\" onclick=\"notify('')\"></span>";
 
-//     msg = "<img src='images/live_com_loading.gif'> " + msg;
-
        n.innerHTML = msg;
 
-       if (!no_hide) {
-               notify_hide_timerid = window.setTimeout("hide_notify()", 5*1000);
-       }
+       window.setTimeout(function() {
+               // goddamnit firefox
+               if (n_type == 2) {
+               n.className = "notify notify_progress visible";
+                       } else if (n_type == 3) {
+                       n.className = "notify notify_error visible";
+                       msg = "<span><img src='images/alert.png'></span>" + msg;
+               } else if (n_type == 4) {
+                       n.className = "notify notify_info visible";
+               } else {
+                       n.className = "notify visible";
+               }
+
+               if (!no_hide) {
+                       notify_hide_timerid = window.setTimeout(function() {
+                               n.removeClassName("visible") }, 5*1000);
+               }
+
+       }, 10);
+
 }
 
 function notify(msg, no_hide) {
@@ -499,27 +502,6 @@ function infobox_callback2(transport, title) {
        }
 }
 
-function filterCR(e, f)
-{
-     var key;
-
-     if(window.event)
-          key = window.event.keyCode;     //IE
-     else
-          key = e.which;     //firefox
-
-       if (key == 13) {
-               if (typeof f != 'undefined') {
-                       f();
-                       return false;
-               } else {
-                       return false;
-               }
-       } else {
-               return true;
-       }
-}
-
 function getInitParam(key) {
        return init_params[key];
 }
@@ -588,15 +570,21 @@ function filterDlgCheckAction(sender) {
                }
 
                // if selected action supports parameters, enable params field
-               if (action == 4 || action == 6 || action == 7) {
+               if (action == 4 || action == 6 || action == 7 || action == 9) {
                        new Effect.Appear(action_param, {duration : 0.5});
-                       if (action != 7) {
-                               Element.show(dijit.byId("filterDlg_actionParam").domNode);
-                               Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
-                       } else {
+
+                       Element.hide(dijit.byId("filterDlg_actionParam").domNode);
+                       Element.hide(dijit.byId("filterDlg_actionParamLabel").domNode);
+                       Element.hide(dijit.byId("filterDlg_actionParamPlugin").domNode);
+
+                       if (action == 7) {
                                Element.show(dijit.byId("filterDlg_actionParamLabel").domNode);
-                               Element.hide(dijit.byId("filterDlg_actionParam").domNode);
+                       } else if (action == 9) {
+                               Element.show(dijit.byId("filterDlg_actionParamPlugin").domNode);
+                       } else {
+                               Element.show(dijit.byId("filterDlg_actionParam").domNode);
                        }
+
                } else {
                        Element.hide(action_param);
                }
@@ -680,7 +668,7 @@ function hotkey_prefix_timeout() {
                        Element.hide('cmdline');
                }
 
-               setTimeout("hotkey_prefix_timeout()", 1000);
+               setTimeout(hotkey_prefix_timeout, 1000);
 
        } catch  (e) {
                exception_error("hotkey_prefix_timeout", e);
@@ -829,7 +817,14 @@ function quickAddFeed() {
                                                onComplete: function(transport) {
                                                        try {
 
-                                                               var reply = JSON.parse(transport.responseText);
+                                                               try {
+                                                                       var reply = JSON.parse(transport.responseText);
+                                                               } catch (e) {
+                                                                       Element.hide("feed_add_spinner");
+                                                                       alert(__("Failed to parse output. This can indicate server timeout and/or network issues. Backend output was logged to browser console."));
+                                                                       console.log('quickAddFeed, backend returned:' + transport.responseText);
+                                                                       return;
+                                                               }
 
                                                                var rc = reply['result'];
 
@@ -906,7 +901,7 @@ function createNewRuleElement(parentNode, replaceNode) {
        try {
                var form = document.forms["filter_new_rule_form"];
 
-               form.reg_exp.value = form.reg_exp.value.replace(/(<([^>]+)>)/ig,"");
+               //form.reg_exp.value = form.reg_exp.value.replace(/(<([^>]+)>)/ig,"");
 
                var query = "backend.php?op=pref-filters&method=printrulename&rule="+
                        param_escape(dojo.formToJson(form));
@@ -956,6 +951,8 @@ function createNewActionElement(parentNode, replaceNode) {
 
                if (form.action_id.value == 7) {
                        form.action_param.value = form.action_param_label.value;
+               } else if (form.action_id.value == 9) {
+                       form.action_param.value = form.action_param_plugin.value;
                }
 
                var query = "backend.php?op=pref-filters&method=printactionname&action="+
@@ -1054,6 +1051,100 @@ function addFilterAction(replaceNode, actionStr) {
        }
 }
 
+function editFilterTest(query) {
+       try {
+
+               if (dijit.byId("filterTestDlg"))
+                       dijit.byId("filterTestDlg").destroyRecursive();
+
+               var test_dlg = new dijit.Dialog({
+                       id: "filterTestDlg",
+                       title: "Test Filter",
+                       style: "width: 600px",
+                       results: 0,
+                       limit: 100,
+                       max_offset: 10000,
+                       getTestResults: function(query, offset) {
+                               var updquery = query + "&offset=" + offset + "&limit=" + test_dlg.limit;
+
+                               console.log("getTestResults:" + offset);
+
+                               new Ajax.Request("backend.php", {
+                                       parameters: updquery,
+                                       onComplete: function (transport) {
+                                               try {
+                                                       var result = JSON.parse(transport.responseText);
+
+                                                       if (result && dijit.byId("filterTestDlg") && dijit.byId("filterTestDlg").open) {
+                                                               test_dlg.results += result.size();
+
+                                                               console.log("got results:" + result.size());
+
+                                                               $("prefFilterProgressMsg").innerHTML = __("Looking for articles (%d processed, %f found)...")
+                                                                       .replace("%f", test_dlg.results)
+                                                                       .replace("%d", offset);
+
+                                                               console.log(offset + " " + test_dlg.max_offset);
+
+                                                               for (var i = 0; i < result.size(); i++) {
+                                                                       var tmp = new Element("table");
+                                                                       tmp.innerHTML = result[i];
+                                                                       dojo.parser.parse(tmp);
+
+                                                                       $("prefFilterTestResultList").innerHTML += tmp.innerHTML;
+                                                               }
+
+                                                               if (test_dlg.results < 30 && offset < test_dlg.max_offset) {
+
+                                                                       // get the next batch
+                                                                       window.setTimeout(function () {
+                                                                               test_dlg.getTestResults(query, offset + test_dlg.limit);
+                                                                       }, 0);
+
+                                                               } else {
+                                                                       // all done
+
+                                                                       Element.hide("prefFilterLoadingIndicator");
+
+                                                                       if (test_dlg.results == 0) {
+                                                                               $("prefFilterTestResultList").innerHTML = "<tr><td align='center'>No recent articles matching this filter have been found.</td></tr>";
+                                                                               $("prefFilterProgressMsg").innerHTML = "Articles matching this filter:";
+                                                                       } else {
+                                                                               $("prefFilterProgressMsg").innerHTML = __("Found %d articles matching this filter:")
+                                                                                       .replace("%d", test_dlg.results);
+                                                                       }
+
+                                                               }
+
+                                                       } else if (!result) {
+                                                               console.log("getTestResults: can't parse results object");
+
+                                                               Element.hide("prefFilterLoadingIndicator");
+
+                                                               notify_error("Error while trying to get filter test results.");
+
+                                                       } else {
+                                                               console.log("getTestResults: dialog closed, bailing out.");
+                                                       }
+                                               } catch (e) {
+                                                       exception_error("editFilterTest/inner", e);
+                                               }
+
+                                       } });
+                       },
+                       href: query});
+
+               dojo.connect(test_dlg, "onLoad", null, function(e) {
+                       test_dlg.getTestResults(query, 0);
+               });
+
+               test_dlg.show();
+
+       } catch (e) {
+               exception_error("editFilterTest", e);
+       }
+}
+
 function quickAddFilter() {
        try {
                var query = "";
@@ -1080,16 +1171,7 @@ function quickAddFilter() {
                        test: function() {
                                var query = "backend.php?" + dojo.formToQuery("filter_new_form") + "&savemode=test";
 
-                               if (dijit.byId("filterTestDlg"))
-                                       dijit.byId("filterTestDlg").destroyRecursive();
-
-                               var test_dlg = new dijit.Dialog({
-                                       id: "filterTestDlg",
-                                       title: "Test Filter",
-                                       style: "width: 600px",
-                                       href: query});
-
-                               test_dlg.show();
+                               editFilterTest(query);
                        },
                        selectRules: function(select) {
                                $$("#filterDlg_Matches input[type=checkbox]").each(function(e) {
@@ -1150,33 +1232,48 @@ function quickAddFilter() {
                        href: query});
 
                if (!inPreferences()) {
+                       var selectedText = getSelectionText();
+
                        var lh = dojo.connect(dialog, "onLoad", function(){
                                dojo.disconnect(lh);
 
-                               var query = "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId();
+                               if (selectedText != "") {
 
-                               new Ajax.Request("backend.php", {
-                               parameters: query,
-                               onComplete: function(transport) {
-                                       var reply = JSON.parse(transport.responseText);
+                                       var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
+                                               getActiveFeedId();
 
-                                       var title = false;
+                                       var rule = { reg_exp: selectedText, feed_id: feed_id, filter_type: 1 };
 
-                                       if (reply && reply) title = reply.title;
+                                       addFilterRule(null, dojo.toJson(rule));
 
-                                       if (title || getActiveFeedId() || activeFeedIsCat()) {
+                               } else {
 
-                                               console.log(title + " " + getActiveFeedId());
+                                       var query = "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId();
 
-                                               var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
-                                                       getActiveFeedId();
+                                       new Ajax.Request("backend.php", {
+                                       parameters: query,
+                                       onComplete: function(transport) {
+                                               var reply = JSON.parse(transport.responseText);
 
-                                               var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
+                                               var title = false;
 
-                                               addFilterRule(null, dojo.toJson(rule));
-                                       }
+                                               if (reply && reply) title = reply.title;
 
-                               } });
+                                               if (title || getActiveFeedId() || activeFeedIsCat()) {
+
+                                                       console.log(title + " " + getActiveFeedId());
+
+                                                       var feed_id = activeFeedIsCat() ? 'CAT:' + parseInt(getActiveFeedId()) :
+                                                               getActiveFeedId();
+
+                                                       var rule = { reg_exp: title, feed_id: feed_id, filter_type: 1 };
+
+                                                       addFilterRule(null, dojo.toJson(rule));
+                                               }
+
+                                       } });
+
+                               }
 
                        });
                }
@@ -1228,7 +1325,7 @@ function unsubscribeFeed(feed_id, title) {
                                                updateFeedList();
                                        } else {
                                                if (feed_id == getActiveFeedId())
-                                                       setTimeout("viewfeed(-5)", 100);
+                                                       setTimeout(function() { viewfeed({feed:-5}) }, 100);
 
                                                if (feed_id < 0) updateFeedList();
                                        }
@@ -1272,10 +1369,8 @@ function backend_sanity_check_callback(transport) {
                        console.log('reading init-params...');
 
                        for (k in params) {
-                               var v = params[k];
-                               console.log("IP: " + k + " => " + v);
-
-                               if (k == "label_base_index") _label_base_index = parseInt(v);
+                               console.log("IP: " + k + " => " + JSON.stringify(params[k]));
+                               if (k == "label_base_index") _label_base_index = parseInt(params[k]);
                        }
 
                        init_params = params;
@@ -1936,3 +2031,32 @@ function feed_to_label_id(feed) {
        return _label_base_index - 1 + Math.abs(feed);
 }
 
+// http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
+
+function getSelectionText() {
+       var text = "";
+
+       if (typeof window.getSelection != "undefined") {
+               var sel = window.getSelection();
+               if (sel.rangeCount) {
+                       var container = document.createElement("div");
+                       for (var i = 0, len = sel.rangeCount; i < len; ++i) {
+                               container.appendChild(sel.getRangeAt(i).cloneContents());
+                       }
+                       text = container.innerHTML;
+               }
+       } else if (typeof document.selection != "undefined") {
+               if (document.selection.type == "Text") {
+                       text = document.selection.createRange().textText;
+               }
+       }
+
+       return text.stripTags();
+}
+
+function openArticlePopup(id) {
+       window.open("backend.php?op=article&method=view&mode=raw&html=1&zoom=1&id=" + id +
+               "&csrf_token=" + getInitParam("csrf_token"),
+               "ttrss_article_popup",
+               "height=900,width=900,resizable=yes,status=no,location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no");
+}
\ No newline at end of file