send/frontend/src/upload.js

88 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-06-02 03:59:27 +00:00
const FileSender = require('./fileSender');
2017-06-06 21:23:10 +00:00
$(document).ready(function(){
let copyBtn = $('#copy-btn');
copyBtn.attr("disabled", false);
copyBtn.html("Copy");
$('#page-one').show();
$('#file-list').hide();
$('#upload-progress').hide();
$('#share-link').hide();
2017-06-06 21:23:10 +00:00
copyBtn.click(()=>{
console.log("copied");
var aux = document.createElement("input");
aux.setAttribute("value", $('#link').attr("value"));
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
copyBtn.attr("disabled", true);
copyBtn.html("Copied!");
});
$('.send-new').click(()=>{
$('#page-one').show();
$('#file-list').show();
$('#upload-progress').hide();
$('#share-link').hide();
copyBtn.attr("disabled", false);
copyBtn.html("Copy");
});
2017-06-01 22:12:30 +00:00
2017-06-06 21:23:10 +00:00
let onChange = event => {
const file = event.target.files[0];
2017-06-06 21:23:10 +00:00
let fileList = $('#uploaded-files');
let row = document.createElement('tr');
let name = document.createElement('td');
let link = document.createElement('td');
let expiry = document.createElement('td');
2017-06-06 21:23:10 +00:00
let cellText = document.createTextNode(file.name);
2017-06-01 17:54:17 +00:00
2017-06-06 21:23:10 +00:00
name.appendChild(cellText);
2017-06-06 21:23:10 +00:00
let progress = document.createElement('p');
row.appendChild(name);
row.appendChild(link);
row.appendChild(expiry);
fileList.append(row);
const fileSender = new FileSender(file);
fileSender.on('progress', percentComplete => {
$('#page-one').hide();
$('#file-list').hide();
$('#upload-progress').show();
$('#upload-filename').innerHTML += file.name;
progress.innerText = `Progress: ${percentComplete}%`;
2017-06-02 03:59:27 +00:00
});
2017-06-06 21:23:10 +00:00
fileSender.upload().then(info => {
const url = `${window.location
.origin}/download/${info.fileId}/#${info.secretKey}`;
$('#link').attr("value", url);
link.innerHTML = url;
localStorage.setItem(info.fileId, info.deleteToken);
let del = document.createElement('td');
let btn = document.createElement('button');
btn.innerHTML = 'x';
btn.classList.add('delete-btn');
btn.addEventListener('click', (e) => {
FileSender.delete(
info.fileId,
localStorage.getItem(info.fileId)
).then(() => {
e.target.parentNode.parentNode.remove();
localStorage.removeItem(info.fileId);
});
});
del.appendChild(btn);
row.appendChild(del);
$('#upload-progress').hide();
$('#share-link').show();
});
};
2017-06-06 21:23:10 +00:00
window.onChange = onChange;
});