]> cgit.babelmonkeys.de Git - adhocweb.git/blobdiff - js/main.js
More robust fetching of features
[adhocweb.git] / js / main.js
index 1bb3a459cee1c76a32db2d64f4ffd08b5c47ff7f..c03a8cf74c391428363da43a2f1269f1fb40d9c3 100644 (file)
@@ -1,5 +1,5 @@
 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
-var show_log = false;
+var show_log = true;
 
 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
 
@@ -41,6 +41,7 @@ function onConnect(status) {
     } else if (status == Strophe.Status.CONNECTED) {
         log('Strophe is connected.');
         queryJID = connection.domain;
+        $('#queryJID').val(queryJID);
         $('#query').show();
         checkFeatures();
     }
@@ -50,6 +51,7 @@ function addNote(elem, text, type) {
     if (!type) {
        type = "info";
     }
+    text = text.replace(/\n/g, "<br/>");
     $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
 }
 
@@ -86,7 +88,7 @@ function addForm(elem, x) {
             item = $("<input type='text'/>").attr("readonly",true);
             break;
         case "jid-multi":
-            item = $("<textarea rows='10' cols'70'/>");
+            item = $("<textarea rows='10' cols='70'/>");
             break;
         case "jid-single":
             item = $("<input type='text'/>");
@@ -127,6 +129,8 @@ function addForm(elem, x) {
                 item.val($(this).find("value").text());
             }
         }
+        if ($(x).attr("type") == "result")
+            item.attr("readonly", true);
         if ($(this).attr("var")) {
             item.attr("name", $(this).attr("var"));
             item.attr("id", $(this).attr("var"));
@@ -194,12 +198,24 @@ function displayResult(result) {
         });
 
         $('#cancelButton').bind("click", function() {
-            var cancelIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
-                .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
-            cmdNode = null
-            sessionid = null;
-            connection.sendIQ(cancelIQ, displayResult);
+            cancelCommand(displayResult);
+        });
+
+        $('#queryForm').unbind('submit');
+        $('#queryForm').bind('submit', function (event) {
+            cancelCommand(function(result) {
+                $('#queryForm').unbind('submit');
+                $('#queryForm').bind('submit', function (event) {
+                    queryJID = $('#queryJID').val();
+                    checkFeatures();
+                    event.preventDefault();
+                });
+            });
+            queryJID = $('#queryJID').val();
+            checkFeatures();
+            event.preventDefault();
         });
+
     } else {
         input = $("<input type='button' value='Start over'/>").bind("click", function() {
             $('#output').empty();
@@ -222,6 +238,14 @@ function runCommand(event) {
     event.preventDefault();
 }
 
+function cancelCommand(callback) {
+    var cancelIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
+        .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
+    cmdNode = null
+    sessionid = null;
+    connection.sendIQ(cancelIQ, callback);
+}
+
 function getCommandNodes() {
     var nodesIQ = $iq({ type: "get", to: queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
     connection.sendIQ(nodesIQ, function(result) {
@@ -236,15 +260,20 @@ function getCommandNodes() {
 
 function checkFeatures() {
     featureIQ = $iq({ type: "get", to: queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
-    connection.sendIQ(featureIQ, function(result) {
-        if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
-            $('#output').append("<p>This entitiy does support AdHoc commands</p>");
-        } else {
-            $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
-        }
-    });
     $('#output').empty();
-    getCommandNodes();
+    connection.sendIQ(featureIQ,
+        function(result) { /* Callback */
+                if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
+                    $('#output').append("<p>This entitiy does support AdHoc commands</p>");
+                    getCommandNodes();
+                } else {
+                    $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
+                }
+        },
+        function(result) { /* Errback */
+            $('#output').append("<p>Couldn't get list of supported features</p>");
+        }
+    );
 }
 
 function showConnect() {
@@ -300,13 +329,13 @@ $(document).ready(function () {
         } else {
             connection.disconnect();
         }
-       event.preventDefault();
+        event.preventDefault();
     });
 
     $('#queryForm').bind('submit', function (event) {
         queryJID = $('#queryJID').val();
         checkFeatures();
-       event.preventDefault();
+        event.preventDefault();
     });
 });