more robust mass input form data handling
This commit is contained in:
parent
50b040dc41
commit
e77df30632
@ -23,6 +23,7 @@
|
||||
var MASS_INPUT_UTIL_NAME = 'massInput';
|
||||
var MASS_INPUT_UTIL_SELECTOR = '[uw-mass-input]';
|
||||
|
||||
var MASS_INPUT_CELL_SELECTOR = '.massinput__cell';
|
||||
var MASS_INPUT_INITIALIZED_CLASS = 'mass-input--initialized';
|
||||
|
||||
var massInputUtil = function(element) {
|
||||
@ -30,8 +31,6 @@
|
||||
var massInputFormSubmitHandler;
|
||||
var massInputForm;
|
||||
|
||||
var massInputEnctype;
|
||||
|
||||
function init() {
|
||||
if (!element) {
|
||||
throw new Error('Mass Input utility cannot be setup without an element!');
|
||||
@ -64,7 +63,7 @@
|
||||
|
||||
var method = massInputForm.getAttribute('method') || 'POST';
|
||||
var url = massInputForm.getAttribute('action') || window.location.href;
|
||||
massInputEnctype = massInputForm.getAttribute('enctype') || 'application/json';
|
||||
var enctype = massInputForm.getAttribute('enctype') || 'application/json';
|
||||
|
||||
var requestFn;
|
||||
if (HttpClient[method.toLowerCase()]) {
|
||||
@ -79,14 +78,17 @@
|
||||
return false;
|
||||
}
|
||||
|
||||
// find the according massinput cell thats hosts the element that triggered the submit
|
||||
var massInputCell = activeElement.closest(MASS_INPUT_CELL_SELECTOR);
|
||||
|
||||
event.preventDefault();
|
||||
var requestBody = serializeForm();
|
||||
var requestBody = serializeForm(massInputCell, enctype);
|
||||
|
||||
if (requestFn) {
|
||||
requestFn(
|
||||
url,
|
||||
{
|
||||
'Content-Type': massInputEnctype,
|
||||
'Content-Type': enctype,
|
||||
'Mass-Input-Shortcircuit': massInputId,
|
||||
},
|
||||
requestBody,
|
||||
@ -109,24 +111,23 @@
|
||||
}
|
||||
}
|
||||
|
||||
function serializeForm() {
|
||||
function serializeForm(massInputCell, enctype) {
|
||||
var formData = new FormData(massInputForm);
|
||||
|
||||
Array.from(element.querySelectorAll('button:focus, button:active')).forEach(function (el) {
|
||||
if (!el.name || !el.value)
|
||||
return;
|
||||
// manually add name and value of submit button to formData
|
||||
if (massInputCell) {
|
||||
var submitButton = massInputCell.querySelector('button[type="submit"][name][value]');
|
||||
if (submitButton) {
|
||||
formData.append(submitButton.name, submitButton.value);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(el);
|
||||
|
||||
formData.append(el.name, el.value);
|
||||
});
|
||||
|
||||
if (massInputEnctype === 'application/x-www-form-urlencoded') {
|
||||
if (enctype === 'application/x-www-form-urlencoded') {
|
||||
return new URLSearchParams(formData);
|
||||
} else if (massInputEnctype === 'multipart/form-data') {
|
||||
} else if (enctype === 'multipart/form-data') {
|
||||
return formData;
|
||||
} else {
|
||||
throw new Error('Unsupported form enctype: ' + massInputEnctype);
|
||||
throw new Error('Unsupported form enctype: ' + enctype);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user