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

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