3 Jappix - An open social platform
4 These are the Jappix Mobile lightweight JS script
6 -------------------------------------------------
10 Last revision: 10/07/11
14 /* BEGIN CONNECTION FUNCTIONS */
16 function doLogin(aForm) {
22 var xid = aForm.xid.value;
25 // A domain is specified
26 if(xid.indexOf('@') != -1) {
27 username = getXIDNick(xid);
28 domain = getXIDHost(xid);
30 // Domain is locked and not the same
31 if((LOCK_HOST == 'on') && (domain != HOST_MAIN)) {
38 // No "@" in the XID, we should add the default domain
44 var pwd = aForm.pwd.value;
48 reg = aForm.reg.checked;
51 if(username && domain && pwd) {
52 // Show the info notification
55 // We define the http binding parameters
59 oArgs.httpbase = HOST_BOSH_MAIN;
61 oArgs.httpbase = HOST_BOSH;
63 // We create the new http-binding connection
64 con = new JSJaCHttpBindingConnection(oArgs);
66 // And we handle everything that happen
67 con.registerHandler('message', handleMessage);
68 con.registerHandler('presence', handlePresence);
69 con.registerHandler('iq', handleIQ);
70 con.registerHandler('onconnect', handleConnected);
71 con.registerHandler('onerror', handleError);
72 con.registerHandler('ondisconnect', handleDisconnected);
74 // We retrieve what the user typed in the login inputs
76 oArgs.username = username;
77 oArgs.domain = domain;
78 oArgs.resource = JAPPIX_RESOURCE + ' Mobile (' + (new Date()).getTime() + ')';
81 oArgs.xmllang = XML_LANG;
85 oArgs.register = true;
91 // Not enough parameters
106 function doLogout() {
110 /* END CONNECTION FUNCTIONS */
112 /* BEGIN SHOW/HIDE FUNCTIONS */
114 function showThis(id) {
115 document.getElementById(id).style.display = 'block';
118 function hideThis(id) {
119 document.getElementById(id).style.display = 'none';
122 function resetPanel(id) {
123 // Hide the opened panels
127 //Show the target panel
132 function resetDOM() {
133 // Reset the "secret" input values
134 document.getElementById('pwd').value = '';
136 // Remove the useless DOM elements
137 var body = document.getElementsByTagName('body')[0];
138 body.removeChild(document.getElementById('talk'));
139 body.removeChild(document.getElementById('chat'));
142 /* END SHOW/HIDE FUNCTIONS */
144 /* BEGIN SYSTEM FUNCTIONS */
146 function exists(id) {
147 if(!document.getElementById(id))
153 function _e(string) {
157 function encodeOnclick(str) {
158 return str.replace(/'/g, '\\$&').replace(/"/g, '"');
161 function getJappixLocation() {
162 var url = window.location.href;
164 // If the URL has variables, remove them
165 if(url.indexOf('?') != -1)
166 url = url.split('?')[0];
167 if(url.indexOf('#') != -1)
168 url = url.split('#')[0];
171 if(!url.match(/(.+)\/$/))
177 /* END SYSTEM FUNCTIONS */
179 /* BEGIN HANDLING FUNCTIONS */
181 function handleMessage(msg) {
182 var type = msg.getType();
184 if(type == 'chat' || type == 'normal') {
186 var body = msg.getBody();
190 var xid = cutResource(msg.getFrom());
191 var hash = hex_md5(xid);
192 var nick = getNick(xid, hash);
198 // Create the chat if it does not exist
201 // Display the message
202 displayMessage(xid, body, nick, hash);
207 function handlePresence(pre) {
208 // Define the variables
209 var xid = cutResource(pre.getFrom());
210 var hash = hex_md5(xid);
211 var type = pre.getType();
212 var show = pre.getShow();
214 // Online buddy: show it!
216 showThis('buddy-' + hash);
218 // Display the correct presence
221 displayPresence(hash, show);
225 displayPresence(hash, show);
229 displayPresence(hash, show);
233 displayPresence(hash, show);
237 displayPresence(hash, 'available');
243 hideThis('buddy-' + hash);
246 function handleIQ(iq) {
248 var iqFrom = iq.getFrom();
249 var iqID = iq.getID();
250 var iqQueryXMLNS = iq.getQueryXMLNS();
251 var iqType = iq.getType();
253 // Create the response
254 if((iqType == 'get') && ((iqQueryXMLNS == NS_DISCO_INFO) || (iqQueryXMLNS == NS_VERSION))) {
255 var iqResponse = new JSJaCIQ();
256 iqResponse.setID(iqID);
257 iqResponse.setTo(iqFrom);
258 iqResponse.setType('result');
262 if((iqQueryXMLNS == NS_DISCO_INFO) && (iqType == 'get')) {
263 /* REF: http://xmpp.org/extensions/xep-0030.html */
265 var iqQuery = iqResponse.setQuery(NS_DISCO_INFO);
267 // We set the name of the client
268 iqQuery.appendChild(iq.appendNode('identity', {
269 'category': 'client',
271 'name': 'Jappix Mobile'
274 // We set all the supported features
275 var fArray = new Array(
281 iqQuery.appendChild(iq.buildNode('feature', {'var': fArray[i]}));
283 con.send(iqResponse);
286 // Software version query
287 else if((iqQueryXMLNS == NS_VERSION) && (iqType == 'get')) {
288 /* REF: http://xmpp.org/extensions/xep-0092.html */
290 var iqQuery = iqResponse.setQuery(NS_VERSION);
292 iqQuery.appendChild(iq.buildNode('name', 'Jappix Mobile'));
293 iqQuery.appendChild(iq.buildNode('version', JAPPIX_VERSION));
294 iqQuery.appendChild(iq.buildNode('os', BrowserDetect.OS));
296 con.send(iqResponse);
300 function handleConnected() {
301 // Reset the elements
305 // Create the talk page
306 document.getElementsByTagName('body')[0].innerHTML +=
308 '<div class="header">' +
309 '<div class="mobile-images"></div>' +
310 '<button onclick="doLogout();">' + _e("Disconnect") + '</button>' +
313 '<div id="roster"></div>' +
317 '<div class="header">' +
318 '<div class="mobile-images"></div>' +
319 '<button onclick="returnToRoster();">' + _e("Previous") + '</button>' +
322 '<div id="chans"></div>' +
325 // Get the roster items
329 function handleError(error) {
333 function handleDisconnected() {
334 // Reset the elements
337 // Show the home page
341 function handleRoster(iq) {
342 // Error: send presence anyway
343 if(!iq || (iq.getType() != 'result'))
344 return sendPresence('', 'available', 1);
346 // Define some pre-vars
347 var current, xid, nick, oneBuddy, oneID, hash;
348 var roster = document.getElementById('roster');
351 var iqNode = iq.getNode();
352 var bItems = iqNode.getElementsByTagName('item');
354 // Display each elements from the roster
355 for(var i = 0; i < bItems.length; i++) {
357 current = iqNode.getElementsByTagName('item').item(i);
358 xid = current.getAttribute('jid').htmlEnc();
359 nick = current.getAttribute('name');
364 nick = getDirectNick(xid);
366 // Display the values
367 oneBuddy = document.createElement('a');
368 oneID = 'buddy-' + hash;
369 oneBuddy.setAttribute('href', '#');
370 oneBuddy.setAttribute('id', oneID);
371 oneBuddy.setAttribute('class', 'one-buddy');
372 oneBuddy.setAttribute('onclick', 'return chat(\'' + encodeOnclick(xid) + '\', \'' + encodeOnclick(nick) + '\');');
373 oneBuddy.innerHTML = nick.htmlEnc();
374 roster.appendChild(oneBuddy);
377 // Start handling buddies presence
378 sendPresence('', 'available', 1);
381 /* END HANDLING FUNCTIONS */
383 /* BEGIN SENDING FUNCTIONS */
385 function sendMessage(aForm) {
387 var body = aForm.body.value;
388 var xid = aForm.xid.value;
389 var hash = hex_md5(xid);
393 var aMsg = new JSJaCMessage();
395 aMsg.setType('chat');
400 aForm.body.value = '';
402 // Display the message we sent
403 displayMessage(xid, body, 'me', hash);
412 function sendPresence(type, show, priority, status) {
413 var presence = new JSJaCPresence();
416 presence.setType(type);
418 presence.setShow(show);
420 presence.setPriority(priority);
422 presence.setStatus(status);
427 /* END SENDING FUNCTIONS */
429 /* BEGIN GETTING FUNCTIONS */
431 function getRoster() {
434 iq.setQuery(NS_ROSTER);
436 con.send(iq, handleRoster);
439 function getDirectNick(xid) {
440 return explodeThis('@', xid, 0);
443 function getNick(xid, hash) {
444 var path = 'buddy-' + hash;
447 return document.getElementById(path).innerHTML;
453 /* END GETTING FUNCTIONS */
455 /* BEGIN RESOURCES FUNCTIONS */
456 function explodeThis(toEx, toStr, i) {
457 // Get the index of our char to explode
458 var index = toStr.indexOf(toEx);
460 // We split if necessary the string
463 toStr = toStr.substr(0, index);
465 toStr = toStr.substr(index + 1);
468 // We return the value
472 function cutResource(aXID) {
473 return explodeThis('/', aXID, 0);
476 function getXIDNick(aXID) {
477 return explodeThis('@', aXID, 0);
480 function getXIDHost(aXID) {
481 return explodeThis('@', aXID, 1);
483 /* END RESOURCES FUNCTIONS */
485 /* BEGIN CHAT FUNCTIONS */
487 function filter(msg) {
494 .replace(/(\s|^)\*(.+)\*(\s|$)/gi,'$1<em>$2</em>$3');
497 msg = applyLinks(msg, 'mini');
502 function displayMessage(xid, body, nick, hash) {
504 var path = 'content-' + hash;
506 // Display the message
510 html += ' class="me">' + _e("You");
512 html += ' class="him">' + nick;
514 html += '</b> ' + filter(body) + '</span>';
516 document.getElementById(path).innerHTML += html;
518 // Scroll to the last element
519 document.getElementById(path).lastChild.scrollIntoView();
522 function returnToRoster() {
530 function chatSwitch(hash) {
531 // Hide the roster page
534 // Hide the other chats
535 var divs = document.getElementsByTagName('div');
537 for(var i = 0; i < divs.length; i++) {
538 if(divs.item(i).getAttribute('class') == 'one-chat')
539 divs.item(i).style.display = 'none';
547 function createChat(xid, nick, hash) {
548 // Define the variables
549 var chat = document.getElementById('chans');
550 var oneChat = document.createElement('div');
552 // Apply the DOM modification
553 oneChat.setAttribute('id', 'chat-' + hash);
554 oneChat.setAttribute('class', 'one-chat');
555 oneChat.innerHTML = '<p>' + nick + '</p><div id="content-' + hash + '"></div><form action="#" method="post" onsubmit="return sendMessage(this);"><input type="text" name="body" /><input type="hidden" name="xid" value="' + xid + '" /><input type="submit" class="submit" value="OK" /></form>';
556 chat.appendChild(oneChat);
559 function chat(xid, nick) {
560 var hash = hex_md5(xid);
562 // If the chat was not yet opened
563 if(!exists('chat-' + hash)) {
566 nick = getNick(xid, hash);
569 createChat(xid, nick, hash);
572 // Switch to the chat
573 chatSwitch('chat-' + hash);
578 /* END CHAT FUNCTIONS */
580 /* BEGIN PRESENCE FUNCTIONS */
582 function displayPresence(hash, show) {
583 document.getElementById('buddy-' + hash).setAttribute('class', 'one-buddy ' + show);
586 /* END PRESENCE FUNCTIONS */
588 /* BEGIN DOCUMENT EVENTS FUNCTIONS */
590 onbeforeunload = doLogout;
592 /* END DOCUMENT EVENTS FUNCTIONS */