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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. var Avatar = require('../../avatar/Avatar');
  2. var numberOfContacts = 0;
  3. var notificationInterval;
  4. /**
  5. * Updates the number of participants in the contact list button and sets
  6. * the glow
  7. * @param delta indicates whether a new user has joined (1) or someone has
  8. * left(-1)
  9. */
  10. function updateNumberOfParticipants(delta) {
  11. numberOfContacts += delta;
  12. if (numberOfContacts === 1) {
  13. // when the user is alone we don't show the number of participants
  14. $("#numberOfParticipants").text('');
  15. ContactList.setVisualNotification(false);
  16. } else if (numberOfContacts > 1) {
  17. ContactList.setVisualNotification(!ContactList.isVisible());
  18. $("#numberOfParticipants").text(numberOfContacts);
  19. } else {
  20. console.error("Invalid number of participants: " + numberOfContacts);
  21. }
  22. }
  23. /**
  24. * Creates the avatar element.
  25. *
  26. * @return the newly created avatar element
  27. */
  28. function createAvatar(jid) {
  29. var avatar = document.createElement('img');
  30. avatar.className = "icon-avatar avatar";
  31. avatar.src = Avatar.getContactListUrl(jid);
  32. return avatar;
  33. }
  34. /**
  35. * Creates the display name paragraph.
  36. *
  37. * @param displayName the display name to set
  38. */
  39. function createDisplayNameParagraph(key, displayName) {
  40. var p = document.createElement('p');
  41. if(displayName)
  42. p.innerText = displayName;
  43. else if(key)
  44. {
  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 (peerJid, displayName) {
  147. if (peerJid === 'localVideoContainer')
  148. peerJid = APP.xmpp.myJid();
  149. var resourceJid = Strophe.getResourceFromJid(peerJid);
  150. var contactName = $('#contacts #' + resourceJid + '>p');
  151. if (contactName && displayName && displayName.length > 0)
  152. contactName.html(displayName);
  153. },
  154. userAvatarChanged: function (resourceJid, contactListUrl) {
  155. // set the avatar in the contact list
  156. var contact = $('#' + resourceJid + '>img');
  157. if (contact && contact.length > 0) {
  158. contact.get(0).src = contactListUrl;
  159. }
  160. }
  161. };
  162. module.exports = ContactList;