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.

ContactList.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 = $('#contactlist>ul>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 = $('#contactlist>ul');
  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. var clElement = contactlist.get(0);
  100. if (resourceJid === APP.xmpp.myResource()
  101. && $('#contactlist>ul .title')[0].nextSibling.nextSibling) {
  102. clElement.insertBefore(newContact,
  103. $('#contactlist>ul .title')[0].nextSibling.nextSibling);
  104. }
  105. else {
  106. clElement.appendChild(newContact);
  107. }
  108. updateNumberOfParticipants(1);
  109. },
  110. /**
  111. * Removes a contact for the given peer jid.
  112. *
  113. * @param peerJid the peerJid corresponding to the contact to remove
  114. */
  115. removeContact: function (peerJid) {
  116. var resourceJid = Strophe.getResourceFromJid(peerJid);
  117. var contact = $('#contactlist>ul>li[id="' + resourceJid + '"]');
  118. if (contact && contact.length > 0) {
  119. var contactlist = $('#contactlist>ul');
  120. contactlist.get(0).removeChild(contact.get(0));
  121. updateNumberOfParticipants(-1);
  122. }
  123. },
  124. setVisualNotification: function (show, stopGlowingIn) {
  125. var glower = $('#contactListButton');
  126. if (show && !notificationInterval) {
  127. notificationInterval = window.setInterval(function () {
  128. glower.toggleClass('active glowing');
  129. }, 800);
  130. }
  131. else if (!show && notificationInterval) {
  132. stopGlowing(glower);
  133. }
  134. if (stopGlowingIn) {
  135. setTimeout(function () {
  136. stopGlowing(glower);
  137. }, stopGlowingIn);
  138. }
  139. },
  140. setClickable: function (resourceJid, isClickable) {
  141. var contact = $('#contactlist>ul>li[id="' + resourceJid + '"]');
  142. if (isClickable) {
  143. contact.addClass('clickable');
  144. } else {
  145. contact.removeClass('clickable');
  146. }
  147. },
  148. onDisplayNameChange: function (peerJid, displayName) {
  149. if (peerJid === 'localVideoContainer')
  150. peerJid = APP.xmpp.myJid();
  151. var resourceJid = Strophe.getResourceFromJid(peerJid);
  152. var contactName = $('#contactlist #' + resourceJid + '>p');
  153. if (contactName && displayName && displayName.length > 0)
  154. contactName.html(displayName);
  155. }
  156. };
  157. module.exports = ContactList;