From: Florian Zeitz Date: Tue, 17 Jan 2012 21:13:45 +0000 (+0100) Subject: Restructure adhoc "class" X-Git-Url: https://git.babelmonkeys.de/?a=commitdiff_plain;h=5a45869a032410c17d02374b8de20e496c114de3;p=adhocweb.git Restructure adhoc "class" --- diff --git a/js/adhoc.js b/js/adhoc.js index 7e2ba52..0266318 100644 --- a/js/adhoc.js +++ b/js/adhoc.js @@ -23,27 +23,33 @@ if (!Function.prototype.bind) { Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands"); -var Adhoc = { - status: { +function Adhoc(view, readycb) { + this.status = { sessionid: null, cmdNode: null, - queryJID: null - }, + queryJID: null, + readycb: readycb, + view: view + }; +} + +Adhoc.prototype = { + constructor: Adhoc, - addNote: function (elem, text, type) { + addNote: function (text, type) { if (!type) { type = "info"; } text = text.replace(/\n/g, "
"); - $(elem).append("

" + text + "

"); + $(this.status.view).append("

" + text + "

"); }, - addForm: function (elem, x) { + addForm: function (x) { var self = this; var form = $("
"); form.submit(function(event) { self.executeCommand("execute", self.serializeToDataform('form'), - function(e) { self.displayResult(elem, e) }); + function(e) { self.displayResult(e) }); event.preventDefault(); }); var fieldset = $("
"); @@ -63,7 +69,7 @@ var Adhoc = { if ($(this).attr("type") !== "hidden") fieldset.append("
"); }); - $(elem).append(form); + $(self.status.view).append(form); }, buildHTMLField: function(fld) { @@ -147,52 +153,48 @@ var Adhoc = { return st.tree(); }, - displayResult: function (elem, result) { + displayResult: function (result) { var self = this; var status = $(result).find("command").attr("status"); var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'}; - $(elem).empty(); - $(result).find("command > *").each(function(index, e) { - if ($(e).is("note")) { - self.addNote(elem, $(e).text(), $(e).attr("type")); - } else if ($(e).is("x[xmlns=jabber:x:data]")) { - self.addForm(elem, e); + $(self.status.view).empty(); + $(result).find("command > *").each(function() { + if ($(this).is("note")) { + self.addNote($(this).text(), $(this).attr("type")); + } else if ($(this).is("x[xmlns=jabber:x:data]")) { + self.addForm(this); } }); if (status === "executing") { for (kind in kinds) { (function(type) { input = $("").click(function() { - self.executeCommand(type, (type!= 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) }); + self.executeCommand(type, (type!= 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(e) }); }); })(kind); if ($(result).find('actions ' + kind).length > 0) input.removeAttr("disabled"); - $(elem).append(input); + $(self.status.view).append(input); } $("").click(function() { - self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) }); - }).appendTo(elem); + self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(e) }); + }).appendTo(self.status.view); $("").click(function() { - self.cancelCommand(function(e) { self.displayResult(elem, e) }); - }).appendTo(elem); + self.cancelCommand(function(e) { self.displayResult(e) }); + }).appendTo(self.status.view); } else { - input = $("").bind("click", function() { - $(elem).empty(); - self.status.sessionid = null; - self.status.cmdNode = null; - self.getCommandNodes(elem); - }); - $(elem).append(input); + self.status.sessionid = null; + self.status.cmdNode = null; + self.status.readycb(); } }, runCommand: function (item, callback) { var cb; - this.status.cmdNode = $(item).attr("id"); /* Save node of executed command (in global var) */ + this.status.cmdNode = $(item).attr("id"); /* Save node of executed command */ cb = function(result) { this.status.sessionid = $(result).find("command").attr("sessionid"); callback(result); @@ -213,43 +215,44 @@ var Adhoc = { }, cancelCommand: function (callback) { + if (this.status.cmdNode == null) return; this.executeCommand("cancel", false, callback); this.status.cmdNode = null this.status.sessionid = null; }, - getCommandNodes: function (elem) { + getCommandNodes: function (callback) { var self = this; var nodesIQ = $iq({ type: "get", to: self.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC}); connection.sendIQ(nodesIQ, function(result) { var items = $("
    "); - $(elem).append(items); - $(result).find("item").each(function(index, e) { - $("
  • ").append($("" + $(e).attr("name") + "").click(function (event) { - self.runCommand(this, function (result) { self.displayResult(elem, result); }); + $(result).find("item").each(function() { + $("
  • ").append($("" + $(this).attr("name") + "").click(function (event) { + self.cancelCommand(function(){}); + self.runCommand(this, function (result) { self.displayResult(result); }); event.preventDefault(); })).appendTo(items); }); + callback(items); }); }, - checkFeatures: function (elem, jid) { - var cb, ecb; + checkFeatures: function (jid, cb, ecb) { + var callback; if (this.status.sessionid) this.cancelCommand(); this.status.queryJID = jid; var featureIQ = $iq({ type: "get", to: this.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO}); - $(elem).empty(); - cb = function(result) { /* Callback */ - if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) { - this.getCommandNodes(elem); - } else { - $(elem).append("

    " + this.status.queryJID + " does NOT support AdHoc commands

    "); - } - } - ecb = function(result) { /* Errback */ - $(elem).append("

    Couldn't get list of supported features

    "); - } - connection.sendIQ(featureIQ, cb.bind(this), ecb.bind(this)); + $(this.status.view).empty(); + + function callback(result) { + if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) { + cb(result); + } else { + ecb(result); + } + } + + connection.sendIQ(featureIQ, callback, ecb); } } diff --git a/js/main.js b/js/main.js index 73ace06..6215961 100644 --- a/js/main.js +++ b/js/main.js @@ -4,6 +4,8 @@ var show_log = true; var localJID = null; var connection = null; +var commandCenter = null; + function log(msg) { var entry = $('
    ').append(document.createTextNode(msg)); $('#log').append(entry); @@ -17,6 +19,17 @@ function rawOutput(data) { log('SENT: ' + data); } +function getFeatures(jid) { + var cb, ecb; + cb = function(result) { /* Callback */ + commandCenter.getCommandNodes(function(items) { $('#output').empty(); $('#output').append(items) }); + } + ecb = function(result) { /* Errback */ + $('#output').append("

    " + jid + " does NOT support AdHoc commands

    "); + } + commandCenter.checkFeatures(jid, cb, ecb); +} + function onConnect(status) { if (status == Strophe.Status.CONNECTING) { log('Strophe is connecting.'); @@ -37,7 +50,13 @@ function onConnect(status) { log('Strophe is connected.'); $('#queryJID').val(connection.domain); $('#query').show(); - Adhoc.checkFeatures("#output", connection.domain); + commandCenter = new Adhoc("#output", function() { + $("").bind("click", function() { + $('#output').empty(); + commandCenter.getCommandNodes(function(items) { $('#output').append(items) }); + }).appendTo('#output'); + }); + getFeatures(connection.domain); } } @@ -98,7 +117,7 @@ $(document).ready(function () { }); $('#queryForm').bind('submit', function (event) { - Adhoc.checkFeatures("#output", $('#queryJID').val()); + getFeatures($('#queryJID').val()); event.preventDefault(); }); });