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.

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