Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ContactListView.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 model = this.model;
  125. let removeContact = this.onRemoveContact.bind(this);
  126. let changeAvatar = this.changeUserAvatar.bind(this);
  127. let displayNameChange = this.onDisplayNameChange.bind(this);
  128. APP.UI.addListener( UIEvents.TOGGLE_ROOM_LOCK,
  129. this.toggleLock.bind(this));
  130. APP.UI.addListener( UIEvents.CONTACT_ADDED,
  131. this.onAddContact.bind(this));
  132. APP.UI.addListener(UIEvents.CONTACT_REMOVED, removeContact);
  133. APP.UI.addListener(UIEvents.USER_AVATAR_CHANGED, changeAvatar);
  134. APP.UI.addListener(UIEvents.DISPLAY_NAME_CHANGED, displayNameChange);
  135. },
  136. /**
  137. * Updating the view according the model
  138. * @param type {String} type of change
  139. * @returns {Promise}
  140. */
  141. toggleLock() {
  142. let isLocked = this.model.isLocked();
  143. let showKey = isLocked ? this.lockKey : this.unlockKey;
  144. let hideKey = !isLocked ? this.lockKey : this.unlockKey;
  145. let showId = `contactList${showKey}`;
  146. let hideId = `contactList${hideKey}`;
  147. $(`#${showId}`).show();
  148. $(`#${hideId}`).hide();
  149. },
  150. /**
  151. * Indicates if the chat is currently visible.
  152. *
  153. * @return <tt>true</tt> if the chat is currently visible, <tt>false</tt> -
  154. * otherwise
  155. */
  156. isVisible () {
  157. return UIUtil.isVisible(document.getElementById("contactlist"));
  158. },
  159. /**
  160. * Handler for Adding a contact for the given id.
  161. * @param isLocal is an id for the local user.
  162. */
  163. onAddContact (data) {
  164. let { id, isLocal } = data;
  165. let contactlist = $('#contacts');
  166. let newContact = document.createElement('li');
  167. newContact.id = id;
  168. newContact.className = "clickable";
  169. newContact.onclick = (event) => {
  170. if (event.currentTarget.className === "clickable") {
  171. APP.UI.emitEvent(UIEvents.CONTACT_CLICKED, id);
  172. }
  173. };
  174. if (interfaceConfig.SHOW_CONTACTLIST_AVATARS)
  175. newContact.appendChild(createAvatar(id));
  176. newContact.appendChild(
  177. createDisplayNameParagraph(
  178. isLocal ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME : null,
  179. isLocal ? null : interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME));
  180. if (APP.conference.isLocalId(id)) {
  181. contactlist.prepend(newContact);
  182. } else {
  183. contactlist.append(newContact);
  184. }
  185. updateNumberOfParticipants(1);
  186. },
  187. /**
  188. * Handler for removing
  189. * a contact for the given id.
  190. */
  191. onRemoveContact (data) {
  192. let { id } = data;
  193. let contact = getContactEl(id);
  194. if (contact.length > 0) {
  195. contact.remove();
  196. updateNumberOfParticipants(-1);
  197. }
  198. },
  199. setClickable (id, isClickable) {
  200. getContactEl(id).toggleClass('clickable', isClickable);
  201. },
  202. /**
  203. * Changes display name of the user
  204. * defined by its id
  205. * @param data
  206. */
  207. onDisplayNameChange (data) {
  208. let { id, name } = data;
  209. if(!name)
  210. return;
  211. if (id === 'localVideoContainer') {
  212. id = APP.conference.getMyUserId();
  213. }
  214. let contactName = $(`#contacts #${id}>p`);
  215. if (contactName.text() !== name) {
  216. contactName.text(name);
  217. }
  218. },
  219. /**
  220. * Changes user avatar
  221. * @param data
  222. */
  223. changeUserAvatar (data) {
  224. let { id, avatar } = data;
  225. // set the avatar in the contact list
  226. let contact = $(`#${id}>img`);
  227. if (contact.length > 0) {
  228. contact.attr('src', avatar);
  229. }
  230. }
  231. };
  232. export default ContactListView;