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.

InviteDialogView.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* global $, APP, JitsiMeetJS */
  2. /**
  3. * Substate for password
  4. * @type {{LOCKED: string, UNLOCKED: string}}
  5. */
  6. const States = {
  7. LOCKED: 'locked',
  8. UNLOCKED: 'unlocked'
  9. };
  10. /**
  11. * Class representing view for Invite dialog
  12. * @class InviteDialogView
  13. */
  14. export default class InviteDialogView {
  15. constructor(model) {
  16. this.unlockHint = "unlockHint";
  17. this.lockHint = "lockHint";
  18. this.model = model;
  19. if (this.model.inviteUrl === null) {
  20. this.inviteAttributes = `data-i18n="[value]inviteUrlDefaultMsg"`;
  21. } else {
  22. this.inviteAttributes
  23. = `value="${this.model.getEncodedInviteUrl()}"`;
  24. }
  25. this.initDialog();
  26. }
  27. /**
  28. * Initialization of dialog property
  29. */
  30. initDialog() {
  31. let dialog = {};
  32. dialog.closeFunction = this.closeFunction.bind(this);
  33. dialog.submitFunction = this.submitFunction.bind(this);
  34. dialog.loadedFunction = this.loadedFunction.bind(this);
  35. dialog.titleKey = "dialog.shareLink";
  36. this.dialog = dialog;
  37. this.dialog.states = this.getStates();
  38. }
  39. /**
  40. * Event handler for submitting dialog
  41. * @param e
  42. * @param v
  43. */
  44. submitFunction(e, v) {
  45. if (v && this.model.inviteUrl) {
  46. JitsiMeetJS.analytics.sendEvent('toolbar.invite.button');
  47. } else {
  48. JitsiMeetJS.analytics.sendEvent('toolbar.invite.cancel');
  49. }
  50. }
  51. /**
  52. * Event handler for load dialog
  53. * @param event
  54. */
  55. loadedFunction(event) {
  56. if (this.model.inviteUrl) {
  57. document.getElementById('inviteLinkRef').select();
  58. } else {
  59. if (event && event.target) {
  60. $(event.target).find('button[value=true]')
  61. .prop('disabled', true);
  62. }
  63. }
  64. }
  65. /**
  66. * Event handler for closing dialog
  67. * @param e
  68. * @param v
  69. * @param m
  70. * @param f
  71. */
  72. closeFunction(e, v, m, f) {
  73. $(document).off('click', '.copyInviteLink', this.copyToClipboard);
  74. if(!v && !m && !f)
  75. JitsiMeetJS.analytics.sendEvent('toolbar.invite.close');
  76. }
  77. /**
  78. * Returns all states of the dialog
  79. * @returns {{}}
  80. */
  81. getStates() {
  82. let {
  83. titleKey
  84. } = this.dialog;
  85. let doneMsg = APP.translation.generateTranslationHTML('dialog.done');
  86. let states = {};
  87. let buttons = {};
  88. buttons[`${doneMsg}`] = true;
  89. states[States.UNLOCKED] = {
  90. titleKey,
  91. html: this.getShareLinkBlock() + this.getAddPasswordBlock(),
  92. buttons
  93. };
  94. states[States.LOCKED] = {
  95. titleKey,
  96. html: this.getShareLinkBlock() + this.getPasswordBlock(),
  97. buttons
  98. };
  99. return states;
  100. }
  101. /**
  102. * Layout for invite link input
  103. * @returns {string}
  104. */
  105. getShareLinkBlock() {
  106. let classes = 'button-control button-control_light copyInviteLink';
  107. return (
  108. `<div class="input-control">
  109. <label class="input-control__label" for="inviteLinkRef"
  110. data-i18n="${this.dialog.titleKey}"></label>
  111. <div class="input-control__container">
  112. <input class="input-control__input inviteLink"
  113. id="inviteLinkRef" type="text"
  114. ${this.inviteAttributes} readonly>
  115. <button data-i18n="dialog.copy" class="${classes}"></button>
  116. </div>
  117. <p class="input-control__hint ${this.lockHint}">
  118. <span class="icon-security-locked"></span>
  119. <span data-i18n="dialog.roomLocked"></span>
  120. </p>
  121. <p class="input-control__hint ${this.unlockHint}">
  122. <span class="icon-security"></span>
  123. <span data-i18n="roomUnlocked"></span>
  124. </p>
  125. </div>`
  126. );
  127. }
  128. /**
  129. * Layout for adding password input
  130. * @returns {string}
  131. */
  132. getAddPasswordBlock() {
  133. let html;
  134. if (this.model.isModerator) {
  135. html = (`
  136. <div class="input-control">
  137. <label class="input-control__label"
  138. for="newPasswordInput" data-i18n="dialog.addPassword">
  139. </label>
  140. <div class="input-control__container">
  141. <input class="input-control__input" id="newPasswordInput"
  142. type="text" data-i18n="dialog.createPassword">
  143. <button id="addPasswordBtn" id="inviteDialogAddPassword"
  144. disabled data-i18n="dialog.add"
  145. class="button-control button-control_light">
  146. </button>
  147. </div>
  148. </div>
  149. `);
  150. } else {
  151. html = '';
  152. }
  153. return html;
  154. }
  155. /**
  156. * Layout for password (when room is locked)
  157. * @returns {string}
  158. */
  159. getPasswordBlock() {
  160. let { password, isModerator } = this.model;
  161. if (isModerator) {
  162. return (`
  163. <div class="input-control">
  164. <label class="input-control__label"
  165. data-i18n="dialog.passwordLabel"></label>
  166. <div class="input-control__container">
  167. <p>
  168. <span class="input-control__text"
  169. data-i18n="dialog.currentPassword"></span>
  170. <span id="inviteDialogPassword"
  171. class="input-control__em">
  172. ${password}
  173. </span>
  174. </p>
  175. <a class="link input-control__right"
  176. id="inviteDialogRemovePassword"
  177. data-i18n="dialog.removePassword"></a>
  178. </div>
  179. </div>
  180. `);
  181. } else {
  182. return (`
  183. <div class="input-control">
  184. <p>A participant protected this call with a password.</p>
  185. </div>
  186. `);
  187. }
  188. }
  189. /**
  190. * Opening the dialog
  191. */
  192. open() {
  193. let {
  194. submitFunction,
  195. loadedFunction,
  196. closeFunction
  197. } = this.dialog;
  198. let states = this.getStates();
  199. let initial = this.model.roomLocked ? States.LOCKED : States.UNLOCKED;
  200. APP.UI.messageHandler.openDialogWithStates(states, {
  201. submit: submitFunction,
  202. loaded: loadedFunction,
  203. close: closeFunction,
  204. size: 'medium'
  205. });
  206. $.prompt.goToState(initial);
  207. this.registerListeners();
  208. this.updateView();
  209. }
  210. /**
  211. * Setting event handlers
  212. * used in dialog
  213. */
  214. registerListeners() {
  215. const ENTER_KEY = 13;
  216. let addPasswordBtn = '#addPasswordBtn';
  217. let copyInviteLink = '.copyInviteLink';
  218. let newPasswordInput = '#newPasswordInput';
  219. let removePassword = '#inviteDialogRemovePassword';
  220. $(document).on('click', copyInviteLink, this.copyToClipboard);
  221. $(removePassword).on('click', () => {
  222. this.model.setRoomUnlocked();
  223. });
  224. let boundSetPassword = this.setPassword.bind(this);
  225. $(document).on('click', addPasswordBtn, boundSetPassword);
  226. let boundDisablePass = this.disableAddPassIfInputEmpty.bind(this);
  227. $(document).on('keypress', newPasswordInput, boundDisablePass);
  228. // We need to handle keydown event because impromptu
  229. // is listening to it too for closing the dialog
  230. $(newPasswordInput).on('keydown', (e) => {
  231. if (e.keyCode === ENTER_KEY) {
  232. e.stopPropagation();
  233. this.setPassword();
  234. }
  235. });
  236. }
  237. setPassword() {
  238. let $passInput = $('#newPasswordInput');
  239. let newPass = $passInput.val();
  240. if(newPass) {
  241. this.model.setRoomLocked(newPass);
  242. }
  243. }
  244. /**
  245. * Checking input and if it's empty then
  246. * disable add pass button
  247. */
  248. disableAddPassIfInputEmpty() {
  249. let $passInput = $('#newPasswordInput');
  250. let $addPassBtn = $('#addPasswordBtn');
  251. if(!$passInput.val()) {
  252. $addPassBtn.prop('disabled', true);
  253. } else {
  254. $addPassBtn.prop('disabled', false);
  255. }
  256. }
  257. /**
  258. * Copying text to clipboard
  259. */
  260. copyToClipboard() {
  261. $('.inviteLink').each(function () {
  262. let $el = $(this).closest('.jqistate');
  263. // TOFIX: We can select only visible elements
  264. if($el.css('display') === 'block') {
  265. this.select();
  266. try {
  267. document.execCommand('copy');
  268. this.blur();
  269. }
  270. catch (err) {
  271. console.error('error when copy the text');
  272. }
  273. }
  274. });
  275. }
  276. /**
  277. * Method syncing the view and the model
  278. */
  279. updateView() {
  280. let pass = this.model.getPassword();
  281. if (this.model.getRoomLocker().lockedElsewhere || !pass)
  282. $('#inviteDialogPassword').attr("data-i18n", "passwordSetRemotely");
  283. else
  284. $('#inviteDialogPassword').text(pass);
  285. // if we are not moderator we cannot remove password
  286. if (APP.conference.isModerator)
  287. $('#inviteDialogRemovePassword').show();
  288. else
  289. $('#inviteDialogRemovePassword').hide();
  290. $('#newPasswordInput').val('');
  291. this.disableAddPassIfInputEmpty();
  292. this.updateInviteLink();
  293. $.prompt.goToState(
  294. (this.model.isLocked())
  295. ? States.LOCKED
  296. : States.UNLOCKED);
  297. let roomLocked = `.${this.lockHint}`;
  298. let roomUnlocked = `.${this.unlockHint}`;
  299. let showDesc = this.model.isLocked() ? roomLocked : roomUnlocked;
  300. let hideDesc = !this.model.isLocked() ? roomLocked : roomUnlocked;
  301. $(showDesc).show();
  302. $(hideDesc).hide();
  303. }
  304. /**
  305. * Updates invite link
  306. */
  307. updateInviteLink() {
  308. // If the invite dialog has been already opened we update the
  309. // information.
  310. let inviteLink = document.querySelectorAll('.inviteLink');
  311. let list = Array.from(inviteLink);
  312. list.forEach((inviteLink) => {
  313. inviteLink.value = this.model.inviteUrl;
  314. inviteLink.select();
  315. });
  316. $('#inviteLinkRef').parent()
  317. .find('button[value=true]').prop('disabled', false);
  318. }
  319. }