PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | js/formidable.js +578 -507 6.36.16.1 View file →
@@ -1,76 +1,31 @@
1 -/* exported frmRecaptcha, frmAfterRecaptcha, frmUpdateField, frmDeleteEntry, frmOnSubmit, frm_resend_email */
1 +/* exported frmRecaptcha, frmAfterRecaptcha */
2 +/* eslint-disable prefer-const */
2 3
3 -var frmFrontForm;
4 -
5 4 function frmFrontFormJS() {
6 5 'use strict';
7 6
8 - /*global jQuery:false, frm_js, grecaptcha, frmProForm, tinyMCE */
7 + /*global jQuery:false, frm_js, grecaptcha, hcaptcha, turnstile, frmProForm, tinyMCE */
9 8 /*global frmThemeOverride_jsErrors, frmThemeOverride_frmPlaceError, frmThemeOverride_frmAfterSubmit */
10 9
11 - var action = '';
12 - var jsErrors = [];
10 + let action = '';
11 + let jsErrors = [];
13 12
14 13 /**
15 - * Maybe add polyfills.
16 - *
17 - * @since 5.4
18 - */
19 - function maybeAddPolyfills() {
20 - var i;
21 - if ( ! Element.prototype.matches ) {
22 - // IE9 supports matches but as msMatchesSelector instead.
23 - Element.prototype.matches = Element.prototype.msMatchesSelector;
24 - }
25 -
26 - if ( ! Element.prototype.closest ) {
27 - Element.prototype.closest = function( s ) {
28 - var el = this;
29 -
30 - do {
31 - if ( el.matches( s ) ) {
32 - return el;
33 - }
34 - el = el.parentElement || el.parentNode;
35 - } while ( el !== null && el.nodeType === 1 );
36 -
37 - return null;
38 - };
39 - }
40 -
41 - // NodeList.forEach().
42 - if ( window.NodeList && ! NodeList.prototype.forEach ) {
43 - NodeList.prototype.forEach = function( callback, thisArg ) {
44 - thisArg = thisArg || window;
45 - for ( i = 0; i < this.length; i++ ) {
46 - callback.call( thisArg, this[ i ], i, this );
47 - }
48 - };
49 - }
50 - }
51 -
52 - /**
53 14 * Triggers custom JS event.
54 15 *
55 16 * @since 5.5.3
56 17 *
57 18 * @param {HTMLElement} el The HTML element.
58 - * @param {String} eventName Event name.
19 + * @param {string} eventName Event name.
59 20 * @param {mixed} data The passed data.
60 21 */
61 22 function triggerCustomEvent( el, eventName, data ) {
62 - var event;
63 -
64 - if ( typeof window.CustomEvent === 'function' ) {
65 - event = new CustomEvent( eventName );
66 - } else if ( document.createEvent ) {
67 - event = document.createEvent( 'HTMLEvents' );
68 - event.initEvent( eventName, false, true );
69 - } else {
23 + if ( typeof window.CustomEvent !== 'function' ) {
70 24 return;
71 25 }
72 26
27 + const event = new CustomEvent( eventName );
73 28 event.frmData = data;
74 29
75 30 el.dispatchEvent( event );
76 31 }
@@ -76,9 +31,9 @@
76 31 }
77 32
78 33 /* Get the ID of the field that changed*/
79 34 function getFieldId( field, fullID ) {
80 - var nameParts, fieldId,
35 + let nameParts, fieldId,
81 36 isRepeating = false,
82 37 fieldName = '';
83 38 if ( field instanceof jQuery ) {
84 39 fieldName = field.attr( 'name' );
@@ -161,12 +116,12 @@
161 116 * Disable the submit button for a given jQuery form object
162 117 *
163 118 * @since 2.03.02
164 119 *
165 - * @param {object} $form
120 + * @param {Object} $form
166 121 */
167 122 function disableSubmitButton( $form ) {
168 - $form.find( 'input[type="submit"], input[type="button"], button[type="submit"]' ).attr( 'disabled', 'disabled' );
123 + $form.find( 'input[type="submit"], input[type="button"], button[type="submit"], button.frm_save_draft' ).attr( 'disabled', 'disabled' );
169 124 }
170 125
171 126 /**
172 127 * Enable the submit button for a given jQuery form object
@@ -172,9 +127,9 @@
172 127 * Enable the submit button for a given jQuery form object
173 128 *
174 129 * @since 2.03.02
175 130 *
176 - * @param {object} $form
131 + * @param {Object} $form
177 132 */
178 133 function enableSubmitButton( $form ) {
179 134 $form.find( 'input[type="submit"], input[type="button"], button[type="submit"]' ).prop( 'disabled', false );
180 135 }
@@ -183,9 +138,9 @@
183 138 * Disable the save draft link for a given jQuery form object
184 139 *
185 140 * @since 4.04.03
186 141 *
187 - * @param {object} $form
142 + * @param {Object} $form
188 143 */
189 144 function disableSaveDraft( $form ) {
190 145 $form.find( 'a.frm_save_draft' ).css( 'pointer-events', 'none' );
191 146 }
@@ -194,18 +149,25 @@
194 149 * Enable the save draft link for a given jQuery form object
195 150 *
196 151 * @since 4.04.03
197 152 *
198 - * @param {object} $form
153 + * @param {Object} $form
199 154 */
200 155 function enableSaveDraft( $form ) {
201 - $form.find( 'a.frm_save_draft' ).css( 'pointer-events', '' );
156 + if ( ! $form.length ) {
157 + return;
158 + }
159 + $form[0].querySelectorAll( '.frm_save_draft' ).forEach( saveDraftButton => {
160 + saveDraftButton.disabled = false;
161 + saveDraftButton.style.pointerEvents = '';
162 + });
202 163 }
203 164
204 165 function validateForm( object ) {
205 - var r, rl, n, nl, fields, field, value, requiredFields,
206 - errors = [];
166 + let errors, r, rl, n, nl, fields, field, requiredFields;
207 167
168 + errors = [];
169 +
208 170 // Make sure required text field is filled in
209 171 requiredFields = jQuery( object ).find(
210 172 '.frm_required_field:visible input, .frm_required_field:visible select, .frm_required_field:visible textarea'
211 173 ).filter( ':not(.frm_optional)' );
@@ -222,29 +184,70 @@
222 184 fields = jQuery( object ).find( 'input,select,textarea' );
223 185 if ( fields.length ) {
224 186 for ( n = 0, nl = fields.length; n < nl; n++ ) {
225 187 field = fields[n];
226 - if ( '' !== field.value ) {
227 - validateFieldValue( field, errors );
188 + if ( '' === field.value ) {
189 + if ( 'number' === field.type ) {
190 + // A number field will return an empty string when it is invalid.
191 + checkValidity( field, errors );
192 + }
193 +
194 + const isConfirmationField = field.name && 0 === field.name.indexOf( 'item_meta[conf_' );
195 + if ( ! isConfirmationField ) {
196 + // Allow a blank confirmation field to still call validateFieldValue.
197 + // If we continue for a confirmation field there are issues with forms submitting with a blank confirmation field.
198 + continue;
199 + }
228 200 }
201 +
202 + validateFieldValue( field, errors, true );
203 + checkValidity( field, errors );
229 204 }
230 205 }
231 206
232 - errors = validateRecaptcha( object, errors );
207 + // Invisible captchas are processed after validation.
208 + // We only want to validate a visible captcha on submit.
209 + if ( ! hasInvisibleRecaptcha( object ) ) {
210 + errors = validateRecaptcha( object, errors );
211 + }
233 212
234 213 return errors;
235 214 }
236 215
237 216 /**
217 + * Check the ValidityState interface for the field.
218 + * If it is invalid, show an error for it.
219 + *
220 + * @param {HTMLElement} field
221 + * @param {Array} errors
222 + * @return {void}
223 + */
224 + function checkValidity( field, errors ) {
225 + let fieldID;
226 + if ( 'object' !== typeof field.validity || false !== field.validity.valid ) {
227 + return;
228 + }
229 +
230 + fieldID = getFieldId( field, true );
231 + if ( 'undefined' === typeof errors[ fieldID ]) {
232 + errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
233 + }
234 +
235 + if ( 'function' === typeof field.reportValidity ) {
236 + // This triggers an error pop up.
237 + field.reportValidity();
238 + }
239 + }
240 +
241 + /**
238 242 * @since 5.0.10
239 243 *
240 - * @param {object} element
244 + * @param {Object} element
241 245 * @param {string} targetClass
242 - * @returns {boolean}
246 + * @return {boolean} True if the element has the target class.
243 247 */
244 248 function hasClass( element, targetClass ) {
245 - var className = ' ' + element.className + ' ';
246 - return -1 !== className.indexOf( ' ' + targetClass + ' ' );
249 + return element.classList && element.classList.contains( targetClass );
247 250 }
248 251
249 252 function maybeValidateChange( field ) {
250 253 if ( field.type === 'url' ) {
@@ -255,10 +258,10 @@
255 258 }
256 259 }
257 260
258 261 function maybeAddHttpToUrl( field ) {
259 - var url = field.value;
260 - var matches = url.match( /^(https?|ftps?|mailto|news|feed|telnet):/ );
262 + const url = field.value;
263 + const matches = url.match( /^(https?|ftps?|mailto|news|feed|telnet):/ );
261 264 if ( field.value !== '' && matches === null ) {
262 265 field.value = 'http://' + url;
263 266 }
264 267 }
@@ -263,9 +266,9 @@
263 266 }
264 267 }
265 268
266 269 function validateField( field ) {
267 - var key,
270 + let key,
268 271 errors = [],
269 272 $fieldCont = jQuery( field ).closest( '.frm_form_field' );
270 273
271 274 if ( $fieldCont.hasClass( 'frm_required_field' ) && ! jQuery( field ).hasClass( 'frm_optional' ) ) {
@@ -272,13 +275,13 @@
272 275 errors = checkRequiredField( field, errors );
273 276 }
274 277
275 278 if ( errors.length < 1 ) {
276 - validateFieldValue( field, errors );
279 + validateFieldValue( field, errors, false );
277 280 }
278 281
279 282 removeFieldError( $fieldCont );
280 - if ( Object.keys( errors ).length > 0 ) {
283 + if ( Object.keys( errors ).length > 0 ) {
281 284 for ( key in errors ) {
282 285 addFieldError( $fieldCont, key, errors );
283 286 }
284 287 }
@@ -283,17 +286,26 @@
283 286 }
284 287 }
285 288 }
286 289
287 - function validateFieldValue( field, errors ) {
290 + /**
291 + * Validates a field value.
292 + *
293 + * @since 6.15 Added `onSubmit` parameter.
294 + *
295 + * @param {HTMLElement} field Field input.
296 + * @param {Object} errors Errors data.
297 + * @param {boolean} onSubmit Is `true` if the form is being submitted.
298 + */
299 + function validateFieldValue( field, errors, onSubmit ) {
288 300 if ( field.type === 'hidden' ) {
289 301 // don't validate
290 302 } else if ( field.type === 'number' ) {
291 303 checkNumberField( field, errors );
292 304 } else if ( field.type === 'email' ) {
293 - checkEmailField( field, errors );
305 + checkEmailField( field, errors, onSubmit );
294 306 } else if ( field.type === 'password' ) {
295 - checkPasswordField( field, errors );
307 + checkPasswordField( field, errors, onSubmit );
296 308 } else if ( field.type === 'url' ) {
297 309 checkUrlField( field, errors );
298 310 } else if ( field.pattern !== null ) {
299 311 checkPatternField( field, errors );
@@ -298,16 +310,20 @@
298 310 } else if ( field.pattern !== null ) {
299 311 checkPatternField( field, errors );
300 312 }
301 313
314 + /**
315 + * @since 6.15 Added `onSubmit` to the data.
316 + */
302 317 triggerCustomEvent( document, 'frm_validate_field_value', {
303 318 field: field,
304 - errors: errors
319 + errors: errors,
320 + onSubmit: onSubmit
305 321 });
306 322 }
307 323
308 324 function checkRequiredField( field, errors ) {
309 - var checkGroup, tempVal, i, placeholder,
325 + let checkGroup, tempVal, i, placeholder,
310 326 val = '',
311 327 fieldID = '',
312 328 fileID = field.getAttribute( 'data-frmfile' );
313 329
@@ -358,8 +374,14 @@
358 374 } else {
359 375 fieldID = getFieldId( field, true );
360 376 }
361 377
378 + // Make sure fieldID is a string.
379 + // fieldID may be a number which doesn't include a .replace function.
380 + if ( 'function' !== typeof fieldID.replace ) {
381 + fieldID = fieldID.toString();
382 + }
383 +
362 384 if ( hasClass( field, 'frm_time_select' ) ) {
363 385 // set id for time field
364 386 fieldID = fieldID.replace( '-H', '' ).replace( '-m', '' );
365 387 } else if ( isSignatureField( field ) ) {
@@ -387,9 +409,9 @@
387 409 return errors;
388 410 }
389 411
390 412 function isSignatureField( field ) {
391 - var name = field.getAttribute( 'name' );
413 + const name = field.getAttribute( 'name' );
392 414 return 'string' === typeof name && '[typed]' === name.substr( -7 );
393 415 }
394 416
395 417 function isAppointmentField( field ) {
@@ -400,9 +422,9 @@
400 422 return 'hidden' === field.type && '_alt' === field.id.substr( -4 ) && hasClass( field.nextElementSibling, 'frm_date_inline' );
401 423 }
402 424
403 425 function getFileVals( fileID ) {
404 - var val = '',
426 + let val = '',
405 427 fileFields = jQuery( 'input[name="file' + fileID + '"], input[name="file' + fileID + '[]"], input[name^="item_meta[' + fileID + ']"]' );
406 428
407 429 fileFields.each( function() {
408 430 if ( val === '' ) {
@@ -412,9 +434,9 @@
412 434 return val;
413 435 }
414 436
415 437 function checkUrlField( field, errors ) {
416 - var fieldID,
438 + let fieldID,
417 439 url = field.value;
418 440
419 441 if ( url !== '' && ! /^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i.test( url ) ) {
420 442 fieldID = getFieldId( field, true );
@@ -423,10 +445,41 @@
423 445 }
424 446 }
425 447 }
426 448
427 - function checkEmailField( field, errors ) {
428 - var fieldID = getFieldId( field, true ),
449 + /**
450 + * Checks if the confirm field should be checked.
451 + *
452 + * @since 6.15
453 + *
454 + * @param {HTMLElement} field Field input.
455 + * @param {boolean} onSubmit Is `true` if the form is being submitted.
456 + */
457 + function shouldCheckConfirmField( field, onSubmit ) {
458 + if ( onSubmit ) {
459 + // Always check on submitting.
460 + return true;
461 + }
462 +
463 + if ( 0 === field.id.indexOf( 'field_conf_' ) ) {
464 + // Always check if it's the confirm field.
465 + return true;
466 + }
467 +
468 + return false;
469 + }
470 +
471 + /**
472 + * Check the email field for errors.
473 + *
474 + * @since 6.15 Added `onSubmit` parameter.
475 + *
476 + * @param {HTMLElement} field Field input.
477 + * @param {Object} errors Errors data.
478 + * @param {boolean} onSubmit Is `true` if the form is being submitted.
479 + */
480 + function checkEmailField( field, errors, onSubmit ) {
481 + const fieldID = getFieldId( field, true ),
429 482 pattern = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i;
430 483
431 484 // validate the current field we're editing first
432 485 if ( '' !== field.value && pattern.test( field.value ) === false ) {
@@ -432,17 +485,30 @@
432 485 if ( '' !== field.value && pattern.test( field.value ) === false ) {
433 486 errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
434 487 }
435 488
436 - confirmField( field, errors );
489 + if ( shouldCheckConfirmField( field, onSubmit ) ) {
490 + confirmField( field, errors );
491 + }
437 492 }
438 493
439 - function checkPasswordField( field, errors ) {
440 - confirmField( field, errors );
494 + /**
495 + * Check the password field for errors.
496 + *
497 + * @since 6.15 Added `onSubmit` parameter.
498 + *
499 + * @param {HTMLElement} field Field input.
500 + * @param {Object} errors Errors data.
501 + * @param {boolean} onSubmit Is `true` if the form is being submitted.
502 + */
503 + function checkPasswordField( field, errors, onSubmit ) {
504 + if ( shouldCheckConfirmField( field, onSubmit ) ) {
505 + confirmField( field, errors );
506 + }
441 507 }
442 508
443 509 function confirmField( field, errors ) {
444 - var value, confirmValue, firstField,
510 + let value, confirmValue, firstField,
445 511 fieldID = getFieldId( field, true ),
446 512 strippedId = field.id.replace( 'conf_', '' ),
447 513 strippedFieldID = fieldID.replace( 'conf_', '' ),
448 514 confirmField = document.getElementById( strippedId.replace( 'field_', 'field_conf_' ) );
@@ -454,9 +520,9 @@
454 520 if ( fieldID !== strippedFieldID ) {
455 521 firstField = document.getElementById( strippedId );
456 522 value = firstField.value;
457 523 confirmValue = confirmField.value;
458 - if ( '' !== value && '' !== confirmValue && value !== confirmValue ) {
524 + if ( value !== confirmValue ) {
459 525 errors[ 'conf_' + strippedFieldID ] = getFieldValidationMessage( confirmField, 'data-confmsg' );
460 526 }
461 527 } else {
462 528 validateField( confirmField );
@@ -463,9 +529,9 @@
463 529 }
464 530 }
465 531
466 532 function checkNumberField( field, errors ) {
467 - var fieldID,
533 + let fieldID,
468 534 number = field.value;
469 535
470 536 if ( number !== '' && isNaN( number / 1 ) !== false ) {
471 537 fieldID = getFieldId( field, true );
@@ -475,9 +541,9 @@
475 541 }
476 542 }
477 543
478 544 function checkPatternField( field, errors ) {
479 - var fieldID,
545 + let fieldID,
480 546 text = field.value,
481 547 format = getFieldValidationMessage( field, 'pattern' );
482 548
483 549 if ( format !== '' && text !== '' ) {
@@ -482,18 +548,61 @@
482 548
483 549 if ( format !== '' && text !== '' ) {
484 550 fieldID = getFieldId( field, true );
485 551 if ( ! ( fieldID in errors ) ) {
486 - format = new RegExp( '^' + format + '$', 'i' );
487 - if ( format.test( text ) === false ) {
488 - errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
552 + if ( 'object' === typeof window.frmProForm && 'function' === typeof window.frmProForm.isIntlPhoneInput && window.frmProForm.isIntlPhoneInput( field ) ) {
553 + if ( ! window.frmProForm.validateIntlPhoneInput( field ) ) {
554 + errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
555 + }
556 + } else {
557 + format = new RegExp( '^' + format + '$', 'i' );
558 + if ( format.test( text ) === false ) {
559 + errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
560 + }
489 561 }
490 562 }
491 563 }
492 564 }
493 565
566 + /**
567 + * Set color for select placeholders.
568 + *
569 + * @since 6.5.1
570 + */
571 + function setSelectPlaceholderColor() {
572 + let selects = document.querySelectorAll( '.form-field select' ),
573 + styleElement = document.querySelector( '.with_frm_style' ),
574 + textColorDisabled = styleElement ? getComputedStyle( styleElement ).getPropertyValue( '--text-color-disabled' ).trim() : '',
575 + changeSelectColor;
576 +
577 + // Exit if there are no select elements or the textColorDisabled property is missing
578 + if ( ! selects.length || ! textColorDisabled ) {
579 + return;
580 + }
581 +
582 + // Function to change the color of a select element
583 + changeSelectColor = function( select ) {
584 + if ( select.options[select.selectedIndex] && hasClass( select.options[select.selectedIndex], 'frm-select-placeholder' ) ) {
585 + select.style.setProperty( 'color', textColorDisabled, 'important' );
586 + } else {
587 + select.style.color = '';
588 + }
589 + };
590 +
591 + // Use a loop to iterate through each select element
592 + Array.prototype.forEach.call( selects, function( select ) {
593 + // Apply the color change to each select element
594 + changeSelectColor( select );
595 +
596 + // Add an event listener for future changes
597 + select.addEventListener( 'change', function() {
598 + changeSelectColor( select );
599 + });
600 + });
601 + }
602 +
494 603 function hasInvisibleRecaptcha( object ) {
495 - var recaptcha, recaptchaID, alreadyChecked;
604 + let recaptcha, recaptchaID, alreadyChecked;
496 605
497 606 if ( isGoingToPrevPage( object ) ) {
498 607 return false;
499 608 }
@@ -503,73 +612,103 @@
503 612 recaptchaID = recaptcha.data( 'rid' );
504 613 alreadyChecked = grecaptcha.getResponse( recaptchaID );
505 614 if ( alreadyChecked.length === 0 ) {
506 615 return recaptcha;
507 - } else {
508 - return false;
509 616 }
510 - } else {
511 - return false;
512 617 }
618 + return false;
513 619 }
514 620
515 621 function executeInvisibleRecaptcha( invisibleRecaptcha ) {
516 - var recaptchaID = invisibleRecaptcha.data( 'rid' );
622 + const recaptchaID = invisibleRecaptcha.data( 'rid' );
517 623 grecaptcha.reset( recaptchaID );
518 624 grecaptcha.execute( recaptchaID );
519 625 }
520 626
521 627 function validateRecaptcha( form, errors ) {
522 - var recaptchaID, response, fieldContainer, fieldID,
523 - $recaptcha = jQuery( form ).find( '.frm-g-recaptcha' );
524 - if ( $recaptcha.length ) {
525 - recaptchaID = $recaptcha.data( 'rid' );
628 + let response;
526 629
527 - try {
528 - response = grecaptcha.getResponse( recaptchaID );
529 - } catch ( e ) {
530 - if ( jQuery( form ).find( 'input[name="recaptcha_checked"]' ).length ) {
531 - return errors;
532 - } else {
533 - response = '';
534 - }
630 + const $recaptcha = jQuery( form ).find( '.frm-g-recaptcha' );
631 + if ( ! $recaptcha.length ) {
632 + return errors;
633 + }
634 +
635 + const recaptchaID = $recaptcha.data( 'rid' );
636 +
637 + try {
638 + response = grecaptcha.getResponse( recaptchaID );
639 + } catch ( e ) {
640 + if ( jQuery( form ).find( 'input[name="recaptcha_checked"]' ).length ) {
641 + return errors;
535 642 }
643 + response = '';
644 + }
536 645
537 - if ( response.length === 0 ) {
538 - fieldContainer = $recaptcha.closest( '.frm_form_field' );
539 - fieldID = fieldContainer.attr( 'id' ).replace( 'frm_field_', '' ).replace( '_container', '' );
540 - errors[ fieldID ] = '';
541 - }
646 + if ( response.length === 0 ) {
647 + const fieldContainer = $recaptcha.closest( '.frm_form_field' );
648 + const fieldID = fieldContainer.attr( 'id' ).replace( 'frm_field_', '' ).replace( '_container', '' );
649 + errors[ fieldID ] = '';
542 650 }
651 +
543 652 return errors;
544 653 }
545 654
655 + /**
656 + * @param {HTMLElement} field
657 + * @param {string} messageType
658 + * @return {string} The error message to display.
659 + */
546 660 function getFieldValidationMessage( field, messageType ) {
547 - var msg, errorHtml;
548 -
549 - msg = field.getAttribute( messageType );
661 + let msg = field.getAttribute( messageType );
550 662 if ( null === msg ) {
551 663 msg = '';
552 664 }
553 665
554 666 if ( '' !== msg && shouldWrapErrorHtmlAroundMessageType( messageType ) ) {
555 - errorHtml = field.getAttribute( 'data-error-html' );
556 - if ( null !== errorHtml ) {
557 - errorHtml = errorHtml.replace( /\+/g, '%20' );
558 - msg = decodeURIComponent( errorHtml ).replace( '[error]', msg );
559 - msg = msg.replace( '[key]', getFieldId( field, false ) );
560 - }
667 + msg = wrapErrorHtml( msg, field );
561 668 }
562 669
563 670 return msg;
564 671 }
565 672
673 + /**
674 + * @param {string} msg
675 + * @param {HTMLElement} field
676 + * @return {string} The error HTML to use.
677 + */
678 + function wrapErrorHtml( msg, field ) {
679 + let errorHtml = field.getAttribute( 'data-error-html' );
680 + if ( null === errorHtml ) {
681 + return msg;
682 + }
683 +
684 + errorHtml = errorHtml.replace( /\+/g, '%20' );
685 + msg = decodeURIComponent( errorHtml ).replace( '[error]', msg );
686 + const fieldId = getFieldId( field, false );
687 + const split = fieldId.split( '-' );
688 + const fieldIdParts = field.id.split( '_' );
689 + fieldIdParts.shift(); // Drop the "field" value from the front.
690 + split[0] = fieldIdParts.join( '_' );
691 + const errorKey = split.join( '-' );
692 + return msg.replace( '[key]', errorKey );
693 + }
694 +
566 695 function shouldWrapErrorHtmlAroundMessageType( type ) {
567 696 return 'pattern' !== type;
568 697 }
569 698
699 + /**
700 + * Check if JS validation should happen.
701 + *
702 + * @param {HTMLElement|Object} object Form object.
703 + * @return {boolean} True if validation is enabled and we are not saving a draft or going to a previous page.
704 + */
570 705 function shouldJSValidate( object ) {
571 - var validate = jQuery( object ).hasClass( 'frm_js_validate' );
706 + if ( 'function' === typeof object.get ) {
707 + // Get the HTMLElement from a jQuery object.
708 + object = object.get( 0 );
709 + }
710 + let validate = hasClass( object, 'frm_js_validate' );
572 711 if ( validate && typeof frmProForm !== 'undefined' && ( frmProForm.savingDraft( object ) || frmProForm.goingToPreviousPage( object ) ) ) {
573 712 validate = false;
574 713 }
575 714
@@ -575,10 +714,15 @@
575 714
576 715 return validate;
577 716 }
578 717
718 + /**
719 + * @param {HTMLElement} object
720 + * @param {string|undefined} action
721 + * @return {void}
722 + */
579 723 function getFormErrors( object, action ) {
580 - var fieldset, data, success, error, shouldTriggerEvent;
724 + let fieldset, data, success, error, shouldTriggerEvent;
581 725
582 726 if ( typeof action === 'undefined' ) {
583 727 jQuery( object ).find( 'input[name="frm_action"]' ).val();
584 728 }
@@ -589,10 +733,10 @@
589 733 data = jQuery( object ).serialize() + '&action=frm_entries_' + action + '&nonce=' + frm_js.nonce; // eslint-disable-line camelcase
590 734 shouldTriggerEvent = object.classList.contains( 'frm_trigger_event_on_submit' );
591 735
592 736 success = function( response ) {
593 - var defaultResponse, formID, replaceContent, pageOrder, formReturned, contSubmit, delay,
594 - $fieldCont, key, inCollapsedSection, frmTrigger;
737 + let defaultResponse, formID, replaceContent, pageOrder, formReturned, contSubmit, delay,
738 + $fieldCont, key, inCollapsedSection, frmTrigger, newTab;
595 739
596 740 defaultResponse = {
597 741 content: '',
598 742 errors: {},
@@ -616,14 +760,27 @@
616 760 return;
617 761 }
618 762
619 763 jQuery( document ).trigger( 'frmBeforeFormRedirect', [ object, response ]);
620 - window.location = response.redirect;
621 - } else if ( response.content !== '' ) {
764 +
765 + if ( ! response.openInNewTab ) {
766 + // We return here because we're redirecting there is no need to update content.
767 + window.location = response.redirect;
768 + return;
769 + }
770 +
771 + // We don't return here because we're opening in a new tab, the old tab will still update.
772 + newTab = window.open( response.redirect, '_blank' );
773 + if ( ! newTab && response.fallbackMsg && response.content ) {
774 + response.content = response.content.trim().replace( /(<\/div><\/div>)$/, ' ' + response.fallbackMsg + '</div></div>' );
775 + }
776 + }
777 +
778 + if ( response.content !== '' ) {
622 779 // the form or success message was returned
623 780
624 781 if ( shouldTriggerEvent ) {
625 - triggerCustomEvent( object, 'frmSubmitEvent' );
782 + triggerCustomEvent( object, 'frmSubmitEvent', { content: response.content });
626 783 return;
627 784 }
628 785
629 786 removeSubmitLoading( jQuery( object ) );
@@ -638,10 +795,12 @@
638 795 delay = maybeSlideOut( replaceContent, response.content );
639 796
640 797 setTimeout(
641 798 function() {
642 - var container, input, previousInput;
799 + let container, input, previousInput;
643 800
801 + afterFormSubmittedBeforeReplace( object, response );
802 +
644 803 replaceContent.replaceWith( response.content );
645 804
646 805 addUrlParam( response );
647 806
@@ -700,9 +859,9 @@
700 859 }
701 860 }
702 861
703 862 jQuery( object ).find( '.frm-g-recaptcha, .g-recaptcha, .h-captcha' ).each( function() {
704 - var $recaptcha = jQuery( this ),
863 + const $recaptcha = jQuery( this ),
705 864 recaptchaID = $recaptcha.data( 'rid' );
706 865
707 866 if ( typeof grecaptcha !== 'undefined' && grecaptcha ) {
708 867 if ( recaptchaID ) {
@@ -723,9 +882,9 @@
723 882
724 883 if ( contSubmit ) {
725 884 object.submit();
726 885 } else {
727 - jQuery( object ).prepend( response.error_message );
886 + object.insertAdjacentHTML( 'afterbegin', response.error_message );
728 887 checkForErrorsAndMaybeSetFocus();
729 888 }
730 889 } else {
731 890 // there may have been a plugin conflict, or the form is not set to submit with ajax
@@ -744,9 +903,9 @@
744 903 postToAjaxUrl( object, data, success, error );
745 904 }
746 905
747 906 function postToAjaxUrl( form, data, success, error ) {
748 - var ajaxUrl, action, ajaxParams;
907 + let ajaxUrl, action, ajaxParams;
749 908
750 909 ajaxUrl = frm_js.ajax_url; // eslint-disable-line camelcase
751 910 action = form.getAttribute( 'action' );
752 911
@@ -768,9 +927,9 @@
768 927 jQuery.ajax( ajaxParams );
769 928 }
770 929
771 930 function afterFormSubmitted( object, response ) {
772 - var formCompleted = jQuery( response.content ).find( '.frm_message' );
931 + const formCompleted = jQuery( response.content ).find( '.frm_message' );
773 932 if ( formCompleted.length ) {
774 933 jQuery( document ).trigger( 'frmFormComplete', [ object, response ]);
775 934 } else {
776 935 jQuery( document ).trigger( 'frmPageChanged', [ object, response ]);
@@ -776,10 +935,26 @@
776 935 jQuery( document ).trigger( 'frmPageChanged', [ object, response ]);
777 936 }
778 937 }
779 938
939 + /**
940 + * Trigger an event before the form is replaced with a success message.
941 + *
942 + * @since 6.9
943 + *
944 + * @param {HTMLElement} object The form.
945 + * @param {Object} response The response from submitting the form with AJAX.
946 + * @return {void}
947 + */
948 + function afterFormSubmittedBeforeReplace( object, response ) {
949 + const formCompleted = jQuery( response.content ).find( '.frm_message' );
950 + if ( formCompleted.length ) {
951 + triggerCustomEvent( document, 'frmFormCompleteBeforeReplace', { object, response });
952 + }
953 + }
954 +
780 955 function removeAddedScripts( formContainer, formID ) {
781 - var endReplace = jQuery( '.frm_end_ajax_' + formID );
956 + const endReplace = jQuery( '.frm_end_ajax_' + formID );
782 957 if ( endReplace.length ) {
783 958 formContainer.nextUntil( '.frm_end_ajax_' + formID ).remove();
784 959 endReplace.remove();
785 960 }
@@ -785,9 +960,9 @@
785 960 }
786 961 }
787 962
788 963 function maybeSlideOut( oldContent, newContent ) {
789 - var c,
964 + let c,
790 965 newClass = 'frm_slideout';
791 966 if ( newContent.indexOf( ' frm_slide' ) !== -1 ) {
792 967 c = oldContent.children();
793 968 if ( newContent.indexOf( ' frm_going_back' ) !== -1 ) {
@@ -800,9 +975,9 @@
800 975 return 0;
801 976 }
802 977
803 978 function addUrlParam( response ) {
804 - var url;
979 + let url;
805 980 if ( history.pushState && typeof response.page !== 'undefined' ) {
806 981 url = addQueryVar( 'frm_page', response.page );
807 982 window.history.pushState({ 'html': response.html }, '', '?' + url );
808 983 }
@@ -808,9 +983,9 @@
808 983 }
809 984 }
810 985
811 986 function addQueryVar( key, value ) {
812 - var kvp, i, x;
987 + let kvp, i, x;
813 988
814 989 key = encodeURI( key );
815 990 value = encodeURI( value );
816 991
@@ -834,13 +1009,14 @@
834 1009 return kvp.join( '&' );
835 1010 }
836 1011
837 1012 function addFieldError( $fieldCont, key, jsErrors ) {
838 - var input, id, describedBy, roleString;
1013 + let input, id, describedBy, roleString;
839 1014 if ( $fieldCont.length && $fieldCont.is( ':visible' ) ) {
840 1015 $fieldCont.addClass( 'frm_blank_field' );
841 1016 input = $fieldCont.find( 'input, select, textarea' );
842 - id = 'frm_error_field_' + key;
1017 + id = getErrorElementId( key, input.get( 0 ) );
1018 +
843 1019 describedBy = input.attr( 'aria-describedby' );
844 1020
845 1021 if ( typeof frmThemeOverride_frmPlaceError === 'function' ) { // eslint-disable-line camelcase
846 1022 frmThemeOverride_frmPlaceError( key, jsErrors );
@@ -871,15 +1047,41 @@
871 1047 jQuery( document ).trigger( 'frmAddFieldError', [ $fieldCont, key, jsErrors ]);
872 1048 }
873 1049 }
874 1050
1051 + /**
1052 + * Get the ID to use for an error element added when submitting with AJAX.
1053 + *
1054 + * @param {string} key
1055 + * @param {HTMLElement} input
1056 + * @return {string} The ID to use for the error element.
1057 + */
1058 + function getErrorElementId( key, input ) {
1059 + if ( isNaN( key ) || ! input.id ) {
1060 + // If key isn't a number, assume it's already in the right format.
1061 + return 'frm_error_field_' + key;
1062 + }
1063 + return 'frm_error_' + input.id;
1064 + }
1065 +
1066 + /**
1067 + * Removes errors before validating with JS.
1068 + * This prevents issues with stale errors that has since been fixed.
1069 + *
1070 + * @param {Object} $fieldCont jQuery object.
1071 + * @return {void}
1072 + */
875 1073 function removeFieldError( $fieldCont ) {
876 - var errorMessage = $fieldCont.find( '.frm_error' ),
877 - errorId = errorMessage.attr( 'id' ),
878 - input = $fieldCont.find( 'input, select, textarea' ),
879 - describedBy = input.attr( 'aria-describedby' );
1074 + const errorMessage = $fieldCont.find( '.frm_error' );
1075 + const errorId = errorMessage.attr( 'id' );
1076 + const input = $fieldCont.find( 'input, select, textarea' );
1077 + let describedBy = input.attr( 'aria-describedby' );
880 1078
881 - $fieldCont.removeClass( 'frm_blank_field has-error' );
1079 + const fieldContainer = $fieldCont.get( 0 );
1080 + if ( fieldContainer && fieldContainer.classList ) {
1081 + fieldContainer.classList.remove( 'frm_blank_field', 'has-error' );
1082 + }
1083 +
882 1084 errorMessage.remove();
883 1085 input.attr( 'aria-invalid', false );
884 1086 input.removeAttr( 'aria-describedby' );
885 1087
@@ -894,13 +1096,21 @@
894 1096 jQuery( '.form-field .frm_error' ).replaceWith( '' );
895 1097 jQuery( '.frm_error_style' ).remove();
896 1098 }
897 1099
1100 + /**
1101 + * @param {HTMLElement|Object} object Form object.
1102 + * @return {void}
1103 + */
898 1104 function scrollToFirstField( object ) {
899 - var field = jQuery( object ).find( '.frm_blank_field' ).first();
900 - if ( field.length ) {
901 - frmFrontForm.scrollMsg( field, object, true );
1105 + if ( 'function' === typeof object.get ) {
1106 + // Get the HTMLElement from a jQuery object.
1107 + object = object.get( 0 );
902 1108 }
1109 + const field = object.querySelector( '.frm_blank_field' );
1110 + if ( field ) {
1111 + frmFrontForm.scrollMsg( jQuery( field ), object, true );
1112 + }
903 1113 }
904 1114
905 1115 function showSubmitLoading( $object ) {
906 1116 showLoadingIndicator( $object );
@@ -915,9 +1125,9 @@
915 1125 }
916 1126 }
917 1127
918 1128 function addLoadingClass( $object ) {
919 - var loadingClass = isGoingToPrevPage( $object ) ? 'frm_loading_prev' : 'frm_loading_form';
1129 + const loadingClass = isGoingToPrevPage( $object ) ? 'frm_loading_prev' : 'frm_loading_form';
920 1130
921 1131 $object.addClass( loadingClass );
922 1132 }
923 1133
@@ -925,9 +1135,9 @@
925 1135 return ( typeof frmProForm !== 'undefined' && frmProForm.goingToPreviousPage( $object ) );
926 1136 }
927 1137
928 1138 function removeSubmitLoading( $object, enable, processesRunning ) {
929 - var loadingForm;
1139 + let loadingForm;
930 1140
931 1141 if ( processesRunning > 0 ) {
932 1142 return;
933 1143 }
@@ -944,9 +1154,9 @@
944 1154 }
945 1155 }
946 1156
947 1157 function showFileLoading( object ) {
948 - var fileval,
1158 + let fileval,
949 1159 loading = document.getElementById( 'frm_loading' );
950 1160 if ( loading !== null ) {
951 1161 fileval = jQuery( object ).find( 'input[type=file]' ).val();
952 1162 if ( typeof fileval !== 'undefined' && fileval !== '' ) {
@@ -956,69 +1166,8 @@
956 1166 }
957 1167 }
958 1168 }
959 1169
960 - function clearDefault() {
961 - /*jshint validthis:true */
962 - toggleDefault( jQuery( this ), 'clear' );
963 - }
964 -
965 - function replaceDefault() {
966 - /*jshint validthis:true */
967 - toggleDefault( jQuery( this ), 'replace' );
968 - }
969 -
970 - function toggleDefault( $thisField, e ) {
971 - // TODO: Fix this for a default value that is a number or array
972 - var thisVal,
973 - v = $thisField.data( 'frmval' ).replace( /(\n|\r\n)/g, '\r' );
974 - if ( v === '' || typeof v === 'undefined' ) {
975 - return false;
976 - }
977 - thisVal = $thisField.val().replace( /(\n|\r\n)/g, '\r' );
978 -
979 - if ( 'replace' === e ) {
980 - if ( thisVal === '' ) {
981 - $thisField.addClass( 'frm_default' ).val( v );
982 - }
983 - } else if ( thisVal == v ) {
984 - $thisField.removeClass( 'frm_default' ).val( '' );
985 - }
986 - }
987 -
988 - function resendEmail() {
989 - /*jshint validthis:true */
990 - var $link = jQuery( this ),
991 - entryId = this.getAttribute( 'data-eid' ),
992 - formId = this.getAttribute( 'data-fid' ),
993 - label = $link.find( '.frm_link_label' );
994 - if ( label.length < 1 ) {
995 - label = $link;
996 - }
997 - label.append( '<span class="frm-wait"></span>' );
998 -
999 - jQuery.ajax({
1000 - type: 'POST',
1001 - url: frm_js.ajax_url, // eslint-disable-line camelcase
1002 - data: {
1003 - action: 'frm_entries_send_email',
1004 - entry_id: entryId,
1005 - form_id: formId,
1006 - nonce: frm_js.nonce // eslint-disable-line camelcase
1007 - },
1008 - success: function( msg ) {
1009 - var admin = document.getElementById( 'wpbody' );
1010 - if ( admin === null ) {
1011 - label.html( msg );
1012 - } else {
1013 - label.html( '' );
1014 - $link.after( msg );
1015 - }
1016 - }
1017 - });
1018 - return false;
1019 - }
1020 -
1021 1170 /**********************************************
1022 1171 * General Helpers
1023 1172 *********************************************/
1024 1173
@@ -1023,68 +1172,12 @@
1023 1172 *********************************************/
1024 1173
1025 1174 function confirmClick() {
1026 1175 /*jshint validthis:true */
1027 - var message = jQuery( this ).data( 'frmconfirm' );
1176 + const message = jQuery( this ).data( 'frmconfirm' );
1028 1177 return confirm( message );
1029 1178 }
1030 1179
1031 - function toggleDiv() {
1032 - /*jshint validthis:true */
1033 - var div = jQuery( this ).data( 'frmtoggle' );
1034 - if ( jQuery( div ).is( ':visible' ) ) {
1035 - jQuery( div ).slideUp( 'fast' );
1036 - } else {
1037 - jQuery( div ).slideDown( 'fast' );
1038 - }
1039 - return false;
1040 - }
1041 -
1042 - /**********************************************
1043 - * Fallback functions
1044 - *********************************************/
1045 -
1046 - function addTrimFallbackForIE() {
1047 - if ( typeof String.prototype.trim !== 'function' ) {
1048 - String.prototype.trim = function() {
1049 - return this.replace( /^\s+|\s+$/g, '' );
1050 - };
1051 - }
1052 - }
1053 -
1054 - function addFilterFallbackForIE() {
1055 - var t, len, res, thisp, i, val;
1056 -
1057 - if ( ! Array.prototype.filter ) {
1058 -
1059 - Array.prototype.filter = function( fun /*, thisp */ ) {
1060 -
1061 - if ( this === void 0 || this === null ) {
1062 - throw new TypeError();
1063 - }
1064 -
1065 - t = Object( this );
1066 - len = t.length >>> 0;
1067 - if ( typeof fun !== 'function' ) {
1068 - throw new TypeError();
1069 - }
1070 -
1071 - res = [];
1072 - thisp = arguments[1];
1073 - for ( i = 0; i < len; i++ ) {
1074 - if ( i in t ) {
1075 - val = t[i]; // in case fun mutates this
1076 - if ( fun.call( thisp, val, i, t ) ) {
1077 - res.push( val );
1078 - }
1079 - }
1080 - }
1081 -
1082 - return res;
1083 - };
1084 - }
1085 - }
1086 -
1087 1180 /**
1088 1181 * Check for -webkit-box-shadow css value for input:-webkit-autofill selector.
1089 1182 * If this is a match, the User is autofilling the input on a Webkit browser.
1090 1183 * We want to delete the Honeypot field, otherwise it will get triggered as spam on autocomplete.
@@ -1089,9 +1182,9 @@
1089 1182 * If this is a match, the User is autofilling the input on a Webkit browser.
1090 1183 * We want to delete the Honeypot field, otherwise it will get triggered as spam on autocomplete.
1091 1184 */
1092 1185 function onHoneypotFieldChange() {
1093 - var css = jQuery( this ).css( 'box-shadow' );
1186 + const css = jQuery( this ).css( 'box-shadow' );
1094 1187 if ( css.match( /inset/ ) ) {
1095 1188 this.parentNode.removeChild( this );
1096 1189 }
1097 1190 }
@@ -1099,9 +1192,9 @@
1099 1192 function maybeMakeHoneypotFieldsUntabbable() {
1100 1193 document.addEventListener( 'keydown', handleKeyUp );
1101 1194
1102 1195 function handleKeyUp( event ) {
1103 - var code;
1196 + let code;
1104 1197
1105 1198 if ( 'undefined' !== typeof event.key ) {
1106 1199 code = event.key;
1107 1200 } else if ( 'undefined' !== typeof event.keyCode && 9 === event.keyCode ) {
@@ -1115,11 +1208,10 @@
1115 1208 }
1116 1209
1117 1210 function makeHoneypotFieldsUntabbable() {
1118 1211 document.querySelectorAll( '.frm_verify' ).forEach(
1119 - function( wrapper ) {
1120 - var input = wrapper.querySelector( 'input[id^=frm_email]' );
1121 - if ( input ) {
1212 + function( input ) {
1213 + if ( input.id && 0 === input.id.indexOf( 'frm_email_' ) ) {
1122 1214 input.setAttribute( 'tabindex', -1 );
1123 1215 }
1124 1216 }
1125 1217 );
@@ -1131,11 +1223,11 @@
1131 1223 *
1132 1224 * @since 4.10.02
1133 1225 */
1134 1226 function changeFocusWhenClickComboFieldLabel() {
1135 - var label;
1227 + let label;
1136 1228
1137 - var comboInputsContainer = document.querySelectorAll( '.frm_combo_inputs_container' );
1229 + const comboInputsContainer = document.querySelectorAll( '.frm_combo_inputs_container' );
1138 1230 comboInputsContainer.forEach( function( inputsContainer ) {
1139 1231 if ( ! inputsContainer.closest( '.frm_form_field' ) ) {
1140 1232 return;
1141 1233 }
@@ -1144,9 +1236,9 @@
1144 1236 if ( ! label ) {
1145 1237 return;
1146 1238 }
1147 1239
1148 - label.addEventListener( 'click', function( e ) {
1240 + label.addEventListener( 'click', function() {
1149 1241 inputsContainer.querySelector( '.frm_form_field:first-child input, .frm_form_field:first-child select, .frm_form_field:first-child textarea' ).focus();
1150 1242 });
1151 1243 });
1152 1244 }
@@ -1151,9 +1243,9 @@
1151 1243 });
1152 1244 }
1153 1245
1154 1246 function checkForErrorsAndMaybeSetFocus() {
1155 - var errors, element, timeoutCallback;
1247 + let errors, element, timeoutCallback;
1156 1248
1157 1249 if ( ! frm_js.focus_first_error ) { // eslint-disable-line camelcase
1158 1250 return;
1159 1251 }
@@ -1173,9 +1265,9 @@
1173 1265
1174 1266 if ( 'undefined' !== typeof element.classList ) {
1175 1267 if ( element.classList.contains( 'html-active' ) ) {
1176 1268 timeoutCallback = function() {
1177 - var textarea = element.querySelector( 'textarea' );
1269 + const textarea = element.querySelector( 'textarea' );
1178 1270 if ( null !== textarea ) {
1179 1271 textarea.focus();
1180 1272 }
1181 1273 };
@@ -1193,27 +1285,16 @@
1193 1285 } while ( element.previousSibling );
1194 1286 }
1195 1287
1196 1288 /**
1197 - * Checks if is on IE browser.
1198 - *
1199 - * @since 5.4
1200 - *
1201 - * @return {Boolean}
1202 - */
1203 - function isIE() {
1204 - return navigator.userAgent.indexOf( 'MSIE' ) > -1 || navigator.userAgent.indexOf( 'Trident' ) > -1;
1205 - }
1206 -
1207 - /**
1208 1289 * Does the same as jQuery( document ).on( 'event', 'selector', handler ).
1209 1290 *
1210 1291 * @since 5.4
1211 1292 *
1212 - * @param {String} event Event name.
1213 - * @param {String} selector Selector.
1214 - * @param {Function} handler Handler.
1215 - * @param {Boolean|Object} options Options to be added to `addEventListener()` method. Default is `false`.
1293 + * @param {string} event Event name.
1294 + * @param {string} selector Selector.
1295 + * @param {Function} handler Handler.
1296 + * @param {boolean | Object} options Options to be added to `addEventListener()` method. Default is `false`.
1216 1297 */
1217 1298 function documentOn( event, selector, handler, options ) {
1218 1299 if ( 'undefined' === typeof options ) {
1219 1300 options = false;
@@ -1219,9 +1300,9 @@
1219 1300 options = false;
1220 1301 }
1221 1302
1222 1303 document.addEventListener( event, function( e ) {
1223 - var target;
1304 + let target;
1224 1305
1225 1306 // loop parent nodes from the target to the delegation node.
1226 1307 for ( target = e.target; target && target != this; target = target.parentNode ) {
1227 1308 if ( target && target.matches && target.matches( selector ) ) {
@@ -1232,15 +1313,15 @@
1232 1313 }, options );
1233 1314 }
1234 1315
1235 1316 function initFloatingLabels() {
1236 - var checkFloatLabel, checkDropdownLabel, checkPlaceholderIE, runOnLoad, selector, floatClass;
1317 + let checkFloatLabel, checkDropdownLabel, runOnLoad, selector, floatClass;
1237 1318
1238 1319 selector = '.frm-show-form .frm_inside_container input, .frm-show-form .frm_inside_container select, .frm-show-form .frm_inside_container textarea';
1239 1320 floatClass = 'frm_label_float_top';
1240 1321
1241 1322 checkFloatLabel = function( input ) {
1242 - var container, shouldFloatTop, firstOpt;
1323 + let container, shouldFloatTop, firstOpt;
1243 1324
1244 1325 container = input.closest( '.frm_inside_container' );
1245 1326 if ( ! container ) {
1246 1327 return;
@@ -1257,22 +1338,18 @@
1257 1338 if ( firstOpt.hasAttribute( 'data-label' ) ) {
1258 1339 firstOpt.textContent = firstOpt.getAttribute( 'data-label' );
1259 1340 firstOpt.removeAttribute( 'data-label' );
1260 1341 }
1261 - } else {
1262 - if ( firstOpt.textContent ) {
1263 - firstOpt.setAttribute( 'data-label', firstOpt.textContent );
1264 - firstOpt.textContent = '';
1265 - }
1342 + } else if ( firstOpt.textContent ) {
1343 + firstOpt.setAttribute( 'data-label', firstOpt.textContent );
1344 + firstOpt.textContent = '';
1266 1345 }
1267 - } else if ( isIE() ) {
1268 - checkPlaceholderIE( input );
1269 1346 }
1270 1347 };
1271 1348
1272 1349 checkDropdownLabel = function() {
1273 1350 document.querySelectorAll( '.frm-show-form .frm_inside_container:not(.' + floatClass + ') select' ).forEach( function( input ) {
1274 - var firstOpt = input.querySelector( 'option:first-child' );
1351 + const firstOpt = input.querySelector( 'option:first-child' );
1275 1352
1276 1353 if ( firstOpt.textContent ) {
1277 1354 firstOpt.setAttribute( 'data-label', firstOpt.textContent );
1278 1355 firstOpt.textContent = '';
@@ -1279,27 +1356,8 @@
1279 1356 }
1280 1357 });
1281 1358 };
1282 1359
1283 - checkPlaceholderIE = function( input ) {
1284 - if ( input.value ) {
1285 - // Don't need to handle this case because placeholder isn't shown.
1286 - return;
1287 - }
1288 -
1289 - if ( document.activeElement === input ) {
1290 - if ( input.hasAttribute( 'data-placeholder' ) ) {
1291 - input.placeholder = input.getAttribute( 'data-placeholder' );
1292 - input.removeAttribute( 'data-placeholder' );
1293 - }
1294 - } else {
1295 - if ( input.placeholder ) {
1296 - input.setAttribute( 'data-placeholder', input.placeholder );
1297 - input.placeholder = '';
1298 - }
1299 - }
1300 - };
1301 -
1302 1360 [ 'focus', 'blur', 'change' ].forEach( function( eventName ) {
1303 1361 documentOn(
1304 1362 eventName,
1305 1363 selector,
@@ -1319,9 +1377,9 @@
1319 1377 checkFloatLabel( document.activeElement );
1320 1378 } else if ( firstLoad ) {
1321 1379 document.querySelectorAll( '.frm_inside_container' ).forEach(
1322 1380 function( container ) {
1323 - var input = container.querySelector( 'input, select, textarea' );
1381 + const input = container.querySelector( 'input, select, textarea' );
1324 1382 if ( input && '' !== input.value ) {
1325 1383 checkFloatLabel( input );
1326 1384 }
1327 1385 }
@@ -1328,14 +1386,8 @@
1328 1386 );
1329 1387 }
1330 1388
1331 1389 checkDropdownLabel();
1332 -
1333 - if ( isIE() ) {
1334 - document.querySelectorAll( selector ).forEach( function( input ) {
1335 - checkPlaceholderIE( input );
1336 - });
1337 - }
1338 1390 };
1339 1391
1340 1392 runOnLoad( true );
1341 1393
@@ -1347,10 +1399,68 @@
1347 1399 runOnLoad();
1348 1400 });
1349 1401 }
1350 1402
1403 + function shouldUpdateValidityMessage( target ) {
1404 + if ( 'INPUT' !== target.nodeName ) {
1405 + return false;
1406 + }
1407 +
1408 + if ( ! target.dataset.invmsg ) {
1409 + return false;
1410 + }
1411 +
1412 + if ( 'text' !== target.getAttribute( 'type' ) ) {
1413 + return false;
1414 + }
1415 +
1416 + if ( target.classList.contains( 'frm_verify' ) ) {
1417 + return false;
1418 + }
1419 +
1420 + return true;
1421 + }
1422 +
1423 + function maybeClearCustomValidityMessage( event, field ) {
1424 + let key,
1425 + isInvalid = false;
1426 +
1427 + if ( ! shouldUpdateValidityMessage( field ) ) {
1428 + return;
1429 + }
1430 +
1431 + for ( key in field.validity ) {
1432 + if ( 'customError' === key ) {
1433 + continue;
1434 + }
1435 + if ( 'valid' !== key && field.validity[ key ] === true ) {
1436 + isInvalid = true;
1437 + break;
1438 + }
1439 + };
1440 +
1441 + if ( ! isInvalid ) {
1442 + field.setCustomValidity( '' );
1443 + }
1444 + }
1445 +
1446 + function maybeShowNewTabFallbackMessage() {
1447 + let messageEl;
1448 +
1449 + if ( ! window.frmShowNewTabFallback ) {
1450 + return;
1451 + }
1452 +
1453 + messageEl = document.querySelector( '#frm_form_' + frmShowNewTabFallback.formId + '_container .frm_message' );
1454 + if ( ! messageEl ) {
1455 + return;
1456 + }
1457 +
1458 + messageEl.insertAdjacentHTML( 'beforeend', ' ' + frmShowNewTabFallback.message );
1459 + }
1460 +
1351 1461 function setCustomValidityMessage() {
1352 - var forms, length, index;
1462 + let forms, length, index;
1353 1463
1354 1464 forms = document.getElementsByClassName( 'frm-show-form' );
1355 1465 length = forms.length;
1356 1466
@@ -1357,37 +1467,44 @@
1357 1467 for ( index = 0; index < length; ++index ) {
1358 1468 forms[ index ].addEventListener(
1359 1469 'invalid',
1360 1470 function( event ) {
1361 - var target = event.target;
1471 + const target = event.target;
1362 1472
1363 - if ( 'INPUT' !== target.nodeName ) {
1364 - return;
1473 + if ( shouldUpdateValidityMessage( target ) ) {
1474 + target.setCustomValidity( target.dataset.invmsg );
1365 1475 }
1476 + },
1477 + true
1478 + );
1479 + }
1480 + }
1366 1481
1367 - if ( ! target.dataset.invmsg ) {
1368 - return;
1482 + function enableSubmitButtonOnBackButtonPress() {
1483 + window.addEventListener( 'pageshow', function( event ) {
1484 + if ( event.persisted ) {
1485 + document.querySelectorAll( '.frm_loading_form' ).forEach(
1486 + function( form ) {
1487 + enableSubmitButton( jQuery( form ) );
1369 1488 }
1489 + );
1490 + removeSubmitLoading();
1491 + }
1492 + });
1493 + }
1370 1494
1371 - if ( 'text' !== target.getAttribute( 'type' ) ) {
1372 - return;
1373 - }
1374 -
1375 - if ( target.classList.contains( 'frm_verify' ) ) {
1376 - return;
1377 - }
1378 -
1379 - target.setCustomValidity( target.dataset.invmsg );
1380 - },
1381 - true
1382 - );
1495 + /**
1496 + * Destroys the formidable generated global hcaptcha object since it wouldn't otherwise render.
1497 + */
1498 + function destroyhCaptcha() {
1499 + if ( ! window.hasOwnProperty( 'hcaptcha' ) || ! document.querySelector( '.frm-show-form .h-captcha' ) ) {
1500 + return;
1383 1501 }
1502 + window.hcaptcha = null;
1384 1503 }
1385 1504
1386 1505 return {
1387 1506 init: function() {
1388 - maybeAddPolyfills();
1389 -
1390 1507 jQuery( document ).off( 'submit.formidable', '.frm-show-form' );
1391 1508 jQuery( document ).on( 'submit.formidable', '.frm-show-form', frmFrontForm.submitForm );
1392 1509
1393 1510 jQuery( '.frm-show-form input[onblur], .frm-show-form textarea[onblur]' ).each( function() {
@@ -1395,14 +1512,8 @@
1395 1512 jQuery( this ).trigger( 'blur' );
1396 1513 }
1397 1514 });
1398 1515
1399 - jQuery( document ).on( 'focus', '.frm_toggle_default', clearDefault );
1400 - jQuery( document ).on( 'blur', '.frm_toggle_default', replaceDefault );
1401 - jQuery( '.frm_toggle_default' ).trigger( 'blur' );
1402 -
1403 - jQuery( document.getElementById( 'frm_resend_email' ) ).on( 'click', resendEmail );
1404 -
1405 1516 jQuery( document ).on( 'change', '.frm-show-form input[name^="item_meta"], .frm-show-form select[name^="item_meta"], .frm-show-form textarea[name^="item_meta"]', frmFrontForm.fieldValueChanged );
1406 1517
1407 1518 jQuery( document ).on( 'change', '[id^=frm_email_]', onHoneypotFieldChange );
1408 1519 maybeMakeHoneypotFieldsUntabbable();
@@ -1407,9 +1518,8 @@
1407 1518 jQuery( document ).on( 'change', '[id^=frm_email_]', onHoneypotFieldChange );
1408 1519 maybeMakeHoneypotFieldsUntabbable();
1409 1520
1410 1521 jQuery( document ).on( 'click', 'a[data-frmconfirm]', confirmClick );
1411 - jQuery( 'a[data-frmtoggle]' ).on( 'click', toggleDiv );
1412 1522
1413 1523 checkForErrorsAndMaybeSetFocus();
1414 1524
1415 1525 // Focus on the first sub field when clicking to the primary label of combo field.
@@ -1414,16 +1524,25 @@
1414 1524
1415 1525 // Focus on the first sub field when clicking to the primary label of combo field.
1416 1526 changeFocusWhenClickComboFieldLabel();
1417 1527
1418 - // Add fallbacks for IE.
1419 - addTrimFallbackForIE(); // Trim only works in IE10+.
1420 - addFilterFallbackForIE(); // Filter is not supported in any version of IE.
1421 -
1422 1528 initFloatingLabels();
1529 + maybeShowNewTabFallbackMessage();
1423 1530
1424 1531 jQuery( document ).on( 'frmAfterAddRow', setCustomValidityMessage );
1425 1532 setCustomValidityMessage();
1533 + jQuery( document ).on( 'frmFieldChanged', maybeClearCustomValidityMessage );
1534 +
1535 + setSelectPlaceholderColor();
1536 +
1537 + // Elementor popup show event. Fix Elementor Popup && FF Captcha field conflicts
1538 + jQuery( document ).on( 'elementor/popup/show', frmRecaptcha );
1539 +
1540 + enableSubmitButtonOnBackButtonPress();
1541 + jQuery( document ).on(
1542 + 'frmPageChanged',
1543 + destroyhCaptcha
1544 + );
1426 1545 },
1427 1546
1428 1547 getFieldId: function( field, fullID ) {
1429 1548 return getFieldId( field, fullID );
@@ -1428,10 +1547,10 @@
1428 1547 getFieldId: function( field, fullID ) {
1429 1548 return getFieldId( field, fullID );
1430 1549 },
1431 1550
1432 - renderRecaptcha: function( captcha ) {
1433 - var formID, recaptchaID,
1551 + renderCaptcha: function( captcha, captchaSelector ) {
1552 + let formID, captchaID,
1434 1553 size = captcha.getAttribute( 'data-size' ),
1435 1554 rendered = captcha.getAttribute( 'data-rid' ) !== null,
1436 1555 params = {
1437 1556 'sitekey': captcha.getAttribute( 'data-sitekey' ),
@@ -1436,9 +1555,11 @@
1436 1555 params = {
1437 1556 'sitekey': captcha.getAttribute( 'data-sitekey' ),
1438 1557 'size': size,
1439 1558 'theme': captcha.getAttribute( 'data-theme' )
1440 - };
1559 + },
1560 + activeCaptcha = getSelectedCaptcha( captchaSelector ),
1561 + captchaContainer = typeof turnstile !== 'undefined' && turnstile === activeCaptcha ? '#' + captcha.id : captcha.id;
1441 1562
1442 1563 if ( rendered ) {
1443 1564 return;
1444 1565 }
@@ -1450,20 +1571,21 @@
1450 1571 frmFrontForm.afterRecaptcha( token, formID );
1451 1572 };
1452 1573 }
1453 1574
1454 - recaptchaID = grecaptcha.render( captcha.id, params );
1455 1575
1456 - captcha.setAttribute( 'data-rid', recaptchaID );
1576 + captchaID = activeCaptcha.render( captchaContainer, params );
1577 +
1578 + captcha.setAttribute( 'data-rid', captchaID );
1457 1579 },
1458 1580
1459 1581 afterSingleRecaptcha: function() {
1460 - var object = jQuery( '.frm-show-form .g-recaptcha' ).closest( 'form' )[0];
1582 + const object = jQuery( '.frm-show-form .g-recaptcha' ).closest( 'form' )[0];
1461 1583 frmFrontForm.submitFormNow( object );
1462 1584 },
1463 1585
1464 - afterRecaptcha: function( token, formID ) {
1465 - var object = jQuery( '#frm_form_' + formID + '_container form' )[0];
1586 + afterRecaptcha: function( _, formID ) {
1587 + const object = jQuery( '#frm_form_' + formID + '_container form' )[0];
1466 1588 frmFrontForm.submitFormNow( object );
1467 1589 },
1468 1590
1469 1591 submitForm: function( e ) {
@@ -1469,10 +1591,15 @@
1469 1591 submitForm: function( e ) {
1470 1592 frmFrontForm.submitFormManual( e, this );
1471 1593 },
1472 1594
1595 + /**
1596 + * @param {Event} e
1597 + * @param {HTMLElement} object The form object that is being submitted.
1598 + * @return {void}
1599 + */
1473 1600 submitFormManual: function( e, object ) {
1474 - var isPro, errors,
1601 + let isPro, errors,
1475 1602 invisibleRecaptcha = hasInvisibleRecaptcha( object ),
1476 1603 classList = object.className.trim().split( /\s+/gi );
1477 1604
1478 1605 if ( classList && invisibleRecaptcha.length < 1 ) {
@@ -1487,31 +1614,30 @@
1487 1614 }
1488 1615
1489 1616 e.preventDefault();
1490 1617
1491 - if ( typeof frmProForm !== 'undefined' && typeof frmProForm.submitAllowed === 'function' ) {
1492 - if ( ! frmProForm.submitAllowed( object ) ) {
1493 - return;
1494 - }
1618 + if ( typeof frmProForm !== 'undefined' && typeof frmProForm.submitAllowed === 'function' && ! frmProForm.submitAllowed( object ) ) {
1619 + return;
1495 1620 }
1496 1621
1622 + errors = frmFrontForm.validateFormSubmit( object );
1623 + if ( Object.keys( errors ).length !== 0 ) {
1624 + return;
1625 + }
1626 +
1497 1627 if ( invisibleRecaptcha.length ) {
1498 1628 showLoadingIndicator( jQuery( object ) );
1499 1629 executeInvisibleRecaptcha( invisibleRecaptcha );
1500 1630 } else {
1501 1631
1502 - errors = frmFrontForm.validateFormSubmit( object );
1632 + showSubmitLoading( jQuery( object ) );
1503 1633
1504 - if ( Object.keys( errors ).length === 0 ) {
1505 - showSubmitLoading( jQuery( object ) );
1506 -
1507 - frmFrontForm.submitFormNow( object, classList );
1508 - }
1634 + frmFrontForm.submitFormNow( object, classList );
1509 1635 }
1510 1636 },
1511 1637
1512 1638 submitFormNow: function( object ) {
1513 - var hasFileFields, antispamInput,
1639 + let hasFileFields, antispamInput,
1514 1640 classList = object.className.trim().split( /\s+/gi );
1515 1641
1516 1642 if ( object.hasAttribute( 'data-token' ) && null === object.querySelector( '[name="antispam_token"]' ) ) {
1517 1643 // include the antispam token on form submit.
@@ -1536,8 +1662,13 @@
1536 1662 object.submit();
1537 1663 }
1538 1664 },
1539 1665
1666 + /**
1667 + * @param {HTMLElement|Object} object Form object. This might be a jQuery object.
1668 + *
1669 + * @return {Array} List of errors.
1670 + */
1540 1671 validateFormSubmit: function( object ) {
1541 1672 if ( typeof tinyMCE !== 'undefined' && jQuery( object ).find( '.wp-editor-wrap' ).length ) {
1542 1673 tinyMCE.triggerSave();
1543 1674 }
@@ -1554,10 +1685,14 @@
1554 1685
1555 1686 return jsErrors;
1556 1687 },
1557 1688
1689 + /**
1690 + * @param {HTMLElement|Object} object Form object. This might be a jQuery object.
1691 + * @return {Array} List of errors.
1692 + */
1558 1693 getAjaxFormErrors: function( object ) {
1559 - var customErrors, key;
1694 + let customErrors, key;
1560 1695
1561 1696 jsErrors = validateForm( object );
1562 1697 if ( typeof frmThemeOverride_jsErrors === 'function' ) { // eslint-disable-line camelcase
1563 1698 action = jQuery( object ).find( 'input[name="frm_action"]' ).val();
@@ -1568,13 +1703,22 @@
1568 1703 }
1569 1704 }
1570 1705 }
1571 1706
1707 + triggerCustomEvent( document, 'frm_get_ajax_form_errors', {
1708 + formEl: object,
1709 + errors: jsErrors
1710 + });
1711 +
1572 1712 return jsErrors;
1573 1713 },
1574 1714
1715 + /**
1716 + * @param {HTMLElement|Object} object Form object. This might be a jQuery object.
1717 + * @return {void}
1718 + */
1575 1719 addAjaxFormErrors: function( object ) {
1576 - var key, $fieldCont;
1720 + let key, $fieldCont;
1577 1721 removeAllErrors();
1578 1722
1579 1723 for ( key in jsErrors ) {
1580 1724 $fieldCont = jQuery( object ).find( '#frm_field_' + key + '_container' );
@@ -1607,14 +1751,14 @@
1607 1751 removeSubmitLoading( $object, enable, processesRunning );
1608 1752 },
1609 1753
1610 1754 scrollToID: function( id ) {
1611 - var object = jQuery( document.getElementById( id ) );
1755 + const object = jQuery( document.getElementById( id ) );
1612 1756 frmFrontForm.scrollMsg( object, false );
1613 1757 },
1614 1758
1615 1759 scrollMsg: function( id, object, animate ) {
1616 - var newPos, m, b, screenTop, screenBottom,
1760 + let newPos, m, b, screenTop, screenBottom,
1617 1761 scrollObj = '';
1618 1762 if ( typeof object === 'undefined' ) {
1619 1763 scrollObj = jQuery( document.getElementById( 'frm_form_' + id + '_container' ) );
1620 1764 if ( scrollObj.length < 1 ) {
@@ -1657,9 +1801,9 @@
1657 1801
1658 1802 fieldValueChanged: function( e ) {
1659 1803 /*jshint validthis:true */
1660 1804
1661 - var fieldId = frmFrontForm.getFieldId( this, false );
1805 + const fieldId = frmFrontForm.getFieldId( this, false );
1662 1806 if ( ! fieldId || typeof fieldId === 'undefined' ) {
1663 1807 return;
1664 1808 }
1665 1809
@@ -1673,55 +1817,8 @@
1673 1817 maybeValidateChange( this );
1674 1818 }
1675 1819 },
1676 1820
1677 - savingDraft: function( object ) {
1678 - console.warn( 'DEPRECATED: function frmFrontForm.savingDraft in v3.0 use frmProForm.savingDraft' );
1679 - if ( typeof frmProForm !== 'undefined' ) {
1680 - return frmProForm.savingDraft( object );
1681 - }
1682 - },
1683 -
1684 - goingToPreviousPage: function( object ) {
1685 - console.warn( 'DEPRECATED: function frmFrontForm.goingToPreviousPage in v3.0 use frmProForm.goingToPreviousPage' );
1686 - if ( typeof frmProForm !== 'undefined' ) {
1687 - return frmProForm.goingToPreviousPage( object );
1688 - }
1689 - },
1690 -
1691 - hideOrShowFields: function() {
1692 - console.warn( 'DEPRECATED: function frmFrontForm.hideOrShowFields in v3.0 use frmProForm.hideOrShowFields' );
1693 - if ( typeof frmProForm !== 'undefined' ) {
1694 - frmProForm.hideOrShowFields();
1695 - }
1696 - },
1697 -
1698 - hidePreviouslyHiddenFields: function() {
1699 - console.warn( 'DEPRECATED: function frmFrontForm.hidePreviouslyHiddenFields in v3.0 use frmProForm.hidePreviouslyHiddenFields' );
1700 - if ( typeof frmProForm !== 'undefined' ) {
1701 - frmProForm.hidePreviouslyHiddenFields();
1702 - }
1703 - },
1704 -
1705 - checkDependentDynamicFields: function( ids ) {
1706 - console.warn( 'DEPRECATED: function frmFrontForm.checkDependentDynamicFields in v3.0 use frmProForm.checkDependentDynamicFields' );
1707 - if ( typeof frmProForm !== 'undefined' ) {
1708 - frmProForm.checkDependentDynamicFields( ids );
1709 - }
1710 - },
1711 -
1712 - checkDependentLookupFields: function( ids ) {
1713 - console.warn( 'DEPRECATED: function frmFrontForm.checkDependentLookupFields in v3.0 use frmProForm.checkDependentLookupFields' );
1714 - if ( typeof frmProForm !== 'undefined' ) {
1715 - frmProForm.checkDependentLookupFields( ids );
1716 - }
1717 - },
1718 -
1719 - loadGoogle: function() {
1720 - console.warn( 'DEPRECATED: function frmFrontForm.loadGoogle in v3.0 use frmProForm.loadGoogle' );
1721 - frmProForm.loadGoogle();
1722 - },
1723 -
1724 1821 escapeHtml: function( text ) {
1725 1822 return text
1726 1823 .replace( /&/g, '&amp;' )
1727 1824 .replace( /</g, '&lt;' )
@@ -1735,91 +1832,65 @@
1735 1832 },
1736 1833
1737 1834 visible: function( classes ) {
1738 1835 jQuery( classes ).css( 'visibility', 'visible' );
1739 - }
1836 + },
1837 +
1838 + triggerCustomEvent: triggerCustomEvent,
1839 + documentOn
1740 1840 };
1741 1841 }
1742 -frmFrontForm = frmFrontFormJS();
1743 1842
1843 +window.frmFrontForm = frmFrontFormJS();
1844 +
1744 1845 jQuery( document ).ready( function() {
1745 1846 frmFrontForm.init();
1746 1847 });
1747 1848
1748 1849 function frmRecaptcha() {
1749 - var c, cl,
1750 - captchas = jQuery( '.frm-g-recaptcha' );
1751 - for ( c = 0, cl = captchas.length; c < cl; c++ ) {
1752 - frmFrontForm.renderRecaptcha( captchas[c]);
1753 - }
1850 + frmCaptcha( '.frm-g-recaptcha' );
1754 1851 }
1755 1852
1756 -function frmAfterRecaptcha( token ) {
1757 - frmFrontForm.afterSingleRecaptcha( token );
1853 +function frmTurnstile() {
1854 + frmCaptcha( '.cf-turnstile' );
1758 1855 }
1759 1856
1760 -function frmUpdateField( entryId, fieldId, value, message, num ) {
1761 - jQuery( document.getElementById( 'frm_update_field_' + entryId + '_' + fieldId + '_' + num ) ).html( '<span class="frm-loading-img"></span>' );
1762 - jQuery.ajax({
1763 - type: 'POST',
1764 - url: frm_js.ajax_url, // eslint-disable-line camelcase
1765 - data: {
1766 - action: 'frm_entries_update_field_ajax',
1767 - entry_id: entryId,
1768 - field_id: fieldId,
1769 - value: value,
1770 - nonce: frm_js.nonce // eslint-disable-line camelcase
1771 - },
1772 - success: function() {
1773 - if ( message.replace( /^\s+|\s+$/g, '' ) === '' ) {
1774 - jQuery( document.getElementById( 'frm_update_field_' + entryId + '_' + fieldId + '_' + num ) ).fadeOut( 'slow' );
1775 - } else {
1776 - jQuery( document.getElementById( 'frm_update_field_' + entryId + '_' + fieldId + '_' + num ) ).replaceWith( message );
1777 - }
1857 +function frmCaptcha( captchaSelector ) {
1858 + let c;
1859 + const captchas = document.querySelectorAll( captchaSelector );
1860 + const cl = captchas.length;
1861 + for ( c = 0; c < cl; c++ ) {
1862 + const closestForm = captchas[c].closest( 'form' );
1863 + const formIsVisible = closestForm && closestForm.offsetParent !== null;
1864 + const captcha = captchas[c];
1865 + if ( ! formIsVisible ) {
1866 + // If the form is not visible, try again later in 400ms.
1867 + // This fixes issues where the form fades visible on page load.
1868 + // Or whne the form is inside of a modal.
1869 + const interval = setInterval(
1870 + function() {
1871 + if ( closestForm && closestForm.offsetParent !== null ) {
1872 + frmFrontForm.renderCaptcha( captcha, captchaSelector );
1873 + clearInterval( interval );
1874 + }
1875 + },
1876 + 400
1877 + );
1878 + continue;
1778 1879 }
1779 - });
1880 + frmFrontForm.renderCaptcha( captcha, captchaSelector );
1881 + }
1780 1882 }
1781 1883
1782 -function frmDeleteEntry( entryId, prefix ) {
1783 - console.warn( 'DEPRECATED: function frmDeleteEntry in v2.0.13 use frmFrontForm.deleteEntry' );
1784 - jQuery( document.getElementById( 'frm_delete_' + entryId ) ).replaceWith( '<span class="frm-loading-img" id="frm_delete_' + entryId + '"></span>' );
1785 - jQuery.ajax({
1786 - type: 'POST',
1787 - url: frm_js.ajax_url, // eslint-disable-line camelcase
1788 - data: {
1789 - action: 'frm_entries_destroy',
1790 - entry: entryId,
1791 - nonce: frm_js.nonce // eslint-disable-line camelcase
1792 - },
1793 - success: function( html ) {
1794 - if ( html.replace( /^\s+|\s+$/g, '' ) === 'success' ) {
1795 - jQuery( document.getElementById( prefix + entryId ) ).fadeOut( 'slow' );
1796 - } else {
1797 - jQuery( document.getElementById( 'frm_delete_' + entryId ) ).replaceWith( html );
1798 - }
1799 - }
1800 - });
1884 +function getSelectedCaptcha( captchaSelector ) {
1885 + if ( captchaSelector === '.frm-g-recaptcha' ) {
1886 + return grecaptcha;
1887 + }
1888 + if ( document.querySelector( '.cf-turnstile' ) ) {
1889 + return turnstile;
1890 + }
1891 + return hcaptcha;
1801 1892 }
1802 1893
1803 -function frmOnSubmit( e ) {
1804 - console.warn( 'DEPRECATED: function frmOnSubmit in v2.0 use frmFrontForm.submitForm' );
1805 - frmFrontForm.submitForm( e, this );
1806 -}
1807 -
1808 -function frm_resend_email( entryId, formId ) { // eslint-disable-line camelcase
1809 - var $link = jQuery( document.getElementById( 'frm_resend_email' ) );
1810 - console.warn( 'DEPRECATED: function frm_resend_email in v2.0' );
1811 - $link.append( '<span class="spinner" style="display:inline"></span>' );
1812 - jQuery.ajax({
1813 - type: 'POST',
1814 - url: frm_js.ajax_url, // eslint-disable-line camelcase
1815 - data: {
1816 - action: 'frm_entries_send_email',
1817 - entry_id: entryId,
1818 - form_id: formId,
1819 - nonce: frm_js.nonce // eslint-disable-line camelcase
1820 - },
1821 - success: function( msg ) {
1822 - $link.replaceWith( msg );
1823 - }
1824 - });
1894 +function frmAfterRecaptcha( token ) {
1895 + frmFrontForm.afterSingleRecaptcha( token );
1825 1896 }