X-Git-Url: http://cgit.babelmonkeys.de/?p=adhocweb.git;a=blobdiff_plain;f=js%2Fmain.js;h=85f2ccaeaa2ec8887710ca2fe8baed05aff0ada7;hp=2cf542ec2cadb8c251f54a8fa790a6c945b68b7b;hb=7ef6024aa84881aba55e61c38073fc9465aac876;hpb=1886915ac6353cf32210666f69ef4a8ede39a04c diff --git a/js/main.js b/js/main.js index 2cf542e..85f2cca 100644 --- a/js/main.js +++ b/js/main.js @@ -1,9 +1,15 @@ var BOSH_SERVICE = 'http://localhost:5280/http-bind/'; +var show_log = true; Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands"); var localJID = null; var connection = null; +var adhoc_status = { + sessionid: null, + cmdNode: null, + queryJID: null +}; function log(msg) { var entry = $('
').append(document.createTextNode(msg)); @@ -36,67 +42,240 @@ function onConnect(status) { } } else if (status == Strophe.Status.CONNECTED) { log('Strophe is connected.'); - checkFeatures(); + adhoc_status.queryJID = connection.domain; + $('#queryJID').val(adhoc_status.queryJID); + $('#query').show(); + checkFeatures("#output"); } } -function displayResult(result) { +function addNote(elem, text, type) { + if (!type) { + type = "info"; + } + text = text.replace(/\n/g, "" + text + "
"); +} + +function addForm(elem, x) { + 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) + $("").text($(x).find("title").text()).appendTo(fieldset); + if ($(x).find("instructions").length > 0) + $("").text($(x).find("instructions").text()).appendTo(fieldset); + $(x).find("field").each(function() { + var item = null; + var type = $(this).attr("type"); + if($(this).attr("label")) { + $("").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset); + $("" + $(e).text() + "
"); }); 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); } + + $("").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 *').remove(); - getCommandNodes(); + input = $("").bind("click", function() { + $(elem).empty(); + adhoc_status.sessionid = null; + adhoc_status.cmdNode = null; + getCommandNodes(elem); }); - $("#output").append(input); + $(elem).append(input); } } -function runCommand() { - execIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() }) - .c("command", { xmlns: Strophe.NS.ADHOC, node: $(this).attr("id"), - action: "execute" }); - connection.sendIQ(execIQ, displayResult); +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"); + callback(result); + }); +} + +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) { + executeCommand("cancel", false, callback); + adhoc_status.cmdNode = null + adhoc_status.sessionid = null; } -function getCommandNodes() { - nodesIQ = $iq({ type: "get", to: "localhost", 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("This entitiy does support AdHoc commands
"); - } else { - $('#output').append("This entitiy does NOT support AdHoc commands
"); +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) { + getCommandNodes(elem); + } else { + $(elem).append("" + adhoc_status.queryJID + " does NOT support AdHoc commands
"); + } + }, + function(result) { /* Errback */ + $(elem).append("Couldn't get list of supported features
"); } - }); - getCommandNodes(); + ); } function showConnect() { @@ -105,11 +284,12 @@ function showConnect() { var button = $('#connect').get(0); button.value = 'connect'; + $('#query').hide(); pass.show(); jid.show(); - $('label').show(); - $('#output *').remove(); - return false; + $('#cred label').show(); + $('#cred br').show(); + $('#output').empty(); } function showDisconnect() { @@ -120,20 +300,23 @@ function showDisconnect() { button.value = 'disconnect'; pass.hide(); jid.hide(); - $('label').hide(); - return false; + $('#cred label').hide(); + $('#cred br').hide(); } $(document).ready(function () { connection = new Strophe.Connection(BOSH_SERVICE); - connection.rawInput = rawInput; - connection.rawOutput = rawOutput; + if (show_log) { + $('#log_container').show(); + connection.rawInput = rawInput; + connection.rawOutput = rawOutput; + } $("#log_toggle").click(function () { $("#log").toggle(); }); - $('#cred').bind('submit', function () { + $('#cred').bind('submit', function (event) { var button = $('#connect').get(0); var jid = $('#jid'); var pass = $('#pass'); @@ -141,20 +324,25 @@ $(document).ready(function () { if (button.value == 'connect') { showDisconnect(); - $('#log *').remove(); + $('#log').empty(); connection.connect(localJID, pass.get(0).value, onConnect); } else { connection.disconnect(); } - return false; + event.preventDefault(); + }); + + $('#queryForm').bind('submit', function (event) { + adhoc_status.queryJID = $('#queryJID').val(); + checkFeatures("#output"); + event.preventDefault(); }); }); -onunload = function() { +window.onunload = window.onbeforeunload = function() { if (connection) { connection.disconnect(); } } -