2016-10-21 14:57:13 +00:00
|
|
|
/* global APP */
|
|
|
|
|
|
|
|
import UIUtil from '../util/UIUtil';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show dialog which asks for required conference password.
|
|
|
|
* @returns {Promise<string>} password or nothing if user canceled
|
|
|
|
*/
|
|
|
|
export default class RequirePasswordDialog {
|
|
|
|
constructor() {
|
|
|
|
this.titleKey = 'dialog.passwordRequired';
|
|
|
|
this.labelKey = 'dialog.passwordLabel';
|
|
|
|
this.errorKey = 'dialog.incorrectPassword';
|
|
|
|
this.errorId = 'passwordRequiredError';
|
|
|
|
this.inputId = 'passwordRequiredInput';
|
|
|
|
this.inputErrorClass = 'error';
|
|
|
|
this.isOpened = false;
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Registering dialog listeners
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
_registerListeners() {
|
|
|
|
let el = document.getElementById(this.inputId);
|
|
|
|
el.addEventListener('keypress', this._hideError.bind(this));
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Helper method returning dialog body
|
|
|
|
* @returns {string}
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
_getBodyMessage() {
|
|
|
|
return (
|
2016-11-09 16:46:58 +00:00
|
|
|
`<div class="form-control">
|
2016-10-21 14:57:13 +00:00
|
|
|
<label class="input-control__label"
|
2016-10-21 15:03:25 +00:00
|
|
|
data-i18n="${this.labelKey}"></label>
|
2016-11-09 16:46:58 +00:00
|
|
|
<input class="input-control__input input-control"
|
|
|
|
name="lockKey" type="text"
|
2016-10-21 14:57:13 +00:00
|
|
|
data-i18n="[placeholder]dialog.password"
|
2016-10-21 15:03:25 +00:00
|
|
|
autofocus id="${this.inputId}">
|
2016-11-09 16:46:58 +00:00
|
|
|
<p class="form-control__hint form-control__hint_error hide"
|
2016-10-21 14:57:13 +00:00
|
|
|
id="${this.errorId}"
|
2016-10-21 15:03:25 +00:00
|
|
|
data-i18n="${this.errorKey}"></p>
|
2016-10-21 14:57:13 +00:00
|
|
|
</div>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Asking for a password
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
askForPassword() {
|
|
|
|
if (!this.isOpened) {
|
|
|
|
return this.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.resolve = resolve;
|
|
|
|
this.reject = reject;
|
|
|
|
this._showError();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Opens the dialog
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
open() {
|
|
|
|
let { titleKey } = this;
|
|
|
|
let msgString = this._getBodyMessage();
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.resolve = resolve;
|
|
|
|
this.reject = reject;
|
|
|
|
let submitFunction = this._submitFunction.bind(this);
|
|
|
|
let closeFunction = this._closeFunction.bind(this);
|
|
|
|
|
2016-11-05 15:13:47 +00:00
|
|
|
this._dialog = APP.UI.messageHandler.openTwoButtonDialog({
|
2016-10-21 14:57:13 +00:00
|
|
|
titleKey,
|
|
|
|
msgString,
|
|
|
|
leftButtonKey: "dialog.Ok",
|
|
|
|
submitFunction,
|
|
|
|
closeFunction,
|
|
|
|
focus: ':input:first'
|
|
|
|
});
|
|
|
|
|
|
|
|
this._registerListeners();
|
|
|
|
this.isOpened = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Submit dialog callback
|
|
|
|
* @param e - event
|
|
|
|
* @param v - value
|
|
|
|
* @param m - message
|
|
|
|
* @param f - form
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
_submitFunction(e, v, m, f) {
|
|
|
|
e.preventDefault();
|
2016-10-24 13:45:19 +00:00
|
|
|
this._processInput(v, f);
|
|
|
|
}
|
2016-10-21 14:57:13 +00:00
|
|
|
|
2016-10-24 13:45:19 +00:00
|
|
|
/**
|
|
|
|
* Processing input in dialog
|
|
|
|
* @param v - value
|
|
|
|
* @param f - form
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_processInput(v, f) {
|
2016-10-21 14:57:13 +00:00
|
|
|
if (v && f.lockKey) {
|
|
|
|
this.resolve(UIUtil.escapeHtml(f.lockKey));
|
|
|
|
} else {
|
|
|
|
this.reject(APP.UI.messageHandler.CANCEL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Close dialog callback
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-24 13:45:19 +00:00
|
|
|
_closeFunction(e, v, m, f) {
|
|
|
|
this._processInput(v, f);
|
2016-10-21 14:57:13 +00:00
|
|
|
this._hideError();
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Method showing error hint
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
_showError() {
|
|
|
|
let className = this.inputErrorClass;
|
2016-10-24 09:12:44 +00:00
|
|
|
let input = document.getElementById(this.inputId);
|
2016-10-21 14:57:13 +00:00
|
|
|
document.getElementById(this.errorId).classList.remove('hide');
|
2016-10-24 09:12:44 +00:00
|
|
|
input.classList.add(className);
|
|
|
|
input.select();
|
2016-10-21 14:57:13 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Method hiding error hint
|
|
|
|
* @private
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
_hideError() {
|
|
|
|
let className = this.inputErrorClass;
|
|
|
|
document.getElementById(this.errorId).classList.add('hide');
|
|
|
|
document.getElementById(this.inputId).classList.remove(className);
|
|
|
|
}
|
|
|
|
|
2016-10-21 15:14:12 +00:00
|
|
|
/**
|
|
|
|
* Close the dialog
|
|
|
|
*/
|
2016-10-21 14:57:13 +00:00
|
|
|
close() {
|
2016-11-05 15:13:47 +00:00
|
|
|
if (this._dialog) {
|
|
|
|
this._dialog.close();
|
|
|
|
}
|
2016-10-21 14:57:13 +00:00
|
|
|
this.isOpened = false;
|
|
|
|
}
|
|
|
|
}
|