view blog.js @ 15:5149a856d9dd default tip

Fix hybrid mode.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 03 Nov 2011 17:28:49 -0700
parents 161d4ea1c3f8
children
line wrap: on
line source

//'use strict';

const WS_SERVICE = 'ws://plugsbee.com:5280/';
var conn = null;
var jid = 'blog@linkmauve.fr'; // FIXME: Strophe should accept anonymous connections.
var password = 'blog';
var service = 'psgxs.linkmauve.fr';
var node = 'blog';
var re = true;

var params = (function() {
	var vars = {};
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split(';');

	for(var i = 0; i < hashes.length; i++) {
		var s = hashes[i].indexOf('=');
		var key = hashes[i].substring(0, s);
		var value = hashes[i].substring(s+1);
		vars[key] = value;
	}

	return vars;
})();

if (params.jid)
	service = params.jid;

if (params.node)
	node = params.node;

var received = {};
var messages = document.getElementById('messages');

var updateMessage = function(name, id) {
	var html = function(name, id) {
		return received[name][id].html;
	}

	var date = function(name, id) {
		return received[name][id].date;
	}

	var divs = messages.getElementsByTagNameNS(ns.xhtml, 'div');
	var container = null;

	for (var i in divs) {
		var div = divs[i]
		if (typeof div != 'object')
			continue;

		if (div.getAttributeNS(ns.e, 'jid') === name) {
			container = div;
			break;
		}
	}

	if (!container) {
		var container = document.createElementNS(ns.xhtml, 'div');
		container.setAttributeNS(ns.e, 'jid', name);
		messages.appendChild(container);
	}

	var articles = container.getElementsByTagNameNS(ns.xhtml, 'article');
	for (var i in articles) {
		var article = articles[i];
		if (typeof article != 'object')
			continue;

		if (article.getAttributeNS(ns.e, 'id') === id) {
			container.replaceChild(html(name, id), article);
			return;
		}
	}

	var article = html(name, id);

	if (!container.firstChild)
		container.appendChild(article);
	else {
		var d = date(name, id);
		var toInsert;
		for (var i in articles) {
			var a = articles[i];
			if (typeof a != 'object')
				continue;

			var ad = new Date();
			ad.set8601(a.getAttributeNS(ns.e, 'date'));

			if (ad < d) {
				toInsert = a;
				break;
			}
		}

		if (toInsert)
			container.insertBefore(article, toInsert);
		else
			container.appendChild(article);
	}
}

var convert = function(item) {
	if (item.ns in parsers)
		return new parsers[item.ns](item);
	return new parsers[''](item);
};

var onMessages = function(stanza) {
	var name = stanza.service+'/'+stanza.node;
	if (!received[name])
		received[name] = {};

	var r = received[name];

	for (var id in stanza.items) {
		r[id] = convert(stanza.items[id]);
		updateMessage(name, id);
	}
}

var onInfo = function(stanza) {
	console.log('info'); // TODO!
	/*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0];
	var x = query.getElementsByTagNameNS(ns.data, 'x')[0];
	var form = forms.parse(x);*/
}

function onConnect() {
	conn.send('<presence/>');
	// TODO: verify the subscription.
	//conn.pubsubSubscribe(service, node);
	if (params.no === 'server') {
		conn.pubsubItems(service, node, onMessages);
		conn.discoInfo(service, node, onInfo);
	}

	/*if (status == Strophe.Status.CONNFAIL) {
		console.log('Failed to connect.');
	} else if (status == Strophe.Status.DISCONNECTING) {
		console.log('Disconnecting.');
	} else if (status == Strophe.Status.DISCONNECTED) {
		console.log('Disconnected.');
		if (re)
			conn.connect(jid, password, onConnect);
	}*/
}

window.addEventListener('load', function () {
	conn = new XMPP(WS_SERVICE);

	// Debug
	conn.on('DOMOutput', function(stanza) {
		console.log('out:', stanza);
	});
	conn.on('DOMInput', function(stanza) {
		console.log('in:', stanza);
	});

	conn.on('connected', onConnect);
	conn.on('message', onMessages);
	conn.connect(jid, password);
}, false);

window.addEventListener('unload', function (e) {
	re = false;
	conn.disconnect();
	e.preventDefault();
}, false);