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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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(key, displayName) {
  37. var p = document.createElement('p');
  38. if(displayName)
  39. p.innerText = displayName;
  40. else if(key)
  41. {
  42. p.setAttribute("data-i18n",key);
  43. p.innerText = APP.translation.translateString(key);
  44. }
  45. return p;
  46. }
  47. function stopGlowing(glower) {
  48. window.clearInterval(notificationInterval);
  49. notificationInterval = false;
  50. glower.removeClass('glowing');
  51. if (!ContactList.isVisible()) {
  52. glower.removeClass('active');
  53. }
  54. }
  55. /**
  56. * Contact list.
  57. */
  58. var ContactList = {
  59. /**
  60. * Indicates if the chat is currently visible.
  61. *
  62. * @return <tt>true</tt> if the chat is currently visible, <tt>false</tt> -
  63. * otherwise
  64. */
  65. isVisible: function () {
  66. return $('#contactlist').is(":visible");
  67. },
  68. /**
  69. * Adds a contact for the given peerJid if such doesn't yet exist.
  70. *
  71. * @param peerJid the peerJid corresponding to the contact
  72. * @param id the user's email or userId used to get the user's avatar
  73. */
  74. ensureAddContact: function (peerJid, id) {
  75. var resourceJid = Strophe.getResourceFromJid(peerJid);
  76. var contact = $('#contacts>li[id="' + resourceJid + '"]');
  77. if (!contact || contact.length <= 0)
  78. ContactList.addContact(peerJid, id);
  79. },
  80. /**
  81. * Adds a contact for the given peer jid.
  82. *
  83. * @param peerJid the jid of the contact to add
  84. * @param id the email or userId of the user
  85. */
  86. addContact: function (peerJid, id) {
  87. var resourceJid = Strophe.getResourceFromJid(peerJid);
  88. var contactlist = $('#contacts');
  89. var newContact = document.createElement('li');
  90. newContact.id = resourceJid;
  91. newContact.className = "clickable";
  92. newContact.onclick = function (event) {
  93. if (event.currentTarget.className === "clickable") {
  94. $(ContactList).trigger('contactclicked', [peerJid]);
  95. }
  96. };
  97. newContact.appendChild(createAvatar(id));
  98. newContact.appendChild(createDisplayNameParagraph("participant"));
  99. if (resourceJid === APP.xmpp.myResource()) {
  100. contactlist.prepend(newContact);
  101. }
  102. else {
  103. contactlist.append(newContact);
  104. }
  105. updateNumberOfParticipants(1);
  106. },
  107. /**
  108. * Removes a contact for the given peer jid.
  109. *
  110. * @param peerJid the peerJid corresponding to the contact to remove
  111. */
  112. removeContact: function (peerJid) {
  113. var resourceJid = Strophe.getResourceFromJid(peerJid);
  114. var contact = $('#contacts>li[id="' + resourceJid + '"]');
  115. if (contact && contact.length > 0) {
  116. var contactlist = $('#contactlist>ul');
  117. contactlist.get(0).removeChild(contact.get(0));
  118. updateNumberOfParticipants(-1);
  119. }
  120. },
  121. setVisualNotification: function (show, stopGlowingIn) {
  122. var glower = $('#contactListButton');
  123. if (show && !notificationInterval) {
  124. notificationInterval = window.setInterval(function () {
  125. glower.toggleClass('active glowing');
  126. }, 800);
  127. }
  128. else if (!show && notificationInterval) {
  129. stopGlowing(glower);
  130. }
  131. if (stopGlowingIn) {
  132. setTimeout(function () {
  133. stopGlowing(glower);
  134. }, stopGlowingIn);
  135. }
  136. },
  137. setClickable: function (resourceJid, isClickable) {
  138. var contact = $('#contacts>li[id="' + resourceJid + '"]');
  139. if (isClickable) {
  140. contact.addClass('clickable');
  141. } else {
  142. contact.removeClass('clickable');
  143. }
  144. },
  145. onDisplayNameChange: function (peerJid, displayName) {
  146. if (peerJid === 'localVideoContainer')
  147. peerJid = APP.xmpp.myJid();
  148. var resourceJid = Strophe.getResourceFromJid(peerJid);
  149. var contactName = $('#contacts #' + resourceJid + '>p');
  150. if (contactName && displayName && displayName.length > 0)
  151. contactName.html(displayName);
  152. },
  153. userAvatarChanged: function (resourceJid, contactListUrl) {
  154. // set the avatar in the contact list
  155. var contact = $('#' + resourceJid + '>img');
  156. if (contact && contact.length > 0) {
  157. contact.get(0).src = contactListUrl;
  158. }
  159. }
  160. };
  161. module.exports = ContactList;