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

RequirePasswordDialog.js 4.1KB

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