]> cgit.babelmonkeys.de Git - adhocweb.git/blob - js/main.js
61ddaa41f427a51f832208a575c2b97a6a5c61ed
[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 displayResult(result) {
46     var status = $(result).find("command").attr("status");
47
48     $("#output *").remove();
49     $(result).find("note").each(function(index, e) {
50         var type = $(e).attr("type");
51         if (!type) {
52            type = "info";
53         }
54         $("#output").append("<p class='" + type + "Note'>" + $(e).text() + "</p>");
55     });
56     if (status == "executing") {
57         $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
58                             "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
59                             "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
60                             "<input type='button' id='executeButton' value='Execute'/>"+
61                             "<input type='button' id='cancelButton' value='Cancel'/>");
62         for (kind in ['prev', 'next', 'complete']) {
63             if ($(result).find('actions ' + kind).length > 0)
64                 $('#' + kind + 'Button').attr("disabled", "false");
65         }
66         $('#cancelButton').bind("click", function() {
67             var cancelIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
68                 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
69             cmdNode = null
70             sessionid = null;
71             connection.sendIQ(cancelIQ, displayResult);
72         });
73     } else {
74         input = $("<input type='button' value='Restart'/>").bind("click", function() {
75             $('#output *').remove();
76             sessionid = null;
77             cmdNode = null;
78             getCommandNodes();
79         });
80         $("#output").append(input);
81     }
82 }
83
84 function runCommand() {
85     cmdNode = $(this).attr("id"); // Save not of executed command (in global)
86     var execIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
87         .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
88     connection.sendIQ(execIQ, function(result) {
89         sessionid = $(result).find("command").attr("sessionid");
90         displayResult(result);
91     });
92 }
93
94 function getCommandNodes() {
95     var nodesIQ = $iq({ type: "get", to: "localhost", id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
96     connection.sendIQ(nodesIQ, function(result) {
97         $('#output').append("<ul id='items'></ul>");
98         $(result).find("item").each(function(index, e) {
99             item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
100             $("#items").append(item);
101         });
102     });
103 }
104
105 function checkFeatures() {
106     featureIQ = $iq({ type: "get", to: "localhost", id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
107     connection.sendIQ(featureIQ, function(result) {
108         if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
109             $('#output').append("<p>This entitiy does support AdHoc commands</p>");
110         } else {
111             $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
112         }
113     });
114     getCommandNodes();
115 }
116
117 function showConnect() {
118     var jid = $('#jid');
119     var pass = $('#pass');
120     var button = $('#connect').get(0);        
121
122     button.value = 'connect';
123     pass.show();
124     jid.show();
125     $('label').show();
126     $('#output *').remove();
127     return false;
128 }
129
130 function showDisconnect() {
131     var jid = $('#jid');
132     var pass = $('#pass');
133     var button = $('#connect').get(0);        
134
135     button.value = 'disconnect';
136     pass.hide();
137     jid.hide();
138     $('label').hide();
139     return false;
140 }
141
142 $(document).ready(function () {
143     connection = new Strophe.Connection(BOSH_SERVICE);
144     connection.rawInput = rawInput;
145     connection.rawOutput = rawOutput;
146
147     $("#log_toggle").click(function () {
148         $("#log").toggle();
149       });
150
151     $('#cred').bind('submit', function () {
152         var button = $('#connect').get(0);
153         var jid = $('#jid');
154         var pass = $('#pass');        
155         localJID = jid.get(0).value;
156
157         if (button.value == 'connect') {
158             showDisconnect();
159             $('#log *').remove();
160             connection.connect(localJID,
161                pass.get(0).value,
162                onConnect);
163         } else {
164             connection.disconnect();
165         }
166         return false;
167     });
168 });
169
170 onunload = function() {
171     if (connection) {
172         connection.disconnect();
173     }
174 }
175