X-Git-Url: http://git.shiar.net/minimedit.git/blobdiff_plain/e64861a7760cfe34596e193a6a3ed2ef39b25892..refs/heads/master:/progress.js diff --git a/progress.js b/progress.js index e59422d..601fec0 100644 --- a/progress.js +++ b/progress.js @@ -2,6 +2,12 @@ function showsize(bytes) { return (bytes / 1024 / 1024).toFixed(2); } +function enablesubmit(form) { + let submit = form.querySelector('input[type="submit"]:disabled'); + if (!submit) return; + submit.disabled = false; +} + function trackupload(input) { var progress = document.getElementById('progress'); if (!progress) { @@ -26,22 +32,25 @@ function trackupload(input) { if (e.target.status == 200) { progress.textContent = 'voltooid'; progress.innerHTML += ' (' + showsize(input.files[0].size) + ' MB)'; - input.value = ''; + window.location.assign(e.target.response); } else { progress.textContent = 'fout'; progress.innerHTML += ': ' + e.target.responseText + ''; + enablesubmit(input.form); } progress.style.width = '100%'; input.parentNode.removeChild(cancel); }, false); ajax.addEventListener('error', function (e) { progress.textContent = 'mislukt: ' + e.target.responseText; + enablesubmit(input.form); }, false); ajax.addEventListener('abort', function (e) { progress.textContent = 'afgebroken'; input.parentNode.removeChild(cancel); input.parentNode.removeChild(progress.parentNode); + enablesubmit(input.form); }, false); ajax.open('POST', input.form.action); @@ -56,12 +65,17 @@ function trackupload(input) { } document.addEventListener('DOMContentLoaded', e => { - for (let row of document.forms[0].elements) { - if (row.type == 'file') { - row.form.onsubmit = () => { - trackupload(row); - return false; - }; - } + for (let form of document.forms) { + form.addEventListener('submit', e => { + if (e.explicitOriginalTarget) { + e.explicitOriginalTarget.disabled = true; + } + if (upload = e.target.querySelector('input[type="file"]')) { + if (upload.value) { + e.preventDefault(); + trackupload(upload); + } + } + }); } });