2012-06-23 14:26:04 +02:00
|
|
|
var helpers = (function (helpers) {
|
|
|
|
helpers.oc = function (a) {
|
|
|
|
// Thanks to Jonathan Snook: http://snook.ca
|
|
|
|
var o = {};
|
|
|
|
for(var i=0; i<a.length; i++) {
|
|
|
|
o[a[i]]='';
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
};
|
2012-06-24 11:24:10 +02:00
|
|
|
|
|
|
|
helpers.hash = function (str) {
|
2012-06-23 14:26:04 +02:00
|
|
|
// FIXME
|
|
|
|
if (str == 'online-users-container') {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
var shaobj = new jsSHA(str);
|
|
|
|
return shaobj.getHash("HEX");
|
|
|
|
};
|
|
|
|
return helpers;
|
|
|
|
})(helpers || {});
|
|
|
|
|
2012-07-08 12:27:13 +02:00
|
|
|
|
2012-06-23 14:26:04 +02:00
|
|
|
var xmppchat = (function (jarnxmpp, $, console) {
|
|
|
|
var ob = jarnxmpp;
|
2012-06-24 13:16:19 +02:00
|
|
|
/* FIXME: XEP-0136 specifies 'urn:xmpp:archive' but the mod_archive_odbc
|
|
|
|
* add-on for ejabberd wants the URL below. This might break for other
|
|
|
|
* Jabber servers.
|
|
|
|
*/
|
|
|
|
ob.Collections = {
|
|
|
|
'URI': 'http://www.xmpp.org/extensions/xep-0136.html#ns'
|
|
|
|
};
|
2012-06-23 14:26:04 +02:00
|
|
|
ob.Messages = jarnxmpp.Messages || {};
|
|
|
|
ob.Presence = jarnxmpp.Presence || {};
|
|
|
|
|
2012-07-01 12:03:43 +02:00
|
|
|
ob.Messages.ClientStorage = (function () {
|
|
|
|
methods = {};
|
|
|
|
|
|
|
|
methods.addMessage = function (jid, msg, direction) {
|
|
|
|
var bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
now = new Date().toISOString(),
|
|
|
|
msgs = store.get(bare_jid) || [];
|
|
|
|
if (msgs.length >= 30) {
|
2012-07-01 19:48:18 +02:00
|
|
|
msgs.shift();
|
2012-07-01 12:03:43 +02:00
|
|
|
}
|
|
|
|
msgs.push(now+' '+direction+' '+msg);
|
|
|
|
store.set(bare_jid, msgs);
|
|
|
|
};
|
|
|
|
|
|
|
|
methods.getMessages = function (jid) {
|
|
|
|
return store.get(jid) || [];
|
|
|
|
};
|
|
|
|
return methods;
|
|
|
|
})();
|
|
|
|
|
|
|
|
ob.Messages.getMessages = function (jid, callback) {
|
|
|
|
var bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
msgs = this.ClientStorage.getMessages(bare_jid);
|
|
|
|
callback(msgs);
|
|
|
|
};
|
|
|
|
|
2012-07-01 12:44:38 +02:00
|
|
|
ob.Messages.sendMessage = function (jid, text, callback) {
|
2012-06-23 14:26:04 +02:00
|
|
|
// TODO: Look in ChatPartners to see what resources we have for the recipient.
|
|
|
|
// if we have one resource, we sent to only that resources, if we have multiple
|
|
|
|
// we send to the bare jid.
|
2012-07-01 12:03:43 +02:00
|
|
|
// FIXME: see if @@content-transform is required
|
|
|
|
var message,
|
|
|
|
that = this;
|
2012-06-23 14:26:04 +02:00
|
|
|
$.getJSON(portal_url + '/content-transform?', {text: text}, function (data) {
|
2012-07-01 12:44:38 +02:00
|
|
|
message = $msg({to: jid, type: 'chat'})
|
|
|
|
.c('body').t(data.text).up()
|
|
|
|
.c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'});
|
2012-06-23 14:26:04 +02:00
|
|
|
xmppchat.connection.send(message);
|
2012-07-01 12:44:38 +02:00
|
|
|
that.ClientStorage.addMessage(jid, data.text, 'to');
|
2012-06-23 14:26:04 +02:00
|
|
|
callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ob.Messages.messageReceived = function (message) {
|
|
|
|
var jid = $(message).attr('from'),
|
|
|
|
bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
resource = Strophe.getResourceFromJid(jid),
|
2012-07-01 13:12:02 +02:00
|
|
|
delayed = $(message).find('delay').length > 0,
|
|
|
|
body = $(message).children('body').text(),
|
2012-06-23 14:26:04 +02:00
|
|
|
event = jQuery.Event('jarnxmpp.message');
|
|
|
|
|
2012-07-01 13:12:02 +02:00
|
|
|
if (body !== "") {
|
|
|
|
var xhtml_body = $(message).find('html > body').contents();
|
|
|
|
if (xhtml_body.length > 0) {
|
|
|
|
event.mtype = 'xhtml';
|
|
|
|
event.body = xhtml_body.html();
|
|
|
|
} else {
|
|
|
|
event.body = body;
|
|
|
|
event.mtype = 'text';
|
|
|
|
}
|
|
|
|
}
|
2012-06-23 14:26:04 +02:00
|
|
|
event.from = jid;
|
|
|
|
event.delayed = delayed;
|
2012-07-01 13:12:02 +02:00
|
|
|
event.message = message;
|
|
|
|
ob.ChatPartners.add(bare_jid, resource);
|
|
|
|
if (event.body) {
|
|
|
|
ob.Messages.ClientStorage.addMessage(jid, event.body, 'from');
|
2012-06-23 14:26:04 +02:00
|
|
|
}
|
2012-07-01 19:48:18 +02:00
|
|
|
if ((xmppchat.Storage.get(xmppchat.username+'-xmpp-status') || 'online') !== 'offline') {
|
|
|
|
// Only trigger the UI event if the user is not offline.
|
|
|
|
$(document).trigger(event);
|
|
|
|
}
|
2012-06-23 14:26:04 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2012-06-24 13:16:19 +02:00
|
|
|
ob.Collections.getLastCollection = function (jid, callback) {
|
|
|
|
var bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
iq = $iq({'type':'get'})
|
|
|
|
.c('list', {'xmlns': this.URI,
|
|
|
|
'with': bare_jid
|
2012-06-23 14:26:04 +02:00
|
|
|
})
|
|
|
|
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
|
2012-06-24 13:16:19 +02:00
|
|
|
.c('before').up()
|
2012-06-23 14:26:04 +02:00
|
|
|
.c('max')
|
2012-06-24 13:16:19 +02:00
|
|
|
.t('1');
|
|
|
|
|
|
|
|
xmppchat.connection.sendIQ(iq,
|
|
|
|
callback,
|
|
|
|
function () {
|
|
|
|
console.log('Error while retrieving collections');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ob.Collections.getLastMessages = function (jid, callback) {
|
|
|
|
var that = this;
|
|
|
|
this.getLastCollection(jid, function (result) {
|
2012-06-25 09:28:24 +02:00
|
|
|
// Retrieve the last page of a collection (max 30 elements).
|
2012-06-24 13:16:19 +02:00
|
|
|
var $collection = $(result).find('chat'),
|
|
|
|
jid = $collection.attr('with'),
|
|
|
|
start = $collection.attr('start'),
|
|
|
|
iq = $iq({'type':'get'})
|
|
|
|
.c('retrieve', {'start': start,
|
|
|
|
'xmlns': that.URI,
|
|
|
|
'with': jid
|
|
|
|
})
|
|
|
|
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
|
|
|
|
.c('max')
|
|
|
|
.t('30');
|
2012-06-25 09:28:24 +02:00
|
|
|
xmppchat.connection.sendIQ(iq, callback);
|
2012-06-24 13:16:19 +02:00
|
|
|
});
|
2012-06-23 14:26:04 +02:00
|
|
|
};
|
|
|
|
|
2012-07-01 21:12:51 +02:00
|
|
|
ob.Presence.getOwnStatus = function () {
|
|
|
|
return xmppchat.Storage.get(xmppchat.username+'-xmpp-status');
|
|
|
|
};
|
|
|
|
|
2012-06-24 11:24:10 +02:00
|
|
|
ob.Presence.onlineCount = function () {
|
|
|
|
return xmppchat.ChatPartners.getTotal();
|
|
|
|
};
|
|
|
|
|
2012-06-23 14:26:04 +02:00
|
|
|
ob.Presence.sendPresence = function (type) {
|
|
|
|
if (type === undefined) {
|
2012-07-01 21:12:51 +02:00
|
|
|
type = this.getOwnStatus() || 'online';
|
2012-06-23 14:26:04 +02:00
|
|
|
}
|
|
|
|
xmppchat.connection.send($pres({'type':type}));
|
|
|
|
};
|
|
|
|
|
|
|
|
ob.Taskbuffer = (function ($) {
|
2012-07-04 11:28:43 +02:00
|
|
|
// Executes tasks one after another (i.e next task is started only when
|
|
|
|
// the previous one has been completed).
|
2012-06-23 14:26:04 +02:00
|
|
|
buffer = {};
|
2012-07-04 11:28:43 +02:00
|
|
|
// Tasks must be objects with keys: 'that', 'method' and 'parameters'
|
|
|
|
// 'that' the context for the method, while 'parameters' is the list of arguments
|
|
|
|
// passed to it.
|
2012-06-23 14:26:04 +02:00
|
|
|
buffer.tasks = [];
|
|
|
|
buffer.deferred = $.when();
|
|
|
|
buffer.handleTasks = function () {
|
|
|
|
var task;
|
|
|
|
// If the current deferred task is resolved and there are more tasks
|
|
|
|
if (buffer.deferred.isResolved() && buffer.tasks.length > 0) {
|
|
|
|
// Get the next task in the queue and set the new deferred.
|
|
|
|
task = buffer.tasks.shift();
|
|
|
|
|
|
|
|
buffer.deferred = $.when(task.method.apply(task.that, task.parameters));
|
|
|
|
|
|
|
|
if (buffer.tasks.length > 0) {
|
|
|
|
buffer.deferred.done(buffer.handleTasks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return buffer;
|
|
|
|
})(jQuery);
|
|
|
|
|
|
|
|
return ob;
|
|
|
|
})(jarnxmpp || {}, jQuery, console || {log: function(){}});
|
|
|
|
|
2012-07-08 12:27:13 +02:00
|
|
|
|
2012-07-08 22:18:49 +02:00
|
|
|
xmppchat.ChatBox = Backbone.Model.extend({
|
2012-07-09 18:47:50 +02:00
|
|
|
|
|
|
|
hash: function (str) {
|
|
|
|
var shaobj = new jsSHA(str);
|
|
|
|
return shaobj.getHash("HEX");
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.set({
|
|
|
|
'user_id' : Strophe.getNodeFromJid(this.get('jid')),
|
|
|
|
'chat_id' : this.hash(this.get('jid'))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
xmppchat.ChatBoxView = Backbone.View.extend({
|
|
|
|
|
|
|
|
initialize: function (){
|
|
|
|
this.el = this.make('div', {'class': 'chatbox'});
|
|
|
|
$('body').append($(this.el).hide());
|
|
|
|
},
|
|
|
|
|
|
|
|
template: _.template('<div class="chat-head chat-head-chatbox">' +
|
|
|
|
'<div class="chat-title"> <%= user_id %> </div>' +
|
|
|
|
'<a href="javascript:void(0)" class="chatbox-button close-chatbox-button">X</a>' +
|
|
|
|
'<br clear="all"/>' +
|
|
|
|
'</div>' +
|
|
|
|
'<div class="chat-content"></div>' +
|
|
|
|
'<form class="sendXMPPMessage" action="" method="post">' +
|
|
|
|
'<textarea ' +
|
|
|
|
'type="text" ' +
|
|
|
|
'class="chat-textarea" ' +
|
|
|
|
'placeholder="Personal message"/>'),
|
|
|
|
|
|
|
|
render: function () {
|
|
|
|
$(this.el).attr('id', this.model.get('chat_id'));
|
|
|
|
$(this.el).html(this.template(this.model.toJSON()));
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
$(this.el).show('fast');
|
|
|
|
}
|
2012-07-08 22:18:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
xmppchat.ChatBoxes = Backbone.Collection.extend({
|
|
|
|
});
|
|
|
|
|
2012-07-09 18:47:50 +02:00
|
|
|
xmppchat.ChatBoxesView = Backbone.View.extend({
|
|
|
|
|
|
|
|
positionNewChat: function () {
|
|
|
|
// TODO:
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function (){
|
|
|
|
this.options.model.on("add", function (item, x, y) {
|
|
|
|
var view = new xmppchat.ChatBoxView({
|
|
|
|
model: item
|
|
|
|
});
|
|
|
|
view.render();
|
|
|
|
this.positionNewChat();
|
|
|
|
view.show();
|
|
|
|
}, this);
|
|
|
|
}
|
2012-07-08 22:18:49 +02:00
|
|
|
});
|
|
|
|
|
2012-07-09 18:47:50 +02:00
|
|
|
|
2012-07-08 12:27:13 +02:00
|
|
|
xmppchat.RosterItem = Backbone.Model.extend({
|
|
|
|
/*
|
|
|
|
* RosterItem: {
|
|
|
|
* 'status': String,
|
|
|
|
* 'subscription': String,
|
|
|
|
* 'resources': Array,
|
|
|
|
* 'fullname': String
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
initialize: function (jid, subscription) {
|
|
|
|
this.id = jid;
|
|
|
|
this.subscription = subscription;
|
|
|
|
|
|
|
|
this.fullname = '';
|
|
|
|
this.resources = [];
|
|
|
|
this.status = 'offline';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
xmppchat.RosterClass = (function (stropheRoster, _, $, console) {
|
2012-07-08 22:18:49 +02:00
|
|
|
var ob = _.clone(stropheRoster),
|
|
|
|
Collection = Backbone.Collection.extend({
|
2012-07-09 18:47:50 +02:00
|
|
|
model: xmppchat.RosterItem,
|
|
|
|
stropheRoster: stropheRoster,
|
|
|
|
|
|
|
|
initialize: function () {
|
|
|
|
this.stropheRoster._connection = xmppchat.connection;
|
|
|
|
},
|
|
|
|
|
|
|
|
comparator : function (rosteritem) {
|
|
|
|
var status = rosteritem.get('status'),
|
|
|
|
rank = 4;
|
|
|
|
switch(status) {
|
|
|
|
case 'offline':
|
|
|
|
rank = 4;
|
|
|
|
break;
|
|
|
|
case 'unavailable':
|
|
|
|
rank = 3;
|
|
|
|
break;
|
|
|
|
case 'away':
|
|
|
|
rank = 2;
|
|
|
|
break;
|
|
|
|
case 'busy':
|
|
|
|
rank = 1;
|
|
|
|
break;
|
|
|
|
case 'online':
|
|
|
|
rank = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rank;
|
|
|
|
},
|
|
|
|
|
|
|
|
isSelf: function (jid) {
|
|
|
|
return (Strophe.getBareJidFromJid(jid) === Strophe.getBareJidFromJid(xmppchat.connection.jid));
|
|
|
|
},
|
|
|
|
|
|
|
|
getRoster: function () {
|
|
|
|
return stropheRoster.get();
|
|
|
|
},
|
|
|
|
|
|
|
|
getItem: function (id) {
|
|
|
|
return Backbone.Collection.prototype.get.call(this, id);
|
|
|
|
},
|
|
|
|
|
|
|
|
addRosterItem: function (item) {
|
|
|
|
var user_id = Strophe.getNodeFromJid(item.jid),
|
|
|
|
model = new xmppchat.RosterItem(item.jid, item.subscription);
|
|
|
|
this.add(model);
|
|
|
|
},
|
|
|
|
|
|
|
|
addResource: function (bare_jid, resource) {
|
|
|
|
var item = this.getItem(bare_jid);
|
|
|
|
if (item) {
|
|
|
|
if (_.indexOf(item.resources, resource) == -1) {
|
|
|
|
item.resources.push(resource);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
item.resources = [resource];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removeResource: function (bare_jid, resource) {
|
|
|
|
var item = this.getItem(bare_jid);
|
|
|
|
if (item) {
|
|
|
|
var idx = _.indexOf(item.resources, resource);
|
|
|
|
if (idx !== -1) {
|
|
|
|
item.resources.splice(idx, 1);
|
|
|
|
return item.resources.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
},
|
2012-07-04 11:28:43 +02:00
|
|
|
|
2012-07-09 18:47:50 +02:00
|
|
|
clearResources: function (bare_jid) {
|
|
|
|
var item = this.getItem(bare_jid);
|
|
|
|
if (item) {
|
|
|
|
item.resources = [];
|
|
|
|
}
|
|
|
|
},
|
2012-07-08 22:18:49 +02:00
|
|
|
|
2012-07-09 18:47:50 +02:00
|
|
|
getTotalResources: function (bare_jid) {
|
|
|
|
var item = this.getItem(bare_jid);
|
|
|
|
if (item) {
|
|
|
|
return _.size(item.resources);
|
|
|
|
}
|
2012-07-08 22:18:49 +02:00
|
|
|
}
|
2012-07-09 18:47:50 +02:00
|
|
|
});
|
2012-07-08 22:18:49 +02:00
|
|
|
|
2012-07-09 18:47:50 +02:00
|
|
|
var collection = new Collection();
|
|
|
|
_.extend(ob, collection);
|
|
|
|
_.extend(ob, Backbone.Events);
|
2012-07-08 12:27:13 +02:00
|
|
|
|
|
|
|
ob.updateHandler = function (items) {
|
|
|
|
var model;
|
2012-07-04 11:28:43 +02:00
|
|
|
for (var i=0; i<items.length; i++) {
|
|
|
|
if (items[i].subscription === 'none') {
|
2012-07-08 12:27:13 +02:00
|
|
|
model = ob.getItem(jid);
|
|
|
|
ob.remove(model);
|
2012-07-04 11:28:43 +02:00
|
|
|
} else {
|
2012-07-08 12:27:13 +02:00
|
|
|
if (ob.getItem(items[i].jid)) {
|
|
|
|
// Update the model
|
|
|
|
model = ob.getItem(jid);
|
|
|
|
model.subscription = item.subscription;
|
|
|
|
} else {
|
|
|
|
ob.addRosterItem(items[i]);
|
|
|
|
}
|
2012-07-04 11:28:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-08 22:18:49 +02:00
|
|
|
ob.presenceHandler = function (presence) {
|
|
|
|
var jid = $(presence).attr('from'),
|
|
|
|
bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
resource = Strophe.getResourceFromJid(jid),
|
|
|
|
ptype = $(presence).attr('type'),
|
|
|
|
status = '';
|
2012-07-08 12:27:13 +02:00
|
|
|
|
2012-07-08 22:18:49 +02:00
|
|
|
if (ob.isSelf(bare_jid)) { return true; }
|
2012-07-08 12:27:13 +02:00
|
|
|
|
2012-07-08 22:18:49 +02:00
|
|
|
if (ptype === 'subscribe') {
|
|
|
|
// FIXME: User wants to subscribe to us. Always approve and
|
|
|
|
// ask to subscribe to him
|
|
|
|
stropheRoster.authorize(bare_jid);
|
|
|
|
stropheRoster.subscribe(bare_jid);
|
|
|
|
|
|
|
|
} else if (ptype === 'unsubscribe') {
|
|
|
|
if (_.indexOf(xmppchat.Roster.getCachedJids(), bare_jid) != -1) {
|
|
|
|
stropheRoster.unauthorize(bare_jid);
|
|
|
|
stropheRoster.unsubscribe(bare_jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (ptype === 'unsubscribed') {
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else if (ptype !== 'error') { // Presence has changed
|
|
|
|
if (_.indexOf(['unavailable', 'offline', 'busy', 'away'], ptype) != -1) {
|
|
|
|
status = ptype;
|
|
|
|
} else {
|
|
|
|
status = ($(presence).find('show').text() === '') ? 'online' : 'away';
|
|
|
|
}
|
|
|
|
if ((status !== 'offline')&&(status !== 'unavailable')) {
|
|
|
|
ob.addResource(bare_jid, resource);
|
|
|
|
model = ob.getItem(bare_jid);
|
|
|
|
model.set({'status': status});
|
|
|
|
} else {
|
|
|
|
if (ob.removeResource(bare_jid, resource) === 0) {
|
|
|
|
model = ob.getItem(bare_jid);
|
|
|
|
model.set({'status': status});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
2012-07-08 12:27:13 +02:00
|
|
|
return ob;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
xmppchat.RosterItemView = Backbone.View.extend({
|
|
|
|
tagName: 'li',
|
2012-07-08 22:18:49 +02:00
|
|
|
|
|
|
|
initialize: function () {
|
2012-07-09 18:47:50 +02:00
|
|
|
var that = this;
|
2012-07-08 22:18:49 +02:00
|
|
|
this.el = this.make('li');
|
|
|
|
|
|
|
|
$(this.el).bind('click', function (e) {
|
|
|
|
e.preventDefault();
|
2012-07-09 18:47:50 +02:00
|
|
|
var jid = $(this).attr('data-recipient');
|
|
|
|
if (!xmppchat.chatboxes.get(jid)) {
|
|
|
|
var chatbox = new xmppchat.ChatBox({
|
|
|
|
'id': jid,
|
|
|
|
'jid': jid
|
|
|
|
});
|
|
|
|
xmppchat.chatboxes.add(chatbox);
|
|
|
|
}
|
2012-07-08 22:18:49 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.options.model.on('change', function (item, changed) {
|
|
|
|
if (_.has(changed.changes, 'status')) {
|
|
|
|
$(this.el).attr('class', item.changed.status);
|
|
|
|
}
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
2012-07-08 12:27:13 +02:00
|
|
|
render: function () {
|
|
|
|
var item = this.model,
|
|
|
|
jid = item.id,
|
|
|
|
bare_jid = Strophe.getBareJidFromJid(jid),
|
|
|
|
user_id = Strophe.getNodeFromJid(jid),
|
|
|
|
fullname = (item.fullname) ? item.fullname : user_id;
|
2012-07-08 22:18:49 +02:00
|
|
|
$(this.el).addClass(item.status).attr('id', 'online-users-'+user_id).attr('data-recipient', bare_jid);
|
|
|
|
$(this.el).append($('<a title="Click to chat with this contact"></a>').text(fullname));
|
|
|
|
$(this.el).append($('<a title="Click to remove this contact" href="#"></a>').addClass('remove-xmpp-contact'));
|
2012-07-08 12:27:13 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
xmppchat.RosterViewClass = (function (roster, _, $, console) {
|
|
|
|
var View = Backbone.View.extend({
|
|
|
|
tagName: 'ul',
|
|
|
|
el: $('#xmpp-contacts'),
|
|
|
|
model: roster,
|
|
|
|
|
2012-07-08 22:18:49 +02:00
|
|
|
initialize: function () {
|
|
|
|
this.model.on("add", function (item) {
|
|
|
|
var view = new xmppchat.RosterItemView({
|
|
|
|
model: item
|
|
|
|
});
|
|
|
|
$(this.el).append(view.render().el);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.model.on('change', function (item) {
|
|
|
|
this.render();
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
this.model.on("remove", function (msg) {
|
|
|
|
console.log('roster remove handler called!!!!!');
|
|
|
|
});
|
|
|
|
},
|
2012-07-08 12:27:13 +02:00
|
|
|
|
|
|
|
render: function () {
|
2012-07-08 22:18:49 +02:00
|
|
|
var models = this.model.sort().models,
|
|
|
|
children = $(this.el).children(),
|
|
|
|
that = this;
|
|
|
|
|
|
|
|
$(models).each(function (idx, model) {
|
|
|
|
var user_id = Strophe.getNodeFromJid(model.id);
|
|
|
|
$(that.el).append(children.filter('#online-users-'+user_id));
|
|
|
|
});
|
2012-07-08 12:27:13 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
var view = new View();
|
2012-07-08 22:18:49 +02:00
|
|
|
return view;
|
2012-07-04 11:28:43 +02:00
|
|
|
});
|
|
|
|
|
2012-07-08 12:27:13 +02:00
|
|
|
// FIXME: Need to get some convention going for naming classes and instances of
|
|
|
|
// models and views.
|
2012-06-23 14:26:04 +02:00
|
|
|
|
2012-07-03 13:15:45 +02:00
|
|
|
// Event handlers
|
|
|
|
// --------------
|
|
|
|
$(document).ready(function () {
|
|
|
|
$(document).unbind('jarnxmpp.connected');
|
|
|
|
$(document).bind('jarnxmpp.connected', function () {
|
|
|
|
// Messages
|
|
|
|
xmppchat.connection.addHandler(xmppchat.Messages.messageReceived, null, 'message', 'chat');
|
|
|
|
xmppchat.UI.restoreOpenChats();
|
|
|
|
|
2012-07-08 12:27:13 +02:00
|
|
|
xmppchat.Roster = xmppchat.RosterClass(Strophe._connectionPlugins.roster, _, $, console);
|
|
|
|
xmppchat.RosterView = Backbone.View.extend(xmppchat.RosterViewClass(xmppchat.Roster, _, $, console));
|
2012-07-08 22:18:49 +02:00
|
|
|
|
|
|
|
xmppchat.connection.addHandler(xmppchat.Roster.presenceHandler, null, 'presence', null);
|
2012-07-08 12:27:13 +02:00
|
|
|
|
|
|
|
xmppchat.Roster.registerCallback(xmppchat.Roster.updateHandler);
|
|
|
|
xmppchat.Roster.getRoster();
|
2012-07-03 13:15:45 +02:00
|
|
|
xmppchat.Presence.sendPresence();
|
2012-07-09 18:47:50 +02:00
|
|
|
|
|
|
|
xmppchat.chatboxes = new xmppchat.ChatBoxes();
|
|
|
|
|
|
|
|
xmppchat.chatboxesview = new xmppchat.ChatBoxesView({
|
|
|
|
'model': xmppchat.chatboxes
|
|
|
|
});
|
2012-07-03 13:15:45 +02:00
|
|
|
});
|
|
|
|
});
|