8 addNote: function (elem, text, type) {
12 text = text.replace(/\n/g, "<br/>");
13 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
16 addForm: function (elem, x) {
17 var form = $("<form action='#'/>");
18 form.submit(function(event) {
19 Adhoc.executeCommand("execute", Adhoc.serializeToDataform('form'),
20 function(e) { Adhoc.displayResult(elem, e) });
21 event.preventDefault();
23 var fieldset = $("<fieldset/>");
24 form.append(fieldset);
25 if ($(x).find("title").length > 0)
26 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
27 if ($(x).find("instructions").length > 0)
28 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
29 $(x).find("field").each(function() {
31 var type = $(this).attr("type");
32 if($(this).attr("label")) {
33 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
34 $("<br/>").appendTo(fieldset);
38 item = $("<input type='hidden'/>");
41 item = $("<input type='checkbox'/>");
44 item = $("<textarea rows='10' cols='70'/>");
47 item = $("<input type='text'/>");
50 item = $("<input type='text'/>").attr("readonly",true);
53 item = $("<textarea rows='10' cols='70'/>");
56 item = $("<input type='text'/>");
59 item = $("<select multiple='multiple'/>");
60 $(this).find("option").each(function() {
61 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
65 item = $("<select/>");
66 $(this).find("option").each(function() {
67 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
71 item = $("<input type='password'/>");
76 item.addClass("df-item");
77 if ($(this).find("value").length > 0) {
79 if ((type == "text-multi") || (type == "jid-multi")) {
81 $(this).find("value").each(function() {
82 value = value + $(this).text() + "\n";
85 } else if (type == "list-multi") {
86 $(this).children("value").each(function() {
87 item.children('option[value="' + $(this).text() + '"]').each(function() {
88 $(this).attr("selected", "selected");
92 item.val($(this).find("value").text());
95 if ($(x).attr("type") == "result")
96 item.attr("readonly", true);
97 if ($(this).attr("var")) {
98 item.attr("name", $(this).attr("var"));
99 item.attr("id", $(this).attr("var"));
101 fieldset.append(item);
102 if (type != "hidden")
103 fieldset.append("<br/>");
105 $(elem).append(form);
108 serializeToDataform: function (form) {
109 st = $build("x", {"xmlns": "jabber:x:data", "type": "submit"});
110 $(form).find(".df-item").each(function(){
111 st.c("field", {"var": $(this).attr("name")});
112 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
113 for (var i = 0; i < this.options.length; i++)
114 if (this.options[i].selected)
115 st.c("value").t(this.options[i].text).up();
116 } else if (this.nodeName.toLowerCase() == "textarea") {
117 var sp_value = this.value.split(/\r?\n|\r/g);
118 for(var i = 0; i < sp_value.length; i++)
119 st.c("value").t(sp_value[i]).up();
120 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
122 st.c("value").t("1");
124 st.c("value").t("0");
127 // if this has value then
128 st.c("value").t($(this).val()).up();
136 displayResult: function (elem, result) {
137 var status = $(result).find("command").attr("status");
138 var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
141 $(result).find("command > *").each(function(index, e) {
142 if ($(e).is("note")) {
143 Adhoc.addNote(elem, $(e).text(), $(e).attr("type"));
144 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
145 Adhoc.addForm(elem, e);
148 if (status == "executing") {
149 for (kind in kinds) {
150 input = $("<input type='button' disabled='disabled' value='" + kinds[kind] + "'/>").click(function() {
151 Adhoc.executeCommand(kind, (kind != 'prev') && Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
153 if ($(result).find('actions ' + kind).length > 0)
154 input.removeAttr("disabled");
155 $(elem).append(input);
158 $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
159 Adhoc.executeCommand("execute", Adhoc.serializeToDataform('form'), function(e) { Adhoc.displayResult(elem, e) });
162 $("<input type='button' value='Cancel'/>").click(function() {
163 Adhoc.cancelCommand(function(e) { Adhoc.displayResult(elem, e) });
166 input = $("<input type='button' value='Start over'/>").bind("click", function() {
168 Adhoc.status.sessionid = null;
169 Adhoc.status.cmdNode = null;
170 Adhoc.getCommandNodes(elem);
172 $(elem).append(input);
176 runCommand: function (item, callback) {
177 Adhoc.status.cmdNode = $(item).attr("id"); // Save node of executed command (in global var)
178 Adhoc.executeCommand("execute", false, function(result) {
179 Adhoc.status.sessionid = $(result).find("command").attr("sessionid");
184 executeCommand: function (type, childs, callback) {
185 if (Adhoc.status.sessionid)
186 var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
187 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, sessionid: Adhoc.status.sessionid, action: type });
189 var execIQ = $iq({ type: "set", to: Adhoc.status.queryJID, id: connection.getUniqueId() })
190 .c("command", { xmlns: Strophe.NS.ADHOC, node: Adhoc.status.cmdNode, action: type });
192 execIQ.cnode(childs);
193 connection.sendIQ(execIQ, callback);
196 cancelCommand: function (callback) {
197 Adhoc.executeCommand("cancel", false, callback);
198 Adhoc.status.cmdNode = null
199 Adhoc.status.sessionid = null;
202 getCommandNodes: function (elem) {
203 var nodesIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
204 connection.sendIQ(nodesIQ, function(result) {
205 var items = $("<ul></ul>");
206 $(elem).append(items);
207 $(result).find("item").each(function(index, e) {
208 $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
209 Adhoc.runCommand(this, function (result) { Adhoc.displayResult(elem, result); });
210 event.preventDefault();
216 checkFeatures: function (elem, jid) {
217 if (Adhoc.status.sessionid)
218 Adhoc.cancelCommand();
219 Adhoc.status.queryJID = jid;
220 var featureIQ = $iq({ type: "get", to: Adhoc.status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
222 connection.sendIQ(featureIQ,
223 function(result) { /* Callback */
224 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
225 Adhoc.getCommandNodes(elem);
227 $(elem).append("<p>" + Adhoc.status.queryJID + " does NOT support AdHoc commands</p>");
230 function(result) { /* Errback */
231 $(elem).append("<p>Couldn't get list of supported features</p>");