Various js improvements and fixes.

- On page load, roster users are offline by default.
- let addUserToRosterUI accept the user's status.
- set the user remove confirmation dialog's position.
This commit is contained in:
JC Brand 2012-07-06 19:02:24 +02:00
parent 997bcdc5f2
commit be5a8b3c40
2 changed files with 28 additions and 32 deletions

41
chat.js
View File

@ -16,17 +16,6 @@ var helpers = (function (helpers) {
var shaobj = new jsSHA(str);
return shaobj.getHash("HEX");
};
helpers.size = function (obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
}
}
return size;
};
return helpers;
})(helpers || {});
@ -55,7 +44,7 @@ var xmppchat = (function (jarnxmpp, $, console) {
methods.add = function (bare_jid, resource) {
if (Object.prototype.hasOwnProperty.call(storage, bare_jid)) {
if (!(resource in helpers.oc(storage[bare_jid]))) {
if (_.indexOf(storage[bare_jid], resource) == -1) {
storage[bare_jid].push(resource);
}
} else {
@ -66,18 +55,16 @@ var xmppchat = (function (jarnxmpp, $, console) {
methods.remove = function (bare_jid, resource) {
// Removes the resource for a user and returns the number of
// resources left over.
if (Object.prototype.hasOwnProperty.call(storage, bare_jid)) {
if (resource in helpers.oc(storage[bare_jid])) {
var idx = storage[bare_jid].indexOf(resource);
if (idx !== undefined) {
storage[bare_jid].splice(idx, 1);
if (storage[bare_jid].length === 0) {
delete storage[bare_jid];
return 0;
}
else {
return storage[bare_jid].length;
}
if (_.has(storage, bare_jid)) {
var idx = _.indexOf(storage[bare_jid], resource);
if (idx !== -1) {
storage[bare_jid].splice(idx, 1);
if (storage[bare_jid].length === 0) {
delete storage[bare_jid];
return 0;
}
else {
return storage[bare_jid].length;
}
}
}
@ -91,7 +78,7 @@ var xmppchat = (function (jarnxmpp, $, console) {
};
methods.getTotal = function () {
return helpers.size(storage);
return _.size(storage);
};
return methods;
@ -231,6 +218,10 @@ var xmppchat = (function (jarnxmpp, $, console) {
ptype = $(presence).attr('type'),
status = '';
if (ob.isOwnUser(bare_jid)) {
return true;
}
if (ptype === 'subscribe') {
// User wants to subscribe to us. Always approve and
// ask to subscribe to him

View File

@ -8,9 +8,9 @@ xmppchat.UI = (function (xmppUI, $, console) {
return xmppchat.base_url + call;
};
ob.addUserToRosterUI = function (user_id, bare_jid, fullname) {
ob.addUserToRosterUI = function (user_id, bare_jid, fullname, userstatus) {
if ($('#online-users-' + user_id).length > 0) { return; }
var li = $('<li></li>').attr('id', 'online-users-'+user_id).attr('data-recipient', bare_jid);
var li = $('<li></li>').addClass(userstatus).attr('id', 'online-users-'+user_id).attr('data-recipient', bare_jid);
li.append($('<a title="Click to chat with this contact"></a>').addClass('user-details-toggle').text(fullname));
li.append($('<a title="Click to remove this contact" href="#"></a>').addClass('remove-xmpp-contact'));
$('#xmpp-contacts').append(li);
@ -52,7 +52,7 @@ xmppchat.UI = (function (xmppUI, $, console) {
existing_user_element.attr('class', status);
} else if ((status !== 'offline') && (status !== 'unavailable')) {
xmppchat.Presence.getUserInfo(user_id, function (data) {
xmppchat.UI.addUserToRosterUI(user_id, bare_jid, data.fullname);
xmppchat.UI.addUserToRosterUI(user_id, bare_jid, data.fullname, status);
});
} else { // status is offline and the user isn't shown as online
return;
@ -212,7 +212,7 @@ xmppchat.UI = (function (xmppUI, $, console) {
}
}
if (!(jid in helpers.oc(this.chats))) {
if (_.indexOf(this.chats, jid) == -1) {
this.chats.push(jid);
}
this.addChatToCookie(jid);
@ -542,7 +542,7 @@ $(document).ready(function () {
// FIXME: We should store the contact name on the jabber server!
xmppchat.Presence.getUserInfo(user_id, function (data) {
xmppchat.UI.addUserToRosterUI(user_id, bare_jid, data.fullname);
xmppchat.UI.addUserToRosterUI(user_id, bare_jid, data.fullname, 'offline');
});
}
});
@ -599,10 +599,15 @@ $(document).ready(function () {
title: 'Are you sure you want to remove this contact?',
dialogClass: 'remove-xmpp-contact-dialog',
resizable: false,
width: 400,
width: 200,
position: {
my: 'center',
at: 'center',
of: '#online-users-container'
},
modal: true,
buttons: {
"Yes, remove.": function() {
"Remove": function() {
$( this ).dialog( "close" );
var jid = $(that).parent().attr('data-recipient');
xmppchat.Roster.unsubscribe(jid);