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

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