PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16.1.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16.1.2
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 +533 -1239 6.35 → 6.16.1.2 View file →
@@ -1,9 +1,14 @@
1 1 /* exported frmRecaptcha, frmAfterRecaptcha */
2 +/* eslint-disable prefer-const */
2 3
3 4 function frmFrontFormJS() {
4 5 'use strict';
5 6
7 + /*global jQuery:false, frm_js, grecaptcha, hcaptcha, turnstile, frmProForm, tinyMCE */
8 + /*global frmThemeOverride_jsErrors, frmThemeOverride_frmPlaceError, frmThemeOverride_frmAfterSubmit */
9 +
10 + let action = '';
6 11 let jsErrors = [];
7 12
8 13 /**
9 14 * Triggers custom JS event.
@@ -11,9 +16,9 @@
11 16 * @since 5.5.3
12 17 *
13 18 * @param {HTMLElement} el The HTML element.
14 19 * @param {string} eventName Event name.
15 - * @param {*} data The passed data.
20 + * @param {mixed} data The passed data.
16 21 */
17 22 function triggerCustomEvent( el, eventName, data ) {
18 23 if ( typeof window.CustomEvent !== 'function' ) {
19 24 return;
@@ -24,35 +29,31 @@
24 29
25 30 el.dispatchEvent( event );
26 31 }
27 32
28 - /**
29 - * Get the ID of the field that changed.
30 - *
31 - * @param {HTMLElement|jQuery} field
32 - * @param {boolean} fullID
33 - * @return {string|number} Field ID.
34 - */
33 + /* Get the ID of the field that changed*/
35 34 function getFieldId( field, fullID ) {
36 - let nameParts;
37 - let fieldId;
38 - let isRepeating = false;
39 - let fieldName = '';
40 -
35 + let nameParts, fieldId,
36 + isRepeating = false,
37 + fieldName = '';
41 38 if ( field instanceof jQuery ) {
42 - field = field.get( 0 );
39 + fieldName = field.attr( 'name' );
40 + } else {
41 + fieldName = field.name;
43 42 }
44 43
45 - fieldName = field.name;
46 -
47 - if ( fieldName === undefined ) {
44 + if ( typeof fieldName === 'undefined' ) {
48 45 fieldName = '';
49 46 }
50 47
51 48 if ( fieldName === '' ) {
52 - fieldName = field.getAttribute( 'data-name' );
49 + if ( field instanceof jQuery ) {
50 + fieldName = field.data( 'name' );
51 + } else {
52 + fieldName = field.getAttribute( 'data-name' );
53 + }
53 54
54 - if ( fieldName === undefined ) {
55 + if ( typeof fieldName === 'undefined' ) {
55 56 fieldName = '';
56 57 }
57 58
58 59 if ( fieldName !== '' && fieldName ) {
@@ -67,24 +68,25 @@
67 68 return 0;
68 69 }
69 70 nameParts = nameParts.filter( function( n ) {
70 71 return n !== '';
71 - } );
72 + });
72 73
73 - fieldId = nameParts[ 0 ];
74 + fieldId = nameParts[0];
74 75
75 76 if ( nameParts.length === 1 ) {
76 77 return fieldId;
77 78 }
78 79
79 - if ( nameParts[ 1 ] === '[form' || nameParts[ 1 ] === '[row_ids' ) {
80 + if ( nameParts[1] === '[form' || nameParts[1] === '[row_ids' ) {
80 81 return 0;
81 82 }
82 83
83 84 // Check if 'this' is in a repeating section
84 - if ( document.querySelector( `input[name="item_meta[${ fieldId }][form]"]` ) ) {
85 + if ( jQuery( 'input[name="item_meta[' + fieldId + '][form]"]' ).length ) {
86 +
85 87 // this is a repeatable section with name: item_meta[repeating-section-id][row-id][field-id]
86 - fieldId = nameParts[ 2 ].replace( '[', '' );
88 + fieldId = nameParts[2].replace( '[', '' );
87 89 isRepeating = true;
88 90 }
89 91
90 92 // Check if 'this' is an other text field and get field ID for it
@@ -90,21 +92,21 @@
90 92 // Check if 'this' is an other text field and get field ID for it
91 93 if ( 'other' === fieldId ) {
92 94 if ( isRepeating ) {
93 95 // name for other fields: item_meta[370][0][other][414]
94 - fieldId = nameParts[ 3 ].replace( '[', '' );
96 + fieldId = nameParts[3].replace( '[', '' );
95 97 } else {
96 98 // Other field name: item_meta[other][370]
97 - fieldId = nameParts[ 1 ].replace( '[', '' );
99 + fieldId = nameParts[1].replace( '[', '' );
98 100 }
99 101 }
100 102
101 103 if ( fullID === true ) {
102 104 // For use in the container div id
103 - if ( fieldId === nameParts[ 0 ] ) {
104 - fieldId = `${ fieldId }-${ nameParts[ 1 ].replace( '[', '' ) }`;
105 + if ( fieldId === nameParts[0]) {
106 + fieldId = fieldId + '-' + nameParts[1].replace( '[', '' );
105 107 } else {
106 - fieldId = `${ fieldId }-${ nameParts[ 0 ] }-${ nameParts[ 1 ].replace( '[', '' ) }`;
108 + fieldId = fieldId + '-' + nameParts[0] + '-' + nameParts[1].replace( '[', '' );
107 109 }
108 110 }
109 111
110 112 return fieldId;
@@ -117,15 +119,9 @@
117 119 *
118 120 * @param {Object} $form
119 121 */
120 122 function disableSubmitButton( $form ) {
121 - const form = $form instanceof jQuery ? $form.get( 0 ) : $form;
122 - if ( ! form ) {
123 - return;
124 - }
125 - form.querySelectorAll( 'input[type="submit"], input[type="button"], button[type="submit"], button.frm_save_draft' ).forEach(
126 - button => button.disabled = true
127 - );
123 + $form.find( 'input[type="submit"], input[type="button"], button[type="submit"], button.frm_save_draft' ).attr( 'disabled', 'disabled' );
128 124 }
129 125
130 126 /**
131 127 * Enable the submit button for a given jQuery form object
@@ -131,16 +127,12 @@
131 127 * Enable the submit button for a given jQuery form object
132 128 *
133 129 * @since 2.03.02
134 130 *
135 - * @param {HTMLElement} form
136 - *
137 - * @return {void}
131 + * @param {Object} $form
138 132 */
139 - function enableSubmitButton( form ) {
140 - form.querySelectorAll( 'input[type="submit"], input[type="button"], button[type="submit"]' ).forEach(
141 - button => button.disabled = false
142 - );
133 + function enableSubmitButton( $form ) {
134 + $form.find( 'input[type="submit"], input[type="button"], button[type="submit"]' ).prop( 'disabled', false );
143 135 }
144 136
145 137 /**
146 138 * Disable the save draft link for a given jQuery form object
@@ -149,69 +141,51 @@
149 141 *
150 142 * @param {Object} $form
151 143 */
152 144 function disableSaveDraft( $form ) {
153 - const form = $form instanceof jQuery ? $form.get( 0 ) : $form;
154 - if ( ! form ) {
155 - return;
156 - }
157 - form.querySelectorAll( 'a.frm_save_draft' ).forEach(
158 - link => link.style.pointerEvents = 'none'
159 - );
145 + $form.find( 'a.frm_save_draft' ).css( 'pointer-events', 'none' );
160 146 }
161 147
162 148 /**
163 - * Enable the save draft link for a given form object.
149 + * Enable the save draft link for a given jQuery form object
164 150 *
165 151 * @since 4.04.03
166 152 *
167 - * @param {jQuery|HTMLElement} $form
153 + * @param {Object} $form
168 154 */
169 155 function enableSaveDraft( $form ) {
170 - const form = $form instanceof jQuery ? $form.get( 0 ) : $form;
171 - if ( ! form ) {
156 + if ( ! $form.length ) {
172 157 return;
173 158 }
174 - form.querySelectorAll( '.frm_save_draft' ).forEach( saveDraftButton => {
175 - saveDraftButton.disabled = false;
159 + $form[0].querySelectorAll( '.frm_save_draft' ).forEach( saveDraftButton => {
160 + saveDraftButton.disabled = false;
176 161 saveDraftButton.style.pointerEvents = '';
177 - } );
162 + });
178 163 }
179 164
180 - /**
181 - * Validate form with JS.
182 - *
183 - * @param {HTMLElement|jQuery} object
184 - * @return {Array} Errors.
185 - */
186 165 function validateForm( object ) {
187 - let errors = [];
166 + let errors, r, rl, n, nl, fields, field, requiredFields;
188 167
189 - const vanillaJsObject = 'function' === typeof object.get ? object.get( 0 ) : object;
168 + errors = [];
190 169
191 - // Required field validation.
192 - vanillaJsObject?.querySelectorAll( '.frm_required_field' ).forEach(
193 - requiredField => {
194 - const isVisible = requiredField.offsetParent !== null;
195 - if ( ! isVisible ) {
196 - return;
170 + // Make sure required text field is filled in
171 + requiredFields = jQuery( object ).find(
172 + '.frm_required_field:visible input, .frm_required_field:visible select, .frm_required_field:visible textarea'
173 + ).filter( ':not(.frm_optional)' );
174 + if ( requiredFields.length ) {
175 + for ( r = 0, rl = requiredFields.length; r < rl; r++ ) {
176 + if ( hasClass( requiredFields[r], 'ed_button' ) ) {
177 + // skip rich text field buttons.
178 + continue;
197 179 }
198 -
199 - requiredField.querySelectorAll( 'input, select, textarea' ).forEach(
200 - requiredInput => {
201 - if ( hasClass( requiredInput, 'frm_optional' ) || hasClass( requiredInput, 'ed_button' ) ) {
202 - // skip rich text field buttons.
203 - return;
204 - }
205 -
206 - errors = checkRequiredField( requiredInput, errors );
207 - }
208 - );
180 + errors = checkRequiredField( requiredFields[r], errors );
209 181 }
210 - );
182 + }
211 183
212 - vanillaJsObject?.querySelectorAll( 'input,select,textarea' ).forEach(
213 - field => {
184 + fields = jQuery( object ).find( 'input,select,textarea' );
185 + if ( fields.length ) {
186 + for ( n = 0, nl = fields.length; n < nl; n++ ) {
187 + field = fields[n];
214 188 if ( '' === field.value ) {
215 189 if ( 'number' === field.type ) {
216 190 // A number field will return an empty string when it is invalid.
217 191 checkValidity( field, errors );
@@ -220,9 +194,9 @@
220 194 const isConfirmationField = field.name && 0 === field.name.indexOf( 'item_meta[conf_' );
221 195 if ( ! isConfirmationField ) {
222 196 // Allow a blank confirmation field to still call validateFieldValue.
223 197 // If we continue for a confirmation field there are issues with forms submitting with a blank confirmation field.
224 - return;
198 + continue;
225 199 }
226 200 }
227 201
228 202 validateFieldValue( field, errors, true );
@@ -227,9 +201,9 @@
227 201
228 202 validateFieldValue( field, errors, true );
229 203 checkValidity( field, errors );
230 204 }
231 - );
205 + }
232 206
233 207 // Invisible captchas are processed after validation.
234 208 // We only want to validate a visible captcha on submit.
235 209 if ( ! hasInvisibleRecaptcha( object ) ) {
@@ -247,14 +221,15 @@
247 221 * @param {Array} errors
248 222 * @return {void}
249 223 */
250 224 function checkValidity( field, errors ) {
225 + let fieldID;
251 226 if ( 'object' !== typeof field.validity || false !== field.validity.valid ) {
252 227 return;
253 228 }
254 229
255 - const fieldID = getFieldId( field, true );
256 - if ( errors[ fieldID ] === undefined ) {
230 + fieldID = getFieldId( field, true );
231 + if ( 'undefined' === typeof errors[ fieldID ]) {
257 232 errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
258 233 }
259 234
260 235 if ( 'function' === typeof field.reportValidity ) {
@@ -273,60 +248,31 @@
273 248 function hasClass( element, targetClass ) {
274 249 return element.classList && element.classList.contains( targetClass );
275 250 }
276 251
277 - /**
278 - * @param {HTMLElement} field
279 - */
280 252 function maybeValidateChange( field ) {
281 253 if ( field.type === 'url' ) {
282 - maybeAddHttpsToUrl( field );
254 + maybeAddHttpToUrl( field );
283 255 }
284 - const form = field.closest( 'form' );
285 - if ( ! form ) {
286 - return;
256 + if ( jQuery( field ).closest( 'form' ).hasClass( 'frm_js_validate' ) ) {
257 + validateField( field );
287 258 }
288 -
289 - // Removing stale errors is universal. Adding errors only happens when JS validation is enabled.
290 - validateField( field, hasClass( form, 'frm_js_validate' ) );
291 259 }
292 260
293 - /**
294 - * @param {HTMLElement} field
295 - */
296 - function maybeAddHttpsToUrl( field ) {
261 + function maybeAddHttpToUrl( field ) {
297 262 const url = field.value;
298 263 const matches = url.match( /^(https?|ftps?|mailto|news|feed|telnet):/ );
299 264 if ( field.value !== '' && matches === null ) {
300 - field.value = `https://${ url }`;
265 + field.value = 'http://' + url;
301 266 }
302 267 }
303 268
304 - /**
305 - * Validate a field with JS.
306 - *
307 - * Removing stale errors is universal. Adding errors only happens when JS validation is enabled.
308 - *
309 - * @since 6.32 Added the `addErrors` parameter.
310 - *
311 - * @param {HTMLElement} field
312 - * @param {boolean} addErrors Whether to add new errors. Defaults to `true`.
313 - *
314 - * @return {void}
315 - */
316 - function validateField( field, addErrors = true ) {
317 - let errors;
318 - let key;
269 + function validateField( field ) {
270 + let key,
271 + errors = [],
272 + $fieldCont = jQuery( field ).closest( '.frm_form_field' );
319 273
320 - errors = [];
321 - const fieldContainer = field.closest( '.frm_form_field' );
322 -
323 - if ( ! fieldContainer ) {
324 - // Hidden fields do not have a field container and do not require JS validation.
325 - return;
326 - }
327 -
328 - if ( hasClass( fieldContainer, 'frm_required_field' ) && ! hasClass( field, 'frm_optional' ) ) {
274 + if ( $fieldCont.hasClass( 'frm_required_field' ) && ! jQuery( field ).hasClass( 'frm_optional' ) ) {
329 275 errors = checkRequiredField( field, errors );
330 276 }
331 277
332 278 if ( errors.length < 1 ) {
@@ -332,20 +278,13 @@
332 278 if ( errors.length < 1 ) {
333 279 validateFieldValue( field, errors, false );
334 280 }
335 281
336 - const hasErrors = Object.keys( errors ).length > 0;
337 -
338 - if ( addErrors ) {
339 - removeFieldError( fieldContainer );
340 - if ( hasErrors ) {
341 - for ( key in errors ) {
342 - addFieldError( fieldContainer, key, errors );
343 - }
282 + removeFieldError( $fieldCont );
283 + if ( Object.keys( errors ).length > 0 ) {
284 + for ( key in errors ) {
285 + addFieldError( $fieldCont, key, errors );
344 286 }
345 - } else if ( ! hasErrors ) {
346 - // JS validation is off, so only remove existing errors once the field passes validation.
347 - removeFieldError( fieldContainer );
348 287 }
349 288 }
350 289
351 290 /**
@@ -371,34 +310,23 @@
371 310 } else if ( field.pattern !== null ) {
372 311 checkPatternField( field, errors );
373 312 }
374 313
375 - if ( 'tel' === field.type && shouldCheckConfirmField( field, onSubmit ) ) {
376 - confirmField( field, errors );
377 - }
378 -
379 314 /**
380 315 * @since 6.15 Added `onSubmit` to the data.
381 316 */
382 317 triggerCustomEvent( document, 'frm_validate_field_value', {
383 - field,
384 - errors,
385 - onSubmit
386 - } );
318 + field: field,
319 + errors: errors,
320 + onSubmit: onSubmit
321 + });
387 322 }
388 323
389 - /**
390 - * @param {HTMLElement} field
391 - * @param {Array} errors
392 - * @return {Array} Errors
393 - */
394 324 function checkRequiredField( field, errors ) {
395 - let tempVal;
396 - let i;
397 - let placeholder;
398 - let val = '';
399 - let fieldID = '';
400 - let fileID = field.getAttribute( 'data-frmfile' );
325 + let checkGroup, tempVal, i, placeholder,
326 + val = '',
327 + fieldID = '',
328 + fileID = field.getAttribute( 'data-frmfile' );
401 329
402 330 if ( field.type === 'hidden' && fileID === null && ! isAppointmentField( field ) && ! isInlineDatepickerField( field ) ) {
403 331 return errors;
404 332 }
@@ -403,26 +331,19 @@
403 331 return errors;
404 332 }
405 333
406 334 if ( field.type === 'checkbox' || field.type === 'radio' ) {
407 - document.querySelectorAll( `input[name="${ field.name }"]` ).forEach( function( input ) {
408 - const requiredField = input.closest( '.frm_required_field' );
409 - if ( ! requiredField ) {
410 - return;
411 - }
412 -
413 - const checkedInputs = requiredField.querySelectorAll( 'input:checked' );
414 - checkedInputs.forEach( function( checkedInput ) {
415 - val = checkedInput.value;
416 - } );
417 - } );
335 + checkGroup = jQuery( 'input[name="' + field.name + '"]' ).closest( '.frm_required_field' ).find( 'input:checked' );
336 + jQuery( checkGroup ).each( function() {
337 + val = this.value;
338 + });
418 339 } else if ( field.type === 'file' || fileID ) {
419 - if ( fileID === undefined ) {
340 + if ( typeof fileID === 'undefined' ) {
420 341 fileID = getFieldId( field, true );
421 342 fileID = fileID.replace( 'file', '' );
422 343 }
423 344
424 - if ( errors[ fileID ] === undefined ) {
345 + if ( typeof errors[ fileID ] === 'undefined' ) {
425 346 val = getFileVals( fileID );
426 347 }
427 348 fieldID = fileID;
428 349 } else {
@@ -430,10 +351,9 @@
430 351 // skip hidden other fields
431 352 return errors;
432 353 }
433 354
434 - val = jQuery( field ).val(); // eslint-disable-line no-jquery/no-val
435 -
355 + val = jQuery( field ).val();
436 356 if ( val === null ) {
437 357 val = '';
438 358 } else if ( typeof val !== 'string' ) {
439 359 tempVal = val;
@@ -438,10 +358,10 @@
438 358 } else if ( typeof val !== 'string' ) {
439 359 tempVal = val;
440 360 val = '';
441 361 for ( i = 0; i < tempVal.length; i++ ) {
442 - if ( tempVal[ i ] !== '' ) {
443 - val = tempVal[ i ];
362 + if ( tempVal[i] !== '' ) {
363 + val = tempVal[i];
444 364 }
445 365 }
446 366 }
447 367
@@ -465,11 +385,9 @@
465 385 // set id for time field
466 386 fieldID = fieldID.replace( '-H', '' ).replace( '-m', '' );
467 387 } else if ( isSignatureField( field ) ) {
468 388 if ( val === '' ) {
469 - const fieldContainer = field.closest( '.frm_form_field' );
470 - const outputField = fieldContainer ? fieldContainer.querySelector( `[name="${ field.getAttribute( 'name' ).replace( '[typed]', '[output]' ) }"]` ) : null;
471 - val = outputField ? outputField.value : '';
389 + val = jQuery( field ).closest( '.frm_form_field' ).find( '[name="' + field.getAttribute( 'name' ).replace( '[typed]', '[output]' ) + '"]' ).val();
472 390 }
473 391 fieldID = fieldID.replace( '-typed', '' );
474 392 }
475 393
@@ -490,57 +408,36 @@
490 408
491 409 return errors;
492 410 }
493 411
494 - /**
495 - * @param {HTMLElement} field
496 - * @return {boolean} True if the input is a typed signature input.
497 - */
498 412 function isSignatureField( field ) {
499 413 const name = field.getAttribute( 'name' );
500 414 return 'string' === typeof name && '[typed]' === name.substr( -7 );
501 415 }
502 416
503 - /**
504 - * @param {HTMLElement} field
505 - * @return {boolean} True if the field is a SSA appointment field.
506 - */
507 417 function isAppointmentField( field ) {
508 418 return hasClass( field, 'ssa_appointment_form_field_appointment_id' );
509 419 }
510 420
511 - /**
512 - * @param {HTMLElement} field
513 - * @return {boolean} True if the field is inline datepicker field.
514 - */
515 421 function isInlineDatepickerField( field ) {
516 422 return 'hidden' === field.type && '_alt' === field.id.substr( -4 ) && hasClass( field.nextElementSibling, 'frm_date_inline' );
517 423 }
518 424
519 - /**
520 - * @param {string|number} fileID
521 - * @return {string} File input value.
522 - */
523 425 function getFileVals( fileID ) {
524 - let val = '';
525 - const fileFields = document.querySelectorAll( `input[name="file${ fileID }"], input[name="file${ fileID }[]"], input[name^="item_meta[${ fileID }]"]` );
426 + let val = '',
427 + fileFields = jQuery( 'input[name="file' + fileID + '"], input[name="file' + fileID + '[]"], input[name^="item_meta[' + fileID + ']"]' );
526 428
527 - fileFields.forEach( function( field ) {
429 + fileFields.each( function() {
528 430 if ( val === '' ) {
529 - val = field.value;
431 + val = this.value;
530 432 }
531 - } );
433 + });
532 434 return val;
533 435 }
534 436
535 - /**
536 - * @param {HTMLElement} field
537 - * @param {Array} errors
538 - * @return {void}
539 - */
540 437 function checkUrlField( field, errors ) {
541 - let fieldID;
542 - const url = field.value;
438 + let fieldID,
439 + url = field.value;
543 440
544 441 if ( url !== '' && ! /^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i.test( url ) ) {
545 442 fieldID = getFieldId( field, true );
546 443 if ( ! ( fieldID in errors ) ) {
@@ -555,9 +452,8 @@
555 452 * @since 6.15
556 453 *
557 454 * @param {HTMLElement} field Field input.
558 455 * @param {boolean} onSubmit Is `true` if the form is being submitted.
559 - * @return {boolean} True if we should confirm the field.
560 456 */
561 457 function shouldCheckConfirmField( field, onSubmit ) {
562 458 if ( onSubmit ) {
563 459 // Always check on submitting.
@@ -581,10 +477,10 @@
581 477 * @param {Object} errors Errors data.
582 478 * @param {boolean} onSubmit Is `true` if the form is being submitted.
583 479 */
584 480 function checkEmailField( field, errors, onSubmit ) {
585 - const fieldID = getFieldId( field, true );
586 - const 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;
481 + const fieldID = getFieldId( field, true ),
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;
587 483
588 484 // validate the current field we're editing first
589 485 if ( '' !== field.value && pattern.test( field.value ) === false ) {
590 486 errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
@@ -609,29 +505,25 @@
609 505 confirmField( field, errors );
610 506 }
611 507 }
612 508
613 - /**
614 - * @param {HTMLElement} field
615 - * @param {Array} errors
616 - * @return {void}
617 - */
618 509 function confirmField( field, errors ) {
619 - const fieldID = getFieldId( field, true );
620 - const strippedId = field.id.replace( 'conf_', '' );
621 - const strippedFieldID = fieldID.replace( 'conf_', '' );
622 - const confirmField = document.getElementById( strippedId.replace( 'field_', 'field_conf_' ) );
510 + let value, confirmValue, firstField,
511 + fieldID = getFieldId( field, true ),
512 + strippedId = field.id.replace( 'conf_', '' ),
513 + strippedFieldID = fieldID.replace( 'conf_', '' ),
514 + confirmField = document.getElementById( strippedId.replace( 'field_', 'field_conf_' ) );
623 515
624 - if ( ! confirmField || errors[ `conf_${ strippedFieldID }` ] !== undefined ) {
516 + if ( confirmField === null || typeof errors[ 'conf_' + strippedFieldID ] !== 'undefined' ) {
625 517 return;
626 518 }
627 519
628 520 if ( fieldID !== strippedFieldID ) {
629 - const firstField = document.getElementById( strippedId );
630 - const { value } = firstField;
631 - const confirmValue = confirmField.value;
521 + firstField = document.getElementById( strippedId );
522 + value = firstField.value;
523 + confirmValue = confirmField.value;
632 524 if ( value !== confirmValue ) {
633 - errors[ `conf_${ strippedFieldID }` ] = getFieldValidationMessage( confirmField, 'data-confmsg' );
525 + errors[ 'conf_' + strippedFieldID ] = getFieldValidationMessage( confirmField, 'data-confmsg' );
634 526 }
635 527 } else {
636 528 validateField( confirmField );
637 529 }
@@ -636,16 +528,11 @@
636 528 validateField( confirmField );
637 529 }
638 530 }
639 531
640 - /**
641 - * @param {HTMLElement} field
642 - * @param {Array} errors
643 - * @return {void}
644 - */
645 532 function checkNumberField( field, errors ) {
646 - let fieldID;
647 - const number = field.value;
533 + let fieldID,
534 + number = field.value;
648 535
649 536 if ( number !== '' && isNaN( number / 1 ) !== false ) {
650 537 fieldID = getFieldId( field, true );
651 538 if ( ! ( fieldID in errors ) ) {
@@ -653,17 +540,12 @@
653 540 }
654 541 }
655 542 }
656 543
657 - /**
658 - * @param {HTMLElement} field
659 - * @param {Array} errors
660 - * @return {void}
661 - */
662 544 function checkPatternField( field, errors ) {
663 - let fieldID;
664 - const text = field.value;
665 - let format = getFieldValidationMessage( field, 'pattern' );
545 + let fieldID,
546 + text = field.value,
547 + format = getFieldValidationMessage( field, 'pattern' );
666 548
667 549 if ( format !== '' && text !== '' ) {
668 550 fieldID = getFieldId( field, true );
669 551 if ( ! ( fieldID in errors ) ) {
@@ -671,9 +553,9 @@
671 553 if ( ! window.frmProForm.validateIntlPhoneInput( field ) ) {
672 554 errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
673 555 }
674 556 } else {
675 - format = new RegExp( `^${ format }$`, 'i' );
557 + format = new RegExp( '^' + format + '$', 'i' );
676 558 if ( format.test( text ) === false ) {
677 559 errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' );
678 560 }
679 561 }
@@ -686,11 +568,13 @@
686 568 *
687 569 * @since 6.5.1
688 570 */
689 571 function setSelectPlaceholderColor() {
690 - const selects = document.querySelectorAll( '.form-field select' );
691 - const styleElement = document.querySelector( '.with_frm_style' );
692 - const textColorDisabled = styleElement ? getComputedStyle( styleElement ).getPropertyValue( '--text-color-disabled' ).trim() : '';
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 +
693 577 // Exit if there are no select elements or the textColorDisabled property is missing
694 578 if ( ! selects.length || ! textColorDisabled ) {
695 579 return;
696 580 }
@@ -695,10 +579,10 @@
695 579 return;
696 580 }
697 581
698 582 // Function to change the color of a select element
699 - const changeSelectColor = function( select ) {
700 - if ( select.options[ select.selectedIndex ] && hasClass( select.options[ select.selectedIndex ], 'frm-select-placeholder' ) ) {
583 + changeSelectColor = function( select ) {
584 + if ( select.options[select.selectedIndex] && hasClass( select.options[select.selectedIndex], 'frm-select-placeholder' ) ) {
701 585 select.style.setProperty( 'color', textColorDisabled, 'important' );
702 586 } else {
703 587 select.style.color = '';
704 588 }
@@ -711,68 +595,50 @@
711 595
712 596 // Add an event listener for future changes
713 597 select.addEventListener( 'change', function() {
714 598 changeSelectColor( select );
715 - } );
716 - } );
599 + });
600 + });
717 601 }
718 602
719 - /**
720 - * @param {HTMLElement|jQuery} object
721 - *
722 - * @return {HTMLElement|false} Captcha element if there is an invisible recaptcha.
723 - */
724 603 function hasInvisibleRecaptcha( object ) {
604 + let recaptcha, recaptchaID, alreadyChecked;
605 +
725 606 if ( isGoingToPrevPage( object ) ) {
726 607 return false;
727 608 }
728 609
729 - const form = object instanceof jQuery ? object.get( 0 ) : object;
730 - if ( ! form ) {
731 - return false;
732 - }
733 -
734 - const recaptcha = form.querySelector( '.frm-g-recaptcha[data-size="invisible"], .g-recaptcha[data-size="invisible"]' );
735 - if ( recaptcha ) {
736 - const recaptchaID = recaptcha.dataset.rid;
737 - const alreadyChecked = grecaptcha.getResponse( recaptchaID );
610 + recaptcha = jQuery( object ).find( '.frm-g-recaptcha[data-size="invisible"], .g-recaptcha[data-size="invisible"]' );
611 + if ( recaptcha.length ) {
612 + recaptchaID = recaptcha.data( 'rid' );
613 + alreadyChecked = grecaptcha.getResponse( recaptchaID );
738 614 if ( alreadyChecked.length === 0 ) {
739 615 return recaptcha;
740 616 }
741 617 }
742 -
743 618 return false;
744 619 }
745 620
746 - /**
747 - * @param {HTMLElement} invisibleRecaptcha
748 - *
749 - * @return {void}
750 - */
751 621 function executeInvisibleRecaptcha( invisibleRecaptcha ) {
752 - const recaptchaID = invisibleRecaptcha.dataset.rid;
622 + const recaptchaID = invisibleRecaptcha.data( 'rid' );
753 623 grecaptcha.reset( recaptchaID );
754 624 grecaptcha.execute( recaptchaID );
755 625 }
756 626
757 627 function validateRecaptcha( form, errors ) {
758 - const formEl = form instanceof jQuery ? form.get( 0 ) : form;
759 - if ( ! formEl ) {
760 - return errors;
761 - }
628 + let response;
762 629
763 - const recaptcha = formEl.querySelector( '.frm-g-recaptcha' );
764 - if ( ! recaptcha ) {
630 + const $recaptcha = jQuery( form ).find( '.frm-g-recaptcha' );
631 + if ( ! $recaptcha.length ) {
765 632 return errors;
766 633 }
767 634
768 - const recaptchaID = recaptcha.dataset.rid;
769 - let response;
635 + const recaptchaID = $recaptcha.data( 'rid' );
770 636
771 637 try {
772 638 response = grecaptcha.getResponse( recaptchaID );
773 639 } catch ( e ) {
774 - if ( formEl.querySelector( 'input[name="recaptcha_checked"]' ) ) {
640 + if ( jQuery( form ).find( 'input[name="recaptcha_checked"]' ).length ) {
775 641 return errors;
776 642 }
777 643 response = '';
778 644 }
@@ -777,13 +643,11 @@
777 643 response = '';
778 644 }
779 645
780 646 if ( response.length === 0 ) {
781 - const fieldContainer = recaptcha.closest( '.frm_form_field' );
782 - if ( fieldContainer?.id ) {
783 - const fieldID = fieldContainer.id.replace( 'frm_field_', '' ).replace( '_container', '' );
784 - errors[ fieldID ] = '';
785 - }
647 + const fieldContainer = $recaptcha.closest( '.frm_form_field' );
648 + const fieldID = fieldContainer.attr( 'id' ).replace( 'frm_field_', '' ).replace( '_container', '' );
649 + errors[ fieldID ] = '';
786 650 }
787 651
788 652 return errors;
789 653 }
@@ -816,15 +680,15 @@
816 680 if ( null === errorHtml ) {
817 681 return msg;
818 682 }
819 683
820 - errorHtml = errorHtml.replace( /\+/g, '%20' );
821 - msg = decodeURIComponent( errorHtml ).replace( '[error]', msg );
822 - const fieldId = getFieldId( field, false );
823 - const split = fieldId.split( '-' );
684 + errorHtml = errorHtml.replace( /\+/g, '%20' );
685 + msg = decodeURIComponent( errorHtml ).replace( '[error]', msg );
686 + const fieldId = getFieldId( field, false );
687 + const split = fieldId.split( '-' );
824 688 const fieldIdParts = field.id.split( '_' );
825 689 fieldIdParts.shift(); // Drop the "field" value from the front.
826 - split[ 0 ] = fieldIdParts.join( '_' );
690 + split[0] = fieldIdParts.join( '_' );
827 691 const errorKey = split.join( '-' );
828 692 return msg.replace( '[key]', errorKey );
829 693 }
830 694
@@ -851,37 +715,30 @@
851 715 return validate;
852 716 }
853 717
854 718 /**
855 - * @param {HTMLElement} object
856 - * @param {string} action
719 + * @param {HTMLElement} object
720 + * @param {string|undefined} action
857 721 * @return {void}
858 722 */
859 723 function getFormErrors( object, action ) {
860 - const fieldsets = object.querySelectorAll( '.frm_form_field' );
861 - fieldsets.forEach( field => field.classList.add( 'frm_doing_ajax' ) );
724 + let fieldset, data, success, error, shouldTriggerEvent;
862 725
863 - const data = `${ jQuery( object ).serialize() }&action=frm_entries_${ action }&nonce=${ frm_js.nonce }`; // eslint-disable-line no-jquery/no-serialize
864 - const shouldTriggerEvent = object.classList.contains( 'frm_trigger_event_on_submit' );
726 + if ( typeof action === 'undefined' ) {
727 + jQuery( object ).find( 'input[name="frm_action"]' ).val();
728 + }
865 729
866 - const doRedirect = response => {
867 - jQuery( document ).trigger( 'frmBeforeFormRedirect', [ object, response ] );
730 + fieldset = jQuery( object ).find( '.frm_form_field' );
731 + fieldset.addClass( 'frm_doing_ajax' );
868 732
869 - if ( ! response.openInNewTab ) {
870 - // We return here because we're redirecting there is no need to update content.
871 - window.location = response.redirect;
872 - return;
873 - }
733 + data = jQuery( object ).serialize() + '&action=frm_entries_' + action + '&nonce=' + frm_js.nonce; // eslint-disable-line camelcase
734 + shouldTriggerEvent = object.classList.contains( 'frm_trigger_event_on_submit' );
874 735
875 - // We don't return here because we're opening in a new tab, the old tab will still update.
876 - const newTab = window.open( response.redirect, '_blank' );
877 - if ( ! newTab && response.fallbackMsg && response.content ) {
878 - response.content = response.content.trim().replace( /(<\/div><\/div>)$/, ` ${ response.fallbackMsg }</div></div>` );
879 - }
880 - };
736 + success = function( response ) {
737 + let defaultResponse, formID, replaceContent, pageOrder, formReturned, contSubmit, delay,
738 + $fieldCont, key, inCollapsedSection, frmTrigger, newTab;
881 739
882 - const success = function( response ) {
883 - const defaultResponse = {
740 + defaultResponse = {
884 741 content: '',
885 742 errors: {},
886 743 pass: false
887 744 };
@@ -887,59 +744,61 @@
887 744 };
888 745
889 746 if ( response === null ) {
890 747 response = defaultResponse;
748 + }
749 +
750 + response = response.replace( /^\s+|\s+$/g, '' );
751 + if ( response.indexOf( '{' ) === 0 ) {
752 + response = JSON.parse( response );
891 753 } else {
892 - // Response is a string. Convert it to an object.
893 - response = response.replace( /^\s+|\s+$/g, '' );
894 - if ( response.indexOf( '{' ) === 0 ) {
895 - response = JSON.parse( response );
896 - } else {
897 - response = defaultResponse;
898 - }
754 + response = defaultResponse;
899 755 }
900 756
901 - let willRedirect = false;
902 -
903 - if ( response.redirect !== undefined ) {
757 + if ( typeof response.redirect !== 'undefined' ) {
904 758 if ( shouldTriggerEvent ) {
905 759 triggerCustomEvent( object, 'frmSubmitEvent' );
906 760 return;
907 761 }
908 762
909 - if ( response.delay ) {
910 - setTimeout( function() {
911 - doRedirect( response );
912 - }, 1000 * response.delay );
913 - } else {
914 - doRedirect( response );
763 + jQuery( document ).trigger( 'frmBeforeFormRedirect', [ object, response ]);
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;
915 769 }
916 770
917 - willRedirect = true;
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 + }
918 776 }
919 777
920 - if ( 'string' === typeof response.content && response.content !== '' ) {
778 + if ( response.content !== '' ) {
921 779 // the form or success message was returned
922 780
923 781 if ( shouldTriggerEvent ) {
924 - triggerCustomEvent( object, 'frmSubmitEvent', { content: response.content } );
782 + triggerCustomEvent( object, 'frmSubmitEvent', { content: response.content });
925 783 return;
926 784 }
927 785
928 786 removeSubmitLoading( jQuery( object ) );
929 - if ( frm_js.offset != -1 ) {
787 + if ( frm_js.offset != -1 ) { // eslint-disable-line camelcase
930 788 frmFrontForm.scrollMsg( jQuery( object ), false );
931 789 }
932 790
933 - const formIdInput = object.querySelector( 'input[name="form_id"]' );
934 - const formID = formIdInput ? formIdInput.value : '';
791 + formID = jQuery( object ).find( 'input[name="form_id"]' ).val();
935 792 response.content = response.content.replace( / frm_pro_form /g, ' frm_pro_form frm_no_hide ' );
936 - const replaceContent = jQuery( object ).closest( '.frm_forms' ); // eslint-disable-line no-jquery/no-closest
793 + replaceContent = jQuery( object ).closest( '.frm_forms' );
937 794 removeAddedScripts( replaceContent, formID );
938 - const delay = maybeSlideOut( replaceContent, response.content );
795 + delay = maybeSlideOut( replaceContent, response.content );
939 796
940 797 setTimeout(
941 798 function() {
799 + let container, input, previousInput;
800 +
942 801 afterFormSubmittedBeforeReplace( object, response );
943 802
944 803 replaceContent.replaceWith( response.content );
945 804
@@ -945,41 +804,48 @@
945 804
946 805 addUrlParam( response );
947 806
948 807 if ( typeof frmThemeOverride_frmAfterSubmit === 'function' ) { // eslint-disable-line camelcase
949 - const pageOrderInput = document.querySelector( `input[name="frm_page_order_${ formID }"]` );
950 - const pageOrder = pageOrderInput ? pageOrderInput.value : '';
951 - const tempDiv = document.createElement( 'div' );
952 - tempDiv.innerHTML = response.content;
953 - const formReturnedInput = tempDiv.querySelector( 'input[name="form_id"]' );
954 - const formReturned = formReturnedInput ? formReturnedInput.value : '';
808 + pageOrder = jQuery( 'input[name="frm_page_order_' + formID + '"]' ).val();
809 + formReturned = jQuery( response.content ).find( 'input[name="form_id"]' ).val();
955 810 frmThemeOverride_frmAfterSubmit( formReturned, pageOrder, response.content, object );
956 811 }
957 812
813 + if ( typeof response.recaptcha !== 'undefined' ) {
814 + container = jQuery( '#frm_form_' + formID + '_container' ).find( '.frm_fields_container' );
815 + input = '<input type="hidden" name="recaptcha_checked" value="' + response.recaptcha + '">';
816 + previousInput = container.find( 'input[name="recaptcha_checked"]' );
817 +
818 + if ( previousInput.length ) {
819 + previousInput.replaceWith( input );
820 + } else {
821 + container.append( input );
822 + }
823 + }
824 +
958 825 afterFormSubmitted( object, response );
959 826 },
960 827 delay
961 828 );
962 - } else if ( response.errors !== undefined && Object.keys( response.errors ).length ) {
829 + } else if ( Object.keys( response.errors ).length ) {
963 830 // errors were returned
964 831 removeSubmitLoading( jQuery( object ), 'enable' );
965 832
966 833 //show errors
967 - let contSubmit = true;
834 + contSubmit = true;
968 835 removeAllErrors();
969 836
970 - let $fieldCont = null;
837 + $fieldCont = null;
971 838
972 - for ( const key in response.errors ) {
973 - const fieldContEl = object.querySelector( `#frm_field_${ key }_container` );
974 - $fieldCont = fieldContEl ? jQuery( fieldContEl ) : jQuery();
839 + for ( key in response.errors ) {
840 + $fieldCont = jQuery( object ).find( '#frm_field_' + key + '_container' );
975 841
976 842 if ( $fieldCont.length ) {
977 - if ( ! $fieldCont.is( ':visible' ) ) { // eslint-disable-line no-jquery/no-is
978 - const inCollapsedSection = $fieldCont.closest( '.frm_toggle_container' ); // eslint-disable-line no-jquery/no-closest, formidable/no-jquery-variable-methods
843 + if ( ! $fieldCont.is( ':visible' ) ) {
844 + inCollapsedSection = $fieldCont.closest( '.frm_toggle_container' );
979 845 if ( inCollapsedSection.length ) {
980 - let frmTrigger = inCollapsedSection.prev();
981 - if ( ! frmTrigger.hasClass( 'frm_trigger' ) ) { // eslint-disable-line formidable/no-jquery-variable-methods
846 + frmTrigger = inCollapsedSection.prev();
847 + if ( ! frmTrigger.hasClass( 'frm_trigger' ) ) {
982 848 // If the frmTrigger object is the section description, check to see if the previous element is the trigger
983 849 frmTrigger = frmTrigger.prev( '.frm_trigger' );
984 850 }
985 851 frmTrigger.trigger( 'click' );
@@ -985,9 +851,9 @@
985 851 frmTrigger.trigger( 'click' );
986 852 }
987 853 }
988 854
989 - if ( $fieldCont.is( ':visible' ) ) { // eslint-disable-line no-jquery/no-is
855 + if ( $fieldCont.is( ':visible' ) ) {
990 856 addFieldError( $fieldCont, key, response.errors );
991 857 contSubmit = false;
992 858 }
993 859 }
@@ -992,10 +858,11 @@
992 858 }
993 859 }
994 860 }
995 861
996 - object.querySelectorAll( '.frm-g-recaptcha, .g-recaptcha, .h-captcha' ).forEach( function( captchaEl ) {
997 - const recaptchaID = captchaEl.dataset.rid;
862 + jQuery( object ).find( '.frm-g-recaptcha, .g-recaptcha, .h-captcha' ).each( function() {
863 + const $recaptcha = jQuery( this ),
864 + recaptchaID = $recaptcha.data( 'rid' );
998 865
999 866 if ( typeof grecaptcha !== 'undefined' && grecaptcha ) {
1000 867 if ( recaptchaID ) {
1001 868 grecaptcha.reset( recaptchaID );
@@ -1002,23 +869,16 @@
1002 869 } else {
1003 870 grecaptcha.reset();
1004 871 }
1005 872 }
1006 -
1007 873 if ( typeof hcaptcha !== 'undefined' && hcaptcha ) {
1008 874 hcaptcha.reset();
1009 875 }
1010 - } );
876 + });
1011 877
1012 - if ( window.turnstile ) {
1013 - object.querySelectorAll( '.frm-cf-turnstile' ).forEach(
1014 - turnstileField => turnstileField.dataset.rid && turnstile.reset( turnstileField.dataset.rid )
1015 - );
1016 - }
878 + jQuery( document ).trigger( 'frmFormErrors', [ object, response ]);
1017 879
1018 - jQuery( document ).trigger( 'frmFormErrors', [ object, response ] );
1019 -
1020 - fieldsets.forEach( field => field.classList.remove( 'frm_doing_ajax' ) );
880 + fieldset.removeClass( 'frm_doing_ajax' );
1021 881 scrollToFirstField( object );
1022 882
1023 883 if ( contSubmit ) {
1024 884 object.submit();
@@ -1025,10 +885,10 @@
1025 885 } else {
1026 886 object.insertAdjacentHTML( 'afterbegin', response.error_message );
1027 887 checkForErrorsAndMaybeSetFocus();
1028 888 }
1029 - } else if ( ! willRedirect ) { // Avoid double submission if redirecting to a page.
1030 - // There may have been a plugin conflict, or the form is not set to submit with ajax.
889 + } else {
890 + // there may have been a plugin conflict, or the form is not set to submit with ajax
1031 891
1032 892 showFileLoading( object );
1033 893
1034 894 object.submit();
@@ -1034,12 +894,10 @@
1034 894 object.submit();
1035 895 }
1036 896 };
1037 897
1038 - const error = function() {
1039 - object.querySelectorAll( 'input[type="submit"], input[type="button"]' ).forEach(
1040 - button => button.disabled = false
1041 - );
898 + error = function() {
899 + jQuery( object ).find( 'input[type="submit"], input[type="button"]' ).prop( 'disabled', false );
1042 900 object.submit();
1043 901 };
1044 902
1045 903 postToAjaxUrl( object, data, success, error );
@@ -1045,20 +903,22 @@
1045 903 postToAjaxUrl( object, data, success, error );
1046 904 }
1047 905
1048 906 function postToAjaxUrl( form, data, success, error ) {
1049 - let ajaxUrl = frm_js.ajax_url;
1050 - const action = form.getAttribute( 'action' );
907 + let ajaxUrl, action, ajaxParams;
1051 908
1052 - if ( 'string' === typeof action && action.includes( '?action=frm_forms_preview' ) ) {
1053 - ajaxUrl = action.split( '?action=frm_forms_preview' )[ 0 ];
909 + ajaxUrl = frm_js.ajax_url; // eslint-disable-line camelcase
910 + action = form.getAttribute( 'action' );
911 +
912 + if ( 'string' === typeof action && -1 !== action.indexOf( '?action=frm_forms_preview' ) ) {
913 + ajaxUrl = action.split( '?action=frm_forms_preview' )[0];
1054 914 }
1055 915
1056 - const ajaxParams = {
916 + ajaxParams = {
1057 917 type: 'POST',
1058 918 url: ajaxUrl,
1059 - data,
1060 - success
919 + data: data,
920 + success: success
1061 921 };
1062 922
1063 923 if ( 'function' === typeof error ) {
1064 924 ajaxParams.error = error;
@@ -1063,19 +923,17 @@
1063 923 if ( 'function' === typeof error ) {
1064 924 ajaxParams.error = error;
1065 925 }
1066 926
1067 - jQuery.ajax( ajaxParams ); // eslint-disable-line no-jquery/no-ajax
927 + jQuery.ajax( ajaxParams );
1068 928 }
1069 929
1070 930 function afterFormSubmitted( object, response ) {
1071 - const tempDiv = document.createElement( 'div' );
1072 - tempDiv.innerHTML = response.content;
1073 - const formCompleted = tempDiv.querySelector( '.frm_message' );
1074 - if ( formCompleted ) {
1075 - jQuery( document ).trigger( 'frmFormComplete', [ object, response ] );
931 + const formCompleted = jQuery( response.content ).find( '.frm_message' );
932 + if ( formCompleted.length ) {
933 + jQuery( document ).trigger( 'frmFormComplete', [ object, response ]);
1076 934 } else {
1077 - jQuery( document ).trigger( 'frmPageChanged', [ object, response ] );
935 + jQuery( document ).trigger( 'frmPageChanged', [ object, response ]);
1078 936 }
1079 937 }
1080 938
1081 939 /**
@@ -1087,30 +945,28 @@
1087 945 * @param {Object} response The response from submitting the form with AJAX.
1088 946 * @return {void}
1089 947 */
1090 948 function afterFormSubmittedBeforeReplace( object, response ) {
1091 - const tempDiv = document.createElement( 'div' );
1092 - tempDiv.innerHTML = response.content;
1093 - const formCompleted = tempDiv.querySelector( '.frm_message' );
1094 - if ( formCompleted ) {
1095 - triggerCustomEvent( document, 'frmFormCompleteBeforeReplace', { object, response } );
949 + const formCompleted = jQuery( response.content ).find( '.frm_message' );
950 + if ( formCompleted.length ) {
951 + triggerCustomEvent( document, 'frmFormCompleteBeforeReplace', { object, response });
1096 952 }
1097 953 }
1098 954
1099 955 function removeAddedScripts( formContainer, formID ) {
1100 - const endReplace = document.querySelectorAll( `.frm_end_ajax_${ formID }` );
956 + const endReplace = jQuery( '.frm_end_ajax_' + formID );
1101 957 if ( endReplace.length ) {
1102 - formContainer.nextUntil( `.frm_end_ajax_${ formID }` ).remove();
1103 - endReplace.forEach( el => el.remove() );
958 + formContainer.nextUntil( '.frm_end_ajax_' + formID ).remove();
959 + endReplace.remove();
1104 960 }
1105 961 }
1106 962
1107 963 function maybeSlideOut( oldContent, newContent ) {
1108 - let c;
1109 - let newClass = 'frm_slideout';
1110 - if ( newContent.includes( ' frm_slide' ) ) {
964 + let c,
965 + newClass = 'frm_slideout';
966 + if ( newContent.indexOf( ' frm_slide' ) !== -1 ) {
1111 967 c = oldContent.children();
1112 - if ( newContent.includes( ' frm_going_back' ) ) {
968 + if ( newContent.indexOf( ' frm_going_back' ) !== -1 ) {
1113 969 newClass += ' frm_going_back';
1114 970 }
1115 971 c.removeClass( 'frm_going_back' );
1116 972 c.addClass( newClass );
@@ -1120,27 +976,29 @@
1120 976 }
1121 977
1122 978 function addUrlParam( response ) {
1123 979 let url;
1124 - if ( history.pushState && response.page !== undefined ) {
980 + if ( history.pushState && typeof response.page !== 'undefined' ) {
1125 981 url = addQueryVar( 'frm_page', response.page );
1126 - window.history.pushState( { html: response.html }, '', `?${ url }` );
982 + window.history.pushState({ 'html': response.html }, '', '?' + url );
1127 983 }
1128 984 }
1129 985
1130 986 function addQueryVar( key, value ) {
987 + let kvp, i, x;
988 +
1131 989 key = encodeURI( key );
1132 990 value = encodeURI( value );
1133 991
1134 - const kvp = document.location.search.substr( 1 ).split( '&' );
992 + kvp = document.location.search.substr( 1 ).split( '&' );
1135 993
1136 - let i = kvp.length;
994 + i = kvp.length;
1137 995 while ( i-- ) {
1138 - const x = kvp[ i ].split( '=' );
996 + x = kvp[i].split( '=' );
1139 997
1140 - if ( x[ 0 ] == key ) {
1141 - x[ 1 ] = value;
1142 - kvp[ i ] = x.join( '=' );
998 + if ( x[0] == key ) {
999 + x[1] = value;
1000 + kvp[i] = x.join( '=' );
1143 1001 break;
1144 1002 }
1145 1003 }
1146 1004
@@ -1151,59 +1009,44 @@
1151 1009 return kvp.join( '&' );
1152 1010 }
1153 1011
1154 1012 function addFieldError( $fieldCont, key, jsErrors ) {
1155 - const container = $fieldCont instanceof jQuery ? $fieldCont.get( 0 ) : $fieldCont;
1013 + let input, id, describedBy, roleString;
1014 + if ( $fieldCont.length && $fieldCont.is( ':visible' ) ) {
1015 + $fieldCont.addClass( 'frm_blank_field' );
1016 + input = $fieldCont.find( 'input, select, textarea' );
1017 + id = getErrorElementId( key, input.get( 0 ) );
1156 1018
1157 - if ( ! container || container.offsetParent === null ) {
1158 - return;
1159 - }
1019 + describedBy = input.attr( 'aria-describedby' );
1160 1020
1161 - container.classList.add( 'frm_blank_field' );
1162 - const inputs = container.querySelectorAll( 'input, select, textarea' );
1163 - const id = getErrorElementId( key, inputs[ 0 ] );
1021 + if ( typeof frmThemeOverride_frmPlaceError === 'function' ) { // eslint-disable-line camelcase
1022 + frmThemeOverride_frmPlaceError( key, jsErrors );
1023 + } else {
1024 + if ( -1 !== jsErrors[key].indexOf( '<div' ) ) {
1025 + $fieldCont.append(
1026 + jsErrors[key]
1027 + );
1028 + } else {
1029 + roleString = frm_js.include_alert_role ? 'role="alert"' : ''; // eslint-disable-line camelcase
1030 + $fieldCont.append( '<div class="frm_error" ' + roleString + ' id="' + id + '">' + jsErrors[key] + '</div>' );
1031 + }
1164 1032
1165 - let describedBy;
1166 -
1167 - if ( typeof frmThemeOverride_frmPlaceError === 'function' ) { // eslint-disable-line camelcase
1168 - frmThemeOverride_frmPlaceError( key, jsErrors );
1169 - } else {
1170 - let errorHtml;
1171 - if ( jsErrors[ key ].includes( '<div' ) ) {
1172 - errorHtml = jsErrors[ key ];
1173 - } else {
1174 - const roleString = frm_js.include_alert_role ? 'role="alert"' : '';
1175 - errorHtml = `<div class="frm_error" ${ roleString } id="${ id }">${ jsErrors[ key ] }</div>`;
1176 - }
1177 - container.insertAdjacentHTML( 'beforeend', errorHtml );
1178 - inputs.forEach( input => {
1179 - describedBy = input.getAttribute( 'aria-describedby' );
1180 - if ( ! describedBy ) {
1033 + if ( typeof describedBy === 'undefined' ) {
1181 1034 describedBy = id;
1182 - } else if ( ! describedBy.includes( id ) && ! describedBy.includes( 'frm_error_field_' ) ) {
1183 - const { errorFirst } = input.dataset;
1184 - if ( errorFirst === '0' ) {
1185 - describedBy = `${ describedBy } ${ id }`;
1035 + } else if ( describedBy.indexOf( id ) === -1 && describedBy.indexOf( 'frm_error_field_' ) === -1 ) {
1036 + if ( input.data( 'error-first' ) === 0 ) {
1037 + describedBy = describedBy + ' ' + id;
1186 1038 } else {
1187 - describedBy = `${ id } ${ describedBy }`;
1039 + describedBy = id + ' ' + describedBy;
1188 1040 }
1189 1041 }
1190 - input.setAttribute( 'aria-describedby', describedBy );
1191 - } );
1192 - }
1193 1042
1194 - inputs.forEach( input => {
1195 - if ( [ 'radio', 'checkbox' ].includes( input.type ) ) {
1196 - const group = input.closest( '[role="radiogroup"], [role="group"]' );
1197 - if ( group ) {
1198 - group.setAttribute( 'aria-invalid', 'true' );
1199 - }
1200 - } else {
1201 - input.setAttribute( 'aria-invalid', 'true' );
1043 + input.attr( 'aria-describedby', describedBy );
1202 1044 }
1203 - } );
1045 + input.attr( 'aria-invalid', true );
1204 1046
1205 - jQuery( document ).trigger( 'frmAddFieldError', [ jQuery( container ), key, jsErrors ] );
1047 + jQuery( document ).trigger( 'frmAddFieldError', [ $fieldCont, key, jsErrors ]);
1048 + }
1206 1049 }
1207 1050
1208 1051 /**
1209 1052 * Get the ID to use for an error element added when submitting with AJAX.
@@ -1212,13 +1055,13 @@
1212 1055 * @param {HTMLElement} input
1213 1056 * @return {string} The ID to use for the error element.
1214 1057 */
1215 1058 function getErrorElementId( key, input ) {
1216 - if ( isNaN( key ) || ! input || ! input.id ) {
1059 + if ( isNaN( key ) || ! input.id ) {
1217 1060 // If key isn't a number, assume it's already in the right format.
1218 - return `frm_error_field_${ key }`;
1061 + return 'frm_error_field_' + key;
1219 1062 }
1220 - return `frm_error_${ input.id }`;
1063 + return 'frm_error_' + input.id;
1221 1064 }
1222 1065
1223 1066 /**
1224 1067 * Removes errors before validating with JS.
@@ -1223,72 +1066,36 @@
1223 1066 /**
1224 1067 * Removes errors before validating with JS.
1225 1068 * This prevents issues with stale errors that has since been fixed.
1226 1069 *
1227 - * @param {HTMLElement|jQuery} fieldCont Field container element.
1070 + * @param {Object} $fieldCont jQuery object.
1228 1071 * @return {void}
1229 1072 */
1230 - function removeFieldError( fieldCont ) {
1231 - const container = fieldCont instanceof jQuery ? fieldCont.get( 0 ) : fieldCont;
1232 - if ( ! container ) {
1233 - return;
1073 + function removeFieldError( $fieldCont ) {
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' );
1078 +
1079 + const fieldContainer = $fieldCont.get( 0 );
1080 + if ( fieldContainer && fieldContainer.classList ) {
1081 + fieldContainer.classList.remove( 'frm_blank_field', 'has-error' );
1234 1082 }
1235 1083
1236 - const errorMessage = container.querySelector( '.frm_error' );
1237 - const input = container.querySelector( 'input, select, textarea' );
1084 + errorMessage.remove();
1085 + input.attr( 'aria-invalid', false );
1086 + input.removeAttr( 'aria-describedby' );
1238 1087
1239 - container.classList.remove( 'frm_blank_field', 'has-error' );
1240 -
1241 - if ( input ) {
1242 - if ( 'true' === input.getAttribute( 'aria-invalid' ) ) {
1243 - input.setAttribute( 'aria-invalid', 'false' );
1244 - } else if ( [ 'radio', 'checkbox' ].includes( input.type ) ) {
1245 - const group = input.closest( '[role="radiogroup"], [role="group"]' );
1246 - if ( group ) {
1247 - group.setAttribute( 'aria-invalid', 'false' );
1248 - }
1249 - }
1088 + if ( typeof describedBy !== 'undefined' ) {
1089 + describedBy = describedBy.replace( errorId, '' );
1090 + input.attr( 'aria-describedby', describedBy );
1250 1091 }
1251 -
1252 - if ( errorMessage ) {
1253 - removeElementFromInputDescribedBy( errorMessage );
1254 - errorMessage.remove();
1255 - }
1256 1092 }
1257 1093
1258 - /**
1259 - * Updates the aria-describedby attribute, removing the target element ID.
1260 - *
1261 - * @since 6.32
1262 - *
1263 - * @param {HTMLElement} el The target element that is removed from the aria-describedby data.
1264 - * @return {void}
1265 - */
1266 - function removeElementFromInputDescribedBy( el ) {
1267 - document.querySelectorAll( `[aria-describedby*="${ el.id }"]` ).forEach( input => {
1268 - let ariaDescribedBy = input.getAttribute( 'aria-describedby' ).split( ' ' );
1269 - ariaDescribedBy = ariaDescribedBy.filter( value => {
1270 - const trimmedValue = value.trim();
1271 - return trimmedValue && trimmedValue !== el.id;
1272 - } );
1273 -
1274 - if ( ariaDescribedBy.length ) {
1275 - input.setAttribute( 'aria-describedby', ariaDescribedBy.join( ' ' ) );
1276 - return;
1277 - }
1278 - input.removeAttribute( 'aria-describedby' );
1279 - } );
1280 - }
1281 -
1282 1094 function removeAllErrors() {
1283 - document.querySelectorAll( '.form-field' ).forEach( field => {
1284 - field.classList.remove( 'frm_blank_field', 'has-error' );
1285 - } );
1286 - document.querySelectorAll( '.form-field .frm_error' ).forEach( el => {
1287 - removeElementFromInputDescribedBy( el );
1288 - el.remove();
1289 - } );
1290 - document.querySelectorAll( '.frm_error_style' ).forEach( error => error.remove() );
1095 + jQuery( '.form-field' ).removeClass( 'frm_blank_field has-error' );
1096 + jQuery( '.form-field .frm_error' ).replaceWith( '' );
1097 + jQuery( '.frm_error_style' ).remove();
1291 1098 }
1292 1099
1293 1100 /**
1294 1101 * @param {HTMLElement|Object} object Form object.
@@ -1311,9 +1118,9 @@
1311 1118 disableSaveDraft( $object );
1312 1119 }
1313 1120
1314 1121 function showLoadingIndicator( $object ) {
1315 - if ( ! $object.hasClass( 'frm_loading_form' ) && ! $object.hasClass( 'frm_loading_prev' ) ) { // eslint-disable-line no-jquery/no-class, formidable/no-jquery-variable-methods
1122 + if ( ! $object.hasClass( 'frm_loading_form' ) && ! $object.hasClass( 'frm_loading_prev' ) ) {
1316 1123 addLoadingClass( $object );
1317 1124 $object.trigger( 'frmStartFormLoading' );
1318 1125 }
1319 1126 }
@@ -1320,44 +1127,45 @@
1320 1127
1321 1128 function addLoadingClass( $object ) {
1322 1129 const loadingClass = isGoingToPrevPage( $object ) ? 'frm_loading_prev' : 'frm_loading_form';
1323 1130
1324 - $object.addClass( loadingClass ); // eslint-disable-line no-jquery/no-class, formidable/no-jquery-variable-methods
1131 + $object.addClass( loadingClass );
1325 1132 }
1326 1133
1327 1134 function isGoingToPrevPage( $object ) {
1328 - return typeof frmProForm !== 'undefined' && frmProForm.goingToPreviousPage( $object );
1135 + return ( typeof frmProForm !== 'undefined' && frmProForm.goingToPreviousPage( $object ) );
1329 1136 }
1330 1137
1331 - function removeSubmitLoading( _, enable, processesRunning ) {
1138 + function removeSubmitLoading( $object, enable, processesRunning ) {
1139 + let loadingForm;
1140 +
1332 1141 if ( processesRunning > 0 ) {
1333 1142 return;
1334 1143 }
1335 1144
1336 - document.querySelectorAll( '.frm_loading_form' ).forEach( function( form ) {
1337 - form.classList.remove( 'frm_loading_form', 'frm_loading_prev' );
1338 - jQuery( form ).trigger( 'frmEndFormLoading' );
1145 + loadingForm = jQuery( '.frm_loading_form' );
1146 + loadingForm.removeClass( 'frm_loading_form' );
1147 + loadingForm.removeClass( 'frm_loading_prev' );
1339 1148
1340 - if ( enable === 'enable' ) {
1341 - enableSubmitButton( form );
1342 - enableSaveDraft( form );
1343 - }
1344 - } );
1149 + loadingForm.trigger( 'frmEndFormLoading' );
1150 +
1151 + if ( enable === 'enable' ) {
1152 + enableSubmitButton( loadingForm );
1153 + enableSaveDraft( loadingForm );
1154 + }
1345 1155 }
1346 1156
1347 1157 function showFileLoading( object ) {
1348 - const loading = document.getElementById( 'frm_loading' );
1349 - if ( ! loading ) {
1350 - return;
1158 + let fileval,
1159 + loading = document.getElementById( 'frm_loading' );
1160 + if ( loading !== null ) {
1161 + fileval = jQuery( object ).find( 'input[type=file]' ).val();
1162 + if ( typeof fileval !== 'undefined' && fileval !== '' ) {
1163 + setTimeout( function() {
1164 + jQuery( loading ).fadeIn( 'slow' );
1165 + }, 2000 );
1166 + }
1351 1167 }
1352 -
1353 - const fileInput = object.querySelector( 'input[type=file]' );
1354 - const fileval = fileInput ? fileInput.value : '';
1355 - if ( fileval !== '' ) {
1356 - setTimeout( function() {
1357 - jQuery( loading ).fadeIn( 'slow' ); // eslint-disable-line no-jquery/no-fade
1358 - }, 2000 );
1359 - }
1360 1168 }
1361 1169
1362 1170 /**********************************************
1363 1171 * General Helpers
@@ -1364,9 +1172,9 @@
1364 1172 *********************************************/
1365 1173
1366 1174 function confirmClick() {
1367 1175 /*jshint validthis:true */
1368 - const message = this.dataset.frmconfirm;
1176 + const message = jQuery( this ).data( 'frmconfirm' );
1369 1177 return confirm( message );
1370 1178 }
1371 1179
1372 1180 /**
@@ -1374,15 +1182,43 @@
1374 1182 * If this is a match, the User is autofilling the input on a Webkit browser.
1375 1183 * We want to delete the Honeypot field, otherwise it will get triggered as spam on autocomplete.
1376 1184 */
1377 1185 function onHoneypotFieldChange() {
1378 - /*jshint validthis:true */
1379 - const css = window.getComputedStyle( this ).boxShadow;
1380 - if ( css?.match( /inset/ ) ) {
1381 - this.remove();
1186 + const css = jQuery( this ).css( 'box-shadow' );
1187 + if ( css.match( /inset/ ) ) {
1188 + this.parentNode.removeChild( this );
1382 1189 }
1383 1190 }
1384 1191
1192 + function maybeMakeHoneypotFieldsUntabbable() {
1193 + document.addEventListener( 'keydown', handleKeyUp );
1194 +
1195 + function handleKeyUp( event ) {
1196 + let code;
1197 +
1198 + if ( 'undefined' !== typeof event.key ) {
1199 + code = event.key;
1200 + } else if ( 'undefined' !== typeof event.keyCode && 9 === event.keyCode ) {
1201 + code = 'Tab';
1202 + }
1203 +
1204 + if ( 'Tab' === code ) {
1205 + makeHoneypotFieldsUntabbable();
1206 + document.removeEventListener( 'keydown', handleKeyUp );
1207 + }
1208 + }
1209 +
1210 + function makeHoneypotFieldsUntabbable() {
1211 + document.querySelectorAll( '.frm_verify' ).forEach(
1212 + function( input ) {
1213 + if ( input.id && 0 === input.id.indexOf( 'frm_email_' ) ) {
1214 + input.setAttribute( 'tabindex', -1 );
1215 + }
1216 + }
1217 + );
1218 + }
1219 + }
1220 +
1385 1221 /**
1386 1222 * Focus on the first sub field when clicking to the primary label of combo field.
1387 1223 *
1388 1224 * @since 4.10.02
@@ -1402,59 +1238,33 @@
1402 1238 }
1403 1239
1404 1240 label.addEventListener( 'click', function() {
1405 1241 inputsContainer.querySelector( '.frm_form_field:first-child input, .frm_form_field:first-child select, .frm_form_field:first-child textarea' ).focus();
1406 - } );
1407 - } );
1242 + });
1243 + });
1408 1244 }
1409 1245
1410 - /**
1411 - * Sets focus on a the first subfield of a combo field that has an error.
1412 - *
1413 - * @since 6.16.3
1414 - *
1415 - * @param {HTMLElement} element
1416 - * @return {boolean} True if the focus was set on a combo field.
1417 - */
1418 - function maybeFocusOnComboSubField( element ) {
1419 - if ( 'FIELDSET' !== element.nodeName ) {
1420 - return false;
1421 - }
1422 - if ( ! element.querySelector( '.frm_combo_inputs_container' ) ) {
1423 - return false;
1424 - }
1425 - const comboSubfield = element.querySelector( '[aria-invalid="true"]' );
1426 - if ( comboSubfield ) {
1427 - focusInput( comboSubfield );
1428 - return true;
1429 - }
1430 - return false;
1431 - }
1246 + function checkForErrorsAndMaybeSetFocus() {
1247 + let errors, element, timeoutCallback;
1432 1248
1433 - function checkForErrorsAndMaybeSetFocus() {
1434 - if ( ! frm_js.focus_first_error ) {
1249 + if ( ! frm_js.focus_first_error ) { // eslint-disable-line camelcase
1435 1250 return;
1436 1251 }
1437 1252
1438 - const errors = document.querySelectorAll( '.frm_form_field .frm_error' );
1253 + errors = document.querySelectorAll( '.frm_form_field .frm_error' );
1439 1254 if ( ! errors.length ) {
1440 1255 return;
1441 1256 }
1442 1257
1443 - let element = errors[ 0 ];
1444 - let timeoutCallback;
1258 + element = errors[0];
1445 1259 do {
1446 1260 element = element.previousSibling;
1447 - if ( [ 'input', 'select', 'textarea' ].includes( element.nodeName.toLowerCase() ) ) {
1448 - focusInput( element );
1261 + if ( -1 !== [ 'input', 'select', 'textarea' ].indexOf( element.nodeName.toLowerCase() ) ) {
1262 + element.focus();
1449 1263 break;
1450 1264 }
1451 1265
1452 - if ( maybeFocusOnComboSubField( element ) ) {
1453 - break;
1454 - }
1455 -
1456 - if ( element.classList !== undefined ) {
1266 + if ( 'undefined' !== typeof element.classList ) {
1457 1267 if ( element.classList.contains( 'html-active' ) ) {
1458 1268 timeoutCallback = function() {
1459 1269 const textarea = element.querySelector( 'textarea' );
1460 1270 if ( null !== textarea ) {
@@ -1464,14 +1274,8 @@
1464 1274 } else if ( element.classList.contains( 'tmce-active' ) ) {
1465 1275 timeoutCallback = function() {
1466 1276 tinyMCE.activeEditor.focus();
1467 1277 };
1468 - } else if ( element.classList.contains( 'frm_opt_container' ) ) {
1469 - const firstInput = element.querySelector( 'input' );
1470 - if ( firstInput ) {
1471 - focusInput( firstInput );
1472 - break;
1473 - }
1474 1278 }
1475 1279
1476 1280 if ( 'function' === typeof timeoutCallback ) {
1477 1281 setTimeout( timeoutCallback, 0 );
@@ -1481,24 +1285,8 @@
1481 1285 } while ( element.previousSibling );
1482 1286 }
1483 1287
1484 1288 /**
1485 - * Focus a visible input, or possibly delay the focus event until the form has faded in.
1486 - *
1487 - * @since 6.16.3
1488 - *
1489 - * @param {HTMLElement} input
1490 - * @return {void}
1491 - */
1492 - function focusInput( input ) {
1493 - if ( input.offsetParent !== null ) {
1494 - input.focus();
1495 - } else {
1496 - triggerCustomEvent( document, 'frmMaybeDelayFocus', { input } );
1497 - }
1498 - }
1499 -
1500 - /**
1501 1289 * Does the same as jQuery( document ).on( 'event', 'selector', handler ).
1502 1290 *
1503 1291 * @since 5.4
1504 1292 *
@@ -1507,9 +1295,9 @@
1507 1295 * @param {Function} handler Handler.
1508 1296 * @param {boolean | Object} options Options to be added to `addEventListener()` method. Default is `false`.
1509 1297 */
1510 1298 function documentOn( event, selector, handler, options ) {
1511 - if ( options === undefined ) {
1299 + if ( 'undefined' === typeof options ) {
1512 1300 options = false;
1513 1301 }
1514 1302
1515 1303 document.addEventListener( event, function( e ) {
@@ -1516,9 +1304,9 @@
1516 1304 let target;
1517 1305
1518 1306 // loop parent nodes from the target to the delegation node.
1519 1307 for ( target = e.target; target && target != this; target = target.parentNode ) {
1520 - if ( target.matches && target.matches( selector ) ) {
1308 + if ( target && target.matches && target.matches( selector ) ) {
1521 1309 handler.call( target, e );
1522 1310 break;
1523 1311 }
1524 1312 }
@@ -1525,23 +1313,27 @@
1525 1313 }, options );
1526 1314 }
1527 1315
1528 1316 function initFloatingLabels() {
1529 - const selector = '.frm-show-form .frm_inside_container input, .frm-show-form .frm_inside_container select, .frm-show-form .frm_inside_container textarea';
1530 - const floatClass = 'frm_label_float_top';
1317 + let checkFloatLabel, checkDropdownLabel, runOnLoad, selector, floatClass;
1531 1318
1532 - const checkFloatLabel = function( input ) {
1533 - const container = input.closest( '.frm_inside_container' );
1319 + selector = '.frm-show-form .frm_inside_container input, .frm-show-form .frm_inside_container select, .frm-show-form .frm_inside_container textarea';
1320 + floatClass = 'frm_label_float_top';
1321 +
1322 + checkFloatLabel = function( input ) {
1323 + let container, shouldFloatTop, firstOpt;
1324 +
1325 + container = input.closest( '.frm_inside_container' );
1534 1326 if ( ! container ) {
1535 1327 return;
1536 1328 }
1537 1329
1538 - const shouldFloatTop = input.value || document.activeElement === input;
1330 + shouldFloatTop = input.value || document.activeElement === input;
1539 1331
1540 1332 container.classList.toggle( floatClass, shouldFloatTop );
1541 1333
1542 1334 if ( 'SELECT' === input.tagName ) {
1543 - const firstOpt = input.querySelector( 'option:first-child' );
1335 + firstOpt = input.querySelector( 'option:first-child' );
1544 1336
1545 1337 if ( shouldFloatTop ) {
1546 1338 if ( firstOpt.hasAttribute( 'data-label' ) ) {
1547 1339 firstOpt.textContent = firstOpt.getAttribute( 'data-label' );
@@ -1553,10 +1345,10 @@
1553 1345 }
1554 1346 }
1555 1347 };
1556 1348
1557 - const checkDropdownLabel = function() {
1558 - document.querySelectorAll( `.frm-show-form .frm_inside_container:not(.${ floatClass }) select` ).forEach( function( input ) {
1349 + checkDropdownLabel = function() {
1350 + document.querySelectorAll( '.frm-show-form .frm_inside_container:not(.' + floatClass + ') select' ).forEach( function( input ) {
1559 1351 const firstOpt = input.querySelector( 'option:first-child' );
1560 1352
1561 1353 if ( firstOpt.textContent ) {
1562 1354 firstOpt.setAttribute( 'data-label', firstOpt.textContent );
@@ -1561,9 +1353,9 @@
1561 1353 if ( firstOpt.textContent ) {
1562 1354 firstOpt.setAttribute( 'data-label', firstOpt.textContent );
1563 1355 firstOpt.textContent = '';
1564 1356 }
1565 - } );
1357 + });
1566 1358 };
1567 1359
1568 1360 [ 'focus', 'blur', 'change' ].forEach( function( eventName ) {
1569 1361 documentOn(
@@ -1573,12 +1365,16 @@
1573 1365 checkFloatLabel( event.target );
1574 1366 },
1575 1367 true
1576 1368 );
1577 - } );
1369 + });
1578 1370
1579 - const runOnLoad = function( firstLoad ) {
1580 - if ( firstLoad && document.activeElement && [ 'INPUT', 'SELECT', 'TEXTAREA' ].includes( document.activeElement.tagName ) ) {
1371 + jQuery( document ).on( 'change', selector, function( event ) {
1372 + checkFloatLabel( event.target );
1373 + });
1374 +
1375 + runOnLoad = function( firstLoad ) {
1376 + if ( firstLoad && document.activeElement && -1 !== [ 'INPUT', 'SELECT', 'TEXTAREA' ].indexOf( document.activeElement.tagName ) ) {
1581 1377 checkFloatLabel( document.activeElement );
1582 1378 } else if ( firstLoad ) {
1583 1379 document.querySelectorAll( '.frm_inside_container' ).forEach(
1584 1380 function( container ) {
@@ -1590,9 +1386,8 @@
1590 1386 );
1591 1387 }
1592 1388
1593 1389 checkDropdownLabel();
1594 - calcProductsTotal();
1595 1390 };
1596 1391
1597 1392 runOnLoad( true );
1598 1393
@@ -1597,13 +1392,13 @@
1597 1392 runOnLoad( true );
1598 1393
1599 1394 jQuery( document ).on( 'frmPageChanged', function( event ) {
1600 1395 runOnLoad();
1601 - } );
1396 + });
1602 1397
1603 1398 document.addEventListener( 'frm_after_start_over', function( event ) {
1604 1399 runOnLoad();
1605 - } );
1400 + });
1606 1401 }
1607 1402
1608 1403 function shouldUpdateValidityMessage( target ) {
1609 1404 if ( 'INPUT' !== target.nodeName ) {
@@ -1625,10 +1420,10 @@
1625 1420 return true;
1626 1421 }
1627 1422
1628 1423 function maybeClearCustomValidityMessage( event, field ) {
1629 - let key;
1630 - let isInvalid = false;
1424 + let key,
1425 + isInvalid = false;
1631 1426
1632 1427 if ( ! shouldUpdateValidityMessage( field ) ) {
1633 1428 return;
1634 1429 }
@@ -1640,9 +1435,9 @@
1640 1435 if ( 'valid' !== key && field.validity[ key ] === true ) {
1641 1436 isInvalid = true;
1642 1437 break;
1643 1438 }
1644 - }
1439 + };
1645 1440
1646 1441 if ( ! isInvalid ) {
1647 1442 field.setCustomValidity( '' );
1648 1443 }
@@ -1648,29 +1443,33 @@
1648 1443 }
1649 1444 }
1650 1445
1651 1446 function maybeShowNewTabFallbackMessage() {
1447 + let messageEl;
1448 +
1652 1449 if ( ! window.frmShowNewTabFallback ) {
1653 1450 return;
1654 1451 }
1655 1452
1656 - const messageEl = document.querySelector( `#frm_form_${ frmShowNewTabFallback.formId }_container .frm_message` );
1453 + messageEl = document.querySelector( '#frm_form_' + frmShowNewTabFallback.formId + '_container .frm_message' );
1657 1454 if ( ! messageEl ) {
1658 1455 return;
1659 1456 }
1660 1457
1661 - messageEl.insertAdjacentHTML( 'beforeend', ` ${ frmShowNewTabFallback.message }` );
1458 + messageEl.insertAdjacentHTML( 'beforeend', ' ' + frmShowNewTabFallback.message );
1662 1459 }
1663 1460
1664 1461 function setCustomValidityMessage() {
1665 - const forms = document.getElementsByClassName( 'frm-show-form' );
1666 - const { length } = forms;
1462 + let forms, length, index;
1667 1463
1668 - for ( let index = 0; index < length; ++index ) {
1464 + forms = document.getElementsByClassName( 'frm-show-form' );
1465 + length = forms.length;
1466 +
1467 + for ( index = 0; index < length; ++index ) {
1669 1468 forms[ index ].addEventListener(
1670 1469 'invalid',
1671 1470 function( event ) {
1672 - const { target } = event;
1471 + const target = event.target;
1673 1472
1674 1473 if ( shouldUpdateValidityMessage( target ) ) {
1675 1474 target.setCustomValidity( target.dataset.invmsg );
1676 1475 }
@@ -1684,14 +1483,14 @@
1684 1483 window.addEventListener( 'pageshow', function( event ) {
1685 1484 if ( event.persisted ) {
1686 1485 document.querySelectorAll( '.frm_loading_form' ).forEach(
1687 1486 function( form ) {
1688 - enableSubmitButton( form );
1487 + enableSubmitButton( jQuery( form ) );
1689 1488 }
1690 1489 );
1691 1490 removeSubmitLoading();
1692 1491 }
1693 - } );
1492 + });
1694 1493 }
1695 1494
1696 1495 /**
1697 1496 * Destroys the formidable generated global hcaptcha object since it wouldn't otherwise render.
@@ -1702,503 +1501,23 @@
1702 1501 }
1703 1502 window.hcaptcha = null;
1704 1503 }
1705 1504
1706 - /**
1707 - * @since 6.16.3
1708 - *
1709 - * @return {string} Unique key, used for duplicate checks.
1710 - */
1711 - function getUniqueKey() {
1712 - const uniqueKey = Array.from( window.crypto.getRandomValues( new Uint8Array( 8 ) ) )
1713 - .map( b => b.toString( 16 ).padStart( 2, '0' ) )
1714 - .join( '' );
1715 - const timestamp = Date.now().toString( 16 );
1716 - return `${ uniqueKey }-${ timestamp }`;
1717 - }
1718 -
1719 - /**
1720 - * Animates the scroll position of the document.
1721 - *
1722 - * @since 6.20
1723 - *
1724 - * @param {number} start
1725 - * @param {number} end
1726 - * @param {number} duration
1727 - * @return {void}
1728 - */
1729 - function animateScroll( start, end, duration ) {
1730 - if ( ! window.hasOwnProperty( 'performance' ) || ! window.hasOwnProperty( 'requestAnimationFrame' ) ) {
1731 - document.documentElement.scrollTop = end;
1732 - return;
1733 - }
1734 -
1735 - const startTime = performance.now();
1736 - const step = currentTime => {
1737 - const progress = Math.min( ( currentTime - startTime ) / duration, 1 );
1738 - document.documentElement.scrollTop = start + ( ( end - start ) * progress );
1739 - if ( progress < 1 ) {
1740 - requestAnimationFrame( step );
1741 - }
1742 - };
1743 - requestAnimationFrame( step );
1744 - }
1745 -
1746 - /**
1747 - * Make sure that the captcha label for a reCAPTCHA or Turnstile field matches the response input ID.
1748 - * This is determined dynamically, so we check for the ID after the input is rendered.
1749 - * hCaptcha is handled separately, in the frmCaptcha function as it is not rendered explicitly.
1750 - *
1751 - * @since 6.25.1
1752 - *
1753 - * @param {HTMLElement} captcha
1754 - * @return {void}
1755 - */
1756 - function maybeFixCaptchaLabel( captcha ) {
1757 - const form = captcha.closest( 'form' );
1758 - if ( ! form ) {
1759 - return;
1760 - }
1761 -
1762 - const label = form.querySelector( 'label[for="g-recaptcha-response"], label[for="cf-turnstile-response"]' );
1763 - const captchaResponse = form.querySelector( '[name="g-recaptcha-response"], [name="cf-turnstile-response"]' );
1764 -
1765 - if ( label && captchaResponse ) {
1766 - label.htmlFor = captchaResponse.id;
1767 - }
1768 - }
1769 -
1770 - /**
1771 - * Check to make sure the quantity field value is within the min and max values.
1772 - *
1773 - * @param {HTMLElement} input
1774 - * @return {number} The quantity value.
1775 - */
1776 - function checkQuantityFieldMinMax( input ) {
1777 - if ( '' === input.value ) {
1778 - // Leave the value if it is empty.
1779 - return 0;
1780 - }
1781 -
1782 - const val = parseFloat( input.value ? input.value.trim() : 0 );
1783 - if ( isNaN( val ) ) {
1784 - return 0;
1785 - }
1786 -
1787 - let max = input.hasAttribute( 'max' ) ? parseFloat( input.getAttribute( 'max' ) ) : 0;
1788 - let min = input.hasAttribute( 'min' ) ? parseFloat( input.getAttribute( 'min' ) ) : 0;
1789 -
1790 - max = isNaN( max ) ? 0 : max;
1791 - min = isNaN( min ) ? 0 : Math.max( 0, min );
1792 -
1793 - if ( val < min ) {
1794 - input.value = min;
1795 - return min;
1796 - }
1797 -
1798 - if ( 0 !== max && val > max ) {
1799 - input.value = max;
1800 - return max;
1801 - }
1802 -
1803 - return val;
1804 - }
1805 -
1806 - function triggerChange( input, fieldKey ) {
1807 - if ( fieldKey === undefined ) {
1808 - fieldKey = 'dependent';
1809 - }
1810 -
1811 - jQuery( input ).trigger( { type: 'change', selfTriggered: true, frmTriggered: fieldKey } );
1812 - }
1813 -
1814 - /**
1815 - * Calculates the total price.
1816 - *
1817 - * @param {Event|undefined} e The event object.
1818 - * @return {void}
1819 - */
1820 - function calcProductsTotal( e ) {
1821 - if ( 'object' === typeof frmProForm ) {
1822 - // Pro is installed, use the Pro JS.
1823 - return;
1824 - }
1825 -
1826 - if ( typeof __FRMCURR === 'undefined' ) {
1827 - return;
1828 - }
1829 -
1830 - const totalFields = document.querySelectorAll( '[data-frmtotal]' );
1831 - if ( ! totalFields.length ) {
1832 - return;
1833 - }
1834 -
1835 - const formTotals = [];
1836 -
1837 - totalFields.forEach( totalField => {
1838 - let total = 0;
1839 - const form = totalField.closest( 'form' );
1840 -
1841 - if ( ! form ) {
1842 - return;
1843 - }
1844 -
1845 - const formId = form.querySelector( 'input[name="form_id"]' ).value;
1846 - const currency = getCurrency( formId );
1847 -
1848 - if ( undefined !== formTotals[ formId ] ) {
1849 - total = formTotals[ formId ];
1850 - } else {
1851 - form.querySelectorAll( 'input[data-frmprice],select:has([data-frmprice])' ).forEach( function( input ) {
1852 - let quantity = 0;
1853 - let price = 0;
1854 - const isSingle = 'hidden' === input.type;
1855 -
1856 - if ( input.tagName === 'SELECT' ) {
1857 - if ( input.selectedIndex !== -1 ) {
1858 - price = input.options[ input.selectedIndex ].getAttribute( 'data-frmprice' );
1859 - }
1860 - } else {
1861 - if ( ! isSingle && ! input.matches( ':checked' ) ) {
1862 - return;
1863 - }
1864 - price = input.getAttribute( 'data-frmprice' );
1865 - }
1866 -
1867 - if ( ! price ) {
1868 - price = 0;
1869 - } else {
1870 - price = preparePrice( price, currency );
1871 - quantity = getQuantity( input );
1872 - price = parseFloat( quantity ) * parseFloat( price );
1873 - }
1874 -
1875 - if ( 'true' === input.getAttribute( 'data-frmdiscount' ) ) {
1876 - price = price * -1;
1877 - }
1878 -
1879 - total += price;
1880 - } );
1881 -
1882 - formTotals[ formId ] = total;
1883 - }
1884 -
1885 - total = isNaN( total ) ? 0 : total;
1886 -
1887 - // Set a decimal separator for currency if no default for it
1888 - currency.decimal_separator = currency.decimal_separator.trim(); // first remove unnecessary space(s)
1889 - if ( ! currency.decimal_separator.length ) {
1890 - currency.decimal_separator = '.';
1891 - }
1892 -
1893 - totalField.value = roundTotal( total, currency );
1894 - total = normalizeTotal( total, currency );
1895 -
1896 - // because of e.g. fields that might be using this field for calculations
1897 - triggerChange( totalField );
1898 -
1899 - total = formatCurrency( total, currency );
1900 - const formatted = totalField.previousElementSibling;
1901 - if ( formatted?.matches( '.frm_total_formatted' ) ) {
1902 - // Use innerHTML so that currency symbols like Euros can render and not their encoded string value.
1903 - formatted.innerHTML = total;
1904 - return;
1905 - }
1906 -
1907 - const formattedEls = totalField.closest( '.frm_form_field' ).querySelectorAll( '.frm_total_formatted' );
1908 - formattedEls.forEach( formattedEl => {
1909 - // Use innerHTML so that currency symbols like Euros can render and not their encoded string value.
1910 - formattedEl.innerHTML = total;
1911 - } );
1912 - } );
1913 - }
1914 -
1915 - /**
1916 - * Round total and maybe add trailing zeros so formatCurrency has a proper format to work with.
1917 - *
1918 - * @param {number} total The total amount to normalize.
1919 - * @param {Object} currency The currency object containing decimal information.
1920 - * @return {string} The normalized total amount.
1921 - */
1922 - function normalizeTotal( total, currency ) {
1923 - const isLargeTotal = total > Number.MAX_SAFE_INTEGER;
1924 -
1925 - total = roundTotal( total, currency );
1926 -
1927 - return maybeAddTrailingZeroToPrice( total, currency, isLargeTotal );
1928 - }
1929 -
1930 - function roundTotal( total, currency ) {
1931 - const isLargeTotal = total > Number.MAX_SAFE_INTEGER;
1932 -
1933 - if ( ! isLargeTotal ) {
1934 - const { decimals } = currency;
1935 - total = decimals > 0 ? round10( total, decimals ) : Math.ceil( total );
1936 - }
1937 -
1938 - return total;
1939 - }
1940 -
1941 - function round10( value, decimals ) {
1942 - return Number( `${ Math.round( `${ value }e${ decimals }` ) }e-${ decimals }` );
1943 - }
1944 -
1945 - /**
1946 - * Format a numeric value according to the specified currency format settings.
1947 - *
1948 - * @param {string} total The numeric value to format.
1949 - * @param {Object} currency The currency formatting configuration.
1950 - * @return {string} The formatted currency string.
1951 - */
1952 - function formatCurrency( total, currency ) {
1953 - total = maybeAddTrailingZeroToPrice( total, currency );
1954 - if ( total.length && ( total[ total.length - 1 ] === '.' || total[ total.length - 1 ] === currency.decimal_separator ) ) {
1955 - total = total.substr( 0, total.length - 1 );
1956 - }
1957 -
1958 - total = maybeRemoveTrailingZerosFromPrice( total, currency );
1959 - total = addThousands( total, currency );
1960 -
1961 - const leftSymbol = currency.symbol_left ? ( currency.symbol_left + currency.symbol_padding ) : '';
1962 - const rightSymbol = currency.symbol_right ? ( currency.symbol_padding + currency.symbol_right ) : '';
1963 -
1964 - return `${ leftSymbol }${ total }${ rightSymbol }`;
1965 - }
1966 -
1967 - /**
1968 - * Gets currency from form id.
1969 - *
1970 - * @param {number} formId Form ID.
1971 - * @return {Object} Currency object.
1972 - */
1973 - function getCurrency( formId ) {
1974 - if ( undefined !== window.__FRMCURR && undefined !== window.__FRMCURR[ formId ] ) {
1975 - return window.__FRMCURR[ formId ];
1976 - }
1977 -
1978 - return {
1979 - symbol_left: '$',
1980 - symbol_right: '',
1981 - symbol_padding: '',
1982 - thousand_separator: ',',
1983 - decimal_separator: '.',
1984 - decimals: 2,
1985 - };
1986 - }
1987 -
1988 - /**
1989 - * Gets quantity.
1990 - *
1991 - * @param {HTMLElement} field The field element.
1992 - * @return {number} The quantity.
1993 - */
1994 - function getQuantity( field ) {
1995 - const fieldID = frmFrontForm.getFieldId( field, false );
1996 - if ( ! fieldID ) {
1997 - return 0;
1998 - }
1999 -
2000 - const quantityField = getQuantityField( field, fieldID );
2001 - if ( ! quantityField ) {
2002 - // If there is no quantity field, assume 1.
2003 - return 1;
2004 - }
2005 -
2006 - return checkQuantityFieldMinMax( quantityField );
2007 - }
2008 -
2009 - /**
2010 - * Gets quantity field.
2011 - *
2012 - * @param {HTMLElement} element The element.
2013 - * @param {number} fieldID The field ID.
2014 - * @return {HTMLElement|null} The quantity field.
2015 - */
2016 - function getQuantityField( element, fieldID ) {
2017 - const quantityFields = element.closest( 'form' ).querySelectorAll( '[data-frmproduct]' );
2018 - if ( ! quantityFields.length ) {
2019 - return null;
2020 - }
2021 -
2022 - fieldID = fieldID.toString();
2023 -
2024 - return Array.from( quantityFields ).find( element => {
2025 - let ids;
2026 -
2027 - ids = JSON.parse( element.getAttribute( 'data-frmproduct' ).trim() );
2028 - if ( '' === ids ) {
2029 - return false;
2030 - }
2031 -
2032 - // Convert to array if necessary because of existing fields that are already using single product fields.
2033 - ids = 'string' === typeof ids ? [ ids ] : ids;
2034 - return ids.includes( fieldID );
2035 - } );
2036 - }
2037 -
2038 - /**
2039 - * Prepare a price for calculation.
2040 - *
2041 - * @param {number|string} price The price to prepare.
2042 - * @param {Object} currency The currency object containing decimal information.
2043 - * @return {string} The prepared price.
2044 - */
2045 - function preparePrice( price, currency ) {
2046 - if ( ! price ) {
2047 - return 0;
2048 - }
2049 - price = `${ price }`; // convert to string just to be sure
2050 -
2051 - const regex = getRegexForPrice( currency );
2052 -
2053 - const matches = price.match( regex );
2054 - if ( null === matches ) {
2055 - return 0;
2056 - }
2057 -
2058 - price = matches.length ? matches[ matches.length - 1 ] : 0;
2059 - price = price.trim();
2060 -
2061 - // Fix issues with parsing Fr.15.00. The regex catches .15.00.
2062 - // This checks for the leading decimal and removes it.
2063 - if ( currency.decimal_separator === '.' && 3 === price.split( '.' ).length && price[ 0 ] === '.' ) {
2064 - price = price.substr( 1 );
2065 - }
2066 -
2067 - if ( price ) {
2068 - price = maybeUseDecimal( price, currency );
2069 - price = price.split( currency.thousand_separator ).join( '' ).replace( currency.decimal_separator, '.' );
2070 - }
2071 -
2072 - return price;
2073 - }
2074 -
2075 - /**
2076 - * @param {Object} currency The currency object.
2077 - * @return {RegExp} The regular expression object.
2078 - */
2079 - function getRegexForPrice( currency ) {
2080 - let regexString = '[0-9,.';
2081 -
2082 - if ( currency.thousand_separator !== '.' && currency.thousand_separator !== ',' ) {
2083 - regexString += currency.thousand_separator;
2084 - }
2085 - if ( currency.decimal_separator !== '.' && currency.decimal_separator !== ',' ) {
2086 - regexString += currency.decimal_separator;
2087 - }
2088 -
2089 - regexString += ']*\\.?\\,?[0-9]+';
2090 -
2091 - return new RegExp( regexString, 'g' );
2092 - }
2093 -
2094 - /**
2095 - * Maybe replace the decimal separator with the currency's decimal separator.
2096 - *
2097 - * @param {string} price The price string.
2098 - * @param {Object} currency The currency object.
2099 - * @return {string} The modified price string.
2100 - */
2101 - function maybeUseDecimal( price, currency ) {
2102 - let usedForDecimal;
2103 - let priceParts;
2104 - if ( '.' === currency.thousand_separator ) {
2105 - priceParts = price.split( '.' );
2106 - usedForDecimal = 2 === priceParts.length && 2 === priceParts[ 1 ].length;
2107 - if ( usedForDecimal ) {
2108 - price = price.replace( '.', currency.decimal_separator );
2109 - }
2110 - }
2111 - return price;
2112 - }
2113 -
2114 - /**
2115 - * Add trailing zeros to a price if necessary and replace the decimal separator.
2116 - *
2117 - * @param {number|string} price The price to format.
2118 - * @param {Object} currency The currency object containing the decimal separator.
2119 - * @param {boolean} [force=false] Whether to force processing even if the price is not a number.
2120 - * @return {string} The formatted price string.
2121 - */
2122 - function maybeAddTrailingZeroToPrice( price, currency, force = false ) {
2123 - if ( 'number' !== typeof price && ! force ) {
2124 - return price;
2125 - }
2126 -
2127 - price = String( price ); // first convert to string
2128 - const pos = price.indexOf( '.' );
2129 -
2130 - if ( pos === -1 ) {
2131 - price = `${ price }.`;
2132 -
2133 - for ( let n = 0; n < currency.decimals; ++n ) {
2134 - price += '0';
2135 - }
2136 - } else {
2137 - const decimalsString = price.substring( pos + 1 );
2138 - if ( decimalsString.length < currency.decimals ) {
2139 - if ( decimalsString.length < 2 ) {
2140 - price += '0';
2141 - }
2142 -
2143 - for ( let n = 2; n < currency.decimals; ++n ) {
2144 - price += '0';
2145 - }
2146 - }
2147 - }
2148 -
2149 - return price.replace( '.', currency.decimal_separator );
2150 - }
2151 -
2152 - /**
2153 - * Format a numeric string by adding thousand separators.
2154 - *
2155 - * @param {string|number} price The numeric value to format.
2156 - * @param {Object} options Formatting options.
2157 - * @param {string} options.decimal_separator Character used as decimal separator.
2158 - * @param {string} options.thousand_separator Character used as thousand separator.
2159 - *
2160 - * @return {string} The price string with thousand separators.
2161 - */
2162 - function addThousands( price, options ) {
2163 - const split = options.decimal_separator === ''
2164 - ? [ price.toString() ]
2165 - : price.split( options.decimal_separator );
2166 -
2167 - if ( options.thousand_separator ) {
2168 - split[ 0 ] = split[ 0 ].replace( /\B(?=(\d{3})+(?!\d))/g, options.thousand_separator );
2169 - }
2170 -
2171 - return split.join( options.decimal_separator );
2172 - }
2173 -
2174 - /**
2175 - * Maybe remove trailing zeros from a price string.
2176 - *
2177 - * @param {string} price The price string.
2178 - * @param {Object} currency The currency data.
2179 - *
2180 - * @return {string} The price string with trailing zeros removed.
2181 - */
2182 - function maybeRemoveTrailingZerosFromPrice( price, currency ) {
2183 - const split = price.split( currency.decimal_separator );
2184 - if ( 2 !== split.length || split[ 1 ].length <= currency.decimals ) {
2185 - return price;
2186 - }
2187 - if ( 0 === currency.decimals ) {
2188 - return split[ 0 ];
2189 - }
2190 - return `${ split[ 0 ] }${ currency.decimal_separator }${ split[ 1 ].substr( 0, currency.decimals ) }`;
2191 - }
2192 -
2193 1505 return {
2194 - init() {
1506 + init: function() {
2195 1507 jQuery( document ).off( 'submit.formidable', '.frm-show-form' );
2196 1508 jQuery( document ).on( 'submit.formidable', '.frm-show-form', frmFrontForm.submitForm );
2197 1509
1510 + jQuery( '.frm-show-form input[onblur], .frm-show-form textarea[onblur]' ).each( function() {
1511 + if ( jQuery( this ).val() === '' ) {
1512 + jQuery( this ).trigger( 'blur' );
1513 + }
1514 + });
1515 +
2198 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 );
2199 1517
2200 - jQuery( document ).on( 'change', '.frm_verify[id^=field_]', onHoneypotFieldChange );
1518 + jQuery( document ).on( 'change', '[id^=frm_email_]', onHoneypotFieldChange );
1519 + maybeMakeHoneypotFieldsUntabbable();
2201 1520
2202 1521 jQuery( document ).on( 'click', 'a[data-frmconfirm]', confirmClick );
2203 1522
2204 1523 checkForErrorsAndMaybeSetFocus();
@@ -2222,71 +1541,55 @@
2222 1541 jQuery( document ).on(
2223 1542 'frmPageChanged',
2224 1543 destroyhCaptcha
2225 1544 );
1545 + },
2226 1546
2227 - jQuery( document ).on( 'frmAfterAddRow frmAfterRemoveRow', calcProductsTotal );
2228 - jQuery( document ).on( 'change', '[type="checkbox"][data-frmprice],[type="radio"][data-frmprice],[type="hidden"][data-frmprice],select:has([data-frmprice])', calcProductsTotal );
2229 - jQuery( document ).on( 'keyup change', '[data-frmproduct],[type="text"][data-frmprice]', calcProductsTotal );
2230 - calcProductsTotal();
1547 + getFieldId: function( field, fullID ) {
1548 + return getFieldId( field, fullID );
2231 1549 },
2232 1550
2233 - getFieldId,
1551 + renderCaptcha: function( captcha, captchaSelector ) {
1552 + let formID, captchaID,
1553 + size = captcha.getAttribute( 'data-size' ),
1554 + rendered = captcha.getAttribute( 'data-rid' ) !== null,
1555 + params = {
1556 + 'sitekey': captcha.getAttribute( 'data-sitekey' ),
1557 + 'size': size,
1558 + 'theme': captcha.getAttribute( 'data-theme' )
1559 + },
1560 + activeCaptcha = getSelectedCaptcha( captchaSelector ),
1561 + captchaContainer = typeof turnstile !== 'undefined' && turnstile === activeCaptcha ? '#' + captcha.id : captcha.id;
2234 1562
2235 - /**
2236 - * Render a captcha field.
2237 - *
2238 - * @param {HTMLElement} captcha
2239 - * @param {string} captchaSelector
2240 - * @return {void}
2241 - */
2242 - renderCaptcha( captcha, captchaSelector ) {
2243 - const rendered = captcha.getAttribute( 'data-rid' ) !== null;
2244 1563 if ( rendered ) {
2245 1564 return;
2246 1565 }
2247 1566
2248 - const size = captcha.getAttribute( 'data-size' );
2249 - const params = {
2250 - sitekey: captcha.getAttribute( 'data-sitekey' ),
2251 - size,
2252 - theme: captcha.getAttribute( 'data-theme' )
2253 - };
2254 -
2255 1567 if ( size === 'invisible' ) {
2256 - const formID = captcha.closest( 'form' )?.querySelector( 'input[name="form_id"]' )?.value;
2257 -
2258 - const captchaLabel = captcha.closest( '.frm_form_field' )?.querySelector( '.frm_primary_label' );
2259 - if ( captchaLabel ) {
2260 - captchaLabel.style.display = 'none';
2261 - }
2262 -
1568 + formID = jQuery( captcha ).closest( 'form' ).find( 'input[name="form_id"]' ).val();
1569 + jQuery( captcha ).closest( '.frm_form_field .frm_primary_label' ).hide();
2263 1570 params.callback = function( token ) {
2264 1571 frmFrontForm.afterRecaptcha( token, formID );
2265 1572 };
2266 1573 }
2267 1574
2268 - const activeCaptcha = getSelectedCaptcha( captchaSelector );
2269 - const captchaContainer = typeof turnstile !== 'undefined' && turnstile === activeCaptcha ? `#${ captcha.id }` : captcha.id;
2270 - const captchaID = activeCaptcha.render( captchaContainer, params );
2271 1575
1576 + captchaID = activeCaptcha.render( captchaContainer, params );
1577 +
2272 1578 captcha.setAttribute( 'data-rid', captchaID );
2273 -
2274 - maybeFixCaptchaLabel( captcha );
2275 1579 },
2276 1580
2277 - afterSingleRecaptcha() {
2278 - const recaptcha = document.querySelector( '.frm-show-form .g-recaptcha' );
2279 - const object = recaptcha ? recaptcha.closest( 'form' ) : null;
1581 + afterSingleRecaptcha: function() {
1582 + const object = jQuery( '.frm-show-form .g-recaptcha' ).closest( 'form' )[0];
2280 1583 frmFrontForm.submitFormNow( object );
2281 1584 },
2282 1585
2283 - afterRecaptcha( _, formID ) {
2284 - const object = document.querySelector( `#frm_form_${ formID }_container form` );
1586 + afterRecaptcha: function( _, formID ) {
1587 + const object = jQuery( '#frm_form_' + formID + '_container form' )[0];
2285 1588 frmFrontForm.submitFormNow( object );
2286 1589 },
2287 1590
2288 - submitForm( e ) {
1591 + submitForm: function( e ) {
2289 1592 frmFrontForm.submitFormManual( e, this );
2290 1593 },
2291 1594
2292 1595 /**
@@ -2293,10 +1596,21 @@
2293 1596 * @param {Event} e
2294 1597 * @param {HTMLElement} object The form object that is being submitted.
2295 1598 * @return {void}
2296 1599 */
2297 - submitFormManual( e, object ) {
2298 - if ( document.body.classList.contains( 'wp-admin' ) && ! object.closest( '.frmapi-form' ) ) {
1600 + submitFormManual: function( e, object ) {
1601 + let isPro, errors,
1602 + invisibleRecaptcha = hasInvisibleRecaptcha( object ),
1603 + classList = object.className.trim().split( /\s+/gi );
1604 +
1605 + if ( classList && invisibleRecaptcha.length < 1 ) {
1606 + isPro = classList.indexOf( 'frm_pro_form' ) > -1;
1607 + if ( ! isPro ) {
1608 + return;
1609 + }
1610 + }
1611 +
1612 + if ( jQuery( 'body' ).hasClass( 'wp-admin' ) && jQuery( object ).closest( '.frmapi-form' ).length < 1 ) {
2299 1613 return;
2300 1614 }
2301 1615
2302 1616 e.preventDefault();
@@ -2304,29 +1618,27 @@
2304 1618 if ( typeof frmProForm !== 'undefined' && typeof frmProForm.submitAllowed === 'function' && ! frmProForm.submitAllowed( object ) ) {
2305 1619 return;
2306 1620 }
2307 1621
2308 - const errors = frmFrontForm.validateFormSubmit( object );
1622 + errors = frmFrontForm.validateFormSubmit( object );
2309 1623 if ( Object.keys( errors ).length !== 0 ) {
2310 1624 return;
2311 1625 }
2312 1626
2313 - const invisibleRecaptcha = hasInvisibleRecaptcha( object );
2314 -
2315 - if ( invisibleRecaptcha ) {
1627 + if ( invisibleRecaptcha.length ) {
2316 1628 showLoadingIndicator( jQuery( object ) );
2317 1629 executeInvisibleRecaptcha( invisibleRecaptcha );
2318 1630 } else {
1631 +
2319 1632 showSubmitLoading( jQuery( object ) );
2320 1633
2321 - frmFrontForm.submitFormNow( object );
1634 + frmFrontForm.submitFormNow( object, classList );
2322 1635 }
2323 1636 },
2324 1637
2325 - submitFormNow( object ) {
2326 - let hasFileFields;
2327 - let antispamInput;
2328 - const classList = object.className.trim().split( /\s+/gi );
1638 + submitFormNow: function( object ) {
1639 + let hasFileFields, antispamInput,
1640 + classList = object.className.trim().split( /\s+/gi );
2329 1641
2330 1642 if ( object.hasAttribute( 'data-token' ) && null === object.querySelector( '[name="antispam_token"]' ) ) {
2331 1643 // include the antispam token on form submit.
2332 1644 antispamInput = document.createElement( 'input' );
@@ -2332,24 +1644,17 @@
2332 1644 antispamInput = document.createElement( 'input' );
2333 1645 antispamInput.type = 'hidden';
2334 1646 antispamInput.name = 'antispam_token';
2335 1647 antispamInput.value = object.getAttribute( 'data-token' );
2336 - object.append( antispamInput );
1648 + object.appendChild( antispamInput );
2337 1649 }
2338 1650
2339 - // Add a unique ID, used for duplicate checks.
2340 - const uniqueIDInput = document.createElement( 'input' );
2341 - uniqueIDInput.type = 'hidden';
2342 - uniqueIDInput.name = 'unique_id';
2343 - uniqueIDInput.value = getUniqueKey();
2344 - object.append( uniqueIDInput );
2345 -
2346 - if ( classList.includes( 'frm_ajax_submit' ) ) {
2347 - const fileInputs = object.querySelectorAll( 'input[type="file"]' );
2348 - hasFileFields = Array.from( fileInputs ).filter( input => !! input.value ).length;
1651 + if ( classList.indexOf( 'frm_ajax_submit' ) > -1 ) {
1652 + hasFileFields = jQuery( object ).find( 'input[type="file"]' ).filter( function() {
1653 + return !! this.value;
1654 + }).length;
2349 1655 if ( hasFileFields < 1 ) {
2350 - const actionInput = object.querySelector( 'input[name="frm_action"]' );
2351 - const action = actionInput ? actionInput.value : '';
1656 + action = jQuery( object ).find( 'input[name="frm_action"]' ).val();
2352 1657 frmFrontForm.checkFormErrors( object, action );
2353 1658 } else {
2354 1659 object.submit();
2355 1660 }
@@ -2362,11 +1667,10 @@
2362 1667 * @param {HTMLElement|Object} object Form object. This might be a jQuery object.
2363 1668 *
2364 1669 * @return {Array} List of errors.
2365 1670 */
2366 - validateFormSubmit( object ) {
2367 - const form = object instanceof jQuery ? object.get( 0 ) : object;
2368 - if ( typeof tinyMCE !== 'undefined' && form?.querySelector( '.wp-editor-wrap' ) ) {
1671 + validateFormSubmit: function( object ) {
1672 + if ( typeof tinyMCE !== 'undefined' && jQuery( object ).find( '.wp-editor-wrap' ).length ) {
2369 1673 tinyMCE.triggerSave();
2370 1674 }
2371 1675
2372 1676 jsErrors = [];
@@ -2385,19 +1689,16 @@
2385 1689 /**
2386 1690 * @param {HTMLElement|Object} object Form object. This might be a jQuery object.
2387 1691 * @return {Array} List of errors.
2388 1692 */
2389 - getAjaxFormErrors( object ) {
2390 - let customErrors;
2391 - let key;
2392 - const form = object instanceof jQuery ? object.get( 0 ) : object;
1693 + getAjaxFormErrors: function( object ) {
1694 + let customErrors, key;
2393 1695
2394 1696 jsErrors = validateForm( object );
2395 1697 if ( typeof frmThemeOverride_jsErrors === 'function' ) { // eslint-disable-line camelcase
2396 - const actionInput = form ? form.querySelector( 'input[name="frm_action"]' ) : null;
2397 - const action = actionInput ? actionInput.value : '';
1698 + action = jQuery( object ).find( 'input[name="frm_action"]' ).val();
2398 1699 customErrors = frmThemeOverride_jsErrors( action, object );
2399 - if ( Object.keys( customErrors ).length ) {
1700 + if ( Object.keys( customErrors ).length ) {
2400 1701 for ( key in customErrors ) {
2401 1702 jsErrors[ key ] = customErrors[ key ];
2402 1703 }
2403 1704 }
@@ -2405,9 +1706,9 @@
2405 1706
2406 1707 triggerCustomEvent( document, 'frm_get_ajax_form_errors', {
2407 1708 formEl: object,
2408 1709 errors: jsErrors
2409 - } );
1710 + });
2410 1711
2411 1712 return jsErrors;
2412 1713 },
2413 1714
@@ -2414,18 +1715,17 @@
2414 1715 /**
2415 1716 * @param {HTMLElement|Object} object Form object. This might be a jQuery object.
2416 1717 * @return {void}
2417 1718 */
2418 - addAjaxFormErrors( object ) {
2419 - let key;
2420 - const form = object instanceof jQuery ? object.get( 0 ) : object;
1719 + addAjaxFormErrors: function( object ) {
1720 + let key, $fieldCont;
2421 1721 removeAllErrors();
2422 1722
2423 1723 for ( key in jsErrors ) {
2424 - const fieldCont = form ? form.querySelector( `#frm_field_${ key }_container` ) : null;
1724 + $fieldCont = jQuery( object ).find( '#frm_field_' + key + '_container' );
2425 1725
2426 - if ( fieldCont ) {
2427 - addFieldError( fieldCont, key, jsErrors );
1726 + if ( $fieldCont.length ) {
1727 + addFieldError( $fieldCont, key, jsErrors );
2428 1728 } else {
2429 1729 // we are unable to show the error, so remove it
2430 1730 delete jsErrors[ key ];
2431 1731 }
@@ -2434,33 +1734,39 @@
2434 1734 scrollToFirstField( object );
2435 1735 checkForErrorsAndMaybeSetFocus();
2436 1736 },
2437 1737
2438 - checkFormErrors: getFormErrors,
2439 - checkRequiredField,
2440 - showSubmitLoading,
2441 - removeSubmitLoading,
1738 + checkFormErrors: function( object, action ) {
1739 + getFormErrors( object, action );
1740 + },
2442 1741
2443 - scrollToID( id ) {
1742 + checkRequiredField: function( field, errors ) {
1743 + return checkRequiredField( field, errors );
1744 + },
1745 +
1746 + showSubmitLoading: function( $object ) {
1747 + showSubmitLoading( $object );
1748 + },
1749 +
1750 + removeSubmitLoading: function( $object, enable, processesRunning ) {
1751 + removeSubmitLoading( $object, enable, processesRunning );
1752 + },
1753 +
1754 + scrollToID: function( id ) {
2444 1755 const object = jQuery( document.getElementById( id ) );
2445 1756 frmFrontForm.scrollMsg( object, false );
2446 1757 },
2447 1758
2448 - scrollMsg( id, object, animate ) {
2449 - let newPos;
2450 - let screenTop;
2451 - let screenBottom;
2452 - let scrollObj = '';
2453 -
2454 - if ( object === undefined ) {
2455 - scrollObj = jQuery( document.getElementById( `frm_form_${ id }_container` ) );
1759 + scrollMsg: function( id, object, animate ) {
1760 + let newPos, m, b, screenTop, screenBottom,
1761 + scrollObj = '';
1762 + if ( typeof object === 'undefined' ) {
1763 + scrollObj = jQuery( document.getElementById( 'frm_form_' + id + '_container' ) );
2456 1764 if ( scrollObj.length < 1 ) {
2457 1765 return;
2458 1766 }
2459 1767 } else if ( typeof id === 'string' ) {
2460 - const formEl = object instanceof jQuery ? object.get( 0 ) : object;
2461 - const fieldEl = formEl ? formEl.querySelector( `#frm_field_${ id }_container` ) : null;
2462 - scrollObj = fieldEl ? jQuery( fieldEl ) : jQuery();
1768 + scrollObj = jQuery( object ).find( '#frm_field_' + id + '_container' );
2463 1769 } else {
2464 1770 scrollObj = id;
2465 1771 }
2466 1772
@@ -2465,17 +1771,17 @@
2465 1771 }
2466 1772
2467 1773 jQuery( scrollObj ).trigger( 'focus' );
2468 1774 newPos = scrollObj.offset().top;
2469 - if ( ! newPos || frm_js.offset === '-1' ) {
1775 + if ( ! newPos || frm_js.offset === '-1' ) { // eslint-disable-line camelcase
2470 1776 return;
2471 1777 }
2472 - newPos = newPos - frm_js.offset;
1778 + newPos = newPos - frm_js.offset; // eslint-disable-line camelcase
2473 1779
2474 - const docMarginTop = getComputedStyle( document.documentElement ).marginTop;
2475 - const bodyMarginTop = getComputedStyle( document.body ).marginTop;
2476 - if ( docMarginTop || bodyMarginTop ) {
2477 - newPos = newPos - parseInt( docMarginTop ) - parseInt( bodyMarginTop );
1780 + m = jQuery( 'html' ).css( 'margin-top' );
1781 + b = jQuery( 'body' ).css( 'margin-top' );
1782 + if ( m || b ) {
1783 + newPos = newPos - parseInt( m ) - parseInt( b );
2478 1784 }
2479 1785
2480 1786 if ( newPos && window.innerHeight ) {
2481 1787 screenTop = document.documentElement.scrollTop || document.body.scrollTop;
@@ -2482,12 +1788,12 @@
2482 1788 screenBottom = screenTop + window.innerHeight;
2483 1789
2484 1790 if ( newPos > screenBottom || newPos < screenTop ) {
2485 1791 // Not in view
2486 - if ( animate === undefined ) {
2487 - document.documentElement.scrollTop = newPos;
1792 + if ( typeof animate === 'undefined' ) {
1793 + jQuery( window ).scrollTop( newPos );
2488 1794 } else {
2489 - animateScroll( screenTop, newPos, 500 );
1795 + jQuery( 'html,body' ).animate({ scrollTop: newPos }, 500 );
2490 1796 }
2491 1797 return false;
2492 1798 }
2493 1799 }
@@ -2492,13 +1798,13 @@
2492 1798 }
2493 1799 }
2494 1800 },
2495 1801
2496 - fieldValueChanged( e ) {
1802 + fieldValueChanged: function( e ) {
2497 1803 /*jshint validthis:true */
2498 1804
2499 1805 const fieldId = frmFrontForm.getFieldId( this, false );
2500 - if ( ! fieldId ) {
1806 + if ( ! fieldId || typeof fieldId === 'undefined' ) {
2501 1807 return;
2502 1808 }
2503 1809
2504 1810 if ( e.frmTriggered && e.frmTriggered == fieldId ) {
@@ -2504,9 +1810,9 @@
2504 1810 if ( e.frmTriggered && e.frmTriggered == fieldId ) {
2505 1811 return;
2506 1812 }
2507 1813
2508 - jQuery( document ).trigger( 'frmFieldChanged', [ this, fieldId, e ] );
1814 + jQuery( document ).trigger( 'frmFieldChanged', [ this, fieldId, e ]);
2509 1815
2510 1816 if ( e.selfTriggered !== true ) {
2511 1817 maybeValidateChange( this );
2512 1818 }
@@ -2511,10 +1817,9 @@
2511 1817 maybeValidateChange( this );
2512 1818 }
2513 1819 },
2514 1820
2515 - escapeHtml( text ) {
2516 - console.warn( 'DEPRECATED: function frmFrontForm.escapeHtml in v6.17' );
1821 + escapeHtml: function( text ) {
2517 1822 return text
2518 1823 .replace( /&/g, '&amp;' )
2519 1824 .replace( /</g, '&lt;' )
2520 1825 .replace( />/g, '&gt;' )
@@ -2521,9 +1826,17 @@
2521 1826 .replace( /"/g, '&quot;' )
2522 1827 .replace( /'/g, '&#039;' );
2523 1828 },
2524 1829
2525 - triggerCustomEvent,
1830 + invisible: function( classes ) {
1831 + jQuery( classes ).css( 'visibility', 'hidden' );
1832 + },
1833 +
1834 + visible: function( classes ) {
1835 + jQuery( classes ).css( 'visibility', 'visible' );
1836 + },
1837 +
1838 + triggerCustomEvent: triggerCustomEvent,
2526 1839 documentOn
2527 1840 };
2528 1841 }
2529 1842
@@ -2530,49 +1843,30 @@
2530 1843 window.frmFrontForm = frmFrontFormJS();
2531 1844
2532 1845 jQuery( document ).ready( function() {
2533 1846 frmFrontForm.init();
2534 -} );
1847 +});
2535 1848
2536 1849 function frmRecaptcha() {
2537 1850 frmCaptcha( '.frm-g-recaptcha' );
2538 1851 }
2539 1852
2540 -function frmHcaptcha() {
2541 - frmCaptcha( '.h-captcha' );
2542 -}
2543 -
2544 1853 function frmTurnstile() {
2545 - frmCaptcha( '.frm-cf-turnstile' );
1854 + frmCaptcha( '.cf-turnstile' );
2546 1855 }
2547 1856
2548 1857 function frmCaptcha( captchaSelector ) {
2549 - if ( '.h-captcha' === captchaSelector ) {
2550 - // hCaptcha is still rendered implicitly, so we only want to handle the label and exit early.
2551 - // Match the hcaptcha labels to the hcaptcha response fields.
2552 - const captchaLabels = document.querySelectorAll( 'label[for="h-captcha-response"]' );
2553 - if ( captchaLabels.length ) {
2554 - captchaLabels.forEach( label => {
2555 - const captchaResponse = label.closest( 'form' )?.querySelector( '[name="h-captcha-response"]' );
2556 - if ( captchaResponse ) {
2557 - label.htmlFor = captchaResponse.id;
2558 - }
2559 - } );
2560 - }
2561 - return;
2562 - }
2563 -
2564 1858 let c;
2565 1859 const captchas = document.querySelectorAll( captchaSelector );
2566 - const cl = captchas.length;
1860 + const cl = captchas.length;
2567 1861 for ( c = 0; c < cl; c++ ) {
2568 - const closestForm = captchas[ c ].closest( 'form' );
1862 + const closestForm = captchas[c].closest( 'form' );
2569 1863 const formIsVisible = closestForm && closestForm.offsetParent !== null;
2570 - const captcha = captchas[ c ];
1864 + const captcha = captchas[c];
2571 1865 if ( ! formIsVisible ) {
2572 1866 // If the form is not visible, try again later in 400ms.
2573 1867 // This fixes issues where the form fades visible on page load.
2574 - // Or when the form is inside of a modal.
1868 + // Or whne the form is inside of a modal.
2575 1869 const interval = setInterval(
2576 1870 function() {
2577 1871 if ( closestForm && closestForm.offsetParent !== null ) {
2578 1872 frmFrontForm.renderCaptcha( captcha, captchaSelector );
@@ -2590,9 +1884,9 @@
2590 1884 function getSelectedCaptcha( captchaSelector ) {
2591 1885 if ( captchaSelector === '.frm-g-recaptcha' ) {
2592 1886 return grecaptcha;
2593 1887 }
2594 - if ( document.querySelector( '.frm-cf-turnstile' ) ) {
1888 + if ( document.querySelector( '.cf-turnstile' ) ) {
2595 1889 return turnstile;
2596 1890 }
2597 1891 return hcaptcha;
2598 1892 }