Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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