Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ContactList.js 4.6KB

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