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.

RequirePasswordDialog.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* global APP */
  2. import UIUtil from '../util/UIUtil';
  3. /**
  4. * Show dialog which asks for required conference password.
  5. * @returns {Promise<string>} password or nothing if user canceled
  6. */
  7. export default class RequirePasswordDialog {
  8. constructor() {
  9. this.titleKey = 'dialog.passwordRequired';
  10. this.labelKey = 'dialog.passwordLabel';
  11. this.errorKey = 'dialog.incorrectPassword';
  12. this.errorId = 'passwordRequiredError';
  13. this.inputId = 'passwordRequiredInput';
  14. this.inputErrorClass = 'error';
  15. this.isOpened = false;
  16. }
  17. _registerListeners() {
  18. let el = document.getElementById(this.inputId);
  19. el.addEventListener('keypress', this._hideError.bind(this));
  20. }
  21. _getBodyMessage() {
  22. let passMsg = APP.translation.translateString("dialog.password");
  23. let label = APP.translation.translateString(this.labelKey);
  24. let error = APP.translation.translateString(this.errorKey);
  25. return (
  26. `<div class="input-control">
  27. <label class="input-control__label"
  28. data-i18n="${this.labelKey}">${label}</label>
  29. <input class="input-control__input" name="lockKey" type="text"
  30. data-i18n="[placeholder]dialog.password"
  31. placeholder="${passMsg}" autofocus id="${this.inputId}">
  32. <p class="input-control__hint input-control__hint_error hide"
  33. id="${this.errorId}"
  34. data-i18n="${this.errorKey}">${error}</p>
  35. </div>`
  36. );
  37. }
  38. askForPassword() {
  39. if (!this.isOpened) {
  40. return this.open();
  41. }
  42. return new Promise((resolve, reject) => {
  43. this.resolve = resolve;
  44. this.reject = reject;
  45. this._showError();
  46. });
  47. }
  48. open() {
  49. let { titleKey } = this;
  50. let msgString = this._getBodyMessage();
  51. return new Promise((resolve, reject) => {
  52. this.resolve = resolve;
  53. this.reject = reject;
  54. let submitFunction = this._submitFunction.bind(this);
  55. let closeFunction = this._closeFunction.bind(this);
  56. APP.UI.messageHandler.openTwoButtonDialog({
  57. titleKey,
  58. msgString,
  59. leftButtonKey: "dialog.Ok",
  60. submitFunction,
  61. closeFunction,
  62. focus: ':input:first'
  63. });
  64. this._registerListeners();
  65. this.isOpened = true;
  66. });
  67. }
  68. _submitFunction(e, v, m, f) {
  69. e.preventDefault();
  70. if (v && f.lockKey) {
  71. this.resolve(UIUtil.escapeHtml(f.lockKey));
  72. } else {
  73. this.reject(APP.UI.messageHandler.CANCEL);
  74. }
  75. }
  76. _closeFunction() {
  77. this._hideError();
  78. this.close();
  79. }
  80. _showError() {
  81. let className = this.inputErrorClass;
  82. document.getElementById(this.errorId).classList.remove('hide');
  83. document.getElementById(this.inputId).classList.add(className);
  84. }
  85. _hideError() {
  86. let className = this.inputErrorClass;
  87. document.getElementById(this.errorId).classList.add('hide');
  88. document.getElementById(this.inputId).classList.remove(className);
  89. }
  90. close() {
  91. APP.UI.messageHandler.closeDialog();
  92. this.isOpened = false;
  93. }
  94. }