]> git.babelmonkeys.de Git - socialXMPP.git/blobdiff - scripts/basic.js
Small layout fixes
[socialXMPP.git] / scripts / basic.js
index cc4fde5a693a6ea63da3ec3760b687b0ce040eaa..e4a63cf114cbf309b0d4cccee1ce78beef3481ad 100644 (file)
@@ -4,6 +4,7 @@ var NS_PEP = 'http://jabber.org/protocol/pubsub#event';
 var NS_TUNE = 'http://jabber.org/protocol/tune';
 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
 
+var localJID = null;
 var connection   = null;
 var show_log     = true;
 
@@ -13,7 +14,7 @@ var appName = 'socialXMPP';
 var roster = new Array();
 
 function log(msg) {
-    var entry = $('<div></div>').append(Strophe.xmlTextNode(msg));
+    var entry = $('<div></div>').append(document.createTextNode(msg));
     $('#log').append(entry);
 }
 
@@ -47,6 +48,23 @@ function populateVCard(e, jid) {
        $("#"+jid2id(jid)+" img").attr('src', 'data:'+mime+';base64,'+binval);
        $("#PHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
     }
+    $(e).find('TEL:has(HOME)').each(function() {
+        $('#TELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('TEL:has(WORK)').each(function() {
+        $('#TELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('EMAIL:has(HOME)').each(function() {
+        $('#EMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
+    $(e).find('EMAIL:has(WORK)').each(function() {
+        $('#EMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
+
+    $('#vCard').click(function () {
+        $('#vCard_container').slideUp("normal");
+      });
+
     $('#vCard_container').slideDown("normal");
 }
 
@@ -65,6 +83,11 @@ function _cbVCard(e) {
 }
 
 function _cbOwnVCard(e) {
+    $('#ownInfo').empty();
+    $('#ownInfo').load('vCard.html #vCard', function() {
+    $('#ownInfo').find('div').each(function() {
+       $(this).attr('id', 'own' + $(this).attr('id'));
+    });
     var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID');
     for (var i=0; i<easy_cases.length; i++) {
        var text = e.getElementsByTagName(easy_cases[i])[0];
@@ -80,13 +103,26 @@ function _cbOwnVCard(e) {
 
        $("#ownPHOTO img").attr('src', 'data:'+mime+';base64,'+binval);
     }
+    $(e).find('TEL:has(HOME)').each(function() {
+        $('#ownTELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('TEL:has(WORK)').each(function() {
+        $('#ownTELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text()));
+    });
+    $(e).find('EMAIL:has(HOME)').each(function() {
+        $('#ownEMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
+    $(e).find('EMAIL:has(WORK)').each(function() {
+        $('#ownEMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text()));
+    });
     $('#ownInfo').show();
+    });
 
     return false;
 }
 
 function getVCard(jid) {
-    var id = 'getvCard'+jid2id(jid);
+    var id = connection.getUniqueId('vCardGet');
     if (roster[jid2id(jid)].vCard == "") {
        var vCardiq = $iq({'to':jid,
                        'id':id,
@@ -97,11 +133,10 @@ function getVCard(jid) {
     } else {
        _cbVCard(roster[jid2id(jid)].vCard);
     }
-
 }
 
 function getOwnInfo() {
-    var id = 'getvCard'+jid2id(connection.jid);
+    var id = connection.getUniqueId('vCardGet');
     var vCardiq = $iq({'to': Strophe.getBareJidFromJid(connection.jid),
                'id': id,
                'type': 'get'}
@@ -109,13 +144,20 @@ function getOwnInfo() {
     connection.addHandler(_cbOwnVCard, null, 'iq', 'result', id);
     connection.send(vCardiq.tree());
 }
-
 function addFriend(jid, nick) {
     roster[jid2id(jid)] = new Buddy(nick, jid);
-    $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><span class="nick">'+nick+'</span></div>');
-    $('#'+jid2id(jid)).click(function() {
+    $('#friends').append('<div class="friend" id="'+jid2id(jid)+'"><img src="imgs/none.png" /><br /><a class="nick">'+nick+'</a></div>');
+    $('#' + jid2id(jid) + ' img').click(function() {
        getVCard(jid);
     });
+    $('#' + jid2id(jid) + ' a').click(function() {
+       var id = jid2id(jid) + 'Chat';
+       if ($('#' + id).length <= 0) {
+           createBubble(jid);
+       }
+       $('#' + id).show();
+    });
+
 }
 
 function _cbRoster(e) {
@@ -129,14 +171,16 @@ function _cbRoster(e) {
        addFriend(entries[item].getAttribute('jid'), nick);
     }
     connection.addHandler(_cbPEP, NS_PEP, 'message');
-    var initialPresence = $pres().c('c', {xmlns: NS_CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
+    var initialPresence = $pres().c('show').t('online').up().c('status').t('Hy, I am an socialXMPP instance').up().c('priority').t('1').up().c('c', {xmlns: NS_CAPS, hash: 'sha-1', node: 'http://jabber.babelmonkeys.de', ver: genCaps()}).up();
     connection.send(initialPresence.tree());
 
+    $('#friends').show();
+
     return false;
 }
 
 function getRoster() {
-    var id = 'getRoster';
+    var id = connection.getUniqueId('roster');
 
     var rosteriq = $iq({'id':id,
                        'type':'get'}
@@ -161,6 +205,57 @@ function _cbDisco(e) {
     return true;
 }
 
+function sendMessage(form, to) {
+    if (form.text.value) {
+       var id = jid2id(to) + 'Chat';
+       var message = $msg({'type': 'chat', 'to': to}).c('body').t(form.text.value);
+       connection.send(message.tree());
+       if ($('#' + id + ' p *').length > 0) {
+           $('#' + id + ' p').append('<br/>');
+       }
+       $('#' + id + ' p').append('<span class="receiver">' + localJID + ': </span>');
+       $('#' + id + ' p').append(form.text.value);
+       form.text.value = '';
+    }
+
+    return false;
+}
+
+function createBubble(jid) {
+    var id = jid2id(jid) + 'Chat';
+    $('body').append('<div class="chat" id="' + id + '" onmousedown="startDrag(this)"><a href="#" onclick="$(' + "'#" + id + "'" + ').hide()">Close</a>' +
+                   '<span class ="chatTitle"> ' + jid + '</span><p></p></div>');
+    $('#' + id).append('<form id="' + id + 'Form" class="chatForm" onsubmit="return sendMessage(this, \'' + jid + '\');" action="#"><input type="text" name="text" id="' + id + 'Text" class="chatForm" /></form>')
+    $('#' + id).css( 'top', $('#' + jid2id(jid)).position().top + 40);
+    $('#' + id).css( 'left', $('#' + jid2id(jid)).position().left + 40);
+}
+
+
+function _cbMessage(msg) {
+    if ($(msg).attr('type') != 'chat')
+       return true;
+    var jid = $(msg).attr('from');
+    var id = jid2id(jid) + 'Chat';
+    var body = $(msg).find('body:first').text();
+    body = escape(body);
+    body = body.replace(/%0A/g, '<br/>');
+    body = body.replace(/%3C/g, '&lt;');
+    body = body.replace(/%3E/g, '&gt;');
+    body = body.replace(/%26/g, '&amp;');
+    body = unescape(body);
+    if ($('#' + id).length <= 0) {
+       createBubble(jid);
+    }
+    if ($('#' + id + ' p *').length > 0) {
+       $('#' + id + ' p').append('<br/>');
+    }
+    $('#' + id + ' p').append('<span class="sender">' + jid + ': </span>');
+    $('#' + id + ' p').append(body);
+    $('#' + id).show();
+
+    return true;
+}
+
 function genCaps() {
     var S = '';
     S += 'client/web//' + appName + '<'
@@ -207,7 +302,6 @@ function _cbPEP(e) {
 function onConnect(status) {
     if (status == Strophe.Status.CONNECTING) {
        log('Strophe is connecting.');
-
     } else if (status == Strophe.Status.CONNFAIL) {
        log('Strophe failed to connect.');
        showConnect();
@@ -216,12 +310,17 @@ function onConnect(status) {
     } else if (status == Strophe.Status.DISCONNECTED) {
        log('Strophe is disconnected.');
        showConnect();
-
+    } else if (status == Strophe.Status.AUTHFAIL) {
+       log('Authentication failed');
+       if (connection) {
+           connection.disconnect();
+       }
     } else if (status == Strophe.Status.CONNECTED) {
        log('Strophe is connected.');
        getOwnInfo();
        getRoster();
        connection.addHandler(_cbDisco, Strophe.NS.DISCO_INFO, 'iq', 'get');
+       connection.addHandler(_cbMessage, Strophe.NS.CLIENT, 'message');
     }
 }
 
@@ -231,9 +330,10 @@ function showConnect() {
     var button = $('#connect').get(0); 
 
     $('#log').empty();
-    $('#ownInfo').empty();
+    $('#ownInfo').hide();
     $('#vCard_container').empty();
-    $('#friends').empty();
+    $('#friends').hide();
+    $('.chat').remove();
     $('label').show();
     jid.show();
     pass.show();
@@ -261,25 +361,58 @@ $(document).ready(function () {
     $("#log_toggle").click(function () {
        $("#log").toggle();     
       });
-      
-    $('#vCard_container').click(function () {
-        $('#vCard_container').slideUp("normal");
-      });
 
     $('#cred').bind('submit', function () {
        var button = $('#connect').get(0);
        var jid = $('#jid');
        var pass = $('#pass');  
+       localJID = jid.get(0).value;
        
        if (button.value == 'connect') {
            showDisconnect();
-           connection.connect(jid.get(0).value,
+           connection.connect(localJID,
                               pass.get(0).value,
                               onConnect);
        } else {
            connection.disconnect();
-           showConnect();
        }
        return false;
     });
 });
+
+// Element moving
+var dragElement = null;
+var mouseX = 0;
+var mouseY = 0;
+var offX = 0;
+var offY = 0;
+
+function startDrag(element) {
+    dragElement = element;
+    offX = mouseX - dragElement.offsetLeft;
+    offY = mouseY - dragElement.offsetTop;
+}
+
+function doDrag(eve) {
+    mouseX = eve.pageX;
+    mouseY = eve.pageY;
+
+    if (dragElement) {
+       dragElement.style.left = (mouseX - offX) + 'px';
+       dragElement.style.top = (mouseY - offY) + 'px';
+    }
+}
+
+function stopDrag(eve) {
+    dragElement = null;
+}
+
+onunload = function() {
+    if (connection) {
+       connection.disconnect();
+    }
+}
+
+onmousemove = doDrag;
+onmouseup = stopDrag;
+