]> cgit.babelmonkeys.de Git - adhocweb.git/blob - js/main.js
2cf542ec2cadb8c251f54a8fa790a6c945b68b7b
[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
8 function log(msg) {
9     var entry = $('<div></div>').append(document.createTextNode(msg));
10     $('#log').append(entry);
11 }
12
13 function rawInput(data) {
14     log('RECV: ' + data);
15 }
16
17 function rawOutput(data) {
18     log('SENT: ' + data);
19 }
20
21 function onConnect(status) {
22     if (status == Strophe.Status.CONNECTING) {
23         log('Strophe is connecting.');
24     } else if (status == Strophe.Status.CONNFAIL) {
25         log('Strophe failed to connect.');
26         showConnect();
27     } else if (status == Strophe.Status.DISCONNECTING) {
28         log('Strophe is disconnecting.');
29     } else if (status == Strophe.Status.DISCONNECTED) {
30         log('Strophe is disconnected.');
31         showConnect();
32     } else if (status == Strophe.Status.AUTHFAIL) {
33         log('Authentication failed');
34         if (connection) {
35             connection.disconnect();
36         }
37     } else if (status == Strophe.Status.CONNECTED) {
38         log('Strophe is connected.');
39         checkFeatures();
40     }
41 }
42
43 function displayResult(result) {
44     var status = $(result).find("command").attr("status");
45
46     $("#output *").remove();
47     $(result).find("note").each(function(index, e) {
48         type = $(e).attr("type");
49         if (!type) {
50            type = "info";
51         }
52         $("#output").append("<p class='" + type + "Note'>" + $(e).text() + "</p>");
53     });
54     if (status == "executing") {
55         $("#output").append("<input type='button' disabled='true' id='prevButton' value='prev'/>"+
56                             "<input type='button' disabled='true' id='nextButton' value='next'/>"+
57                             "<input type='button' disabled='true' id='completeButton' value='complete'/>"+
58                             "<input type='button' id='executeButton' value='execute'/>");
59         for (kind in ['prev', 'next', 'complete']) {
60             if ($(result).find('actions ' + kind).length > 0)
61                 $('#' + kind + 'Button').attr("disabled", "false");
62         }
63     } else {
64         input = $("<input type='button' value='Restart'/>").bind("click", function() {
65             $('#output *').remove();
66             getCommandNodes();
67         });
68         $("#output").append(input);
69     }
70 }
71
72 function runCommand() {
73     execIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
74         .c("command", { xmlns: Strophe.NS.ADHOC, node: $(this).attr("id"),
75             action: "execute" });
76     connection.sendIQ(execIQ, displayResult);
77 }
78
79 function getCommandNodes() {
80     nodesIQ = $iq({ type: "get", to: "localhost", id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
81     connection.sendIQ(nodesIQ, function(result) {
82         $('#output').append("<ul id='items'></ul>");
83         $(result).find("item").each(function(index, e) {
84             item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
85             $("#items").append(item);
86         });
87     });
88 }
89
90 function checkFeatures() {
91     featureIQ = $iq({ type: "get", to: "localhost", id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
92     connection.sendIQ(featureIQ, function(result) {
93         if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
94             $('#output').append("<p>This entitiy does support AdHoc commands</p>");
95         } else {
96             $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
97         }
98     });
99     getCommandNodes();
100 }
101
102 function showConnect() {
103     var jid = $('#jid');
104     var pass = $('#pass');
105     var button = $('#connect').get(0);        
106
107     button.value = 'connect';
108     pass.show();
109     jid.show();
110     $('label').show();
111     $('#output *').remove();
112     return false;
113 }
114
115 function showDisconnect() {
116     var jid = $('#jid');
117     var pass = $('#pass');
118     var button = $('#connect').get(0);        
119
120     button.value = 'disconnect';
121     pass.hide();
122     jid.hide();
123     $('label').hide();
124     return false;
125 }
126
127 $(document).ready(function () {
128     connection = new Strophe.Connection(BOSH_SERVICE);
129     connection.rawInput = rawInput;
130     connection.rawOutput = rawOutput;
131
132     $("#log_toggle").click(function () {
133         $("#log").toggle();
134       });
135
136     $('#cred').bind('submit', function () {
137         var button = $('#connect').get(0);
138         var jid = $('#jid');
139         var pass = $('#pass');        
140         localJID = jid.get(0).value;
141
142         if (button.value == 'connect') {
143             showDisconnect();
144             $('#log *').remove();
145             connection.connect(localJID,
146                pass.get(0).value,
147                onConnect);
148         } else {
149             connection.disconnect();
150         }
151         return false;
152     });
153 });
154
155 onunload = function() {
156     if (connection) {
157         connection.disconnect();
158     }
159 }
160