Commit be5a8b3c authored by JC Brand's avatar JC Brand

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.
parent 997bcdc5
...@@ -16,17 +16,6 @@ var helpers = (function (helpers) { ...@@ -16,17 +16,6 @@ var helpers = (function (helpers) {
var shaobj = new jsSHA(str); var shaobj = new jsSHA(str);
return shaobj.getHash("HEX"); 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; return helpers;
})(helpers || {}); })(helpers || {});
...@@ -55,7 +44,7 @@ var xmppchat = (function (jarnxmpp, $, console) { ...@@ -55,7 +44,7 @@ var xmppchat = (function (jarnxmpp, $, console) {
methods.add = function (bare_jid, resource) { methods.add = function (bare_jid, resource) {
if (Object.prototype.hasOwnProperty.call(storage, bare_jid)) { 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); storage[bare_jid].push(resource);
} }
} else { } else {
...@@ -66,10 +55,9 @@ var xmppchat = (function (jarnxmpp, $, console) { ...@@ -66,10 +55,9 @@ var xmppchat = (function (jarnxmpp, $, console) {
methods.remove = function (bare_jid, resource) { methods.remove = function (bare_jid, resource) {
// Removes the resource for a user and returns the number of // Removes the resource for a user and returns the number of
// resources left over. // resources left over.
if (Object.prototype.hasOwnProperty.call(storage, bare_jid)) { if (_.has(storage, bare_jid)) {
if (resource in helpers.oc(storage[bare_jid])) { var idx = _.indexOf(storage[bare_jid], resource);
var idx = storage[bare_jid].indexOf(resource); if (idx !== -1) {
if (idx !== undefined) {
storage[bare_jid].splice(idx, 1); storage[bare_jid].splice(idx, 1);
if (storage[bare_jid].length === 0) { if (storage[bare_jid].length === 0) {
delete storage[bare_jid]; delete storage[bare_jid];
...@@ -80,7 +68,6 @@ var xmppchat = (function (jarnxmpp, $, console) { ...@@ -80,7 +68,6 @@ var xmppchat = (function (jarnxmpp, $, console) {
} }
} }
} }
}
return 0; return 0;
}; };
...@@ -91,7 +78,7 @@ var xmppchat = (function (jarnxmpp, $, console) { ...@@ -91,7 +78,7 @@ var xmppchat = (function (jarnxmpp, $, console) {
}; };
methods.getTotal = function () { methods.getTotal = function () {
return helpers.size(storage); return _.size(storage);
}; };
return methods; return methods;
...@@ -231,6 +218,10 @@ var xmppchat = (function (jarnxmpp, $, console) { ...@@ -231,6 +218,10 @@ var xmppchat = (function (jarnxmpp, $, console) {
ptype = $(presence).attr('type'), ptype = $(presence).attr('type'),
status = ''; status = '';
if (ob.isOwnUser(bare_jid)) {
return true;
}
if (ptype === 'subscribe') { if (ptype === 'subscribe') {
// User wants to subscribe to us. Always approve and // User wants to subscribe to us. Always approve and
// ask to subscribe to him // ask to subscribe to him
......
...@@ -8,9 +8,9 @@ xmppchat.UI = (function (xmppUI, $, console) { ...@@ -8,9 +8,9 @@ xmppchat.UI = (function (xmppUI, $, console) {
return xmppchat.base_url + call; 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; } 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 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')); li.append($('<a title="Click to remove this contact" href="#"></a>').addClass('remove-xmpp-contact'));
$('#xmpp-contacts').append(li); $('#xmpp-contacts').append(li);
...@@ -52,7 +52,7 @@ xmppchat.UI = (function (xmppUI, $, console) { ...@@ -52,7 +52,7 @@ xmppchat.UI = (function (xmppUI, $, console) {
existing_user_element.attr('class', status); existing_user_element.attr('class', status);
} else if ((status !== 'offline') && (status !== 'unavailable')) { } else if ((status !== 'offline') && (status !== 'unavailable')) {
xmppchat.Presence.getUserInfo(user_id, function (data) { 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 } else { // status is offline and the user isn't shown as online
return; return;
...@@ -212,7 +212,7 @@ xmppchat.UI = (function (xmppUI, $, console) { ...@@ -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.chats.push(jid);
} }
this.addChatToCookie(jid); this.addChatToCookie(jid);
...@@ -542,7 +542,7 @@ $(document).ready(function () { ...@@ -542,7 +542,7 @@ $(document).ready(function () {
// FIXME: We should store the contact name on the jabber server! // FIXME: We should store the contact name on the jabber server!
xmppchat.Presence.getUserInfo(user_id, function (data) { 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 () { ...@@ -599,10 +599,15 @@ $(document).ready(function () {
title: 'Are you sure you want to remove this contact?', title: 'Are you sure you want to remove this contact?',
dialogClass: 'remove-xmpp-contact-dialog', dialogClass: 'remove-xmpp-contact-dialog',
resizable: false, resizable: false,
width: 400, width: 200,
position: {
my: 'center',
at: 'center',
of: '#online-users-container'
},
modal: true, modal: true,
buttons: { buttons: {
"Yes, remove.": function() { "Remove": function() {
$( this ).dialog( "close" ); $( this ).dialog( "close" );
var jid = $(that).parent().attr('data-recipient'); var jid = $(that).parent().attr('data-recipient');
xmppchat.Roster.unsubscribe(jid); xmppchat.Roster.unsubscribe(jid);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment