2015-07-28 21:52:32 +00:00
|
|
|
/* global $, APP, Strophe */
|
2015-07-20 17:32:04 +00:00
|
|
|
var Avatar = require('../../avatar/Avatar');
|
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
var numberOfContacts = 0;
|
|
|
|
var notificationInterval;
|
|
|
|
|
2014-08-22 15:37:11 +00:00
|
|
|
/**
|
2015-01-07 14:54:03 +00:00
|
|
|
* Updates the number of participants in the contact list button and sets
|
|
|
|
* the glow
|
|
|
|
* @param delta indicates whether a new user has joined (1) or someone has
|
|
|
|
* left(-1)
|
2014-08-22 15:37:11 +00:00
|
|
|
*/
|
2015-01-07 14:54:03 +00:00
|
|
|
function updateNumberOfParticipants(delta) {
|
2015-07-06 10:24:40 +00:00
|
|
|
numberOfContacts += delta;
|
|
|
|
if (numberOfContacts === 1) {
|
|
|
|
// when the user is alone we don't show the number of participants
|
2015-01-07 14:54:03 +00:00
|
|
|
$("#numberOfParticipants").text('');
|
2015-07-06 10:24:40 +00:00
|
|
|
ContactList.setVisualNotification(false);
|
|
|
|
} else if (numberOfContacts > 1) {
|
|
|
|
ContactList.setVisualNotification(!ContactList.isVisible());
|
2015-01-07 14:54:03 +00:00
|
|
|
$("#numberOfParticipants").text(numberOfContacts);
|
2015-07-06 10:24:40 +00:00
|
|
|
} else {
|
|
|
|
console.error("Invalid number of participants: " + numberOfContacts);
|
2015-01-07 14:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-02 09:30:46 +00:00
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
/**
|
|
|
|
* Creates the avatar element.
|
|
|
|
*
|
2015-07-28 21:52:32 +00:00
|
|
|
* @return {object} the newly created avatar element
|
2015-01-07 14:54:03 +00:00
|
|
|
*/
|
2015-07-20 17:32:04 +00:00
|
|
|
function createAvatar(jid) {
|
2015-01-07 14:54:03 +00:00
|
|
|
var avatar = document.createElement('img');
|
|
|
|
avatar.className = "icon-avatar avatar";
|
2015-07-20 17:32:04 +00:00
|
|
|
avatar.src = Avatar.getContactListUrl(jid);
|
2015-01-07 14:54:03 +00:00
|
|
|
|
|
|
|
return avatar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the display name paragraph.
|
|
|
|
*
|
|
|
|
* @param displayName the display name to set
|
|
|
|
*/
|
2015-03-09 15:50:13 +00:00
|
|
|
function createDisplayNameParagraph(key, displayName) {
|
2015-01-07 14:54:03 +00:00
|
|
|
var p = document.createElement('p');
|
2015-03-09 15:50:13 +00:00
|
|
|
if(displayName)
|
|
|
|
p.innerText = displayName;
|
2015-07-28 21:52:32 +00:00
|
|
|
else if(key) {
|
2015-03-09 15:50:13 +00:00
|
|
|
p.setAttribute("data-i18n",key);
|
|
|
|
p.innerText = APP.translation.translateString(key);
|
|
|
|
}
|
2015-01-07 14:54:03 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function stopGlowing(glower) {
|
|
|
|
window.clearInterval(notificationInterval);
|
|
|
|
notificationInterval = false;
|
|
|
|
glower.removeClass('glowing');
|
|
|
|
if (!ContactList.isVisible()) {
|
|
|
|
glower.removeClass('active');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Contact list.
|
|
|
|
*/
|
|
|
|
var ContactList = {
|
2014-08-22 15:37:11 +00:00
|
|
|
/**
|
|
|
|
* Indicates if the chat is currently visible.
|
|
|
|
*
|
|
|
|
* @return <tt>true</tt> if the chat is currently visible, <tt>false</tt> -
|
|
|
|
* otherwise
|
|
|
|
*/
|
2015-01-07 14:54:03 +00:00
|
|
|
isVisible: function () {
|
2014-08-22 15:37:11 +00:00
|
|
|
return $('#contactlist').is(":visible");
|
2015-01-07 14:54:03 +00:00
|
|
|
},
|
2014-08-22 15:37:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a contact for the given peerJid if such doesn't yet exist.
|
|
|
|
*
|
|
|
|
* @param peerJid the peerJid corresponding to the contact
|
|
|
|
*/
|
2015-07-20 17:32:04 +00:00
|
|
|
ensureAddContact: function (peerJid) {
|
2014-08-22 15:37:11 +00:00
|
|
|
var resourceJid = Strophe.getResourceFromJid(peerJid);
|
|
|
|
|
2015-06-19 13:45:39 +00:00
|
|
|
var contact = $('#contacts>li[id="' + resourceJid + '"]');
|
2014-08-22 15:37:11 +00:00
|
|
|
|
|
|
|
if (!contact || contact.length <= 0)
|
2015-07-20 17:32:04 +00:00
|
|
|
ContactList.addContact(peerJid);
|
2015-01-07 14:54:03 +00:00
|
|
|
},
|
2014-08-22 15:37:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a contact for the given peer jid.
|
|
|
|
*
|
|
|
|
* @param peerJid the jid of the contact to add
|
|
|
|
*/
|
2015-07-20 17:32:04 +00:00
|
|
|
addContact: function (peerJid) {
|
2014-08-22 15:37:11 +00:00
|
|
|
var resourceJid = Strophe.getResourceFromJid(peerJid);
|
|
|
|
|
2015-06-19 13:45:39 +00:00
|
|
|
var contactlist = $('#contacts');
|
2014-08-22 15:37:11 +00:00
|
|
|
|
|
|
|
var newContact = document.createElement('li');
|
|
|
|
newContact.id = resourceJid;
|
2014-10-06 10:29:48 +00:00
|
|
|
newContact.className = "clickable";
|
2015-01-07 14:54:03 +00:00
|
|
|
newContact.onclick = function (event) {
|
|
|
|
if (event.currentTarget.className === "clickable") {
|
2014-11-24 14:58:50 +00:00
|
|
|
$(ContactList).trigger('contactclicked', [peerJid]);
|
2014-10-06 10:29:48 +00:00
|
|
|
}
|
|
|
|
};
|
2014-08-22 15:37:11 +00:00
|
|
|
|
2015-07-20 17:32:04 +00:00
|
|
|
newContact.appendChild(createAvatar(peerJid));
|
2015-03-09 15:50:13 +00:00
|
|
|
newContact.appendChild(createDisplayNameParagraph("participant"));
|
2014-08-22 15:37:11 +00:00
|
|
|
|
2015-06-19 13:45:39 +00:00
|
|
|
if (resourceJid === APP.xmpp.myResource()) {
|
|
|
|
contactlist.prepend(newContact);
|
2014-08-22 15:37:11 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-06-19 13:45:39 +00:00
|
|
|
contactlist.append(newContact);
|
2014-08-22 15:37:11 +00:00
|
|
|
}
|
2014-10-02 09:30:46 +00:00
|
|
|
updateNumberOfParticipants(1);
|
2015-01-07 14:54:03 +00:00
|
|
|
},
|
2014-08-22 15:37:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a contact for the given peer jid.
|
|
|
|
*
|
|
|
|
* @param peerJid the peerJid corresponding to the contact to remove
|
|
|
|
*/
|
2015-01-07 14:54:03 +00:00
|
|
|
removeContact: function (peerJid) {
|
2014-08-22 15:37:11 +00:00
|
|
|
var resourceJid = Strophe.getResourceFromJid(peerJid);
|
|
|
|
|
2015-06-19 13:45:39 +00:00
|
|
|
var contact = $('#contacts>li[id="' + resourceJid + '"]');
|
2014-08-22 15:37:11 +00:00
|
|
|
|
|
|
|
if (contact && contact.length > 0) {
|
|
|
|
var contactlist = $('#contactlist>ul');
|
|
|
|
|
|
|
|
contactlist.get(0).removeChild(contact.get(0));
|
2014-10-02 09:30:46 +00:00
|
|
|
|
|
|
|
updateNumberOfParticipants(-1);
|
2014-08-22 15:37:11 +00:00
|
|
|
}
|
2015-01-07 14:54:03 +00:00
|
|
|
},
|
2014-08-22 15:37:11 +00:00
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
setVisualNotification: function (show, stopGlowingIn) {
|
2014-10-31 11:47:12 +00:00
|
|
|
var glower = $('#contactListButton');
|
2014-10-02 09:30:46 +00:00
|
|
|
|
2014-10-31 11:47:12 +00:00
|
|
|
if (show && !notificationInterval) {
|
|
|
|
notificationInterval = window.setInterval(function () {
|
|
|
|
glower.toggleClass('active glowing');
|
|
|
|
}, 800);
|
|
|
|
}
|
|
|
|
else if (!show && notificationInterval) {
|
2015-01-07 14:54:03 +00:00
|
|
|
stopGlowing(glower);
|
2014-10-02 09:30:46 +00:00
|
|
|
}
|
2015-01-07 14:54:03 +00:00
|
|
|
if (stopGlowingIn) {
|
|
|
|
setTimeout(function () {
|
|
|
|
stopGlowing(glower);
|
|
|
|
}, stopGlowingIn);
|
2014-08-22 15:37:11 +00:00
|
|
|
}
|
2015-01-07 14:54:03 +00:00
|
|
|
},
|
2014-08-22 15:37:11 +00:00
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
setClickable: function (resourceJid, isClickable) {
|
2015-06-19 13:45:39 +00:00
|
|
|
var contact = $('#contacts>li[id="' + resourceJid + '"]');
|
2015-01-07 14:54:03 +00:00
|
|
|
if (isClickable) {
|
2014-10-06 10:29:48 +00:00
|
|
|
contact.addClass('clickable');
|
|
|
|
} else {
|
|
|
|
contact.removeClass('clickable');
|
|
|
|
}
|
2015-01-19 09:20:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onDisplayNameChange: function (peerJid, displayName) {
|
|
|
|
if (peerJid === 'localVideoContainer')
|
2015-02-06 12:54:19 +00:00
|
|
|
peerJid = APP.xmpp.myJid();
|
2015-01-19 09:20:00 +00:00
|
|
|
|
|
|
|
var resourceJid = Strophe.getResourceFromJid(peerJid);
|
|
|
|
|
2015-06-19 13:45:39 +00:00
|
|
|
var contactName = $('#contacts #' + resourceJid + '>p');
|
2015-01-19 09:20:00 +00:00
|
|
|
|
|
|
|
if (contactName && displayName && displayName.length > 0)
|
|
|
|
contactName.html(displayName);
|
2015-06-29 14:24:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
userAvatarChanged: function (resourceJid, contactListUrl) {
|
|
|
|
// set the avatar in the contact list
|
|
|
|
var contact = $('#' + resourceJid + '>img');
|
|
|
|
if (contact && contact.length > 0) {
|
|
|
|
contact.get(0).src = contactListUrl;
|
|
|
|
}
|
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
}
|
|
|
|
};
|
2014-10-06 10:29:48 +00:00
|
|
|
|
2015-01-07 14:54:03 +00:00
|
|
|
module.exports = ContactList;
|