Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ContactListView.js 7.7KB

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