1 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
10 addNote: function (elem, text, type) {
14 text = text.replace(/\n/g, "<br/>");
15 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
18 addForm: function (elem, x) {
19 var form = $("<form action='#'/>");
20 form.submit(function(event) {
21 Adhoc.executeCommand("execute", Adhoc.serializeToDataform('form'),
22 function(e) { Adhoc.displayResult(elem, e) });
23 event.preventDefault();
25 var fieldset = $("<fieldset/>");
26 form.append(fieldset);
27 if ($(x).find("title").length > 0)
28 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
29 if ($(x).find("instructions").length > 0)
30 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
31 $(x).find("field").each(function() {
33 var type = $(this).attr("type");
34 if($(this).attr("label")) {
35 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
36 $("<br/>").appendTo(fieldset);
40 item = $("<input type='hidden'/>");
43 item = $("<input type='checkbox'/>");
46 item = $("<textarea rows='10' cols='70'/>");
49 item = $("<input type='text'/>");
52 item = $("<input type='text'/>").attr("readonly",true);
55 item = $("<textarea rows='10' cols='70'/>");
58 item = $("<input type='text'/>");
61 item = $("<select multiple='multiple'/>");
62 $(this).find("option").each(function() {
63 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
67 item = $("<select/>");
68 $(this).find("option").each(function() {
69 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
73 item = $("<input type='password'/>");
78 item.addClass("df-item");
79 if ($(this).find("value").length > 0) {
81 if ((type == "text-multi") || (type == "jid-multi")) {
83 $(this).find("value").each(function() {
84 value = value + $(this).text() + "\n";
87 } else if (type == "list-multi") {
88 $(this).children("value").each(function() {
89 item.children('option[value="' + $(this).text() + '"]').each(function() {
90 $(this).attr("selected", "selected");
94 item.val($(this).find("value").text());
97 if ($(x).attr("type") == "result")
98 item.attr("readonly", true);
99 if ($(this).attr("var")) {
100 item.attr("name", $(this).attr("var"));
101 item.attr("id", $(this).attr("var"));
103 fieldset.append(item);
104 if (type != "hidden")
105 fieldset.append("<br/>");
107 $(elem).append(form);
110 serializeToDataform: function (form) {
111 st = $build("x", {"xmlns": "jabber:x:data", "type": "submit"});
112 $(form).find(".df-item").each(function(){
113 st.c("field", {"var": $(this).attr("name")});
114 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
115 for (var i = 0; i < this.options.length; i++)
116 if (this.options[i].selected)
117 st.c("value").t(this.options[i].text).up();
118 } else if (this.nodeName.toLowerCase() == "textarea") {
119 var sp_value = this.value.split(/\r?\n|\r/g);
120 for(var i = 0; i < sp_value.length; i++)
121 st.c("value").t(sp_value[i]).up();
122 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
124 st.c("value").t("1");
126 st.c("value").t("0");
129 // if this has value then
130 st.c("value").t($(this).val()).up();
138 displayResult: function (elem, result) {
139 var status = $(result).find("command").attr("status");
140 var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
143 $(result).find("command > *").each(function(index, e) {
144 if ($(e).is("note")) {
145 Adhoc.addNote(elem, $(e).text(), $(e).attr("type"));
146 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
147 Adhoc.addForm(elem, e);
150 if (status == "executing") {
151 for (kind in kinds) {
152 input = $("<input type='button' disabled='disabled' value='" + kinds[kind] + "'/>").click(function() {
153 Adhoc.executeCommand(kind, (kind != 'prev') && Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
155 if ($(result).find('actions ' + kind).length > 0)
156 input.removeAttr("disabled");
157 $(elem).append(input);
160 $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
161 Adhoc.executeCommand("execute", Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
164 $("<input type='button' value='Cancel'/>").click(function() {
165 Adhoc.cancelCommand(function(e) { Adhoc.displayResult(elem, e) });
168 input = $("<input type='button' value='Start over'/>").bind("click", function() {
170 Adhoc.status.sessionid = null;
171 Adhoc.status.cmdNode = null;
172 Adhoc.getCommandNodes(elem);
174 $(elem).append(input);
178 runCommand: function (item, callback) {
179 Adhoc.status.cmdNode = $(item).attr("id"); // Save node of executed command (in global var)
180 Adhoc.executeCommand("execute", false, function(result) {
181 Adhoc.status.sessionid = $(result).find("command").attr("sessionid");
186 executeCommand: function (type, childs, callback) {
187 if (Adhoc.status.sessionid)
188 var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
189 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, sessionid: Adhoc.status.sessionid, action: type });
191 var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
192 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, action: type });
194 execIQ.cnode(childs);
195 connection.sendIQ(execIQ, callback);
198 cancelCommand: function (callback) {
199 Adhoc.executeCommand("cancel", false, callback);
200 Adhoc.status.cmdNode = null
201 Adhoc.status.sessionid = null;
204 getCommandNodes: function (elem) {
205 var nodesIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
206 connection.sendIQ(nodesIQ, function(result) {
207 var items = $("<ul></ul>");
208 $(elem).append(items);
209 $(result).find("item").each(function(index, e) {
210 $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
211 Adhoc.runCommand(this, function (result) { Adhoc.displayResult(elem, result); });
212 event.preventDefault();
218 checkFeatures: function (elem, jid) {
219 if (Adhoc.status.sessionid)
220 Adhoc.cancelCommand();
221 Adhoc.status.queryJID = jid;
222 var featureIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
224 connection.sendIQ(featureIQ,
225 function(result) { /* Callback */
226 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
227 Adhoc.getCommandNodes(elem);
229 $(elem).append("<p>" + Adhoc.status.queryJID + " does NOT support AdHoc commands</p>");
232 function(result) { /* Errback */
233 $(elem).append("<p>Couldn't get list of supported features</p>");