您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ContactListView.js 7.2KB

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