您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ContactList.js 4.0KB

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