From 7ef6024aa84881aba55e61c38073fc9465aac876 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Sat, 25 Dec 2010 04:16:10 +0100 Subject: [PATCH] Prepare for moving adhoc code into a separate file * Don't explicitly use '#output' everywhere * Separate UI and protocol better * Some XHTML 1.1 fixes --- index.html | 28 ++++++----- js/main.js | 139 ++++++++++++++++++++++++++--------------------------- style.css | 6 ++- 3 files changed, 90 insertions(+), 83 deletions(-) diff --git a/index.html b/index.html index b99d27e..195bd94 100644 --- a/index.html +++ b/index.html @@ -12,23 +12,27 @@
-
-
- -
-
- -
- + +
+
+ +
+
+ +
+ +
-
- - - + +
+ + + +
diff --git a/js/main.js b/js/main.js index 955ceb9..85f2cca 100644 --- a/js/main.js +++ b/js/main.js @@ -45,7 +45,7 @@ function onConnect(status) { adhoc_status.queryJID = connection.domain; $('#queryJID').val(adhoc_status.queryJID); $('#query').show(); - checkFeatures(); + checkFeatures("#output"); } } @@ -58,8 +58,11 @@ function addNote(elem, text, type) { } function addForm(elem, x) { - var form = $("
"); - form.submit(function(event){event.preventDefault();}); + var form = $(""); + form.submit(function(event) { + executeCommand("execute", serializeToDataform('form'), function(e) { displayResult(elem, e) }); + event.preventDefault(); + }); var fieldset = $("
"); form.append(fieldset); if ($(x).find("title").length > 0) @@ -145,8 +148,8 @@ function addForm(elem, x) { $(elem).append(form); } -function serializeToDataform(form, st) { - st.c("x", {"xmlns": "jabber:x:data", "type": "submit"}); +function serializeToDataform(form) { + st = $build("x", {"xmlns": "jabber:x:data", "type": "submit"}); $(form).find(".df-item").each(function(){ st.c("field", {"var": $(this).attr("name")}); if (this.nodeName.toLowerCase() == "select" && this.multiple) { @@ -170,111 +173,107 @@ function serializeToDataform(form, st) { st.up(); }); st.up(); + return st.tree(); } -function displayResult(result) { +function displayResult(elem, result) { var status = $(result).find("command").attr("status"); + var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'}; - $("#output").empty(); + $(elem).empty(); $(result).find("command > *").each(function(index, e) { if ($(e).is("note")) { - addNote("#output", $(e).text(), $(e).attr("type")); + addNote(elem, $(e).text(), $(e).attr("type")); } else if ($(e).is("x[xmlns=jabber:x:data]")) { - addForm("#output", e); + addForm(elem, e); } }); if (status == "executing") { - $("#output").append(""+ - ""+ - ""+ - ""+ - ""); - for (kind in ['prev', 'next', 'complete']) { + for (kind in kinds) { + input = $("").click(function() { + if (kind == 'prev') + executeCommand(kind, false, function(e) { displayResult(elem, e) }); + else + executeCommand(kind, serializeToDataform('form'), function(e) { displayResult(elem, e) }); + }); if ($(result).find('actions ' + kind).length > 0) - $('#' + kind + 'Button').attr("disabled", "false"); + input.removeAttr("disabled"); + $(elem).append(input); } - $('#executeButton').bind("click", function() { - var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() }) - .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "execute" }); - serializeToDataform($('form'), execIQ); - connection.sendIQ(execIQ, displayResult); - }); - $('#cancelButton').bind("click", function() { - cancelCommand(displayResult); - }); - - $('#queryForm').unbind('submit'); - $('#queryForm').bind('submit', function (event) { - cancelCommand(function(result) { - $('#queryForm').unbind('submit'); - $('#queryForm').bind('submit', function (event) { - adhoc_status.queryJID = $('#queryJID').val(); - checkFeatures(); - event.preventDefault(); - }); - }); - adhoc_status.queryJID = $('#queryJID').val(); - checkFeatures(); - event.preventDefault(); - }); + $("").click(function() { + executeCommand("execute", serializeToDataform('form'), function(e) { displayResult(elem, e) }); + }).appendTo(elem); + $("").click(function() { + cancelCommand(function(e) { displayResult(elem, e) }); + }).appendTo(elem); } else { input = $("").bind("click", function() { - $('#output').empty(); + $(elem).empty(); adhoc_status.sessionid = null; adhoc_status.cmdNode = null; - getCommandNodes(); + getCommandNodes(elem); }); - $("#output").append(input); + $(elem).append(input); } } -function runCommand(event) { - adhoc_status.cmdNode = $(this).attr("id"); // Save node of executed command (in global var) - var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() }) - .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, action: "execute" }); - connection.sendIQ(execIQ, function(result) { +function runCommand(item, callback) { + adhoc_status.cmdNode = $(item).attr("id"); // Save node of executed command (in global var) + executeCommand("execute", false, function(result) { adhoc_status.sessionid = $(result).find("command").attr("sessionid"); - displayResult(result); + callback(result); }); - event.preventDefault(); +} + +function executeCommand(type, childs, callback) { + if (adhoc_status.sessionid) + var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() }) + .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: type }); + else + var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() }) + .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, action: type }); + if (childs) + execIQ.cnode(childs); + connection.sendIQ(execIQ, callback); } function cancelCommand(callback) { - var cancelIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() }) - .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "cancel" }); + executeCommand("cancel", false, callback); adhoc_status.cmdNode = null adhoc_status.sessionid = null; - connection.sendIQ(cancelIQ, callback); } -function getCommandNodes() { - var nodesIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC}); +function getCommandNodes(elem) { + var nodesIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC}); connection.sendIQ(nodesIQ, function(result) { - $('#output').append("
    "); + var items = $("
      "); + $(elem).append(items); $(result).find("item").each(function(index, e) { - link = $("" + $(e).attr("name") + "").click(runCommand) - item = $("
    • ").append(link); - $("#items").append(item); + $("
    • ").append($("" + $(e).attr("name") + "").click(function (event) { + runCommand(this, function (result) { displayResult(elem, result); }); + event.preventDefault(); + })).appendTo(items); }); }); } -function checkFeatures() { - featureIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO}); - $('#output').empty(); +function checkFeatures(elem) { + if (adhoc_status.sessionid) + cancelCommand(); + featureIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO}); + $(elem).empty(); connection.sendIQ(featureIQ, function(result) { /* Callback */ - if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) { - $('#output').append("

      This entitiy does support AdHoc commands

      "); - getCommandNodes(); - } else { - $('#output').append("

      This entitiy does NOT support AdHoc commands

      "); - } + if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) { + getCommandNodes(elem); + } else { + $(elem).append("

      " + adhoc_status.queryJID + " does NOT support AdHoc commands

      "); + } }, function(result) { /* Errback */ - $('#output').append("

      Couldn't get list of supported features

      "); + $(elem).append("

      Couldn't get list of supported features

      "); } ); } @@ -337,7 +336,7 @@ $(document).ready(function () { $('#queryForm').bind('submit', function (event) { adhoc_status.queryJID = $('#queryJID').val(); - checkFeatures(); + checkFeatures("#output"); event.preventDefault(); }); }); diff --git a/style.css b/style.css index 26e8b37..b26b844 100644 --- a/style.css +++ b/style.css @@ -25,7 +25,7 @@ a { display: none } -#login{ +#login { float: left; margin-right: 2em; padding: 1em; @@ -33,6 +33,10 @@ a { color: #000000 } +#login fieldset, #query fieldset { + border: 0px +} + #cred input[type="submit"] { margin-left: 0em } -- 2.39.5