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

InviteDialogView.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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"
  143. data-i18n="[placeholder]dialog.createPassword">
  144. <button id="addPasswordBtn" id="inviteDialogAddPassword"
  145. disabled data-i18n="dialog.add"
  146. class="button-control button-control_light">
  147. </button>
  148. </div>
  149. </div>
  150. `);
  151. } else {
  152. html = '';
  153. }
  154. return html;
  155. }
  156. /**
  157. * Layout for password (when room is locked)
  158. * @returns {string}
  159. */
  160. getPasswordBlock() {
  161. let { password, isModerator } = this.model;
  162. if (isModerator) {
  163. return (`
  164. <div class="input-control">
  165. <label class="input-control__label"
  166. data-i18n="dialog.passwordLabel"></label>
  167. <div class="input-control__container">
  168. <p>
  169. <span class="input-control__text"
  170. data-i18n="dialog.currentPassword"></span>
  171. <span id="inviteDialogPassword"
  172. class="input-control__em">
  173. ${password}
  174. </span>
  175. </p>
  176. <a class="link input-control__right"
  177. id="inviteDialogRemovePassword"
  178. data-i18n="dialog.removePassword"></a>
  179. </div>
  180. </div>
  181. `);
  182. } else {
  183. return (`
  184. <div class="input-control">
  185. <p>A participant protected this call with a password.</p>
  186. </div>
  187. `);
  188. }
  189. }
  190. /**
  191. * Opening the dialog
  192. */
  193. open() {
  194. let {
  195. submitFunction,
  196. loadedFunction,
  197. closeFunction
  198. } = this.dialog;
  199. let states = this.getStates();
  200. let initial = this.model.roomLocked ? States.LOCKED : States.UNLOCKED;
  201. APP.UI.messageHandler.openDialogWithStates(states, {
  202. submit: submitFunction,
  203. loaded: loadedFunction,
  204. close: closeFunction,
  205. size: 'medium'
  206. });
  207. $.prompt.goToState(initial);
  208. this.registerListeners();
  209. this.updateView();
  210. }
  211. /**
  212. * Setting event handlers
  213. * used in dialog
  214. */
  215. registerListeners() {
  216. const ENTER_KEY = 13;
  217. let addPasswordBtn = '#addPasswordBtn';
  218. let copyInviteLink = '.copyInviteLink';
  219. let newPasswordInput = '#newPasswordInput';
  220. let removePassword = '#inviteDialogRemovePassword';
  221. $(document).on('click', copyInviteLink, this.copyToClipboard);
  222. $(removePassword).on('click', () => {
  223. this.model.setRoomUnlocked();
  224. });
  225. let boundSetPassword = this._setPassword.bind(this);
  226. $(document).on('click', addPasswordBtn, boundSetPassword);
  227. let boundDisablePass = this.disableAddPassIfInputEmpty.bind(this);
  228. $(document).on('keypress', newPasswordInput, boundDisablePass);
  229. // We need to handle keydown event because impromptu
  230. // is listening to it too for closing the dialog
  231. $(newPasswordInput).on('keydown', (e) => {
  232. if (e.keyCode === ENTER_KEY) {
  233. e.stopPropagation();
  234. this._setPassword();
  235. }
  236. });
  237. }
  238. /**
  239. * Marking room as locked
  240. * @private
  241. */
  242. _setPassword() {
  243. let $passInput = $('#newPasswordInput');
  244. let newPass = $passInput.val();
  245. if(newPass) {
  246. this.model.setRoomLocked(newPass);
  247. }
  248. }
  249. /**
  250. * Checking input and if it's empty then
  251. * disable add pass button
  252. */
  253. disableAddPassIfInputEmpty() {
  254. let $passInput = $('#newPasswordInput');
  255. let $addPassBtn = $('#addPasswordBtn');
  256. if(!$passInput.val()) {
  257. $addPassBtn.prop('disabled', true);
  258. } else {
  259. $addPassBtn.prop('disabled', false);
  260. }
  261. }
  262. /**
  263. * Copying text to clipboard
  264. */
  265. copyToClipboard() {
  266. $('.inviteLink').each(function () {
  267. let $el = $(this).closest('.jqistate');
  268. // TOFIX: We can select only visible elements
  269. if($el.css('display') === 'block') {
  270. this.select();
  271. try {
  272. document.execCommand('copy');
  273. this.blur();
  274. }
  275. catch (err) {
  276. console.error('error when copy the text');
  277. }
  278. }
  279. });
  280. }
  281. /**
  282. * Method syncing the view and the model
  283. */
  284. updateView() {
  285. let pass = this.model.getPassword();
  286. if (this.model.getRoomLocker().lockedElsewhere || !pass)
  287. $('#inviteDialogPassword').attr("data-i18n", "passwordSetRemotely");
  288. else
  289. $('#inviteDialogPassword').text(pass);
  290. // if we are not moderator we cannot remove password
  291. if (APP.conference.isModerator)
  292. $('#inviteDialogRemovePassword').show();
  293. else
  294. $('#inviteDialogRemovePassword').hide();
  295. $('#newPasswordInput').val('');
  296. this.disableAddPassIfInputEmpty();
  297. this.updateInviteLink();
  298. $.prompt.goToState(
  299. (this.model.isLocked())
  300. ? States.LOCKED
  301. : States.UNLOCKED);
  302. let roomLocked = `.${this.lockHint}`;
  303. let roomUnlocked = `.${this.unlockHint}`;
  304. let showDesc = this.model.isLocked() ? roomLocked : roomUnlocked;
  305. let hideDesc = !this.model.isLocked() ? roomLocked : roomUnlocked;
  306. $(showDesc).show();
  307. $(hideDesc).hide();
  308. }
  309. /**
  310. * Updates invite link
  311. */
  312. updateInviteLink() {
  313. // If the invite dialog has been already opened we update the
  314. // information.
  315. let inviteLink = document.querySelectorAll('.inviteLink');
  316. let list = Array.from(inviteLink);
  317. list.forEach((inviteLink) => {
  318. inviteLink.value = this.model.inviteUrl;
  319. inviteLink.select();
  320. });
  321. $('#inviteLinkRef').parent()
  322. .find('button[value=true]').prop('disabled', false);
  323. }
  324. }