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