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

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