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