2017-08-31 18:43:36 +02:00
|
|
|
const html = require('choo/html');
|
|
|
|
|
|
|
|
module.exports = function(state, emit) {
|
|
|
|
const fileInfo = state.fileInfo;
|
2018-02-20 21:21:00 +01:00
|
|
|
const invalid = fileInfo.password === null;
|
|
|
|
const label = invalid
|
|
|
|
? html`
|
|
|
|
<label class="error" for="password-input">
|
2018-01-24 19:23:13 +01:00
|
|
|
${state.translate('passwordTryAgain')}
|
|
|
|
</label>`
|
2018-02-20 21:21:00 +01:00
|
|
|
: html`
|
2018-02-13 20:32:59 +01:00
|
|
|
<label for="password-input">
|
2017-08-31 18:43:36 +02:00
|
|
|
${state.translate('unlockInputLabel')}
|
|
|
|
</label>`;
|
2018-02-20 21:21:00 +01:00
|
|
|
const inputClass = invalid
|
|
|
|
? 'input input--noBtn input--error'
|
|
|
|
: 'input input--noBtn';
|
2017-08-31 18:43:36 +02:00
|
|
|
const div = html`
|
2018-02-13 20:32:59 +01:00
|
|
|
<div class="passwordSection">
|
2017-08-31 18:43:36 +02:00
|
|
|
${label}
|
2018-02-13 20:32:59 +01:00
|
|
|
<form class="passwordForm" onsubmit=${checkPassword} data-no-csrf>
|
|
|
|
<input id="password-input"
|
2018-02-20 21:21:00 +01:00
|
|
|
class="${inputClass}"
|
2017-10-25 16:50:37 +02:00
|
|
|
maxlength="64"
|
2017-08-31 18:43:36 +02:00
|
|
|
autocomplete="off"
|
|
|
|
placeholder="${state.translate('unlockInputPlaceholder')}"
|
2017-11-02 22:27:54 +01:00
|
|
|
oninput=${inputChanged}
|
2018-02-12 18:53:34 +01:00
|
|
|
type="password" />
|
2017-08-31 18:43:36 +02:00
|
|
|
<input type="submit"
|
2018-02-13 20:32:59 +01:00
|
|
|
id="password-btn"
|
2018-02-19 23:02:06 +01:00
|
|
|
class="inputBtn inputBtn--hidden"
|
2017-08-31 18:43:36 +02:00
|
|
|
value="${state.translate('unlockButtonLabel')}"/>
|
|
|
|
</form>
|
|
|
|
</div>`;
|
|
|
|
|
2018-02-12 18:53:34 +01:00
|
|
|
if (!(div instanceof String)) {
|
2018-02-13 20:32:59 +01:00
|
|
|
setTimeout(() => document.getElementById('password-input').focus());
|
2018-02-12 18:53:34 +01:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:27:54 +01:00
|
|
|
function inputChanged() {
|
2018-02-13 20:32:59 +01:00
|
|
|
const input = document.getElementById('password-input');
|
|
|
|
const btn = document.getElementById('password-btn');
|
2018-02-20 21:21:00 +01:00
|
|
|
input.classList.remove('input--error');
|
2017-11-02 22:27:54 +01:00
|
|
|
if (input.value.length > 0) {
|
2018-02-13 20:32:59 +01:00
|
|
|
btn.classList.remove('inputBtn--hidden');
|
|
|
|
input.classList.remove('input--noBtn');
|
2017-11-02 22:27:54 +01:00
|
|
|
} else {
|
2018-02-13 20:32:59 +01:00
|
|
|
btn.classList.add('inputBtn--hidden');
|
|
|
|
input.classList.add('input--noBtn');
|
2017-11-02 22:27:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:43:36 +02:00
|
|
|
function checkPassword(event) {
|
|
|
|
event.preventDefault();
|
2018-02-13 20:32:59 +01:00
|
|
|
const password = document.getElementById('password-input').value;
|
2017-08-31 18:43:36 +02:00
|
|
|
if (password.length > 0) {
|
2018-02-13 20:32:59 +01:00
|
|
|
document.getElementById('password-btn').disabled = true;
|
2017-08-31 18:43:36 +02:00
|
|
|
state.fileInfo.url = window.location.href;
|
|
|
|
state.fileInfo.password = password;
|
2018-01-24 19:23:13 +01:00
|
|
|
emit('getMetadata');
|
2017-08-31 18:43:36 +02:00
|
|
|
}
|
2018-02-01 21:49:18 +01:00
|
|
|
return false;
|
2017-08-31 18:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return div;
|
|
|
|
};
|