2018-02-13 20:32:59 +01:00
|
|
|
const html = require('choo/html');
|
|
|
|
|
2018-02-16 21:56:53 +01:00
|
|
|
module.exports = function(file, state, emit) {
|
|
|
|
const setting = state.settingPassword;
|
|
|
|
const formClass = file.hasPassword
|
|
|
|
? 'passwordInput'
|
|
|
|
: 'passwordInput passwordInput--hidden';
|
|
|
|
const inputClass = setting ? 'input input--copied' : 'input input--noBtn';
|
|
|
|
const btnClass = setting
|
|
|
|
? 'inputBtn inputBtn--loading'
|
|
|
|
: 'inputBtn inputBtn--hidden';
|
|
|
|
const action = file.hasPassword
|
|
|
|
? state.translate('changePasswordButton')
|
|
|
|
: state.translate('addPasswordButton');
|
2018-02-13 20:32:59 +01:00
|
|
|
return html`
|
2018-02-16 21:56:53 +01:00
|
|
|
<div>
|
2018-02-13 20:32:59 +01:00
|
|
|
<form
|
2018-02-16 21:56:53 +01:00
|
|
|
class="${formClass}"
|
|
|
|
onsubmit=${setPassword}
|
2018-02-13 20:32:59 +01:00
|
|
|
data-no-csrf>
|
|
|
|
<input id="password-input"
|
2018-02-16 21:56:53 +01:00
|
|
|
${setting ? 'disabled' : ''}
|
|
|
|
class="${inputClass}"
|
2018-02-13 20:32:59 +01:00
|
|
|
maxlength="32"
|
|
|
|
autocomplete="off"
|
|
|
|
type="password"
|
|
|
|
oninput=${inputChanged}
|
2018-02-16 21:56:53 +01:00
|
|
|
placeholder="${
|
|
|
|
file.hasPassword
|
|
|
|
? passwordPlaceholder(file.password)
|
|
|
|
: state.translate('unlockInputPlaceholder')
|
|
|
|
}">
|
2018-02-13 20:32:59 +01:00
|
|
|
<input type="submit"
|
|
|
|
id="password-btn"
|
2018-02-16 21:56:53 +01:00
|
|
|
${setting ? 'disabled' : ''}
|
|
|
|
class="${btnClass}"
|
|
|
|
value="${setting ? '' : action}">
|
2018-02-13 20:32:59 +01:00
|
|
|
</form>
|
2018-02-16 21:56:53 +01:00
|
|
|
<div class="passwordInput__msg">${message(
|
|
|
|
setting,
|
|
|
|
file.hasPassword,
|
|
|
|
state.translate('passwordIsSet')
|
|
|
|
)}</div>
|
|
|
|
</div>
|
2018-02-13 20:32:59 +01:00
|
|
|
`;
|
|
|
|
|
|
|
|
function inputChanged() {
|
2018-02-16 21:56:53 +01:00
|
|
|
const pwdmsg = document.querySelector('.passwordInput__msg');
|
|
|
|
if (pwdmsg) {
|
|
|
|
pwdmsg.textContent = '';
|
|
|
|
}
|
2018-02-13 20:32:59 +01: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 21:56:53 +01:00
|
|
|
|
|
|
|
function setPassword(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const password = document.getElementById('password-input').value;
|
|
|
|
if (password.length > 0) {
|
|
|
|
emit('password', { password, file });
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-13 20:32:59 +01:00
|
|
|
};
|
2018-02-16 21:56:53 +01:00
|
|
|
|
|
|
|
function passwordPlaceholder(password) {
|
|
|
|
return password ? password.replace(/./g, '●') : '●●●●●●●●●●●●';
|
|
|
|
}
|
|
|
|
|
|
|
|
function message(setting, pwd, deflt) {
|
|
|
|
if (setting || !pwd) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return deflt;
|
|
|
|
}
|