From feb385b418a787ec8a0c495886b9ec5d84c22f19 Mon Sep 17 00:00:00 2001 From: Felix Hamann Date: Tue, 23 Apr 2019 21:44:26 +0200 Subject: [PATCH] massinput js util cleanup --- static/js/utils/massInput.js | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/static/js/utils/massInput.js b/static/js/utils/massInput.js index 9120f97d2..2e8b66863 100644 --- a/static/js/utils/massInput.js +++ b/static/js/utils/massInput.js @@ -37,7 +37,7 @@ throw new Error('Mass Input utility cannot be setup without being wrapped in a
!'); } - massInputFormSubmitHandler = makeSubmitHandler(massInputForm); + massInputFormSubmitHandler = makeSubmitHandler(); massInputForm.addEventListener('submit', massInputFormSubmitHandler); // mark initialized @@ -50,7 +50,7 @@ }; } - function makeSubmitHandler(massInputForm) { + function makeSubmitHandler() { if (!HttpClient) { throw new Error('HttpClient not found!'); } @@ -73,16 +73,7 @@ } event.preventDefault(); - 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); - } - var requestBody = formBody.join("&"); + var requestBody = serializeForm(); if (requestFn) { requestFn( @@ -93,7 +84,7 @@ }, requestBody, ).then(function(response) { - return response.text() + return response.text(); }).then(function(response) { processResponse(response); }); @@ -114,6 +105,19 @@ } } + 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); + } + + return formBody.join("&"); + } + function reset() { element.classList.remove(MASS_INPUT_INITIALIZED_CLASS); massInputForm.removeEventListener('submit', massInputFormSubmitHandler)