aboutsummaryrefslogtreecommitdiffstats
path: root/webclient/lib/basic.js
diff options
context:
space:
mode:
Diffstat (limited to 'webclient/lib/basic.js')
-rw-r--r--webclient/lib/basic.js347
1 files changed, 194 insertions, 153 deletions
diff --git a/webclient/lib/basic.js b/webclient/lib/basic.js
index eb65ecf..604e37c 100644
--- a/webclient/lib/basic.js
+++ b/webclient/lib/basic.js
@@ -1,181 +1,222 @@
var BOSH_SERVICE = 'http://alias.fr.nf/http-bind';
var server_component = 'object.alias.fr.nf';
-var connection = null;
-jQuery.expr[':'].Contains = function(a,i,m){
- return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
-};
-
-function log(msg, color)
-{
- $('#log').append($('<div></div>').css('background-color', color).text(msg));
-}
-
-function rawInput(data)
-{
- log('RECV: ' + data, '#FBB6B4');
-}
-
-function rawOutput(data)
-{
- log('SENT: ' + data, '#B5BBFB');
-}
-
-function getRoster()
-{
- var roster = $iq({type : 'get'}).c('query', {xmlns : Strophe.NS.ROSTER});
- connection.sendIQ(roster, onRoster);
-}
+/**
+ * Alias namespace
+ */
+var Alias = {
+
+ /**
+ * Status constants
+ */
+ Status: {
+ ONLINE: 2,
+ AWAY: 1,
+ OFFLINE: 0
+ },
+
+ /**
+ * Strophe xmpp connection
+ */
+ connection: null,
+
+ /**
+ * Send a roster request to server
+ */
+ getRoster: function() {
+ var roster = $iq({type : 'get'}).c('query', {xmlns : Strophe.NS.ROSTER});
+ Alias.connection.sendIQ(roster, Alias.onRoster);
+ },
-function getPresence(contact)
-{
- if ( contact.hasClass("online") )
- return 2;
- else if ( contact.hasClass("away") )
- return 1;
- else
- return 0;
-}
+ /**
+ * Return the status of a contact
+ * @param contact a jquery object
+ * @returns {Number} the status number
+ * @see Alias.Status
+ */
+ getPresence: function(contact) {
+ if ( contact.hasClass("online") )
+ return Alias.Status["ONLINE"];
+ else if ( contact.hasClass("away") )
+ return Alias.Status["AWAY"];
+ else
+ return Alias.Status["OFFLINE"];
+ },
+
+ /**
+ * Convert a jid to an id string suitable for css id
+ * @param {String} jid
+ * @returns {String} id
+ */
+ jid_to_id: function(jid) {
+ return Strophe.getBareJidFromJid(jid).replace(/[@.]/g,'-');
+ },
-function jid_to_id(jid) {
- return Strophe.getBareJidFromJid(jid).replace(/[@.]/g,'-');
-}
+ /**
+ * Return the id of a contact
+ * @param contact jquery object
+ * @returns {String} id
+ */
+ getID: function(contact) {
+ return contact.find('.roster-jid').text();
+ },
+
+ /**
+ * Get the home node of a contact
+ * @param contact jquery object
+ */
+ getHome: function(contact) {
+ var name = Base64.encode(Alias.getID(contact)) + '@' + server_component;
+ var iq = $iq({type : 'get', to : name}).c('query', {xmlns : 'alias:query', type:'content'});
+ Alias.connection.sendIQ(iq, Alias.onHome);
+ },
-function getJID(contact)
-{
- return contact.find('.roster-jid').text();
-}
+ /**
+ * Called when receiving home node, display in the main div
+ * @param iq XML object
+ */
+ onHome: function(iq) {
+ var content = $(iq).find('content').text();
+ $('#main').html(content);
+ },
+
+ /**
+ * Insert a contact keeping the sorting of the roster
+ * The contacts are sorted based on their status and on their names
+ * @param contact jQuery object
+ */
+ insertContact: function(contact) {
+ var presence = Alias.getPresence(contact);
+ var jid = Alias.getID(contact);
+ var contacts = $('#roster li');
+ if (contacts.length > 0) {
+ var inserted = false;
+ contacts.each(function () {
+ var locpres = Alias.getPresence($(this));
+ var locjid = Alias.getID($(this));
+ if (presence > locpres)
+ {
+ $(this).before(contact);
+ inserted = true;
+ return false;
+ }
+ else if ( (presence == locpres) && (jid < locjid) )
+ {
+ $(this).before(contact);
+ inserted = true;
+ return false;
+ }
+ });
-function getHome(contact)
-{
- var name = Base64.encode(getJID(contact)) + '@' + server_component;
- var iq = $iq({type : 'get', to : name}).c('query', {xmlns : 'alias:query', type:'content'});
- connection.sendIQ(iq, onHome);
-}
-
-function onHome(iq)
-{
- var content = $(iq).find('content').text();
- $('#main').html(content);
-}
+ if (!inserted)
+ $('#roster ul').append(contact);
+ }
+ else
+ $('#roster ul').append(contact);
+ contact.click(function(){Alias.getHome($(this));});
+ },
-function insertContact(contact)
-{
- var presence = getPresence(contact);
- var jid = getJID(contact);
- var contacts = $('#roster li');
- if (contacts.length > 0) {
- var inserted = false;
- contacts.each(function () {
- var locpres = getPresence($(this));
- var locjid = getJID($(this));
- if (presence > locpres)
+ onPresence: function(presence) {
+ var who = $(presence).attr('from');
+ var type = $(presence).attr('type');
+ if (type !== 'error')
+ {
+ var contact = $('#' + Alias.jid_to_id(who));
+ contact.removeClass('online away offline');
+ if (type === 'unavailable')
{
- $(this).before(contact);
- inserted = true;
- return false;
+ contact.addClass('offline');
}
- else if ( (presence == locpres) && (jid < locjid) )
+ else
{
- $(this).before(contact);
- inserted = true;
- return false;
+ var show = $(presence).find('show').text();
+ if (show === '' || show === '')
+ {
+ contact.addClass('online');
+ }
+ else
+ {
+ contact.addClass('away');
+ }
}
- });
+ contact.remove();
+ Alias.insertContact(contact);
+ }
+ return true;
+ },
- if (!inserted)
- $('#roster ul').append(contact);
- }
- else
- $('#roster ul').append(contact);
- contact.click(function(){getHome($(this));});
-}
+ onRoster: function(iq) {
+ $('#roster li').remove();
+ var elems = iq.getElementsByTagName('query');
+ var query = elems[0];
+ Strophe.forEachChild(query, 'item', function(item)
+ {
+ var jid = item.getAttribute('jid');
+ var name = item.getAttribute('name') || jid;
+ var id = Alias.jid_to_id(jid);
+ var contact = $("<li id='" + id + "' class='roster-contact offline'>"
+ + "<div class='roster-name'>" + name + "</div>"
+ + "<div class='roster-jid'>" + jid + "</div></li>");
+ Alias.insertContact(contact);
+ });
+ Alias.connection.addHandler(Alias.onPresence,null,'presence', null, null, null, null);
+ Alias.connection.send($pres());
+ return true;
+ },
-function onPresence(presence)
-{
- var who = $(presence).attr('from');
- var type = $(presence).attr('type');
- if (type !== 'error')
- {
- var contact = $('#'+jid_to_id(who));
- contact.removeClass('online away offline');
- if (type === 'unavailable')
+ onConnect: function(status) {
+ var jid = $('#jid').get(0).value;
+ if ( status == Strophe.Status.CONNECTING )
{
- contact.addClass('offline');
- }
- else
+ log('Strophe is connecting.');
+ } else if ( status == Strophe.Status.CONNFAIL )
{
- var show = $(presence).find('show').text();
- if (show === '' || show === '')
- {
- contact.addClass('online');
- }
- else
- {
- contact.addClass('away');
- }
+ log('Strophe failed to connect.');
+ } else if ( status == Strophe.Status.DISCONNECTING )
+ {
+ log('Strophe is disconnecting.');
+ } else if ( status == Strophe.Status.DISCONNECTED )
+ {
+ log('Strophe is disconnected.');
+ } else if ( status == Strophe.Status.CONNECTED )
+ {
+ log('Strophe is connected.');
+ Alias.getRoster();
+ $('#password').val('');
+ $('#login').dialog('close');
+ $('#status').append($('<a href="#" id="connect">Disconnect</a>'));
+ $('#connect').click(function(){
+ Alias.connection.disconnect();
+ $('#roster ul').empty();
+ $('#login').dialog('open');
+ $(this).remove();
+ });
}
- contact.remove();
- insertContact(contact);
}
- return true;
+
+};
+
+function log(msg, color) {
+ $('#log').append($('<div></div>').css('background-color', color).text(msg));
}
-function onRoster(iq)
-{
- $('#roster li').remove();
- var elems = iq.getElementsByTagName('query');
- var query = elems[0];
- Strophe.forEachChild(query, 'item', function(item)
- {
- var jid = item.getAttribute('jid');
- var name = item.getAttribute('name') || jid;
- var id = jid_to_id(jid);
- var contact = $("<li id='" + id + "' class='roster-contact offline'>"
- + "<div class='roster-name'>" + name + "</div>"
- + "<div class='roster-jid'>" + jid + "</div></li>");
- insertContact(contact);
- });
- connection.addHandler(onPresence,null,'presence', null, null, null, null);
- connection.send($pres());
- return true;
+function rawInput(data) {
+ log('RECV: ' + data, '#FBB6B4');
}
-function onConnect(status)
-{
- var jid = $('#jid').get(0).value;
- if ( status == Strophe.Status.CONNECTING )
- {
- log('Strophe is connecting.');
- } else if ( status == Strophe.Status.CONNFAIL )
- {
- log('Strophe failed to connect.');
- } else if ( status == Strophe.Status.DISCONNECTING )
- {
- log('Strophe is disconnecting.');
- } else if ( status == Strophe.Status.DISCONNECTED )
- {
- log('Strophe is disconnected.');
- } else if ( status == Strophe.Status.CONNECTED )
- {
- log('Strophe is connected.');
- getRoster();
- $('#password').val('');
- $('#login').dialog('close');
- $('#status').append($('<a href="#" id="connect">Disconnect</a>'));
- $('#connect').click(function(){
- connection.disconnect();
- $('#roster ul').empty();
- $('#login').dialog('open');
- $(this).remove();
- });
- }
+function rawOutput(data) {
+ log('SENT: ' + data, '#B5BBFB');
}
+jQuery.expr[':'].Contains = function(a,i,m){
+ return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
+};
+
$(document).ready(function(){
- connection = new Strophe.Connection(BOSH_SERVICE);
+ var connection = new Strophe.Connection(BOSH_SERVICE);
connection.rawInput = rawInput;
connection.rawOutput = rawOutput;
+ Alias.connection = connection;
$('#login').dialog({
autoOpen: true,
@@ -183,9 +224,9 @@ $(document).ready(function(){
title: 'Connect',
buttons: {
'Connect' : function () {
- connection.connect($('#jid').get(0).value,
+ Alias.connection.connect($('#jid').get(0).value,
$('#pass').get(0).value,
- onConnect);}
+ Alias.onConnect);}
}
});