From 780c99259fb351579d09f38196dae8c9efe32b0b Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 4 May 2019 14:53:37 +0200 Subject: [PATCH] Working but incomplete --- static/js/utils/massInput.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/static/js/utils/massInput.js b/static/js/utils/massInput.js index 302afd995..f048e2c93 100644 --- a/static/js/utils/massInput.js +++ b/static/js/utils/massInput.js @@ -30,6 +30,8 @@ var massInputFormSubmitHandler; var massInputForm; + var massInputEnctype; + function init() { if (!element) { throw new Error('Mass Input utility cannot be setup without an element!'); @@ -62,7 +64,7 @@ var method = massInputForm.getAttribute('method') || 'POST'; var url = massInputForm.getAttribute('action') || window.location.href; - var enctype = massInputForm.getAttribute('enctype') || 'application/json'; + massInputEnctype = massInputForm.getAttribute('enctype') || 'application/json'; var requestFn; if (HttpClient[method.toLowerCase()]) { @@ -84,7 +86,7 @@ requestFn( url, { - 'Content-Type': enctype, + 'Content-Type': massInputEnctype, 'Mass-Input-Shortcircuit': massInputId, }, requestBody, @@ -109,15 +111,23 @@ function serializeForm() { var formData = new FormData(massInputForm); - var formBody = []; - for(var formField of formData) { - var encodedKey = encodeURIComponent(formField[0]); - var encodedValue = encodeURIComponent(formField[1]); - formBody.push(encodedKey + "=" + encodedValue); + Array.from(element.querySelectorAll('button:focus, button:active')).forEach(function (el) { + if (!el.name || !el.value) + return; + + console.log(el); + + formData.append(el.name, el.value); + }); + + if (massInputEnctype === 'application/x-www-form-urlencoded') { + return new URLSearchParams(formData); + } else if (massInputEnctype === 'multipart/form-data') { + return formData; + } else { + throw new Error('Unsupported form enctype: ' + massInputEnctype); } - - return formBody.join("&"); } function reset() {