]> cgit.babelmonkeys.de Git - adhocweb.git/blob - js/main.js
b634c9a16d4d6fb56f4e1da9c0f26e6bb3630f4d
[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                 value = new Array();
127                 $(this).find("value").each(function() {
128                     value[value.length] = $(this).text();
129                 });
130             } else {
131                 item.val($(this).find("value").text());
132             }
133         }
134         if ($(x).attr("type") == "result")
135             item.attr("readonly", true);
136         if ($(this).attr("var")) {
137             item.attr("name", $(this).attr("var"));
138             item.attr("id", $(this).attr("var"));
139         }
140         fieldset.append(item);
141         if (type != "hidden")
142             fieldset.append("<br/>");
143     });
144     $(elem).append(form);
145 }
146
147 function serializeToDataform(form, st) {
148     st.c("x", {"xmlns": "jabber:x:data", "type": "submit"});
149     $(form).find(".df-item").each(function(){
150         st.c("field", {"var": $(this).attr("name")});
151         if (this.nodeName.toLowerCase() == "select" && this.multiple) {
152             for (var i = 0; i < this.options.length; i++)
153                 if (options[i].selected)
154                     st.c("value").t(options[i]).up();
155         } else if (this.nodeName.toLowerCase() == "textarea") {
156             var sp_value = this.value.split(/\r?\n|\r/g);
157             for(var i = 0; i < sp_value.length; i++)
158                 st.c("value").t(sp_value[i]).up();
159         } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
160             if (this.checked) {
161                 st.c("value").t("1");
162             } else {
163                 st.c("value").t("0");
164             }
165         } else {
166             // if this has value then
167             st.c("value").t($(this).val()).up();
168         }
169         st.up();
170     });
171     st.up();
172 }
173
174 function displayResult(result) {
175     var status = $(result).find("command").attr("status");
176
177     $("#output").empty();
178     $(result).find("command > *").each(function(index, e) {
179         if ($(e).is("note")) {
180             addNote("#output", $(e).text(), $(e).attr("type"));
181         } else if ($(e).is("x[xmlns=jabber:x:data]")) {
182             addForm("#output", e);
183         }
184     });
185     if (status == "executing") {
186         $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
187                             "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
188                             "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
189                             "<input type='button' id='executeButton' value='Execute'/>"+
190                             "<input type='button' id='cancelButton' value='Cancel'/>");
191         for (kind in ['prev', 'next', 'complete']) {
192             if ($(result).find('actions ' + kind).length > 0)
193                 $('#' + kind + 'Button').attr("disabled", "false");
194         }
195         $('#executeButton').bind("click", function() {
196             var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
197                 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "execute" });
198             serializeToDataform($('form'), execIQ);
199             connection.sendIQ(execIQ, displayResult);
200         });
201
202         $('#cancelButton').bind("click", function() {
203             cancelCommand(displayResult);
204         });
205
206         $('#queryForm').unbind('submit');
207         $('#queryForm').bind('submit', function (event) {
208             cancelCommand(function(result) {
209                 $('#queryForm').unbind('submit');
210                 $('#queryForm').bind('submit', function (event) {
211                     adhoc_status.queryJID = $('#queryJID').val();
212                     checkFeatures();
213                     event.preventDefault();
214                 });
215             });
216             adhoc_status.queryJID = $('#queryJID').val();
217             checkFeatures();
218             event.preventDefault();
219         });
220
221     } else {
222         input = $("<input type='button' value='Start over'/>").bind("click", function() {
223             $('#output').empty();
224             adhoc_status.sessionid = null;
225             adhoc_status.cmdNode = null;
226             getCommandNodes();
227         });
228         $("#output").append(input);
229     }
230 }
231
232 function runCommand(event) {
233     adhoc_status.cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
234     var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
235         .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, action: "execute" });
236     connection.sendIQ(execIQ, function(result) {
237         adhoc_status.sessionid = $(result).find("command").attr("sessionid");
238         displayResult(result);
239     });
240     event.preventDefault();
241 }
242
243 function cancelCommand(callback) {
244     var cancelIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
245         .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "cancel" });
246     adhoc_status.cmdNode = null
247     adhoc_status.sessionid = null;
248     connection.sendIQ(cancelIQ, callback);
249 }
250
251 function getCommandNodes() {
252     var nodesIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
253     connection.sendIQ(nodesIQ, function(result) {
254         $('#output').append("<ul id='items'></ul>");
255         $(result).find("item").each(function(index, e) {
256             link = $("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(runCommand)
257             item = $("<li></li>").append(link);
258             $("#items").append(item);
259         });
260     });
261 }
262
263 function checkFeatures() {
264     featureIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
265     $('#output').empty();
266     connection.sendIQ(featureIQ,
267         function(result) { /* Callback */
268                 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
269                     $('#output').append("<p>This entitiy does support AdHoc commands</p>");
270                     getCommandNodes();
271                 } else {
272                     $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
273                 }
274         },
275         function(result) { /* Errback */
276             $('#output').append("<p>Couldn't get list of supported features</p>");
277         }
278     );
279 }
280
281 function showConnect() {
282     var jid = $('#jid');
283     var pass = $('#pass');
284     var button = $('#connect').get(0);        
285
286     button.value = 'connect';
287     $('#query').hide();
288     pass.show();
289     jid.show();
290     $('#cred label').show();
291     $('#cred br').show();
292     $('#output').empty();
293 }
294
295 function showDisconnect() {
296     var jid = $('#jid');
297     var pass = $('#pass');
298     var button = $('#connect').get(0);        
299
300     button.value = 'disconnect';
301     pass.hide();
302     jid.hide();
303     $('#cred label').hide();
304     $('#cred br').hide();
305 }
306
307 $(document).ready(function () {
308     connection = new Strophe.Connection(BOSH_SERVICE);
309     if (show_log) {
310         $('#log_container').show();
311         connection.rawInput = rawInput;
312         connection.rawOutput = rawOutput;
313     }
314
315     $("#log_toggle").click(function () {
316         $("#log").toggle();
317       });
318
319     $('#cred').bind('submit', function (event) {
320         var button = $('#connect').get(0);
321         var jid = $('#jid');
322         var pass = $('#pass');        
323         localJID = jid.get(0).value;
324
325         if (button.value == 'connect') {
326             showDisconnect();
327             $('#log').empty();
328             connection.connect(localJID,
329                pass.get(0).value,
330                onConnect);
331         } else {
332             connection.disconnect();
333         }
334         event.preventDefault();
335     });
336
337     $('#queryForm').bind('submit', function (event) {
338         adhoc_status.queryJID = $('#queryJID').val();
339         checkFeatures();
340         event.preventDefault();
341     });
342 });
343
344 window.onunload = window.onbeforeunload = function() {
345     if (connection) {
346         connection.disconnect();
347     }
348 }