X-Git-Url: http://cgit.babelmonkeys.de/?p=adhocweb.git;a=blobdiff_plain;f=js%2Fadhoc.js;h=51932499f7a099ac94a96f8daa20d78f4509d1c9;hp=3f2ae2b4e96843896f95fdc1a4dc8c540be2e529;hb=HEAD;hpb=9f26ed9cdd3330b0fb922e1475d3175caa46d528
diff --git a/js/adhoc.js b/js/adhoc.js
index 3f2ae2b..5193249 100644
--- a/js/adhoc.js
+++ b/js/adhoc.js
@@ -1,3 +1,9 @@
+// adhocweb
+// Copyright (c) 2010-2013 Florian Zeitz
+//
+// This project is MIT licensed.
+// Please see the COPYING file for more information.
+
/*
* Implementation of ECMA Script 5 like bind from:
* https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
@@ -23,27 +29,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 = $("");
+ 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 = $("");
@@ -51,19 +63,18 @@ var Adhoc = {
$(x).find("title").each(function() { $("").text($(this).text()).appendTo(fieldset); });
$(x).find("instructions").each(function() { $("").text($(this).text()).appendTo(fieldset); });
$(x).find("field").each(function() {
+ var clearfix = $("");
var item = self.buildHTMLField(this);
var label = $(this).attr("label");
if(label) {
- $("").text(label).attr("for", $(this).attr("var")).appendTo(fieldset);
- $("
").appendTo(fieldset);
+ $("").text(label).attr("for", $(this).attr("var")).appendTo(clearfix);
}
if ($(x).attr("type") === "result")
item.attr("readonly", true);
- fieldset.append(item);
- if ($(this).attr("type") !== "hidden")
- fieldset.append("
");
+ clearfix.append(item);
+ fieldset.append(clearfix);
});
- $(elem).append(form);
+ $(self.status.view).append(form);
},
buildHTMLField: function(fld) {
@@ -147,50 +158,49 @@ 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'};
+ var actions = $(result).find("actions:first");
- $(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") {
+ var controls = $("");
for (kind in kinds) {
- input = $("").click(function() {
- self.executeCommand(kind, (kind != 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
- });
- if ($(result).find('actions ' + kind).length > 0)
+ var input;
+ (function (type) {
+ input = $("").click(function() {
+ self.executeCommand(type, (type != 'prev') && self.serializeToDataform('form'), function(e) { self.displayResult(e) });
+ }).appendTo(controls);
+ })(kind);
+ if (actions.find(kind).length > 0)
input.removeAttr("disabled");
- $(elem).append(input);
+ if (actions.attr("execute") == kind)
+ input.addClass("primary");
}
- $("").click(function() {
- self.executeCommand("execute", self.serializeToDataform('form'), function(e) { self.displayResult(elem, e) });
- }).appendTo(elem);
-
- $("").click(function() {
- self.cancelCommand(function(e) { self.displayResult(elem, e) });
- }).appendTo(elem);
+ $("").click(function() {
+ self.cancelCommand(function(e) { self.displayResult(e) });
+ }).appendTo(controls);
+ $(self.status.view + " fieldset").append(controls);
} 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);
@@ -211,43 +221,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);
}
}