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.

ContactListView.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* global $, APP, interfaceConfig */
  2. import Avatar from '../../avatar/Avatar';
  3. import UIEvents from '../../../../service/UI/UIEvents';
  4. import UIUtil from '../../util/UIUtil';
  5. let numberOfContacts = 0;
  6. /**
  7. * Updates the number of participants in the contact list button and sets
  8. * the glow
  9. * @param delta indicates whether a new user has joined (1) or someone has
  10. * left(-1)
  11. */
  12. function updateNumberOfParticipants(delta) {
  13. numberOfContacts += delta;
  14. if (numberOfContacts <= 0) {
  15. console.error("Invalid number of participants: " + numberOfContacts);
  16. return;
  17. }
  18. $("#numberOfParticipants").text(numberOfContacts);
  19. $("#contacts_container>div.title").text(
  20. APP.translation.translateString("contactlist")
  21. + ' (' + numberOfContacts + ')');
  22. }
  23. /**
  24. * Creates the avatar element.
  25. *
  26. * @return {object} the newly created avatar element
  27. */
  28. function createAvatar(jid) {
  29. let avatar = document.createElement('img');
  30. avatar.className = "icon-avatar avatar";
  31. avatar.src = Avatar.getAvatarUrl(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. let p = document.createElement('p');
  41. if (displayName) {
  42. p.innerHTML = displayName;
  43. } else if(key) {
  44. p.setAttribute("data-i18n",key);
  45. p.innerHTML = APP.translation.translateString(key);
  46. }
  47. return p;
  48. }
  49. /**
  50. * Getter for current contact element
  51. * @param id
  52. * @returns {JQuery}
  53. */
  54. function getContactEl (id) {
  55. return $(`#contacts>li[id="${id}"]`);
  56. }
  57. /**
  58. * Contact list.
  59. */
  60. var ContactListView = {
  61. init (model) {
  62. this.model = model;
  63. this.lockKey = 'roomLocked';
  64. this.unlockKey = 'roomUnlocked';
  65. this.addInviteButton();
  66. this.registerListeners();
  67. this.toggleLock();
  68. },
  69. /**
  70. * Adds layout for invite button
  71. */
  72. addInviteButton() {
  73. let container = document.getElementById('contacts_container');
  74. let title = container.firstElementChild;
  75. let htmlLayout = this.getInviteButtonLayout();
  76. title.insertAdjacentHTML('afterend', htmlLayout);
  77. $(document).on('click', '#addParticipantsBtn', () => {
  78. APP.UI.emitEvent(UIEvents.INVITE_CLICKED);
  79. });
  80. },
  81. /**
  82. * Returns layout for invite button
  83. */
  84. getInviteButtonLayout() {
  85. let classes = 'button-control button-control_primary';
  86. classes += ' button-control_full-width';
  87. let key = 'addParticipants';
  88. let text = APP.translation.translateString(key);
  89. let lockedHtml = this.getLockDescriptionLayout(this.lockKey);
  90. let unlockedHtml = this.getLockDescriptionLayout(this.unlockKey);
  91. let html = (
  92. `<div class="sideToolbarBlock first">
  93. <button id="addParticipantsBtn"
  94. data-i18n="${key}"
  95. class="${classes}">
  96. ${text}
  97. </button>
  98. <div>
  99. ${lockedHtml}
  100. ${unlockedHtml}
  101. </div>
  102. </div>`);
  103. return html;
  104. },
  105. /**
  106. * Adds layout for lock description
  107. */
  108. getLockDescriptionLayout(key) {
  109. let classes = "input-control__hint input-control_full-width";
  110. let description = APP.translation.translateString(key);
  111. let padlockSuffix = '';
  112. if (key === this.lockKey) {
  113. padlockSuffix = '-locked';
  114. }
  115. return `<p id="contactList${key}" class="${classes}">
  116. <span class="icon-security${padlockSuffix}"></span>
  117. <span data-i18n="${key}">${description}</span>
  118. </p>`;
  119. },
  120. /**
  121. * Setup listeners
  122. */
  123. registerListeners() {
  124. let removeContact = this.onRemoveContact.bind(this);
  125. let changeAvatar = this.changeUserAvatar.bind(this);
  126. let displayNameChange = this.onDisplayNameChange.bind(this);
  127. APP.UI.addListener( UIEvents.TOGGLE_ROOM_LOCK,
  128. this.toggleLock.bind(this));
  129. APP.UI.addListener( UIEvents.CONTACT_ADDED,
  130. this.onAddContact.bind(this));
  131. APP.UI.addListener(UIEvents.CONTACT_REMOVED, removeContact);
  132. APP.UI.addListener(UIEvents.USER_AVATAR_CHANGED, changeAvatar);
  133. APP.UI.addListener(UIEvents.DISPLAY_NAME_CHANGED, displayNameChange);
  134. },
  135. /**
  136. * Updating the view according the model
  137. * @param type {String} type of change
  138. * @returns {Promise}
  139. */
  140. toggleLock() {
  141. let isLocked = this.model.isLocked();
  142. let showKey = isLocked ? this.lockKey : this.unlockKey;
  143. let hideKey = !isLocked ? this.lockKey : this.unlockKey;
  144. let showId = `contactList${showKey}`;
  145. let hideId = `contactList${hideKey}`;
  146. $(`#${showId}`).show();
  147. $(`#${hideId}`).hide();
  148. },
  149. /**
  150. * Indicates if the chat is currently visible.
  151. *
  152. * @return <tt>true</tt> if the chat is currently visible, <tt>false</tt> -
  153. * otherwise
  154. */
  155. isVisible () {
  156. return UIUtil.isVisible(document.getElementById("contactlist"));
  157. },
  158. /**
  159. * Handler for Adding a contact for the given id.
  160. * @param isLocal is an id for the local user.
  161. */
  162. onAddContact (data) {
  163. let { id, isLocal } = data;
  164. let contactlist = $('#contacts');
  165. let newContact = document.createElement('li');
  166. newContact.id = id;
  167. newContact.className = "clickable";
  168. newContact.onclick = (event) => {
  169. if (event.currentTarget.className === "clickable") {
  170. APP.UI.emitEvent(UIEvents.CONTACT_CLICKED, id);
  171. }
  172. };
  173. if (interfaceConfig.SHOW_CONTACTLIST_AVATARS)
  174. newContact.appendChild(createAvatar(id));
  175. newContact.appendChild(
  176. createDisplayNameParagraph(
  177. isLocal ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME : null,
  178. isLocal ? null : interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME));
  179. if (APP.conference.isLocalId(id)) {
  180. contactlist.prepend(newContact);
  181. } else {
  182. contactlist.append(newContact);
  183. }
  184. updateNumberOfParticipants(1);
  185. },
  186. /**
  187. * Handler for removing
  188. * a contact for the given id.
  189. */
  190. onRemoveContact (data) {
  191. let { id } = data;
  192. let contact = getContactEl(id);
  193. if (contact.length > 0) {
  194. contact.remove();
  195. updateNumberOfParticipants(-1);
  196. }
  197. },
  198. setClickable (id, isClickable) {
  199. getContactEl(id).toggleClass('clickable', isClickable);
  200. },
  201. /**
  202. * Changes display name of the user
  203. * defined by its id
  204. * @param data
  205. */
  206. onDisplayNameChange (data) {
  207. let { id, name } = data;
  208. if(!name)
  209. return;
  210. if (id === 'localVideoContainer') {
  211. id = APP.conference.getMyUserId();
  212. }
  213. let contactName = $(`#contacts #${id}>p`);
  214. if (contactName.text() !== name) {
  215. contactName.text(name);
  216. }
  217. },
  218. /**
  219. * Changes user avatar
  220. * @param data
  221. */
  222. changeUserAvatar (data) {
  223. let { id, avatar } = data;
  224. // set the avatar in the contact list
  225. let contact = $(`#${id}>img`);
  226. if (contact.length > 0) {
  227. contact.attr('src', avatar);
  228. }
  229. }
  230. };
  231. export default ContactListView;