Files
2024_WORDPRESS_PLUGIN_fbpatch/js/calculations.js
2024-07-17 15:30:32 +02:00

71 lines
1.9 KiB
JavaScript

function patch_form_calculation_CIPF() {
let form_calculation = document.querySelector('form.fb_form.multistep');
if (form_calculation === null)
return;
/*
* finds the input:checked in the element .calculate_field
* and trigger the event 'change' on it
* - this event 'change' is added by form builder in divi-form-calc-min.js :
* $(document).on(
* 'change',
* '.calculate_field input:not([type="hidden"]), .calculate_field select',
* function () {
* ...
* }
* );
*
*/
function trigger_change(element) {
/*
* jquery version
*
let inputs = $(element).find('input:checked');
inputs.trigger('change');
*/
/*
* js version
*
*/
let inputs = element.querySelectorAll('input:checked');
// loop through inputs and trigger 'change' event on each
inputs.forEach(function(input) {
// Triggering 'change' event
let change_event = new Event('change', {
bubbles: true,
cancelable: true,
});
input.dispatchEvent(change_event);
});
}
// create an observer on form to check if child nodes are modified
const observer_form = new MutationObserver(wait_for_calculation_class);
observer_form.observe(form_calculation, {
subtree: true,
attributes: true,
});
// observe mutations to see if they include the addition of class .calculate_field
// if the class is added, call the function that might trigger the change event on it
function wait_for_calculation_class(mutationsList) {
mutationsList.forEach((mutation) => {
// check if class where added
if (mutation.type !== 'attributes')
return;
if (mutation.attributeName !== 'class')
return;
// check if added class is .calculate_field
let target = mutation.target;
if (target.classList.contains('calculate_field')) {
// If the class is added, trigger the 'change' event
trigger_change(target);
}
});
}
}
patch_form_calculation_CIPF();