X-Git-Url: http://git.babelmonkeys.de/?a=blobdiff_plain;f=scripts%2Fbasic.js;h=7b57e5262f69df21cfafec1c64b13f172e62783f;hb=17615ff384777c0fd065dedd248af983db8f9fca;hp=a4b4a186717282899f116943ca279dd01739a530;hpb=9c8e638b6a3ff798e86cdfc576336ad4faced97c;p=socialXMPP.git diff --git a/scripts/basic.js b/scripts/basic.js index a4b4a18..7b57e52 100644 --- a/scripts/basic.js +++ b/scripts/basic.js @@ -1,9 +1,12 @@ +var DEBUG = false; +var BOSH_SERVICE = '/http-bind/'; + var NS_VCARD = 'vcard-temp'; var NS_CAPS= 'http://jabber.org/protocol/caps'; 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; @@ -48,18 +51,38 @@ function populateVCard(e, jid) { $("#PHOTO img").attr('src', 'data:'+mime+';base64,'+binval); } $(e).find('TEL:has(HOME)').each(function() { + $('#TELHOME').append(' '); $('#TELHOME').append(Strophe.xmlTextNode($(this).find('NUMBER').text())); }); $(e).find('TEL:has(WORK)').each(function() { + $('#TELWORK').append(' '); $('#TELWORK').append(Strophe.xmlTextNode($(this).find('NUMBER').text())); }); $(e).find('EMAIL:has(HOME)').each(function() { + $('#EMAILHOME').append(' '); $('#EMAILHOME').append(Strophe.xmlTextNode($(this).find('USERID').text())); }); $(e).find('EMAIL:has(WORK)').each(function() { + $('#EMAILWORK').append(' '); $('#EMAILWORK').append(Strophe.xmlTextNode($(this).find('USERID').text())); }); + + if (!roster[jid2id(jid)].tune.isEmpty()) { + $('#vCard ul').append('
  • Tune: Listening to '+ + roster[jid2id(jid)].tune.title + ' by ' + roster[jid2id(jid)].tune.artist + + ' from ' + roster[jid2id(jid)].tune.source + '
  • ') + } + + $('#vCard').click(function () { + $('#vCard_container').slideUp("normal", function() { + $('#box-overlay').hide(); + }); + roster[jid2id(jid)].visible = false; + }); + + $('#box-overlay').show(); $('#vCard_container').slideDown("normal"); + roster[jid2id(jid)].visible = true; } function _cbVCard(e) { @@ -78,10 +101,7 @@ 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')); - }); + $('#ownInfo').load('vCard.html #ownvCard', function() { var easy_cases = new Array('FN', 'FAMILY', 'MIDDLE', 'GIVEN', 'NICKNAME', 'BDAY', 'CTRY', 'USERID'); for (var i=0; i
    '+nick+''); - $('#'+jid2id(jid)).click(function() { + $('#friends').append('

    '+nick+'
    '); + $('#' + 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) { @@ -158,14 +184,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').css('display', 'table'); + return false; } function getRoster() { - var id = 'getRoster'; + var id = connection.getUniqueId('roster'); var rosteriq = $iq({'id':id, 'type':'get'} @@ -179,7 +207,11 @@ function _cbDisco(e) { var id = e.getAttribute('id'); var jid = e.getAttribute('from'); - var response = $iq({id: id, type: 'result', to: jid}); + if (jid) { + var response = $iq({id: id, type: 'result', to: jid}); + } else { + var response = $iq({id: id, type: 'result'}); + } var query = response.c('query', {xmlns: Strophe.NS.DISCO_INFO}) query.c('identity', {category: 'client', type: 'web', name: appName}).up(); for (var i = 0; i < features.length; i++) { @@ -190,9 +222,37 @@ 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('
    '); + } + $('#' + id + ' p').append('' + localJID + ': '); + $('#' + id + ' p').append(form.text.value); + form.text.value = ''; + } + + return false; +} + +function createBubble(jid) { + var id = jid2id(jid) + 'Chat'; + $('body').append('
    Close' + + ' ' + jid + '

    '); + $('#' + id).append('
    ') + $('#' + 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($(msg).attr('from')) + 'Chat'; + var id = jid2id(jid) + 'Chat'; var body = $(msg).find('body:first').text(); body = escape(body); body = body.replace(/%0A/g, '
    '); @@ -200,13 +260,15 @@ function _cbMessage(msg) { body = body.replace(/%3E/g, '>'); body = body.replace(/%26/g, '&'); body = unescape(body); - if ($('#' + id).length > 0) { + if ($('#' + id).length <= 0) { + createBubble(jid); + } + if ($('#' + id + ' p *').length > 0) { $('#' + id + ' p').append('
    '); - $('#' + id + ' p').append('' + jid + ': '); - $('#' + id + ' p').append(body); - } else { - $('body').append('

    ' + jid + ': ' + body + '

    '); } + $('#' + id + ' p').append('' + jid + ': '); + $('#' + id + ' p').append(body); + $('#' + id).show(); return true; } @@ -221,6 +283,33 @@ function genCaps() { return b64_sha1(S); } +function handleTune(jid, tuneXML) { + var tune = new Tune(); + if (tuneXML.childNodes.length > 0) { + tune.artist = Strophe.getText(tuneXML.getElementsByTagName('artist')[0]); + tune.length= Strophe.getText(tuneXML.getElementsByTagName('length')[0]); + tune.rating= Strophe.getText(tuneXML.getElementsByTagName('rating')[0]); + tune.source= Strophe.getText(tuneXML.getElementsByTagName('source')[0]); + tune.title= Strophe.getText(tuneXML.getElementsByTagName('title')[0]); + tune.track= Strophe.getText(tuneXML.getElementsByTagName('track')[0]); + tune.uri= Strophe.getText(tuneXML.getElementsByTagName('uri')[0]); + if (roster[jid2id(jid)].visible == true) { + if ( $('#tune').length > 0) { + $('#tune').empty(); + $('#tune').append('Tune: '); + $('#tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + + ' by ' + tune.artist + ' from ' + tune.source)); + } else { + $('#vCard ul').append('
  • Tune: Listening to '+ tune.title + + ' by ' + tune.artist + ' from ' + tune.source + '
  • ') + } + } + } else { + $('#tune').remove(); + } + roster[jid2id(jid)].tune = tune; +} + function _cbPEP(e) { var from = e.getAttribute('from'); if (Strophe.getBareJidFromJid(from) == Strophe.getBareJidFromJid(connection.jid)) { @@ -229,27 +318,7 @@ function _cbPEP(e) { var items = e.getElementsByTagName('items')[0]; // Handle Tune if (items.getAttribute('node') == NS_TUNE) { - var tune = new Tune(); - if (items.getElementsByTagName('tune')[0].childNodes.length > 0) { - tune.artist = Strophe.getText(items.getElementsByTagName('artist')[0]); - tune.length= Strophe.getText(items.getElementsByTagName('length')[0]); - tune.rating= Strophe.getText(items.getElementsByTagName('rating')[0]); - tune.source= Strophe.getText(items.getElementsByTagName('source')[0]); - tune.title= Strophe.getText(items.getElementsByTagName('title')[0]); - tune.track= Strophe.getText(items.getElementsByTagName('track')[0]); - tune.uri= Strophe.getText(items.getElementsByTagName('uri')[0]); - if ( $('#'+jid2id(from)+' .tune').length > 0 ) { - $('#'+jid2id(from)+' .tune').empty(); - $('#'+jid2id(from)+' .tune').append(Strophe.xmlTextNode('Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source)); - } else { - $('#'+jid2id(from)).append('
    Listening to '+ tune.title + ' by ' + tune.artist + ' from ' + tune.source + ''); - } - } else { - if ( $('#'+jid2id(from)+' .tune').length > 0 ) { - $('#'+jid2id(from)+' .tune').empty(); - } - } - roster[jid2id(from)].tune = tune; + handleTune(from, items.getElementsByTagName('tune')[0]); } return true; } @@ -266,9 +335,10 @@ function onConnect(status) { log('Strophe is disconnected.'); showConnect(); } else if (status == Strophe.Status.AUTHFAIL) { - log('Authentication failed.'); - connection.disconnect(); - showConnect(); + log('Authentication failed'); + if (connection) { + connection.disconnect(); + } } else if (status == Strophe.Status.CONNECTED) { log('Strophe is connected.'); getOwnInfo(); @@ -284,9 +354,11 @@ 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(); @@ -308,6 +380,9 @@ function showDisconnect() { } $(document).ready(function () { + if (DEBUG) { + $('#log_container').show(); + } connection = new Strophe.Connection(BOSH_SERVICE); connection.rawInput = rawInput; connection.rawOutput = rawOutput; @@ -315,25 +390,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; +