Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ContactListView.js 7.7KB

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