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