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