From 49b0543cfc5d8efe1a7d97181c500aef8aa488dd Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sun, 27 Dec 2020 03:46:55 +0100 Subject: [PATCH] progress: disable submit button while posting Prevent duplicates from unavoidable double clicks. --- progress.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/progress.js b/progress.js index 575b98e..601fec0 100644 --- a/progress.js +++ b/progress.js @@ -2,11 +2,13 @@ function showsize(bytes) { return (bytes / 1024 / 1024).toFixed(2); } -function trackupload(input) { - if (!input.value) { - return true; // default form action - } +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) { progress = document.createElement('DIV'); @@ -35,17 +37,20 @@ function trackupload(input) { 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); @@ -57,15 +62,20 @@ function trackupload(input) { cancel.onclick = function () { ajax.abort() }; cancel.style.float = 'left'; input.parentNode.insertBefore(cancel, progress.parentNode); - return false; } document.addEventListener('DOMContentLoaded', e => { - for (let row of document.forms[0].elements) { - if (row.type == 'file') { - row.form.onsubmit = () => { - return trackupload(row); - }; - } + 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); + } + } + }); } }); -- 2.30.0