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

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