]> cgit.babelmonkeys.de Git - adhocweb.git/blob - js/main.js
Add support for list-multi
[adhocweb.git] / js / main.js
1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
2 var show_log = true;
3
4 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
5
6 var localJID = null;
7 var connection   = null;
8 var adhoc_status = {
9     sessionid: null,
10     cmdNode: null,
11     queryJID: null
12 };
13
14 function log(msg) {
15     var entry = $('<div></div>').append(document.createTextNode(msg));
16     $('#log').append(entry);
17 }
18
19 function rawInput(data) {
20     log('RECV: ' + data);
21 }
22
23 function rawOutput(data) {
24     log('SENT: ' + data);
25 }
26
27 function onConnect(status) {
28     if (status == Strophe.Status.CONNECTING) {
29         log('Strophe is connecting.');
30     } else if (status == Strophe.Status.CONNFAIL) {
31         log('Strophe failed to connect.');
32         showConnect();
33     } else if (status == Strophe.Status.DISCONNECTING) {
34         log('Strophe is disconnecting.');
35     } else if (status == Strophe.Status.DISCONNECTED) {
36         log('Strophe is disconnected.');
37         showConnect();
38     } else if (status == Strophe.Status.AUTHFAIL) {
39         log('Authentication failed');
40         if (connection) {
41             connection.disconnect();
42         }
43     } else if (status == Strophe.Status.CONNECTED) {
44         log('Strophe is connected.');
45         adhoc_status.queryJID = connection.domain;
46         $('#queryJID').val(adhoc_status.queryJID);
47         $('#query').show();
48         checkFeatures();
49     }
50 }
51
52 function addNote(elem, text, type) {
53     if (!type) {
54        type = "info";
55     }
56     text = text.replace(/\n/g, "<br/>");
57     $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
58 }
59
60 function addForm(elem, x) {
61     var form = $("<form/>");
62     form.submit(function(event){event.preventDefault();});
63     var fieldset = $("<fieldset/>");
64     form.append(fieldset);
65     if ($(x).find("title").length > 0)
66         $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
67     if ($(x).find("instructions").length > 0)
68         $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
69     $(x).find("field").each(function() {
70         var item = null;
71         var type = $(this).attr("type");
72         if($(this).attr("label")) {
73             $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
74             $("<br/>").appendTo(fieldset);
75         }
76         switch(type) {
77         case "hidden":
78             item = $("<input type='hidden'/>");
79             break;
80         case "boolean":
81             item = $("<input type='checkbox'/>");
82             break;
83         case "text-multi":
84             item = $("<textarea rows='10' cols='70'/>");
85             break;
86         case "text-single":
87             item = $("<input type='text'/>");
88             break;
89         case "fixed":
90             item = $("<input type='text'/>").attr("readonly",true);
91             break;
92         case "jid-multi":
93             item = $("<textarea rows='10' cols='70'/>");
94             break;
95         case "jid-single":
96             item = $("<input type='text'/>");
97             break;
98         case "list-multi":
99             item = $("<select multiple='multiple'/>");
100             $(this).find("option").each(function() {
101                 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
102             });
103             break;
104         case "list-single":
105             item = $("<select/>");
106             $(this).find("option").each(function() {
107                 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
108             });
109             break;
110         case "text-private":
111             item = $("<input type='password'/>");
112             break;
113         default:
114             item = $("<input/>");
115         }
116         item.addClass("df-item");
117         if ($(this).find("value").length > 0) {
118             var value = null;
119             if ((type == "text-multi") || (type == "jid-multi")) {
120                 value = "";
121                 $(this).find("value").each(function() {
122                     value = value + $(this).text() + "\n";
123                 });
124                 item.val(value);
125             } else if (type == "list-multi") {
126                 $(this).children("value").each(function() {
127                     item.children('option[value="' + $(this).text() + '"]').each(function() {
128                         $(this).attr("selected", "selected");
129                     });
130                 });
131             } else {
132                 item.val($(this).find("value").text());
133             }
134         }
135         if ($(x).attr("type") == "result")
136             item.attr("readonly", true);
137         if ($(this).attr("var")) {
138             item.attr("name", $(this).attr("var"));
139             item.attr("id", $(this).attr("var"));
140         }
141         fieldset.append(item);
142         if (type != "hidden")
143             fieldset.append("<br/>");
144     });
145     $(elem).append(form);
146 }
147
148 function serializeToDataform(form, st) {
149     st.c("x", {"xmlns": "jabber:x:data", "type": "submit"});
150     $(form).find(".df-item").each(function(){
151         st.c("field", {"var": $(this).attr("name")});
152         if (this.nodeName.toLowerCase() == "select" && this.multiple) {
153             for (var i = 0; i < this.options.length; i++)
154                 if (this.options[i].selected)
155                     st.c("value").t(this.options[i].text).up();
156         } else if (this.nodeName.toLowerCase() == "textarea") {
157             var sp_value = this.value.split(/\r?\n|\r/g);
158             for(var i = 0; i < sp_value.length; i++)
159                 st.c("value").t(sp_value[i]).up();
160         } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
161             if (this.checked) {
162                 st.c("value").t("1");
163             } else {
164                 st.c("value").t("0");
165             }
166         } else {
167             // if this has value then
168             st.c("value").t($(this).val()).up();
169         }
170         st.up();
171     });
172     st.up();
173 }
174
175 function displayResult(result) {
176     var status = $(result).find("command").attr("status");
177
178     $("#output").empty();
179     $(result).find("command > *").each(function(index, e) {
180         if ($(e).is("note")) {
181             addNote("#output", $(e).text(), $(e).attr("type"));
182         } else if ($(e).is("x[xmlns=jabber:x:data]")) {
183             addForm("#output", e);
184         }
185     });
186     if (status == "executing") {
187         $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
188                             "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
189                             "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
190                             "<input type='button' id='executeButton' value='Execute'/>"+
191                             "<input type='button' id='cancelButton' value='Cancel'/>");
192         for (kind in ['prev', 'next', 'complete']) {
193             if ($(result).find('actions ' + kind).length > 0)
194                 $('#' + kind + 'Button').attr("disabled", "false");
195         }
196         $('#executeButton').bind("click", function() {
197             var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
198                 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "execute" });
199             serializeToDataform($('form'), execIQ);
200             connection.sendIQ(execIQ, displayResult);
201         });
202
203         $('#cancelButton').bind("click", function() {
204             cancelCommand(displayResult);
205         });
206
207         $('#queryForm').unbind('submit');
208         $('#queryForm').bind('submit', function (event) {
209             cancelCommand(function(result) {
210                 $('#queryForm').unbind('submit');
211                 $('#queryForm').bind('submit', function (event) {
212                     adhoc_status.queryJID = $('#queryJID').val();
213                     checkFeatures();
214                     event.preventDefault();
215                 });
216             });
217             adhoc_status.queryJID = $('#queryJID').val();
218             checkFeatures();
219             event.preventDefault();
220         });
221
222     } else {
223         input = $("<input type='button' value='Start over'/>").bind("click", function() {
224             $('#output').empty();
225             adhoc_status.sessionid = null;
226             adhoc_status.cmdNode = null;
227             getCommandNodes();
228         });
229         $("#output").append(input);
230     }
231 }
232
233 function runCommand(event) {
234     adhoc_status.cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
235     var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
236         .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, action: "execute" });
237     connection.sendIQ(execIQ, function(result) {
238         adhoc_status.sessionid = $(result).find("command").attr("sessionid");
239         displayResult(result);
240     });
241     event.preventDefault();
242 }
243
244 function cancelCommand(callback) {
245     var cancelIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
246         .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "cancel" });
247     adhoc_status.cmdNode = null
248     adhoc_status.sessionid = null;
249     connection.sendIQ(cancelIQ, callback);
250 }
251
252 function getCommandNodes() {
253     var nodesIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
254     connection.sendIQ(nodesIQ, function(result) {
255         $('#output').append("<ul id='items'></ul>");
256         $(result).find("item").each(function(index, e) {
257             link = $("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(runCommand)
258             item = $("<li></li>").append(link);
259             $("#items").append(item);
260         });
261     });
262 }
263
264 function checkFeatures() {
265     featureIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
266     $('#output').empty();
267     connection.sendIQ(featureIQ,
268         function(result) { /* Callback */
269                 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
270                     $('#output').append("<p>This entitiy does support AdHoc commands</p>");
271                     getCommandNodes();
272                 } else {
273                     $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
274                 }
275         },
276         function(result) { /* Errback */
277             $('#output').append("<p>Couldn't get list of supported features</p>");
278         }
279     );
280 }
281
282 function showConnect() {
283     var jid = $('#jid');
284     var pass = $('#pass');
285     var button = $('#connect').get(0);        
286
287     button.value = 'connect';
288     $('#query').hide();
289     pass.show();
290     jid.show();
291     $('#cred label').show();
292     $('#cred br').show();
293     $('#output').empty();
294 }
295
296 function showDisconnect() {
297     var jid = $('#jid');
298     var pass = $('#pass');
299     var button = $('#connect').get(0);        
300
301     button.value = 'disconnect';
302     pass.hide();
303     jid.hide();
304     $('#cred label').hide();
305     $('#cred br').hide();
306 }
307
308 $(document).ready(function () {
309     connection = new Strophe.Connection(BOSH_SERVICE);
310     if (show_log) {
311         $('#log_container').show();
312         connection.rawInput = rawInput;
313         connection.rawOutput = rawOutput;
314     }
315
316     $("#log_toggle").click(function () {
317         $("#log").toggle();
318       });
319
320     $('#cred').bind('submit', function (event) {
321         var button = $('#connect').get(0);
322         var jid = $('#jid');
323         var pass = $('#pass');        
324         localJID = jid.get(0).value;
325
326         if (button.value == 'connect') {
327             showDisconnect();
328             $('#log').empty();
329             connection.connect(localJID,
330                pass.get(0).value,
331                onConnect);
332         } else {
333             connection.disconnect();
334         }
335         event.preventDefault();
336     });
337
338     $('#queryForm').bind('submit', function (event) {
339         adhoc_status.queryJID = $('#queryJID').val();
340         checkFeatures();
341         event.preventDefault();
342     });
343 });
344
345 window.onunload = window.onbeforeunload = function() {
346     if (connection) {
347         connection.disconnect();
348     }
349 }