drop.chapril.org-firefoxsend/app/templates/passwordInput/index.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

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