massinput js util cleanup

This commit is contained in:
Felix Hamann 2019-04-23 21:44:26 +02:00 committed by Gregor Kleen
parent bdd1878aed
commit feb385b418

View File

@ -37,7 +37,7 @@
throw new Error('Mass Input utility cannot be setup without being wrapped in a <form>!'); throw new Error('Mass Input utility cannot be setup without being wrapped in a <form>!');
} }
massInputFormSubmitHandler = makeSubmitHandler(massInputForm); massInputFormSubmitHandler = makeSubmitHandler();
massInputForm.addEventListener('submit', massInputFormSubmitHandler); massInputForm.addEventListener('submit', massInputFormSubmitHandler);
// mark initialized // mark initialized
@ -50,7 +50,7 @@
}; };
} }
function makeSubmitHandler(massInputForm) { function makeSubmitHandler() {
if (!HttpClient) { if (!HttpClient) {
throw new Error('HttpClient not found!'); throw new Error('HttpClient not found!');
} }
@ -73,16 +73,7 @@
} }
event.preventDefault(); event.preventDefault();
var formData = new FormData(massInputForm); var requestBody = serializeForm();
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("&");
if (requestFn) { if (requestFn) {
requestFn( requestFn(
@ -93,7 +84,7 @@
}, },
requestBody, requestBody,
).then(function(response) { ).then(function(response) {
return response.text() return response.text();
}).then(function(response) { }).then(function(response) {
processResponse(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() { function reset() {
element.classList.remove(MASS_INPUT_INITIALIZED_CLASS); element.classList.remove(MASS_INPUT_INITIALIZED_CLASS);
massInputForm.removeEventListener('submit', massInputFormSubmitHandler) massInputForm.removeEventListener('submit', massInputFormSubmitHandler)