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