2018-02-13 19:32:59 +00:00
|
|
|
const html = require('choo/html');
|
|
|
|
|
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-13 19:32:59 +00:00
|
|
|
maxlength="32"
|
|
|
|
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-16 20:56:53 +00:00
|
|
|
<div class="passwordInput__msg">${message(
|
2018-02-20 19:27:56 +00:00
|
|
|
loading,
|
|
|
|
pwd,
|
2018-02-16 20:56:53 +00:00
|
|
|
state.translate('passwordIsSet')
|
|
|
|
)}</div>
|
|
|
|
</div>
|
2018-02-13 19:32:59 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
function inputChanged() {
|
2018-02-16 20:56:53 +00:00
|
|
|
const pwdmsg = document.querySelector('.passwordInput__msg');
|
|
|
|
if (pwdmsg) {
|
|
|
|
pwdmsg.textContent = '';
|
|
|
|
}
|
2018-02-13 19:32:59 +00:00
|
|
|
const resetInput = document.getElementById('password-input');
|
|
|
|
const resetBtn = document.getElementById('password-btn');
|
|
|
|
if (resetInput.value.length > 0) {
|
|
|
|
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;
|
|
|
|
}
|