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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. this._dialog = 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. this._processInput(v, f);
  93. }
  94. /**
  95. * Processing input in dialog
  96. * @param v - value
  97. * @param f - form
  98. * @private
  99. */
  100. _processInput(v, f) {
  101. if (v && f.lockKey) {
  102. this.resolve(UIUtil.escapeHtml(f.lockKey));
  103. } else {
  104. this.reject(APP.UI.messageHandler.CANCEL);
  105. }
  106. }
  107. /**
  108. * Close dialog callback
  109. * @private
  110. */
  111. _closeFunction(e, v, m, f) {
  112. this._processInput(v, f);
  113. this._hideError();
  114. this.close();
  115. }
  116. /**
  117. * Method showing error hint
  118. * @private
  119. */
  120. _showError() {
  121. let className = this.inputErrorClass;
  122. let input = document.getElementById(this.inputId);
  123. document.getElementById(this.errorId).classList.remove('hide');
  124. input.classList.add(className);
  125. input.select();
  126. }
  127. /**
  128. * Method hiding error hint
  129. * @private
  130. */
  131. _hideError() {
  132. let className = this.inputErrorClass;
  133. document.getElementById(this.errorId).classList.add('hide');
  134. document.getElementById(this.inputId).classList.remove(className);
  135. }
  136. /**
  137. * Close the dialog
  138. */
  139. close() {
  140. if (this._dialog) {
  141. this._dialog.close();
  142. }
  143. this.isOpened = false;
  144. }
  145. }