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.3KB

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