2018-02-13 19:32:59 +00:00
|
|
|
const html = require('choo/html');
|
2018-02-20 19:46:27 +00:00
|
|
|
const MAX_LENGTH = 32;
|
2018-02-13 19:32:59 +00:00
|
|
|
|
2018-02-16 20:56:53 +00:00
|
|
|
module.exports = function(file, state, emit) {
|
2018-02-20 19:27:56 +00:00
|
|
|
const loading = state.settingPassword;
|
|
|
|
const pwd = file.hasPassword;
|
|
|
|
const formClass = pwd
|
2018-02-16 20:56:53 +00:00
|
|
|
? 'passwordInput'
|
|
|
|
: 'passwordInput passwordInput--hidden';
|
2018-02-20 19:27:56 +00:00
|
|
|
const inputClass = loading || pwd ? 'input' : 'input input--noBtn';
|
2018-02-21 17:54:53 +00:00
|
|
|
let btnClass = 'inputBtn inputBtn--password inputBtn--hidden';
|
2018-02-20 19:27:56 +00:00
|
|
|
if (loading) {
|
2018-02-21 17:54:53 +00:00
|
|
|
btnClass = 'inputBtn inputBtn--password inputBtn--loading';
|
2018-02-20 19:27:56 +00:00
|
|
|
} else if (pwd) {
|
2018-02-21 17:54:53 +00:00
|
|
|
btnClass = 'inputBtn inputBtn--password';
|
2018-02-20 19:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const action = pwd
|
2018-02-16 20:56:53 +00:00
|
|
|
? state.translate('changePasswordButton')
|
|
|
|
: state.translate('addPasswordButton');
|
2018-02-13 19:32:59 +00:00
|
|
|
return html`
|
2018-02-16 20:56:53 +00:00
|
|
|
<div>
|
2018-02-13 19:32:59 +00:00
|
|
|
<form
|
2018-02-16 20:56:53 +00:00
|
|
|
class="${formClass}"
|
|
|
|
onsubmit=${setPassword}
|
2018-02-13 19:32:59 +00:00
|
|
|
data-no-csrf>
|
|
|
|
<input id="password-input"
|
2018-02-20 19:27:56 +00:00
|
|
|
${loading ? 'disabled' : ''}
|
2018-02-16 20:56:53 +00:00
|
|
|
class="${inputClass}"
|
2018-02-20 19:46:27 +00:00
|
|
|
maxlength="${MAX_LENGTH}"
|
2018-02-13 19:32:59 +00:00
|
|
|
autocomplete="off"
|
|
|
|
type="password"
|
|
|
|
oninput=${inputChanged}
|
2018-02-21 17:54:53 +00:00
|
|
|
onfocus=${focused}
|
2018-02-16 20:56:53 +00:00
|
|
|
placeholder="${
|
2018-02-20 19:27:56 +00:00
|
|
|
pwd
|
2018-02-16 20:56:53 +00:00
|
|
|
? passwordPlaceholder(file.password)
|
|
|
|
: state.translate('unlockInputPlaceholder')
|
|
|
|
}">
|
2018-02-13 19:32:59 +00:00
|
|
|
<input type="submit"
|
|
|
|
id="password-btn"
|
2018-02-20 19:27:56 +00:00
|
|
|
${loading ? 'disabled' : ''}
|
2018-02-16 20:56:53 +00:00
|
|
|
class="${btnClass}"
|
2018-02-20 19:27:56 +00:00
|
|
|
value="${loading ? '' : action}">
|
2018-02-13 19:32:59 +00:00
|
|
|
</form>
|
2018-02-20 19:46:27 +00:00
|
|
|
<label
|
|
|
|
class="passwordInput__msg"
|
|
|
|
for="password-input">${message(
|
|
|
|
loading,
|
|
|
|
pwd,
|
|
|
|
state.translate('passwordIsSet')
|
|
|
|
)}</label>
|
|
|
|
</div>`;
|
2018-02-13 19:32:59 +00:00
|
|
|
|
|
|
|
function inputChanged() {
|
2018-02-20 19:46:27 +00:00
|
|
|
const resetInput = document.getElementById('password-input');
|
|
|
|
const resetBtn = document.getElementById('password-btn');
|
2018-02-16 20:56:53 +00:00
|
|
|
const pwdmsg = document.querySelector('.passwordInput__msg');
|
2018-02-20 19:46:27 +00:00
|
|
|
const length = resetInput.value.length;
|
|
|
|
|
|
|
|
if (length === MAX_LENGTH) {
|
|
|
|
pwdmsg.textContent = state.translate('maxPasswordLength', {
|
|
|
|
length: MAX_LENGTH
|
|
|
|
});
|
|
|
|
} else {
|
2018-02-16 20:56:53 +00:00
|
|
|
pwdmsg.textContent = '';
|
|
|
|
}
|
2018-02-20 19:46:27 +00:00
|
|
|
if (length > 0) {
|
2018-02-13 19:32:59 +00:00
|
|
|
resetBtn.classList.remove('inputBtn--hidden');
|
|
|
|
resetInput.classList.remove('input--noBtn');
|
|
|
|
} else {
|
|
|
|
resetBtn.classList.add('inputBtn--hidden');
|
|
|
|
resetInput.classList.add('input--noBtn');
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 20:56:53 +00:00
|
|
|
|
2018-02-21 17:54:53 +00:00
|
|
|
function focused(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const el = document.getElementById('password-input');
|
|
|
|
if (el.placeholder !== state.translate('unlockInputPlaceholder')) {
|
|
|
|
el.placeholder = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 20:56:53 +00:00
|
|
|
function setPassword(event) {
|
|
|
|
event.preventDefault();
|
2018-02-21 17:54:53 +00:00
|
|
|
const el = document.getElementById('password-input');
|
|
|
|
const password = el.value;
|
2018-02-16 20:56:53 +00:00
|
|
|
if (password.length > 0) {
|
|
|
|
emit('password', { password, file });
|
2018-02-21 17:54:53 +00:00
|
|
|
} else {
|
|
|
|
el.focus();
|
2018-02-16 20:56:53 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-13 19:32:59 +00:00
|
|
|
};
|
2018-02-16 20:56:53 +00:00
|
|
|
|
|
|
|
function passwordPlaceholder(password) {
|
|
|
|
return password ? password.replace(/./g, '●') : '●●●●●●●●●●●●';
|
|
|
|
}
|
|
|
|
|
2018-02-20 19:27:56 +00:00
|
|
|
function message(loading, pwd, deflt) {
|
|
|
|
if (loading || !pwd) {
|
2018-02-16 20:56:53 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return deflt;
|
|
|
|
}
|