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.

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