]> cgit.babelmonkeys.de Git - adhocweb.git/blob - js/main.js
Add licensing information
[adhocweb.git] / js / main.js
1 // adhocweb
2 // Copyright (c) 2010-2013 Florian Zeitz
3 //
4 // This project is MIT licensed.
5 // Please see the COPYING file for more information.
6
7 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
8 var show_log = true;
9
10 var localJID = null;
11 var connection   = null;
12
13 var commandCenter = null;
14
15 function log(msg) {
16     var entry = $('<div></div>').append(document.createTextNode(msg));
17     $('#log').append(entry);
18 }
19
20 function rawInput(data) {
21     log('RECV: ' + data);
22 }
23
24 function rawOutput(data) {
25     log('SENT: ' + data);
26 }
27
28 function getFeatures(jid) {
29     var cb, ecb;
30     cb = function(result) { /* Callback */
31         commandCenter.getCommandNodes(function(items) { $('#output').empty(); $('#output').append(items) });
32     }
33     ecb = function(result) { /* Errback */
34         $('#output').append("<p>" + jid + " does NOT support AdHoc commands</p>");
35     }
36     commandCenter.checkFeatures(jid, cb, ecb);
37 }
38
39 function onConnect(status) {
40     if (status == Strophe.Status.CONNECTING) {
41         log('Strophe is connecting.');
42     } else if (status == Strophe.Status.CONNFAIL) {
43         log('Strophe failed to connect.');
44         showConnect();
45     } else if (status == Strophe.Status.DISCONNECTING) {
46         log('Strophe is disconnecting.');
47     } else if (status == Strophe.Status.DISCONNECTED) {
48         log('Strophe is disconnected.');
49         showConnect();
50     } else if (status == Strophe.Status.AUTHFAIL) {
51         log('Authentication failed');
52         if (connection) {
53             connection.disconnect();
54         }
55     } else if (status == Strophe.Status.CONNECTED) {
56         log('Strophe is connected.');
57         $('#queryJID').val(connection.domain);
58         $('#query').show();
59         commandCenter = new Adhoc("#output", function() {
60                 $("<input type='button' value='Start over'/>").bind("click", function() {
61                         $('#output').empty();
62                         commandCenter.getCommandNodes(function(items) { $('#output').append(items) });
63                 }).appendTo('#output');
64         });
65         getFeatures(connection.domain);
66     }
67 }
68
69 function showConnect() {
70     var jid = $('#jid');
71     var pass = $('#pass');
72     var button = $('#connect').get(0);        
73
74     button.value = 'connect';
75     $('#query').hide();
76     pass.show();
77     jid.show();
78     $('#cred label').show();
79     $('#cred br').show();
80     $('#output').empty();
81 }
82
83 function showDisconnect() {
84     var jid = $('#jid');
85     var pass = $('#pass');
86     var button = $('#connect').get(0);        
87
88     button.value = 'disconnect';
89     pass.hide();
90     jid.hide();
91     $('#cred label').hide();
92     $('#cred br').hide();
93 }
94
95 $(document).ready(function () {
96     connection = new Strophe.Connection(BOSH_SERVICE);
97     if (show_log) {
98         $('#log_container').show();
99         connection.rawInput = rawInput;
100         connection.rawOutput = rawOutput;
101     }
102
103     $("#log_toggle").click(function () {
104         $("#log").toggle();
105       });
106
107     $('#cred').bind('submit', function (event) {
108         var button = $('#connect').get(0);
109         var jid = $('#jid');
110         var pass = $('#pass');        
111         localJID = jid.get(0).value;
112
113         if (button.value == 'connect') {
114             showDisconnect();
115             $('#log').empty();
116             connection.connect(localJID,
117                pass.get(0).value,
118                onConnect);
119         } else {
120             connection.disconnect();
121         }
122         event.preventDefault();
123     });
124
125     $('#queryForm').bind('submit', function (event) {
126         getFeatures($('#queryJID').val());
127         event.preventDefault();
128     });
129 });
130
131 window.onunload = window.onbeforeunload = function() {
132     if (connection) {
133         connection.disconnect();
134     }
135 }