2020-07-16 14:26:08 +02:00
|
|
|
function disableSubmit(checks, button) {
|
|
|
|
let form = document.getElementsByTagName('form')[0];
|
|
|
|
let inputs = form.getElementsByTagName('input');
|
2020-09-07 14:52:49 +02:00
|
|
|
button.disabled = true;
|
|
|
|
addRequiredEventListener(inputs, checks, form, button);
|
2020-12-07 12:09:10 +01:00
|
|
|
disableDoubleSubmit(form, button);
|
2020-09-07 14:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function addRequiredEventListener(inputs, checks, form, button) {
|
|
|
|
let eventType = 'input';
|
2020-07-16 14:26:08 +02:00
|
|
|
for (i = 0; i < inputs.length; i++) {
|
2020-09-07 14:52:49 +02:00
|
|
|
if (inputs[i].required) {
|
|
|
|
eventType = 'input';
|
|
|
|
if (inputs[i].type === 'checkbox') {
|
|
|
|
eventType = 'click';
|
2020-07-16 14:26:08 +02:00
|
|
|
}
|
2020-09-07 14:52:49 +02:00
|
|
|
inputs[i].addEventListener(eventType, function () {
|
|
|
|
toggleButton(checks, form, inputs, button);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 12:09:10 +01:00
|
|
|
function disableDoubleSubmit(form, button) {
|
|
|
|
form.addEventListener('submit', function() {
|
|
|
|
document.body.classList.add('waiting');
|
|
|
|
button.disabled = true;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-07 14:52:49 +02:00
|
|
|
function toggleButton(checks, form, inputs, button) {
|
|
|
|
if (checks !== undefined) {
|
|
|
|
if (checks() === false) {
|
|
|
|
button.disabled = true;
|
|
|
|
return
|
|
|
|
}
|
2020-07-16 14:26:08 +02:00
|
|
|
}
|
2020-09-07 14:52:49 +02:00
|
|
|
button.disabled = !allRequiredDone(form, inputs);
|
2020-07-16 14:26:08 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 14:52:49 +02:00
|
|
|
function allRequiredDone(form, inputs) {
|
2020-07-16 14:26:08 +02:00
|
|
|
for (i = 0; i < inputs.length; i++) {
|
2020-09-07 14:52:49 +02:00
|
|
|
if (inputs[i].required) {
|
|
|
|
if (inputs[i].type === 'checkbox' && !inputs[i].checked) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (inputs[i].value === '') {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-16 14:26:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|