|
// import { ScrollTrigger } from 'gsap/ScrollTrigger'; |
|
|
|
class Forms { |
|
constructor() { |
|
if (typeof jQuery === 'undefined') return; |
|
|
|
this.events(); |
|
this.loadForms(); |
|
} |
|
|
|
events() { |
|
document.addEventListener('gform_main_scripts_loaded', () => { |
|
if (typeof gform === 'undefined') return; |
|
gform.scriptsLoaded = !0; |
|
this.loadForms(); |
|
}); |
|
|
|
// (optional) Hooks related to form display after form submit |
|
jQuery(document).on('gform_post_render', (event, form_id, current_page) => { |
|
const formEl = document.querySelector(`.js-gravity-form.loaded[data-id="${form_id}"]`); |
|
if (formEl) { |
|
// Remove confirmation messages after form submit |
|
if (formEl.classList.contains('submitted')) { |
|
formEl.classList.remove('submitted'); |
|
} else { |
|
const confirmationMessagesContainer = formEl.querySelector('.gform_confirmation_wrapper'); |
|
|
|
if (confirmationMessagesContainer) { |
|
confirmationMessagesContainer.remove(); |
|
} |
|
} |
|
|
|
// Remove empty divs after form submit |
|
const emptyDivs = formEl.querySelectorAll('.gform_custom_empty_div'); |
|
emptyDivs.forEach((emptyDiv) => { |
|
emptyDiv.remove(); |
|
}); |
|
} |
|
}); |
|
|
|
// (optional) Show form again after form submit |
|
jQuery(document).on('gform_confirmation_loaded', (event, formId) => { |
|
const ev = new CustomEvent('gform_submitted', { detail: { event, formId } }); |
|
window.dispatchEvent(ev); |
|
|
|
const formEl = document.querySelector(`.js-gravity-form.loaded[data-id="${formId}"]`); |
|
if (formEl) { |
|
formEl.classList.add('submitted'); |
|
|
|
const fields = formEl.querySelector('form').querySelectorAll('input:not([type=submit]):not([type=hidden]), textarea, select'); |
|
fields.forEach((input) => { |
|
if (input.type === 'radio' || input.type === 'checkbox') { |
|
input.checked = ''; |
|
} else { |
|
input.value = ''; |
|
} |
|
}); |
|
|
|
const wrapper = formEl.querySelector('.gform_wrapper'); |
|
wrapper.style.removeProperty('display'); |
|
|
|
this.loadFormHooks(formId); |
|
} |
|
}); |
|
} |
|
|
|
loadForms() { |
|
if (typeof gform === 'undefined' || !gform.scriptsLoaded || !gform.domLoaded) return; |
|
|
|
const formContainers = document.querySelectorAll('.js-gravity-form'); |
|
for (const formContainer of formContainers) { |
|
if (formContainer.classList.contains('loaded')) continue; |
|
formContainer.classList.add('loaded'); |
|
|
|
const formId = formContainer.dataset.id; |
|
|
|
fetch(`${wp_vars.adminAjax}?action=gf_get_form&form_id=${formId}`, { |
|
method: 'GET', |
|
credentials: 'same-origin', |
|
}) |
|
.then((res) => res.json()) |
|
.then((datas) => { |
|
const html = datas.data; |
|
|
|
jQuery(formContainer).html(html); |
|
this.loadFormHooks(formId); |
|
|
|
// use it if you are using ScrollTrigger, ScrollSmoother, etc. |
|
// ScrollTrigger.refresh(); |
|
|
|
const ev = new CustomEvent('gform_loaded', { detail: { formId } }); |
|
window.dispatchEvent(ev); |
|
}); |
|
} |
|
} |
|
|
|
loadFormHooks(formId) { |
|
jQuery(document).trigger('gform_post_render', [parseInt(formId), 0]); |
|
if (window.gformInitDatepicker) { |
|
window.gformInitDatepicker(); |
|
} |
|
if (window.gformInitPriceFields) { |
|
window.gformInitPriceFields(); |
|
} |
|
} |
|
} |
|
|
|
document.addEventListener("DOMContentLoaded", function() { |
|
gform.domLoaded = !0; |
|
new Forms(); |
|
}); |
Hi! Thanks for this. I'm using your code in a project with Swup and Gravity Forms. It works fine... but I can't fix an error in the console that says that

cannot read properties of undefined on defaultPrevented.
Have you struggled also with it?