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>!');
}
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)