Skip to content

Commit 94aed66

Browse files
committed
Rework JS upon FluentForm Conversational update.
1 parent 19c6508 commit 94aed66

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

assets/js/hcaptcha-fluentform.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ const hCaptchaFluentForm = window.hCaptchaFluentForm || ( function( window, $ )
2121
onHCaptchaLoaded() {
2222
const formSelector = '.ffc_conv_form';
2323

24+
// We assume there should be only one conversational form on the page.
25+
const form = document.querySelector( formSelector );
26+
const hasCaptcha = () => form.querySelector( 'h-captcha' ) !== null;
27+
28+
if ( hasCaptcha() ) {
29+
return;
30+
}
31+
2432
const hasOwnCaptcha = () => {
2533
return document.getElementById( 'hcaptcha-container' ) !== null;
2634
};
@@ -29,15 +37,13 @@ const hCaptchaFluentForm = window.hCaptchaFluentForm || ( function( window, $ )
2937
* Process conversational form.
3038
*/
3139
const processForm = () => {
32-
// We assume there should be only one conversational form on the page.
33-
const form = document.querySelector( formSelector );
3440
const submitBtnSelector = '.ff-btn';
3541

36-
const isSubmitVisible = ( qForm ) => {
37-
return qForm.querySelector( submitBtnSelector ) !== null;
38-
};
39-
4042
const addCaptcha = () => {
43+
if ( hasCaptcha() ) {
44+
return;
45+
}
46+
4147
const hCaptchaHiddenClass = 'h-captcha-hidden';
4248
const hCaptchaClass = 'h-captcha';
4349
const hiddenCaptcha = document.getElementsByClassName( hCaptchaHiddenClass )[ 0 ];
@@ -63,17 +69,17 @@ const hCaptchaFluentForm = window.hCaptchaFluentForm || ( function( window, $ )
6369
const mutationObserverCallback = ( mutationList ) => {
6470
for ( const mutation of mutationList ) {
6571
if (
66-
! (
67-
mutation.type === 'attributes' &&
68-
mutation.attributeName === 'class' &&
69-
mutation.oldValue && mutation.oldValue.includes( 'q-is-inactive' )
70-
)
72+
mutation.type === 'attributes' &&
73+
mutation.attributeName === 'class'
7174
) {
72-
continue;
73-
}
74-
75-
if ( isSubmitVisible( mutation.target ) ) {
76-
addCaptcha();
75+
const el = mutation.target;
76+
77+
if (
78+
el.classList.contains( 'vff' ) &&
79+
el.classList.contains( 'ffc_last_step' )
80+
) {
81+
addCaptcha();
82+
}
7783
}
7884
}
7985
};
@@ -82,18 +88,14 @@ const hCaptchaFluentForm = window.hCaptchaFluentForm || ( function( window, $ )
8288
return;
8389
}
8490

85-
const qFormSelector = '.q-form';
86-
const qForms = form.querySelectorAll( qFormSelector );
8791
const config = {
8892
attributes: true,
89-
attributeOldValue: true,
93+
attributeFilter: [ 'class' ],
94+
subtree: true,
9095
};
9196

92-
[ ...qForms ].map( ( qForm ) => {
93-
const observer = new MutationObserver( mutationObserverCallback );
94-
observer.observe( qForm, config );
95-
return qForm;
96-
} );
97+
const observer = new MutationObserver( mutationObserverCallback );
98+
observer.observe( form, config );
9799
};
98100

99101
function waitForElement( selector ) {

0 commit comments

Comments
 (0)