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.5KB

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