fox-send/app/ui/download.js

115 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-10-25 02:07:10 +00:00
/* global downloadMetadata */
const html = require('choo/html');
const archiveTile = require('./archiveTile');
2018-10-30 18:37:33 +00:00
const intro = require('./intro');
2018-10-25 02:07:10 +00:00
function password(state, emit) {
const fileInfo = state.fileInfo;
const invalid = fileInfo.password === null;
const div = html`
<div class="h-full flex flex-col items-center justify-center border border-grey-light bg-white py-8">
2018-10-25 02:07:10 +00:00
<label
2018-10-30 21:00:37 +00:00
id="password-error"
2018-11-02 09:27:59 +00:00
class="${invalid ? '' : 'invisible'} text-red my-4"
2018-10-25 02:07:10 +00:00
for="password-input">
${state.translate('passwordTryAgain')}
</label>
2018-10-30 21:00:37 +00:00
<form class="w-5/6" onsubmit=${checkPassword} data-no-csrf>
2018-10-25 02:07:10 +00:00
<input id="password-input"
2018-10-30 21:00:37 +00:00
class="w-full border rounded ${
invalid ? 'border-red' : 'border-grey'
2018-11-02 09:27:59 +00:00
} leading-loose px-2 py-1"
2018-10-30 21:00:37 +00:00
maxlength="32"
2018-10-25 02:07:10 +00:00
autocomplete="off"
placeholder="${state.translate('unlockInputPlaceholder')}"
oninput=${inputChanged}
type="password" />
<input type="submit"
id="password-btn"
2018-10-30 21:00:37 +00:00
class="hidden"
2018-10-25 02:07:10 +00:00
value="${state.translate('unlockInputLabel')}"/>
</form>
</div>`;
if (!(div instanceof String)) {
setTimeout(() => document.getElementById('password-input').focus());
}
function inputChanged() {
2018-10-30 21:00:37 +00:00
const label = document.getElementById('password-error');
const input = document.getElementById('password-input');
label.classList.add('invisible');
input.classList.remove('border-red');
2018-10-25 02:07:10 +00:00
}
function checkPassword(event) {
event.preventDefault();
const password = document.getElementById('password-input').value;
if (password.length > 0) {
document.getElementById('password-btn').disabled = true;
state.fileInfo.url = window.location.href;
state.fileInfo.password = password;
emit('getMetadata');
}
return false;
}
return div;
}
function createFileInfo(state) {
return {
id: state.params.id,
secretKey: state.params.key,
nonce: downloadMetadata.nonce,
requiresPassword: downloadMetadata.pwd
};
}
module.exports = function(state, emit) {
let content = '';
if (!state.fileInfo) {
state.fileInfo = createFileInfo(state);
}
if (!state.transfer && !state.fileInfo.requiresPassword) {
emit('getMetadata');
}
if (state.transfer) {
switch (state.transfer.state) {
case 'downloading':
case 'decrypting':
content = archiveTile.downloading(state, emit);
break;
case 'complete':
2018-10-30 21:00:37 +00:00
content = html`
2018-10-31 18:31:17 +00:00
<div id="download-complete" class="flex flex-col items-center justify-center h-full bg-white border border-grey-light p-2">
2018-11-02 09:27:59 +00:00
<h1 class="text-center font-bold my-4 text-2xl">${state.translate(
'downloadFinish'
)}</h1>
<p class="mb-4">
<a href="/" class="text-blue font-medium">${state.translate(
2018-10-30 21:00:37 +00:00
'sendYourFilesLink'
)}</a>
</p>
</div>`;
2018-10-25 02:07:10 +00:00
break;
default:
content = archiveTile.preview(state, emit);
}
} else if (state.fileInfo.requiresPassword && !state.fileInfo.password) {
content = password(state, emit);
}
return html`
2018-10-30 02:06:15 +00:00
<main class="main container">
2018-11-02 09:27:59 +00:00
<section class="relative h-full w-full p-6 md:flex md:flex-row">
2018-11-02 18:28:56 +00:00
<div class="md:mr-6 md:w-1/2">${content}</div>
2018-11-02 09:27:59 +00:00
<div class="md:w-1/2 mt-6 md:mt-0">${intro(state)}</div>
2018-10-25 02:07:10 +00:00
</section>
</main>`;
};