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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  18. * Registering dialog listeners
  19. * @private
  20. */
  21. _registerListeners() {
  22. let el = document.getElementById(this.inputId);
  23. el.addEventListener('keypress', this._hideError.bind(this));
  24. }
  25. /**
  26. * Helper method returning dialog body
  27. * @returns {string}
  28. * @private
  29. */
  30. _getBodyMessage() {
  31. return (
  32. `<div class="input-control">
  33. <label class="input-control__label"
  34. data-i18n="${this.labelKey}"></label>
  35. <input class="input-control__input" name="lockKey" type="text"
  36. data-i18n="[placeholder]dialog.password"
  37. autofocus id="${this.inputId}">
  38. <p class="input-control__hint input-control__hint_error hide"
  39. id="${this.errorId}"
  40. data-i18n="${this.errorKey}"></p>
  41. </div>`
  42. );
  43. }
  44. /**
  45. * Asking for a password
  46. * @returns {Promise}
  47. */
  48. askForPassword() {
  49. if (!this.isOpened) {
  50. return this.open();
  51. }
  52. return new Promise((resolve, reject) => {
  53. this.resolve = resolve;
  54. this.reject = reject;
  55. this._showError();
  56. });
  57. }
  58. /**
  59. * Opens the dialog
  60. * @returns {Promise}
  61. */
  62. open() {
  63. let { titleKey } = this;
  64. let msgString = this._getBodyMessage();
  65. return new Promise((resolve, reject) => {
  66. this.resolve = resolve;
  67. this.reject = reject;
  68. let submitFunction = this._submitFunction.bind(this);
  69. let closeFunction = this._closeFunction.bind(this);
  70. APP.UI.messageHandler.openTwoButtonDialog({
  71. titleKey,
  72. msgString,
  73. leftButtonKey: "dialog.Ok",
  74. submitFunction,
  75. closeFunction,
  76. focus: ':input:first'
  77. });
  78. this._registerListeners();
  79. this.isOpened = true;
  80. });
  81. }
  82. /**
  83. * Submit dialog callback
  84. * @param e - event
  85. * @param v - value
  86. * @param m - message
  87. * @param f - form
  88. * @private
  89. */
  90. _submitFunction(e, v, m, f) {
  91. e.preventDefault();
  92. if (v && f.lockKey) {
  93. this.resolve(UIUtil.escapeHtml(f.lockKey));
  94. } else {
  95. this.reject(APP.UI.messageHandler.CANCEL);
  96. }
  97. }
  98. /**
  99. * Close dialog callback
  100. * @private
  101. */
  102. _closeFunction() {
  103. this._hideError();
  104. this.close();
  105. }
  106. /**
  107. * Method showing error hint
  108. * @private
  109. */
  110. _showError() {
  111. let className = this.inputErrorClass;
  112. let input = document.getElementById(this.inputId);
  113. document.getElementById(this.errorId).classList.remove('hide');
  114. input.classList.add(className);
  115. input.select();
  116. }
  117. /**
  118. * Method hiding error hint
  119. * @private
  120. */
  121. _hideError() {
  122. let className = this.inputErrorClass;
  123. document.getElementById(this.errorId).classList.add('hide');
  124. document.getElementById(this.inputId).classList.remove(className);
  125. }
  126. /**
  127. * Close the dialog
  128. */
  129. close() {
  130. APP.UI.messageHandler.closeDialog();
  131. this.isOpened = false;
  132. }
  133. }