comparison mod_admin_web/admin_web/www_files/js/main.js @ 288:9233d7ee3c09

mod_admin_web: Initial PoC commit
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 17 Dec 2010 03:34:53 +0100
parents
children a9e69088e678
comparison
equal deleted inserted replaced
287:6144fe6161f1 288:9233d7ee3c09
1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
2 var show_log = false;
3
4 Strophe.addNamespace('S2SPUBSUB', 'http://prosody.im/streams/s2s');
5 Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
6 Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps');
7
8 var localJID = null;
9 var connection = null;
10
11 var pubsubHost = null; /* TODO: Replace this inside Lua */
12
13 function log(msg) {
14 var entry = $('<div></div>').append(document.createTextNode(msg));
15 $('#log').append(entry);
16 }
17
18 function rawInput(data) {
19 log('RECV: ' + data);
20 }
21
22 function rawOutput(data) {
23 log('SENT: ' + data);
24 }
25
26 function _cbNewS2S(e) {
27 var items, retract, id, jid;
28 items = e.getElementsByTagName('item');
29 for (i = 0; i < items.length; i++) {
30 id = items[i].attributes['id'].value;
31 jid = items[i].getElementsByTagName('session')[0].attributes['jid'].value;
32 if (items[i].getElementsByTagName('out')[0]) {
33 $('#s2sout').append('<li id="' + id + '">' + jid + '</li>');
34 } else {
35 $('#s2sin').append('<li id="' + id + '">' + jid + '</li>');
36 }
37 }
38 retract = e.getElementsByTagName('retract')[0];
39 if (retract) {
40 id = retract.attributes['id'].value;
41 $('#' + id).remove();
42 }
43 return true;
44 }
45
46 function onConnect(status) {
47 if (status == Strophe.Status.CONNECTING) {
48 log('Strophe is connecting.');
49 } else if (status == Strophe.Status.CONNFAIL) {
50 log('Strophe failed to connect.');
51 showConnect();
52 } else if (status == Strophe.Status.DISCONNECTING) {
53 log('Strophe is disconnecting.');
54 } else if (status == Strophe.Status.DISCONNECTED) {
55 log('Strophe is disconnected.');
56 showConnect();
57 } else if (status == Strophe.Status.AUTHFAIL) {
58 log('Authentication failed');
59 if (connection) {
60 connection.disconnect();
61 }
62 } else if (status == Strophe.Status.CONNECTED) {
63 log('Strophe is connected.');
64 showDisconnect();
65 pubsubHost = 'pubsub.' + connection.domain;
66 connection.send($iq({to: pubsubHost, type: 'set', id: connection.getUniqueId()}).c('pubsub', {xmlns: Strophe.NS.PUBSUB})
67 .c('subscribe', {node: Strophe.NS.S2SPUBSUB, jid: connection.jid}));
68 connection.addHandler(_cbNewS2S, Strophe.NS.PUBSUB + '#event', 'message');
69 connection.sendIQ($iq({to: pubsubHost, type: 'get', id: connection.getUniqueId()}).c('pubsub', {xmlns: Strophe.NS.PUBSUB})
70 .c('items', {node: Strophe.NS.S2SPUBSUB}), _cbNewS2S);
71 }
72 }
73
74 function showConnect() {
75 var jid = $('#jid');
76 var pass = $('#pass');
77 var button = $('#connect').get(0);
78
79 button.value = 'connect';
80 pass.show();
81 jid.show();
82 $('.container').hide();
83 $('#cred label').show();
84 $('#cred br').show();
85 $('ul').empty();
86 }
87
88 function showDisconnect() {
89 var jid = $('#jid');
90 var pass = $('#pass');
91 var button = $('#connect').get(0);
92
93 button.value = 'disconnect';
94 pass.hide();
95 jid.hide();
96 $('.container').show();
97 $('#cred label').hide();
98 $('#cred br').hide();
99 }
100
101 $(document).ready(function () {
102 connection = new Strophe.Connection(BOSH_SERVICE);
103 if (show_log) {
104 $('#log_container').show();
105 connection.rawInput = rawInput;
106 connection.rawOutput = rawOutput;
107 }
108
109 $("#log_toggle").click(function () {
110 $("#log").toggle();
111 });
112
113 $('#cred').bind('submit', function (event) {
114 var button = $('#connect').get(0);
115 var jid = $('#jid');
116 var pass = $('#pass');
117 localJID = jid.get(0).value;
118
119 if (button.value == 'connect') {
120 $('#log').empty();
121 connection.connect(localJID,
122 pass.get(0).value,
123 onConnect);
124 } else {
125 connection.disconnect();
126 }
127 event.preventDefault();
128 });
129
130 });
131
132 window.onunload = window.onbeforeunload = function() {
133 if (connection) {
134 connection.disconnect();
135 }
136 }