You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContactList.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* global $, APP, interfaceConfig */
  2. import Avatar from '../../avatar/Avatar';
  3. import UIEvents from '../../../../service/UI/UIEvents';
  4. import UIUtil from '../../util/UIUtil';
  5. let numberOfContacts = 0;
  6. let notificationInterval;
  7. /**
  8. * Updates the number of participants in the contact list button and sets
  9. * the glow
  10. * @param delta indicates whether a new user has joined (1) or someone has
  11. * left(-1)
  12. */
  13. function updateNumberOfParticipants(delta) {
  14. numberOfContacts += delta;
  15. if (numberOfContacts <= 0) {
  16. console.error("Invalid number of participants: " + numberOfContacts);
  17. return;
  18. }
  19. $("#numberOfParticipants").text(numberOfContacts);
  20. $("#contacts_container>div.title").text(
  21. APP.translation.translateString("contactlist")
  22. + ' (' + numberOfContacts + ')');
  23. }
  24. /**
  25. * Creates the avatar element.
  26. *
  27. * @return {object} the newly created avatar element
  28. */
  29. function createAvatar(jid) {
  30. let avatar = document.createElement('img');
  31. avatar.className = "icon-avatar avatar";
  32. avatar.src = Avatar.getAvatarUrl(jid);
  33. return avatar;
  34. }
  35. /**
  36. * Creates the display name paragraph.
  37. *
  38. * @param displayName the display name to set
  39. */
  40. function createDisplayNameParagraph(key, displayName) {
  41. let p = document.createElement('p');
  42. if (displayName) {
  43. p.innerHTML = displayName;
  44. } else if(key) {
  45. p.setAttribute("data-i18n",key);
  46. p.innerHTML = APP.translation.translateString(key);
  47. }
  48. return p;
  49. }
  50. function stopGlowing(glower) {
  51. window.clearInterval(notificationInterval);
  52. notificationInterval = false;
  53. glower.removeClass('glowing');
  54. if (!ContactList.isVisible()) {
  55. glower.removeClass('active');
  56. }
  57. }
  58. function getContactEl (id) {
  59. return $(`#contacts>li[id="${id}"]`);
  60. }
  61. function contactElExists (id) {
  62. return getContactEl(id).length > 0;
  63. }
  64. /**
  65. * Contact list.
  66. */
  67. var ContactList = {
  68. init (emitter) {
  69. this.emitter = emitter;
  70. },
  71. /**
  72. * Indicates if the chat is currently visible.
  73. *
  74. * @return <tt>true</tt> if the chat is currently visible, <tt>false</tt> -
  75. * otherwise
  76. */
  77. isVisible () {
  78. return UIUtil.isVisible(document.getElementById("contactlist"));
  79. },
  80. /**
  81. * Adds a contact for the given id.
  82. * @param isLocal is an id for the local user.
  83. */
  84. addContact (id, isLocal) {
  85. let contactlist = $('#contacts');
  86. let newContact = document.createElement('li');
  87. newContact.id = id;
  88. newContact.className = "clickable";
  89. newContact.onclick = (event) => {
  90. if (event.currentTarget.className === "clickable") {
  91. this.emitter.emit(UIEvents.CONTACT_CLICKED, id);
  92. }
  93. };
  94. if (interfaceConfig.SHOW_CONTACTLIST_AVATARS)
  95. newContact.appendChild(createAvatar(id));
  96. newContact.appendChild(
  97. createDisplayNameParagraph(
  98. isLocal ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME : null,
  99. isLocal ? null : interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME));
  100. if (APP.conference.isLocalId(id)) {
  101. contactlist.prepend(newContact);
  102. } else {
  103. contactlist.append(newContact);
  104. }
  105. updateNumberOfParticipants(1);
  106. },
  107. /**
  108. * Removes a contact for the given id.
  109. *
  110. */
  111. removeContact (id) {
  112. let contact = getContactEl(id);
  113. if (contact.length > 0) {
  114. contact.remove();
  115. updateNumberOfParticipants(-1);
  116. }
  117. },
  118. setClickable (id, isClickable) {
  119. getContactEl(id).toggleClass('clickable', isClickable);
  120. },
  121. onDisplayNameChange (id, displayName) {
  122. if(!displayName)
  123. return;
  124. if (id === 'localVideoContainer') {
  125. id = APP.conference.getMyUserId();
  126. }
  127. let contactName = $(`#contacts #${id}>p`);
  128. if (contactName.text() !== displayName) {
  129. contactName.text(displayName);
  130. }
  131. },
  132. changeUserAvatar (id, avatarUrl) {
  133. // set the avatar in the contact list
  134. let contact = $(`#${id}>img`);
  135. if (contact.length > 0) {
  136. contact.attr('src', avatarUrl);
  137. }
  138. }
  139. };
  140. export default ContactList;