X-Git-Url: http://cgit.babelmonkeys.de/?p=adhocweb.git;a=blobdiff_plain;f=js%2Fadhoc.js;h=0266318c9e99626dd0b9d0a053ffc2899e54e02d;hp=7e2ba521bc30e910dcc9a258bc973493d2e6855b;hb=5a45869a032410c17d02374b8de20e496c114de3;hpb=e55b23864a9911c15d930b43ed9afe1df5943bf7 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); } }