| @@ -1,9 +1,13 @@ | ||
| 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 | + | |
| 6 | 10 | let jsErrors = []; |
| 7 | 11 | |
| 8 | 12 | /** |
| 9 | 13 | * Triggers custom JS event. |
| @@ -11,9 +15,9 @@ | ||
| 11 | 15 | * @since 5.5.3 |
| 12 | 16 | * |
| 13 | 17 | * @param {HTMLElement} el The HTML element. |
| 14 | 18 | * @param {string} eventName Event name. |
| 15 | - * @param {*} data The passed data. | |
| 19 | + * @param {mixed} data The passed data. | |
| 16 | 20 | */ |
| 17 | 21 | function triggerCustomEvent( el, eventName, data ) { |
| 18 | 22 | if ( typeof window.CustomEvent !== 'function' ) { |
| 19 | 23 | return; |
| @@ -32,27 +36,29 @@ | ||
| 32 | 36 | * @param {boolean} fullID |
| 33 | 37 | * @return {string|number} Field ID. |
| 34 | 38 | */ |
| 35 | 39 | function getFieldId( field, fullID ) { |
| 36 | - let nameParts; | |
| 37 | - let fieldId; | |
| 38 | - let isRepeating = false; | |
| 39 | - let fieldName = ''; | |
| 40 | - | |
| 40 | + let nameParts, fieldId, | |
| 41 | + isRepeating = false, | |
| 42 | + fieldName = ''; | |
| 41 | 43 | if ( field instanceof jQuery ) { |
| 42 | - field = field.get( 0 ); | |
| 44 | + fieldName = field.attr( 'name' ); | |
| 45 | + } else { | |
| 46 | + fieldName = field.name; | |
| 43 | 47 | } |
| 44 | 48 | |
| 45 | - fieldName = field.name; | |
| 46 | - | |
| 47 | - if ( fieldName === undefined ) { | |
| 49 | + if ( typeof fieldName === 'undefined' ) { | |
| 48 | 50 | fieldName = ''; |
| 49 | 51 | } |
| 50 | 52 | |
| 51 | 53 | if ( fieldName === '' ) { |
| 52 | - fieldName = field.getAttribute( 'data-name' ); | |
| 54 | + if ( field instanceof jQuery ) { | |
| 55 | + fieldName = field.data( 'name' ); | |
| 56 | + } else { | |
| 57 | + fieldName = field.getAttribute( 'data-name' ); | |
| 58 | + } | |
| 53 | 59 | |
| 54 | - if ( fieldName === undefined ) { | |
| 60 | + if ( typeof fieldName === 'undefined' ) { | |
| 55 | 61 | fieldName = ''; |
| 56 | 62 | } |
| 57 | 63 | |
| 58 | 64 | if ( fieldName !== '' && fieldName ) { |
| @@ -67,24 +73,25 @@ | ||
| 67 | 73 | return 0; |
| 68 | 74 | } |
| 69 | 75 | nameParts = nameParts.filter( function( n ) { |
| 70 | 76 | return n !== ''; |
| 71 | - } ); | |
| 77 | + }); | |
| 72 | 78 | |
| 73 | - fieldId = nameParts[ 0 ]; | |
| 79 | + fieldId = nameParts[0]; | |
| 74 | 80 | |
| 75 | 81 | if ( nameParts.length === 1 ) { |
| 76 | 82 | return fieldId; |
| 77 | 83 | } |
| 78 | 84 | |
| 79 | - if ( nameParts[ 1 ] === '[form' || nameParts[ 1 ] === '[row_ids' ) { | |
| 85 | + if ( nameParts[1] === '[form' || nameParts[1] === '[row_ids' ) { | |
| 80 | 86 | return 0; |
| 81 | 87 | } |
| 82 | 88 | |
| 83 | 89 | // Check if 'this' is in a repeating section |
| 84 | - if ( document.querySelector( `input[name="item_meta[${ fieldId }][form]"]` ) ) { | |
| 90 | + if ( document.querySelector( 'input[name="item_meta[' + fieldId + '][form]"]' ) ) { | |
| 91 | + | |
| 85 | 92 | // this is a repeatable section with name: item_meta[repeating-section-id][row-id][field-id] |
| 86 | - fieldId = nameParts[ 2 ].replace( '[', '' ); | |
| 93 | + fieldId = nameParts[2].replace( '[', '' ); | |
| 87 | 94 | isRepeating = true; |
| 88 | 95 | } |
| 89 | 96 | |
| 90 | 97 | // Check if 'this' is an other text field and get field ID for it |
| @@ -90,21 +97,21 @@ | ||
| 90 | 97 | // Check if 'this' is an other text field and get field ID for it |
| 91 | 98 | if ( 'other' === fieldId ) { |
| 92 | 99 | if ( isRepeating ) { |
| 93 | 100 | // name for other fields: item_meta[370][0][other][414] |
| 94 | - fieldId = nameParts[ 3 ].replace( '[', '' ); | |
| 101 | + fieldId = nameParts[3].replace( '[', '' ); | |
| 95 | 102 | } else { |
| 96 | 103 | // Other field name: item_meta[other][370] |
| 97 | - fieldId = nameParts[ 1 ].replace( '[', '' ); | |
| 104 | + fieldId = nameParts[1].replace( '[', '' ); | |
| 98 | 105 | } |
| 99 | 106 | } |
| 100 | 107 | |
| 101 | 108 | if ( fullID === true ) { |
| 102 | 109 | // For use in the container div id |
| 103 | - if ( fieldId === nameParts[ 0 ] ) { | |
| 104 | - fieldId = `${ fieldId }-${ nameParts[ 1 ].replace( '[', '' ) }`; | |
| 110 | + if ( fieldId === nameParts[0]) { | |
| 111 | + fieldId = fieldId + '-' + nameParts[1].replace( '[', '' ); | |
| 105 | 112 | } else { |
| 106 | - fieldId = `${ fieldId }-${ nameParts[ 0 ] }-${ nameParts[ 1 ].replace( '[', '' ) }`; | |
| 113 | + fieldId = fieldId + '-' + nameParts[0] + '-' + nameParts[1].replace( '[', '' ); | |
| 107 | 114 | } |
| 108 | 115 | } |
| 109 | 116 | |
| 110 | 117 | return fieldId; |
| @@ -117,15 +124,9 @@ | ||
| 117 | 124 | * |
| 118 | 125 | * @param {Object} $form |
| 119 | 126 | */ |
| 120 | 127 | 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 | - ); | |
| 128 | + $form.find( 'input[type="submit"], input[type="button"], button[type="submit"], button.frm_save_draft' ).attr( 'disabled', 'disabled' ); | |
| 128 | 129 | } |
| 129 | 130 | |
| 130 | 131 | /** |
| 131 | 132 | * Enable the submit button for a given jQuery form object |
| @@ -131,16 +132,12 @@ | ||
| 131 | 132 | * Enable the submit button for a given jQuery form object |
| 132 | 133 | * |
| 133 | 134 | * @since 2.03.02 |
| 134 | 135 | * |
| 135 | - * @param {HTMLElement} form | |
| 136 | - * | |
| 137 | - * @return {void} | |
| 136 | + * @param {Object} $form | |
| 138 | 137 | */ |
| 139 | - function enableSubmitButton( form ) { | |
| 140 | - form.querySelectorAll( 'input[type="submit"], input[type="button"], button[type="submit"]' ).forEach( | |
| 141 | - button => button.disabled = false | |
| 142 | - ); | |
| 138 | + function enableSubmitButton( $form ) { | |
| 139 | + $form.find( 'input[type="submit"], input[type="button"], button[type="submit"]' ).prop( 'disabled', false ); | |
| 143 | 140 | } |
| 144 | 141 | |
| 145 | 142 | /** |
| 146 | 143 | * Disable the save draft link for a given jQuery form object |
| @@ -149,33 +146,26 @@ | ||
| 149 | 146 | * |
| 150 | 147 | * @param {Object} $form |
| 151 | 148 | */ |
| 152 | 149 | 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 | - ); | |
| 150 | + $form.find( 'a.frm_save_draft' ).css( 'pointer-events', 'none' ); | |
| 160 | 151 | } |
| 161 | 152 | |
| 162 | 153 | /** |
| 163 | - * Enable the save draft link for a given form object. | |
| 154 | + * Enable the save draft link for a given jQuery form object | |
| 164 | 155 | * |
| 165 | 156 | * @since 4.04.03 |
| 166 | 157 | * |
| 167 | - * @param {jQuery|HTMLElement} $form | |
| 158 | + * @param {jQuery} $form | |
| 168 | 159 | */ |
| 169 | 160 | function enableSaveDraft( $form ) { |
| 170 | - const form = $form instanceof jQuery ? $form.get( 0 ) : $form; | |
| 171 | - if ( ! form ) { | |
| 161 | + if ( ! $form.length ) { | |
| 172 | 162 | return; |
| 173 | 163 | } |
| 174 | - form.querySelectorAll( '.frm_save_draft' ).forEach( saveDraftButton => { | |
| 175 | - saveDraftButton.disabled = false; | |
| 164 | + $form[0].querySelectorAll( '.frm_save_draft' ).forEach( saveDraftButton => { | |
| 165 | + saveDraftButton.disabled = false; | |
| 176 | 166 | saveDraftButton.style.pointerEvents = ''; |
| 177 | - } ); | |
| 167 | + }); | |
| 178 | 168 | } |
| 179 | 169 | |
| 180 | 170 | /** |
| 181 | 171 | * Validate form with JS. |
| @@ -247,14 +237,15 @@ | ||
| 247 | 237 | * @param {Array} errors |
| 248 | 238 | * @return {void} |
| 249 | 239 | */ |
| 250 | 240 | function checkValidity( field, errors ) { |
| 241 | + let fieldID; | |
| 251 | 242 | if ( 'object' !== typeof field.validity || false !== field.validity.valid ) { |
| 252 | 243 | return; |
| 253 | 244 | } |
| 254 | 245 | |
| 255 | - const fieldID = getFieldId( field, true ); | |
| 256 | - if ( errors[ fieldID ] === undefined ) { | |
| 246 | + fieldID = getFieldId( field, true ); | |
| 247 | + if ( 'undefined' === typeof errors[ fieldID ]) { | |
| 257 | 248 | errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' ); |
| 258 | 249 | } |
| 259 | 250 | |
| 260 | 251 | if ( 'function' === typeof field.reportValidity ) { |
| @@ -278,27 +269,24 @@ | ||
| 278 | 269 | * @param {HTMLElement} field |
| 279 | 270 | */ |
| 280 | 271 | function maybeValidateChange( field ) { |
| 281 | 272 | if ( field.type === 'url' ) { |
| 282 | - maybeAddHttpsToUrl( field ); | |
| 273 | + maybeAddHttpToUrl( field ); | |
| 283 | 274 | } |
| 284 | 275 | const form = field.closest( 'form' ); |
| 285 | - if ( ! form ) { | |
| 286 | - return; | |
| 276 | + if ( form && hasClass( form, 'frm_js_validate' ) ) { | |
| 277 | + validateField( field ); | |
| 287 | 278 | } |
| 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 | 279 | } |
| 292 | 280 | |
| 293 | 281 | /** |
| 294 | 282 | * @param {HTMLElement} field |
| 295 | 283 | */ |
| 296 | - function maybeAddHttpsToUrl( field ) { | |
| 284 | + function maybeAddHttpToUrl( field ) { | |
| 297 | 285 | const url = field.value; |
| 298 | 286 | const matches = url.match( /^(https?|ftps?|mailto|news|feed|telnet):/ ); |
| 299 | 287 | if ( field.value !== '' && matches === null ) { |
| 300 | - field.value = `https://${ url }`; | |
| 288 | + field.value = 'http://' + url; | |
| 301 | 289 | } |
| 302 | 290 | } |
| 303 | 291 | |
| 304 | 292 | /** |
| @@ -303,22 +291,16 @@ | ||
| 303 | 291 | |
| 304 | 292 | /** |
| 305 | 293 | * Validate a field with JS. |
| 306 | 294 | * |
| 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 | 295 | * @param {HTMLElement} field |
| 312 | - * @param {boolean} addErrors Whether to add new errors. Defaults to `true`. | |
| 313 | 296 | * |
| 314 | 297 | * @return {void} |
| 315 | 298 | */ |
| 316 | - function validateField( field, addErrors = true ) { | |
| 317 | - let errors; | |
| 318 | - let key; | |
| 299 | + function validateField( field ) { | |
| 300 | + let errors, key; | |
| 319 | 301 | |
| 320 | - errors = []; | |
| 302 | + errors = []; | |
| 321 | 303 | const fieldContainer = field.closest( '.frm_form_field' ); |
| 322 | 304 | |
| 323 | 305 | if ( ! fieldContainer ) { |
| 324 | 306 | // Hidden fields do not have a field container and do not require JS validation. |
| @@ -332,20 +314,14 @@ | ||
| 332 | 314 | if ( errors.length < 1 ) { |
| 333 | 315 | validateFieldValue( field, errors, false ); |
| 334 | 316 | } |
| 335 | 317 | |
| 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 | - } | |
| 318 | + const $fieldCont = jQuery( fieldContainer ); | |
| 319 | + removeFieldError( $fieldCont ); | |
| 320 | + if ( Object.keys( errors ).length > 0 ) { | |
| 321 | + for ( key in errors ) { | |
| 322 | + addFieldError( $fieldCont, key, errors ); | |
| 344 | 323 | } |
| 345 | - } else if ( ! hasErrors ) { | |
| 346 | - // JS validation is off, so only remove existing errors once the field passes validation. | |
| 347 | - removeFieldError( fieldContainer ); | |
| 348 | 324 | } |
| 349 | 325 | } |
| 350 | 326 | |
| 351 | 327 | /** |
| @@ -371,20 +347,16 @@ | ||
| 371 | 347 | } else if ( field.pattern !== null ) { |
| 372 | 348 | checkPatternField( field, errors ); |
| 373 | 349 | } |
| 374 | 350 | |
| 375 | - if ( 'tel' === field.type && shouldCheckConfirmField( field, onSubmit ) ) { | |
| 376 | - confirmField( field, errors ); | |
| 377 | - } | |
| 378 | - | |
| 379 | 351 | /** |
| 380 | 352 | * @since 6.15 Added `onSubmit` to the data. |
| 381 | 353 | */ |
| 382 | 354 | triggerCustomEvent( document, 'frm_validate_field_value', { |
| 383 | - field, | |
| 384 | - errors, | |
| 385 | - onSubmit | |
| 386 | - } ); | |
| 355 | + field: field, | |
| 356 | + errors: errors, | |
| 357 | + onSubmit: onSubmit | |
| 358 | + }); | |
| 387 | 359 | } |
| 388 | 360 | |
| 389 | 361 | /** |
| 390 | 362 | * @param {HTMLElement} field |
| @@ -391,14 +363,12 @@ | ||
| 391 | 363 | * @param {Array} errors |
| 392 | 364 | * @return {Array} Errors |
| 393 | 365 | */ |
| 394 | 366 | 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' ); | |
| 367 | + let tempVal, i, placeholder, | |
| 368 | + val = '', | |
| 369 | + fieldID = '', | |
| 370 | + fileID = field.getAttribute( 'data-frmfile' ); | |
| 401 | 371 | |
| 402 | 372 | if ( field.type === 'hidden' && fileID === null && ! isAppointmentField( field ) && ! isInlineDatepickerField( field ) ) { |
| 403 | 373 | return errors; |
| 404 | 374 | } |
| @@ -403,9 +373,9 @@ | ||
| 403 | 373 | return errors; |
| 404 | 374 | } |
| 405 | 375 | |
| 406 | 376 | if ( field.type === 'checkbox' || field.type === 'radio' ) { |
| 407 | - document.querySelectorAll( `input[name="${ field.name }"]` ).forEach( function( input ) { | |
| 377 | + document.querySelectorAll( 'input[name="' + field.name + '"]' ).forEach( function( input ) { | |
| 408 | 378 | const requiredField = input.closest( '.frm_required_field' ); |
| 409 | 379 | if ( ! requiredField ) { |
| 410 | 380 | return; |
| 411 | 381 | } |
| @@ -415,14 +385,14 @@ | ||
| 415 | 385 | val = checkedInput.value; |
| 416 | 386 | } ); |
| 417 | 387 | } ); |
| 418 | 388 | } else if ( field.type === 'file' || fileID ) { |
| 419 | - if ( fileID === undefined ) { | |
| 389 | + if ( typeof fileID === 'undefined' ) { | |
| 420 | 390 | fileID = getFieldId( field, true ); |
| 421 | 391 | fileID = fileID.replace( 'file', '' ); |
| 422 | 392 | } |
| 423 | 393 | |
| 424 | - if ( errors[ fileID ] === undefined ) { | |
| 394 | + if ( typeof errors[ fileID ] === 'undefined' ) { | |
| 425 | 395 | val = getFileVals( fileID ); |
| 426 | 396 | } |
| 427 | 397 | fieldID = fileID; |
| 428 | 398 | } else { |
| @@ -430,10 +400,9 @@ | ||
| 430 | 400 | // skip hidden other fields |
| 431 | 401 | return errors; |
| 432 | 402 | } |
| 433 | 403 | |
| 434 | - val = jQuery( field ).val(); // eslint-disable-line no-jquery/no-val | |
| 435 | - | |
| 404 | + val = jQuery( field ).val(); | |
| 436 | 405 | if ( val === null ) { |
| 437 | 406 | val = ''; |
| 438 | 407 | } else if ( typeof val !== 'string' ) { |
| 439 | 408 | tempVal = val; |
| @@ -438,10 +407,10 @@ | ||
| 438 | 407 | } else if ( typeof val !== 'string' ) { |
| 439 | 408 | tempVal = val; |
| 440 | 409 | val = ''; |
| 441 | 410 | for ( i = 0; i < tempVal.length; i++ ) { |
| 442 | - if ( tempVal[ i ] !== '' ) { | |
| 443 | - val = tempVal[ i ]; | |
| 411 | + if ( tempVal[i] !== '' ) { | |
| 412 | + val = tempVal[i]; | |
| 444 | 413 | } |
| 445 | 414 | } |
| 446 | 415 | } |
| 447 | 416 | |
| @@ -465,11 +434,9 @@ | ||
| 465 | 434 | // set id for time field |
| 466 | 435 | fieldID = fieldID.replace( '-H', '' ).replace( '-m', '' ); |
| 467 | 436 | } else if ( isSignatureField( field ) ) { |
| 468 | 437 | 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 : ''; | |
| 438 | + val = jQuery( field ).closest( '.frm_form_field' ).find( '[name="' + field.getAttribute( 'name' ).replace( '[typed]', '[output]' ) + '"]' ).val(); | |
| 472 | 439 | } |
| 473 | 440 | fieldID = fieldID.replace( '-typed', '' ); |
| 474 | 441 | } |
| 475 | 442 | |
| @@ -520,16 +487,16 @@ | ||
| 520 | 487 | * @param {string|number} fileID |
| 521 | 488 | * @return {string} File input value. |
| 522 | 489 | */ |
| 523 | 490 | function getFileVals( fileID ) { |
| 524 | - let val = ''; | |
| 525 | - const fileFields = document.querySelectorAll( `input[name="file${ fileID }"], input[name="file${ fileID }[]"], input[name^="item_meta[${ fileID }]"]` ); | |
| 491 | + let val = '', | |
| 492 | + fileFields = jQuery( 'input[name="file' + fileID + '"], input[name="file' + fileID + '[]"], input[name^="item_meta[' + fileID + ']"]' ); | |
| 526 | 493 | |
| 527 | - fileFields.forEach( function( field ) { | |
| 494 | + fileFields.each( function() { | |
| 528 | 495 | if ( val === '' ) { |
| 529 | - val = field.value; | |
| 496 | + val = this.value; | |
| 530 | 497 | } |
| 531 | - } ); | |
| 498 | + }); | |
| 532 | 499 | return val; |
| 533 | 500 | } |
| 534 | 501 | |
| 535 | 502 | /** |
| @@ -537,10 +504,10 @@ | ||
| 537 | 504 | * @param {Array} errors |
| 538 | 505 | * @return {void} |
| 539 | 506 | */ |
| 540 | 507 | function checkUrlField( field, errors ) { |
| 541 | - let fieldID; | |
| 542 | - const url = field.value; | |
| 508 | + let fieldID, | |
| 509 | + url = field.value; | |
| 543 | 510 | |
| 544 | 511 | if ( url !== '' && ! /^http(s)?:\/\/(?:localhost|(?:[\da-z\.-]+\.[\da-z\.-]+))/i.test( url ) ) { |
| 545 | 512 | fieldID = getFieldId( field, true ); |
| 546 | 513 | if ( ! ( fieldID in errors ) ) { |
| @@ -581,10 +548,10 @@ | ||
| 581 | 548 | * @param {Object} errors Errors data. |
| 582 | 549 | * @param {boolean} onSubmit Is `true` if the form is being submitted. |
| 583 | 550 | */ |
| 584 | 551 | 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; | |
| 552 | + const fieldID = getFieldId( field, true ), | |
| 553 | + 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 | 554 | |
| 588 | 555 | // validate the current field we're editing first |
| 589 | 556 | if ( '' !== field.value && pattern.test( field.value ) === false ) { |
| 590 | 557 | errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' ); |
| @@ -615,23 +582,24 @@ | ||
| 615 | 582 | * @param {Array} errors |
| 616 | 583 | * @return {void} |
| 617 | 584 | */ |
| 618 | 585 | 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_' ) ); | |
| 586 | + let value, confirmValue, firstField, | |
| 587 | + fieldID = getFieldId( field, true ), | |
| 588 | + strippedId = field.id.replace( 'conf_', '' ), | |
| 589 | + strippedFieldID = fieldID.replace( 'conf_', '' ), | |
| 590 | + confirmField = document.getElementById( strippedId.replace( 'field_', 'field_conf_' ) ); | |
| 623 | 591 | |
| 624 | - if ( ! confirmField || errors[ `conf_${ strippedFieldID }` ] !== undefined ) { | |
| 592 | + if ( confirmField === null || typeof errors[ 'conf_' + strippedFieldID ] !== 'undefined' ) { | |
| 625 | 593 | return; |
| 626 | 594 | } |
| 627 | 595 | |
| 628 | 596 | if ( fieldID !== strippedFieldID ) { |
| 629 | - const firstField = document.getElementById( strippedId ); | |
| 630 | - const { value } = firstField; | |
| 631 | - const confirmValue = confirmField.value; | |
| 597 | + firstField = document.getElementById( strippedId ); | |
| 598 | + value = firstField.value; | |
| 599 | + confirmValue = confirmField.value; | |
| 632 | 600 | if ( value !== confirmValue ) { |
| 633 | - errors[ `conf_${ strippedFieldID }` ] = getFieldValidationMessage( confirmField, 'data-confmsg' ); | |
| 601 | + errors[ 'conf_' + strippedFieldID ] = getFieldValidationMessage( confirmField, 'data-confmsg' ); | |
| 634 | 602 | } |
| 635 | 603 | } else { |
| 636 | 604 | validateField( confirmField ); |
| 637 | 605 | } |
| @@ -642,10 +610,10 @@ | ||
| 642 | 610 | * @param {Array} errors |
| 643 | 611 | * @return {void} |
| 644 | 612 | */ |
| 645 | 613 | function checkNumberField( field, errors ) { |
| 646 | - let fieldID; | |
| 647 | - const number = field.value; | |
| 614 | + let fieldID, | |
| 615 | + number = field.value; | |
| 648 | 616 | |
| 649 | 617 | if ( number !== '' && isNaN( number / 1 ) !== false ) { |
| 650 | 618 | fieldID = getFieldId( field, true ); |
| 651 | 619 | if ( ! ( fieldID in errors ) ) { |
| @@ -659,11 +627,11 @@ | ||
| 659 | 627 | * @param {Array} errors |
| 660 | 628 | * @return {void} |
| 661 | 629 | */ |
| 662 | 630 | function checkPatternField( field, errors ) { |
| 663 | - let fieldID; | |
| 664 | - const text = field.value; | |
| 665 | - let format = getFieldValidationMessage( field, 'pattern' ); | |
| 631 | + let fieldID, | |
| 632 | + text = field.value, | |
| 633 | + format = getFieldValidationMessage( field, 'pattern' ); | |
| 666 | 634 | |
| 667 | 635 | if ( format !== '' && text !== '' ) { |
| 668 | 636 | fieldID = getFieldId( field, true ); |
| 669 | 637 | if ( ! ( fieldID in errors ) ) { |
| @@ -671,9 +639,9 @@ | ||
| 671 | 639 | if ( ! window.frmProForm.validateIntlPhoneInput( field ) ) { |
| 672 | 640 | errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' ); |
| 673 | 641 | } |
| 674 | 642 | } else { |
| 675 | - format = new RegExp( `^${ format }$`, 'i' ); | |
| 643 | + format = new RegExp( '^' + format + '$', 'i' ); | |
| 676 | 644 | if ( format.test( text ) === false ) { |
| 677 | 645 | errors[ fieldID ] = getFieldValidationMessage( field, 'data-invmsg' ); |
| 678 | 646 | } |
| 679 | 647 | } |
| @@ -686,11 +654,13 @@ | ||
| 686 | 654 | * |
| 687 | 655 | * @since 6.5.1 |
| 688 | 656 | */ |
| 689 | 657 | 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() : ''; | |
| 658 | + let selects = document.querySelectorAll( '.form-field select' ), | |
| 659 | + styleElement = document.querySelector( '.with_frm_style' ), | |
| 660 | + textColorDisabled = styleElement ? getComputedStyle( styleElement ).getPropertyValue( '--text-color-disabled' ).trim() : '', | |
| 661 | + changeSelectColor; | |
| 662 | + | |
| 693 | 663 | // Exit if there are no select elements or the textColorDisabled property is missing |
| 694 | 664 | if ( ! selects.length || ! textColorDisabled ) { |
| 695 | 665 | return; |
| 696 | 666 | } |
| @@ -695,10 +665,10 @@ | ||
| 695 | 665 | return; |
| 696 | 666 | } |
| 697 | 667 | |
| 698 | 668 | // 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' ) ) { | |
| 669 | + changeSelectColor = function( select ) { | |
| 670 | + if ( select.options[select.selectedIndex] && hasClass( select.options[select.selectedIndex], 'frm-select-placeholder' ) ) { | |
| 701 | 671 | select.style.setProperty( 'color', textColorDisabled, 'important' ); |
| 702 | 672 | } else { |
| 703 | 673 | select.style.color = ''; |
| 704 | 674 | } |
| @@ -711,68 +681,57 @@ | ||
| 711 | 681 | |
| 712 | 682 | // Add an event listener for future changes |
| 713 | 683 | select.addEventListener( 'change', function() { |
| 714 | 684 | changeSelectColor( select ); |
| 715 | - } ); | |
| 716 | - } ); | |
| 685 | + }); | |
| 686 | + }); | |
| 717 | 687 | } |
| 718 | 688 | |
| 719 | 689 | /** |
| 720 | 690 | * @param {HTMLElement|jQuery} object |
| 721 | - * | |
| 722 | - * @return {HTMLElement|false} Captcha element if there is an invisible recaptcha. | |
| 691 | + * @return {boolean} True if there is an invisible recaptcha. | |
| 723 | 692 | */ |
| 724 | 693 | function hasInvisibleRecaptcha( object ) { |
| 694 | + let recaptcha, recaptchaID, alreadyChecked; | |
| 695 | + | |
| 725 | 696 | if ( isGoingToPrevPage( object ) ) { |
| 726 | 697 | return false; |
| 727 | 698 | } |
| 728 | 699 | |
| 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 ); | |
| 700 | + recaptcha = jQuery( object ).find( '.frm-g-recaptcha[data-size="invisible"], .g-recaptcha[data-size="invisible"]' ); | |
| 701 | + if ( recaptcha.length ) { | |
| 702 | + recaptchaID = recaptcha.data( 'rid' ); | |
| 703 | + alreadyChecked = grecaptcha.getResponse( recaptchaID ); | |
| 738 | 704 | if ( alreadyChecked.length === 0 ) { |
| 739 | 705 | return recaptcha; |
| 740 | 706 | } |
| 741 | 707 | } |
| 742 | - | |
| 743 | 708 | return false; |
| 744 | 709 | } |
| 745 | 710 | |
| 746 | 711 | /** |
| 747 | - * @param {HTMLElement} invisibleRecaptcha | |
| 748 | - * | |
| 749 | - * @return {void} | |
| 712 | + * @param {jQuery} invisibleRecaptcha | |
| 750 | 713 | */ |
| 751 | 714 | function executeInvisibleRecaptcha( invisibleRecaptcha ) { |
| 752 | - const recaptchaID = invisibleRecaptcha.dataset.rid; | |
| 715 | + const recaptchaID = invisibleRecaptcha.data( 'rid' ); | |
| 753 | 716 | grecaptcha.reset( recaptchaID ); |
| 754 | 717 | grecaptcha.execute( recaptchaID ); |
| 755 | 718 | } |
| 756 | 719 | |
| 757 | 720 | function validateRecaptcha( form, errors ) { |
| 758 | - const formEl = form instanceof jQuery ? form.get( 0 ) : form; | |
| 759 | - if ( ! formEl ) { | |
| 760 | - return errors; | |
| 761 | - } | |
| 721 | + let response; | |
| 762 | 722 | |
| 763 | - const recaptcha = formEl.querySelector( '.frm-g-recaptcha' ); | |
| 764 | - if ( ! recaptcha ) { | |
| 723 | + const $recaptcha = jQuery( form ).find( '.frm-g-recaptcha' ); | |
| 724 | + if ( ! $recaptcha.length ) { | |
| 765 | 725 | return errors; |
| 766 | 726 | } |
| 767 | 727 | |
| 768 | - const recaptchaID = recaptcha.dataset.rid; | |
| 769 | - let response; | |
| 728 | + const recaptchaID = $recaptcha.data( 'rid' ); | |
| 770 | 729 | |
| 771 | 730 | try { |
| 772 | 731 | response = grecaptcha.getResponse( recaptchaID ); |
| 773 | 732 | } catch ( e ) { |
| 774 | - if ( formEl.querySelector( 'input[name="recaptcha_checked"]' ) ) { | |
| 733 | + if ( jQuery( form ).find( 'input[name="recaptcha_checked"]' ).length ) { | |
| 775 | 734 | return errors; |
| 776 | 735 | } |
| 777 | 736 | response = ''; |
| 778 | 737 | } |
| @@ -777,13 +736,11 @@ | ||
| 777 | 736 | response = ''; |
| 778 | 737 | } |
| 779 | 738 | |
| 780 | 739 | 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 | - } | |
| 740 | + const fieldContainer = $recaptcha.closest( '.frm_form_field' ); | |
| 741 | + const fieldID = fieldContainer.attr( 'id' ).replace( 'frm_field_', '' ).replace( '_container', '' ); | |
| 742 | + errors[ fieldID ] = ''; | |
| 786 | 743 | } |
| 787 | 744 | |
| 788 | 745 | return errors; |
| 789 | 746 | } |
| @@ -816,15 +773,15 @@ | ||
| 816 | 773 | if ( null === errorHtml ) { |
| 817 | 774 | return msg; |
| 818 | 775 | } |
| 819 | 776 | |
| 820 | - errorHtml = errorHtml.replace( /\+/g, '%20' ); | |
| 821 | - msg = decodeURIComponent( errorHtml ).replace( '[error]', msg ); | |
| 822 | - const fieldId = getFieldId( field, false ); | |
| 823 | - const split = fieldId.split( '-' ); | |
| 777 | + errorHtml = errorHtml.replace( /\+/g, '%20' ); | |
| 778 | + msg = decodeURIComponent( errorHtml ).replace( '[error]', msg ); | |
| 779 | + const fieldId = getFieldId( field, false ); | |
| 780 | + const split = fieldId.split( '-' ); | |
| 824 | 781 | const fieldIdParts = field.id.split( '_' ); |
| 825 | 782 | fieldIdParts.shift(); // Drop the "field" value from the front. |
| 826 | - split[ 0 ] = fieldIdParts.join( '_' ); | |
| 783 | + split[0] = fieldIdParts.join( '_' ); | |
| 827 | 784 | const errorKey = split.join( '-' ); |
| 828 | 785 | return msg.replace( '[key]', errorKey ); |
| 829 | 786 | } |
| 830 | 787 | |
| @@ -856,16 +813,18 @@ | ||
| 856 | 813 | * @param {string} action |
| 857 | 814 | * @return {void} |
| 858 | 815 | */ |
| 859 | 816 | function getFormErrors( object, action ) { |
| 860 | - const fieldsets = object.querySelectorAll( '.frm_form_field' ); | |
| 861 | - fieldsets.forEach( field => field.classList.add( 'frm_doing_ajax' ) ); | |
| 817 | + let fieldset, data, success, error, shouldTriggerEvent; | |
| 862 | 818 | |
| 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' ); | |
| 819 | + fieldset = jQuery( object ).find( '.frm_form_field' ); | |
| 820 | + fieldset.addClass( 'frm_doing_ajax' ); | |
| 865 | 821 | |
| 822 | + data = jQuery( object ).serialize() + '&action=frm_entries_' + action + '&nonce=' + frm_js.nonce; // eslint-disable-line camelcase | |
| 823 | + shouldTriggerEvent = object.classList.contains( 'frm_trigger_event_on_submit' ); | |
| 824 | + | |
| 866 | 825 | const doRedirect = response => { |
| 867 | - jQuery( document ).trigger( 'frmBeforeFormRedirect', [ object, response ] ); | |
| 826 | + jQuery( document ).trigger( 'frmBeforeFormRedirect', [ object, response ]); | |
| 868 | 827 | |
| 869 | 828 | if ( ! response.openInNewTab ) { |
| 870 | 829 | // We return here because we're redirecting there is no need to update content. |
| 871 | 830 | window.location = response.redirect; |
| @@ -874,14 +833,17 @@ | ||
| 874 | 833 | |
| 875 | 834 | // We don't return here because we're opening in a new tab, the old tab will still update. |
| 876 | 835 | const newTab = window.open( response.redirect, '_blank' ); |
| 877 | 836 | if ( ! newTab && response.fallbackMsg && response.content ) { |
| 878 | - response.content = response.content.trim().replace( /(<\/div><\/div>)$/, ` ${ response.fallbackMsg }</div></div>` ); | |
| 837 | + response.content = response.content.trim().replace( /(<\/div><\/div>)$/, ' ' + response.fallbackMsg + '</div></div>' ); | |
| 879 | 838 | } |
| 880 | 839 | }; |
| 881 | 840 | |
| 882 | - const success = function( response ) { | |
| 883 | - const defaultResponse = { | |
| 841 | + success = function( response ) { | |
| 842 | + let defaultResponse, formID, replaceContent, pageOrder, formReturned, contSubmit, delay, | |
| 843 | + $fieldCont, key, inCollapsedSection, frmTrigger; | |
| 844 | + | |
| 845 | + defaultResponse = { | |
| 884 | 846 | content: '', |
| 885 | 847 | errors: {}, |
| 886 | 848 | pass: false |
| 887 | 849 | }; |
| @@ -897,11 +859,9 @@ | ||
| 897 | 859 | response = defaultResponse; |
| 898 | 860 | } |
| 899 | 861 | } |
| 900 | 862 | |
| 901 | - let willRedirect = false; | |
| 902 | - | |
| 903 | - if ( response.redirect !== undefined ) { | |
| 863 | + if ( typeof response.redirect !== 'undefined' ) { | |
| 904 | 864 | if ( shouldTriggerEvent ) { |
| 905 | 865 | triggerCustomEvent( object, 'frmSubmitEvent' ); |
| 906 | 866 | return; |
| 907 | 867 | } |
| @@ -912,10 +872,8 @@ | ||
| 912 | 872 | }, 1000 * response.delay ); |
| 913 | 873 | } else { |
| 914 | 874 | doRedirect( response ); |
| 915 | 875 | } |
| 916 | - | |
| 917 | - willRedirect = true; | |
| 918 | 876 | } |
| 919 | 877 | |
| 920 | 878 | if ( 'string' === typeof response.content && response.content !== '' ) { |
| 921 | 879 | // the form or success message was returned |
| @@ -920,26 +878,27 @@ | ||
| 920 | 878 | if ( 'string' === typeof response.content && response.content !== '' ) { |
| 921 | 879 | // the form or success message was returned |
| 922 | 880 | |
| 923 | 881 | if ( shouldTriggerEvent ) { |
| 924 | - triggerCustomEvent( object, 'frmSubmitEvent', { content: response.content } ); | |
| 882 | + triggerCustomEvent( object, 'frmSubmitEvent', { content: response.content }); | |
| 925 | 883 | return; |
| 926 | 884 | } |
| 927 | 885 | |
| 928 | 886 | removeSubmitLoading( jQuery( object ) ); |
| 929 | - if ( frm_js.offset != -1 ) { | |
| 887 | + if ( frm_js.offset != -1 ) { // eslint-disable-line camelcase | |
| 930 | 888 | frmFrontForm.scrollMsg( jQuery( object ), false ); |
| 931 | 889 | } |
| 932 | 890 | |
| 933 | - const formIdInput = object.querySelector( 'input[name="form_id"]' ); | |
| 934 | - const formID = formIdInput ? formIdInput.value : ''; | |
| 891 | + formID = jQuery( object ).find( 'input[name="form_id"]' ).val(); | |
| 935 | 892 | 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 | |
| 893 | + replaceContent = jQuery( object ).closest( '.frm_forms' ); | |
| 937 | 894 | removeAddedScripts( replaceContent, formID ); |
| 938 | - const delay = maybeSlideOut( replaceContent, response.content ); | |
| 895 | + delay = maybeSlideOut( replaceContent, response.content ); | |
| 939 | 896 | |
| 940 | 897 | setTimeout( |
| 941 | 898 | function() { |
| 899 | + let container, input, previousInput; | |
| 900 | + | |
| 942 | 901 | afterFormSubmittedBeforeReplace( object, response ); |
| 943 | 902 | |
| 944 | 903 | replaceContent.replaceWith( response.content ); |
| 945 | 904 | |
| @@ -945,41 +904,48 @@ | ||
| 945 | 904 | |
| 946 | 905 | addUrlParam( response ); |
| 947 | 906 | |
| 948 | 907 | 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 : ''; | |
| 908 | + pageOrder = jQuery( 'input[name="frm_page_order_' + formID + '"]' ).val(); | |
| 909 | + formReturned = jQuery( response.content ).find( 'input[name="form_id"]' ).val(); | |
| 955 | 910 | frmThemeOverride_frmAfterSubmit( formReturned, pageOrder, response.content, object ); |
| 956 | 911 | } |
| 957 | 912 | |
| 913 | + if ( typeof response.recaptcha !== 'undefined' ) { | |
| 914 | + container = jQuery( '#frm_form_' + formID + '_container' ).find( '.frm_fields_container' ); | |
| 915 | + input = '<input type="hidden" name="recaptcha_checked" value="' + response.recaptcha + '">'; | |
| 916 | + previousInput = container.find( 'input[name="recaptcha_checked"]' ); | |
| 917 | + | |
| 918 | + if ( previousInput.length ) { | |
| 919 | + previousInput.replaceWith( input ); | |
| 920 | + } else { | |
| 921 | + container.append( input ); | |
| 922 | + } | |
| 923 | + } | |
| 924 | + | |
| 958 | 925 | afterFormSubmitted( object, response ); |
| 959 | 926 | }, |
| 960 | 927 | delay |
| 961 | 928 | ); |
| 962 | - } else if ( response.errors !== undefined && Object.keys( response.errors ).length ) { | |
| 929 | + } else if ( Object.keys( response.errors ).length ) { | |
| 963 | 930 | // errors were returned |
| 964 | 931 | removeSubmitLoading( jQuery( object ), 'enable' ); |
| 965 | 932 | |
| 966 | 933 | //show errors |
| 967 | - let contSubmit = true; | |
| 934 | + contSubmit = true; | |
| 968 | 935 | removeAllErrors(); |
| 969 | 936 | |
| 970 | - let $fieldCont = null; | |
| 937 | + $fieldCont = null; | |
| 971 | 938 | |
| 972 | - for ( const key in response.errors ) { | |
| 973 | - const fieldContEl = object.querySelector( `#frm_field_${ key }_container` ); | |
| 974 | - $fieldCont = fieldContEl ? jQuery( fieldContEl ) : jQuery(); | |
| 939 | + for ( key in response.errors ) { | |
| 940 | + $fieldCont = jQuery( object ).find( '#frm_field_' + key + '_container' ); | |
| 975 | 941 | |
| 976 | 942 | 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 | |
| 943 | + if ( ! $fieldCont.is( ':visible' ) ) { | |
| 944 | + inCollapsedSection = $fieldCont.closest( '.frm_toggle_container' ); | |
| 979 | 945 | if ( inCollapsedSection.length ) { |
| 980 | - let frmTrigger = inCollapsedSection.prev(); | |
| 981 | - if ( ! frmTrigger.hasClass( 'frm_trigger' ) ) { // eslint-disable-line formidable/no-jquery-variable-methods | |
| 946 | + frmTrigger = inCollapsedSection.prev(); | |
| 947 | + if ( ! frmTrigger.hasClass( 'frm_trigger' ) ) { | |
| 982 | 948 | // If the frmTrigger object is the section description, check to see if the previous element is the trigger |
| 983 | 949 | frmTrigger = frmTrigger.prev( '.frm_trigger' ); |
| 984 | 950 | } |
| 985 | 951 | frmTrigger.trigger( 'click' ); |
| @@ -985,9 +951,9 @@ | ||
| 985 | 951 | frmTrigger.trigger( 'click' ); |
| 986 | 952 | } |
| 987 | 953 | } |
| 988 | 954 | |
| 989 | - if ( $fieldCont.is( ':visible' ) ) { // eslint-disable-line no-jquery/no-is | |
| 955 | + if ( $fieldCont.is( ':visible' ) ) { | |
| 990 | 956 | addFieldError( $fieldCont, key, response.errors ); |
| 991 | 957 | contSubmit = false; |
| 992 | 958 | } |
| 993 | 959 | } |
| @@ -992,10 +958,11 @@ | ||
| 992 | 958 | } |
| 993 | 959 | } |
| 994 | 960 | } |
| 995 | 961 | |
| 996 | - object.querySelectorAll( '.frm-g-recaptcha, .g-recaptcha, .h-captcha' ).forEach( function( captchaEl ) { | |
| 997 | - const recaptchaID = captchaEl.dataset.rid; | |
| 962 | + jQuery( object ).find( '.frm-g-recaptcha, .g-recaptcha, .h-captcha' ).each( function() { | |
| 963 | + const $recaptcha = jQuery( this ), | |
| 964 | + recaptchaID = $recaptcha.data( 'rid' ); | |
| 998 | 965 | |
| 999 | 966 | if ( typeof grecaptcha !== 'undefined' && grecaptcha ) { |
| 1000 | 967 | if ( recaptchaID ) { |
| 1001 | 968 | grecaptcha.reset( recaptchaID ); |
| @@ -1002,23 +969,22 @@ | ||
| 1002 | 969 | } else { |
| 1003 | 970 | grecaptcha.reset(); |
| 1004 | 971 | } |
| 1005 | 972 | } |
| 1006 | - | |
| 1007 | 973 | if ( typeof hcaptcha !== 'undefined' && hcaptcha ) { |
| 1008 | 974 | hcaptcha.reset(); |
| 1009 | 975 | } |
| 1010 | - } ); | |
| 976 | + }); | |
| 1011 | 977 | |
| 1012 | 978 | if ( window.turnstile ) { |
| 1013 | - object.querySelectorAll( '.frm-cf-turnstile' ).forEach( | |
| 979 | + object.querySelectorAll( '.cf-turnstile' ).forEach( | |
| 1014 | 980 | turnstileField => turnstileField.dataset.rid && turnstile.reset( turnstileField.dataset.rid ) |
| 1015 | 981 | ); |
| 1016 | 982 | } |
| 1017 | 983 | |
| 1018 | - jQuery( document ).trigger( 'frmFormErrors', [ object, response ] ); | |
| 984 | + jQuery( document ).trigger( 'frmFormErrors', [ object, response ]); | |
| 1019 | 985 | |
| 1020 | - fieldsets.forEach( field => field.classList.remove( 'frm_doing_ajax' ) ); | |
| 986 | + fieldset.removeClass( 'frm_doing_ajax' ); | |
| 1021 | 987 | scrollToFirstField( object ); |
| 1022 | 988 | |
| 1023 | 989 | if ( contSubmit ) { |
| 1024 | 990 | object.submit(); |
| @@ -1025,10 +991,10 @@ | ||
| 1025 | 991 | } else { |
| 1026 | 992 | object.insertAdjacentHTML( 'afterbegin', response.error_message ); |
| 1027 | 993 | checkForErrorsAndMaybeSetFocus(); |
| 1028 | 994 | } |
| 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. | |
| 995 | + } else { | |
| 996 | + // there may have been a plugin conflict, or the form is not set to submit with ajax | |
| 1031 | 997 | |
| 1032 | 998 | showFileLoading( object ); |
| 1033 | 999 | |
| 1034 | 1000 | object.submit(); |
| @@ -1034,12 +1000,10 @@ | ||
| 1034 | 1000 | object.submit(); |
| 1035 | 1001 | } |
| 1036 | 1002 | }; |
| 1037 | 1003 | |
| 1038 | - const error = function() { | |
| 1039 | - object.querySelectorAll( 'input[type="submit"], input[type="button"]' ).forEach( | |
| 1040 | - button => button.disabled = false | |
| 1041 | - ); | |
| 1004 | + error = function() { | |
| 1005 | + jQuery( object ).find( 'input[type="submit"], input[type="button"]' ).prop( 'disabled', false ); | |
| 1042 | 1006 | object.submit(); |
| 1043 | 1007 | }; |
| 1044 | 1008 | |
| 1045 | 1009 | postToAjaxUrl( object, data, success, error ); |
| @@ -1045,20 +1009,22 @@ | ||
| 1045 | 1009 | postToAjaxUrl( object, data, success, error ); |
| 1046 | 1010 | } |
| 1047 | 1011 | |
| 1048 | 1012 | function postToAjaxUrl( form, data, success, error ) { |
| 1049 | - let ajaxUrl = frm_js.ajax_url; | |
| 1050 | - const action = form.getAttribute( 'action' ); | |
| 1013 | + let ajaxUrl, action, ajaxParams; | |
| 1051 | 1014 | |
| 1052 | - if ( 'string' === typeof action && action.includes( '?action=frm_forms_preview' ) ) { | |
| 1053 | - ajaxUrl = action.split( '?action=frm_forms_preview' )[ 0 ]; | |
| 1015 | + ajaxUrl = frm_js.ajax_url; // eslint-disable-line camelcase | |
| 1016 | + action = form.getAttribute( 'action' ); | |
| 1017 | + | |
| 1018 | + if ( 'string' === typeof action && -1 !== action.indexOf( '?action=frm_forms_preview' ) ) { | |
| 1019 | + ajaxUrl = action.split( '?action=frm_forms_preview' )[0]; | |
| 1054 | 1020 | } |
| 1055 | 1021 | |
| 1056 | - const ajaxParams = { | |
| 1022 | + ajaxParams = { | |
| 1057 | 1023 | type: 'POST', |
| 1058 | 1024 | url: ajaxUrl, |
| 1059 | - data, | |
| 1060 | - success | |
| 1025 | + data: data, | |
| 1026 | + success: success | |
| 1061 | 1027 | }; |
| 1062 | 1028 | |
| 1063 | 1029 | if ( 'function' === typeof error ) { |
| 1064 | 1030 | ajaxParams.error = error; |
| @@ -1063,19 +1029,17 @@ | ||
| 1063 | 1029 | if ( 'function' === typeof error ) { |
| 1064 | 1030 | ajaxParams.error = error; |
| 1065 | 1031 | } |
| 1066 | 1032 | |
| 1067 | - jQuery.ajax( ajaxParams ); // eslint-disable-line no-jquery/no-ajax | |
| 1033 | + jQuery.ajax( ajaxParams ); | |
| 1068 | 1034 | } |
| 1069 | 1035 | |
| 1070 | 1036 | 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 ] ); | |
| 1037 | + const formCompleted = jQuery( response.content ).find( '.frm_message' ); | |
| 1038 | + if ( formCompleted.length ) { | |
| 1039 | + jQuery( document ).trigger( 'frmFormComplete', [ object, response ]); | |
| 1076 | 1040 | } else { |
| 1077 | - jQuery( document ).trigger( 'frmPageChanged', [ object, response ] ); | |
| 1041 | + jQuery( document ).trigger( 'frmPageChanged', [ object, response ]); | |
| 1078 | 1042 | } |
| 1079 | 1043 | } |
| 1080 | 1044 | |
| 1081 | 1045 | /** |
| @@ -1087,30 +1051,28 @@ | ||
| 1087 | 1051 | * @param {Object} response The response from submitting the form with AJAX. |
| 1088 | 1052 | * @return {void} |
| 1089 | 1053 | */ |
| 1090 | 1054 | 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 } ); | |
| 1055 | + const formCompleted = jQuery( response.content ).find( '.frm_message' ); | |
| 1056 | + if ( formCompleted.length ) { | |
| 1057 | + triggerCustomEvent( document, 'frmFormCompleteBeforeReplace', { object, response }); | |
| 1096 | 1058 | } |
| 1097 | 1059 | } |
| 1098 | 1060 | |
| 1099 | 1061 | function removeAddedScripts( formContainer, formID ) { |
| 1100 | - const endReplace = document.querySelectorAll( `.frm_end_ajax_${ formID }` ); | |
| 1062 | + const endReplace = jQuery( '.frm_end_ajax_' + formID ); | |
| 1101 | 1063 | if ( endReplace.length ) { |
| 1102 | - formContainer.nextUntil( `.frm_end_ajax_${ formID }` ).remove(); | |
| 1103 | - endReplace.forEach( el => el.remove() ); | |
| 1064 | + formContainer.nextUntil( '.frm_end_ajax_' + formID ).remove(); | |
| 1065 | + endReplace.remove(); | |
| 1104 | 1066 | } |
| 1105 | 1067 | } |
| 1106 | 1068 | |
| 1107 | 1069 | function maybeSlideOut( oldContent, newContent ) { |
| 1108 | - let c; | |
| 1109 | - let newClass = 'frm_slideout'; | |
| 1110 | - if ( newContent.includes( ' frm_slide' ) ) { | |
| 1070 | + let c, | |
| 1071 | + newClass = 'frm_slideout'; | |
| 1072 | + if ( newContent.indexOf( ' frm_slide' ) !== -1 ) { | |
| 1111 | 1073 | c = oldContent.children(); |
| 1112 | - if ( newContent.includes( ' frm_going_back' ) ) { | |
| 1074 | + if ( newContent.indexOf( ' frm_going_back' ) !== -1 ) { | |
| 1113 | 1075 | newClass += ' frm_going_back'; |
| 1114 | 1076 | } |
| 1115 | 1077 | c.removeClass( 'frm_going_back' ); |
| 1116 | 1078 | c.addClass( newClass ); |
| @@ -1120,27 +1082,29 @@ | ||
| 1120 | 1082 | } |
| 1121 | 1083 | |
| 1122 | 1084 | function addUrlParam( response ) { |
| 1123 | 1085 | let url; |
| 1124 | - if ( history.pushState && response.page !== undefined ) { | |
| 1086 | + if ( history.pushState && typeof response.page !== 'undefined' ) { | |
| 1125 | 1087 | url = addQueryVar( 'frm_page', response.page ); |
| 1126 | - window.history.pushState( { html: response.html }, '', `?${ url }` ); | |
| 1088 | + window.history.pushState({ 'html': response.html }, '', '?' + url ); | |
| 1127 | 1089 | } |
| 1128 | 1090 | } |
| 1129 | 1091 | |
| 1130 | 1092 | function addQueryVar( key, value ) { |
| 1093 | + let kvp, i, x; | |
| 1094 | + | |
| 1131 | 1095 | key = encodeURI( key ); |
| 1132 | 1096 | value = encodeURI( value ); |
| 1133 | 1097 | |
| 1134 | - const kvp = document.location.search.substr( 1 ).split( '&' ); | |
| 1098 | + kvp = document.location.search.substr( 1 ).split( '&' ); | |
| 1135 | 1099 | |
| 1136 | - let i = kvp.length; | |
| 1100 | + i = kvp.length; | |
| 1137 | 1101 | while ( i-- ) { |
| 1138 | - const x = kvp[ i ].split( '=' ); | |
| 1102 | + x = kvp[i].split( '=' ); | |
| 1139 | 1103 | |
| 1140 | - if ( x[ 0 ] == key ) { | |
| 1141 | - x[ 1 ] = value; | |
| 1142 | - kvp[ i ] = x.join( '=' ); | |
| 1104 | + if ( x[0] == key ) { | |
| 1105 | + x[1] = value; | |
| 1106 | + kvp[i] = x.join( '=' ); | |
| 1143 | 1107 | break; |
| 1144 | 1108 | } |
| 1145 | 1109 | } |
| 1146 | 1110 | |
| @@ -1151,59 +1115,44 @@ | ||
| 1151 | 1115 | return kvp.join( '&' ); |
| 1152 | 1116 | } |
| 1153 | 1117 | |
| 1154 | 1118 | function addFieldError( $fieldCont, key, jsErrors ) { |
| 1155 | - const container = $fieldCont instanceof jQuery ? $fieldCont.get( 0 ) : $fieldCont; | |
| 1119 | + let input, id, describedBy, roleString; | |
| 1120 | + if ( $fieldCont.length && $fieldCont.is( ':visible' ) ) { | |
| 1121 | + $fieldCont.addClass( 'frm_blank_field' ); | |
| 1122 | + input = $fieldCont.find( 'input, select, textarea' ); | |
| 1123 | + id = getErrorElementId( key, input.get( 0 ) ); | |
| 1156 | 1124 | |
| 1157 | - if ( ! container || container.offsetParent === null ) { | |
| 1158 | - return; | |
| 1159 | - } | |
| 1125 | + describedBy = input.attr( 'aria-describedby' ); | |
| 1160 | 1126 | |
| 1161 | - container.classList.add( 'frm_blank_field' ); | |
| 1162 | - const inputs = container.querySelectorAll( 'input, select, textarea' ); | |
| 1163 | - const id = getErrorElementId( key, inputs[ 0 ] ); | |
| 1127 | + if ( typeof frmThemeOverride_frmPlaceError === 'function' ) { // eslint-disable-line camelcase | |
| 1128 | + frmThemeOverride_frmPlaceError( key, jsErrors ); | |
| 1129 | + } else { | |
| 1130 | + if ( -1 !== jsErrors[key].indexOf( '<div' ) ) { | |
| 1131 | + $fieldCont.append( | |
| 1132 | + jsErrors[key] | |
| 1133 | + ); | |
| 1134 | + } else { | |
| 1135 | + roleString = frm_js.include_alert_role ? 'role="alert"' : ''; // eslint-disable-line camelcase | |
| 1136 | + $fieldCont.append( '<div class="frm_error" ' + roleString + ' id="' + id + '">' + jsErrors[key] + '</div>' ); | |
| 1137 | + } | |
| 1164 | 1138 | |
| 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 ? input.getAttribute( 'aria-describedby' ) : null; | |
| 1180 | - if ( ! describedBy ) { | |
| 1139 | + if ( typeof describedBy === 'undefined' ) { | |
| 1181 | 1140 | 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 }`; | |
| 1141 | + } else if ( describedBy.indexOf( id ) === -1 && describedBy.indexOf( 'frm_error_field_' ) === -1 ) { | |
| 1142 | + if ( input.data( 'error-first' ) === 0 ) { | |
| 1143 | + describedBy = describedBy + ' ' + id; | |
| 1186 | 1144 | } else { |
| 1187 | - describedBy = `${ id } ${ describedBy }`; | |
| 1145 | + describedBy = id + ' ' + describedBy; | |
| 1188 | 1146 | } |
| 1189 | 1147 | } |
| 1190 | - input.setAttribute( 'aria-describedby', describedBy ); | |
| 1191 | - } ); | |
| 1192 | - } | |
| 1193 | 1148 | |
| 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' ); | |
| 1149 | + input.attr( 'aria-describedby', describedBy ); | |
| 1202 | 1150 | } |
| 1203 | - } ); | |
| 1151 | + input.attr( 'aria-invalid', true ); | |
| 1204 | 1152 | |
| 1205 | - jQuery( document ).trigger( 'frmAddFieldError', [ jQuery( container ), key, jsErrors ] ); | |
| 1153 | + jQuery( document ).trigger( 'frmAddFieldError', [ $fieldCont, key, jsErrors ]); | |
| 1154 | + } | |
| 1206 | 1155 | } |
| 1207 | 1156 | |
| 1208 | 1157 | /** |
| 1209 | 1158 | * Get the ID to use for an error element added when submitting with AJAX. |
| @@ -1214,11 +1163,11 @@ | ||
| 1214 | 1163 | */ |
| 1215 | 1164 | function getErrorElementId( key, input ) { |
| 1216 | 1165 | if ( isNaN( key ) || ! input || ! input.id ) { |
| 1217 | 1166 | // If key isn't a number, assume it's already in the right format. |
| 1218 | - return `frm_error_field_${ key }`; | |
| 1167 | + return 'frm_error_field_' + key; | |
| 1219 | 1168 | } |
| 1220 | - return `frm_error_${ input.id }`; | |
| 1169 | + return 'frm_error_' + input.id; | |
| 1221 | 1170 | } |
| 1222 | 1171 | |
| 1223 | 1172 | /** |
| 1224 | 1173 | * Removes errors before validating with JS. |
| @@ -1223,72 +1172,36 @@ | ||
| 1223 | 1172 | /** |
| 1224 | 1173 | * Removes errors before validating with JS. |
| 1225 | 1174 | * This prevents issues with stale errors that has since been fixed. |
| 1226 | 1175 | * |
| 1227 | - * @param {HTMLElement|jQuery} fieldCont Field container element. | |
| 1176 | + * @param {Object} $fieldCont jQuery object. | |
| 1228 | 1177 | * @return {void} |
| 1229 | 1178 | */ |
| 1230 | - function removeFieldError( fieldCont ) { | |
| 1231 | - const container = fieldCont instanceof jQuery ? fieldCont.get( 0 ) : fieldCont; | |
| 1232 | - if ( ! container ) { | |
| 1233 | - return; | |
| 1179 | + function removeFieldError( $fieldCont ) { | |
| 1180 | + const errorMessage = $fieldCont.find( '.frm_error' ); | |
| 1181 | + const errorId = errorMessage.attr( 'id' ); | |
| 1182 | + const input = $fieldCont.find( 'input, select, textarea' ); | |
| 1183 | + let describedBy = input.attr( 'aria-describedby' ); | |
| 1184 | + | |
| 1185 | + const fieldContainer = $fieldCont.get( 0 ); | |
| 1186 | + if ( fieldContainer && fieldContainer.classList ) { | |
| 1187 | + fieldContainer.classList.remove( 'frm_blank_field', 'has-error' ); | |
| 1234 | 1188 | } |
| 1235 | 1189 | |
| 1236 | - const errorMessage = container.querySelector( '.frm_error' ); | |
| 1237 | - const input = container.querySelector( 'input, select, textarea' ); | |
| 1190 | + errorMessage.remove(); | |
| 1191 | + input.attr( 'aria-invalid', false ); | |
| 1192 | + input.removeAttr( 'aria-describedby' ); | |
| 1238 | 1193 | |
| 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 | - } | |
| 1194 | + if ( typeof describedBy !== 'undefined' ) { | |
| 1195 | + describedBy = describedBy.replace( errorId, '' ); | |
| 1196 | + input.attr( 'aria-describedby', describedBy ); | |
| 1250 | 1197 | } |
| 1251 | - | |
| 1252 | - if ( errorMessage ) { | |
| 1253 | - removeElementFromInputDescribedBy( errorMessage ); | |
| 1254 | - errorMessage.remove(); | |
| 1255 | - } | |
| 1256 | 1198 | } |
| 1257 | 1199 | |
| 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 | 1200 | 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() ); | |
| 1201 | + jQuery( '.form-field' ).removeClass( 'frm_blank_field has-error' ); | |
| 1202 | + jQuery( '.form-field .frm_error' ).replaceWith( '' ); | |
| 1203 | + jQuery( '.frm_error_style' ).remove(); | |
| 1291 | 1204 | } |
| 1292 | 1205 | |
| 1293 | 1206 | /** |
| 1294 | 1207 | * @param {HTMLElement|Object} object Form object. |
| @@ -1311,9 +1224,9 @@ | ||
| 1311 | 1224 | disableSaveDraft( $object ); |
| 1312 | 1225 | } |
| 1313 | 1226 | |
| 1314 | 1227 | 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 | |
| 1228 | + if ( ! $object.hasClass( 'frm_loading_form' ) && ! $object.hasClass( 'frm_loading_prev' ) ) { | |
| 1316 | 1229 | addLoadingClass( $object ); |
| 1317 | 1230 | $object.trigger( 'frmStartFormLoading' ); |
| 1318 | 1231 | } |
| 1319 | 1232 | } |
| @@ -1320,44 +1233,45 @@ | ||
| 1320 | 1233 | |
| 1321 | 1234 | function addLoadingClass( $object ) { |
| 1322 | 1235 | const loadingClass = isGoingToPrevPage( $object ) ? 'frm_loading_prev' : 'frm_loading_form'; |
| 1323 | 1236 | |
| 1324 | - $object.addClass( loadingClass ); // eslint-disable-line no-jquery/no-class, formidable/no-jquery-variable-methods | |
| 1237 | + $object.addClass( loadingClass ); | |
| 1325 | 1238 | } |
| 1326 | 1239 | |
| 1327 | 1240 | function isGoingToPrevPage( $object ) { |
| 1328 | - return typeof frmProForm !== 'undefined' && frmProForm.goingToPreviousPage( $object ); | |
| 1241 | + return ( typeof frmProForm !== 'undefined' && frmProForm.goingToPreviousPage( $object ) ); | |
| 1329 | 1242 | } |
| 1330 | 1243 | |
| 1331 | 1244 | function removeSubmitLoading( _, enable, processesRunning ) { |
| 1245 | + let loadingForm; | |
| 1246 | + | |
| 1332 | 1247 | if ( processesRunning > 0 ) { |
| 1333 | 1248 | return; |
| 1334 | 1249 | } |
| 1335 | 1250 | |
| 1336 | - document.querySelectorAll( '.frm_loading_form' ).forEach( function( form ) { | |
| 1337 | - form.classList.remove( 'frm_loading_form', 'frm_loading_prev' ); | |
| 1338 | - jQuery( form ).trigger( 'frmEndFormLoading' ); | |
| 1251 | + loadingForm = jQuery( '.frm_loading_form' ); | |
| 1252 | + loadingForm.removeClass( 'frm_loading_form' ); | |
| 1253 | + loadingForm.removeClass( 'frm_loading_prev' ); | |
| 1339 | 1254 | |
| 1340 | - if ( enable === 'enable' ) { | |
| 1341 | - enableSubmitButton( form ); | |
| 1342 | - enableSaveDraft( form ); | |
| 1343 | - } | |
| 1344 | - } ); | |
| 1255 | + loadingForm.trigger( 'frmEndFormLoading' ); | |
| 1256 | + | |
| 1257 | + if ( enable === 'enable' ) { | |
| 1258 | + enableSubmitButton( loadingForm ); | |
| 1259 | + enableSaveDraft( loadingForm ); | |
| 1260 | + } | |
| 1345 | 1261 | } |
| 1346 | 1262 | |
| 1347 | 1263 | function showFileLoading( object ) { |
| 1348 | - const loading = document.getElementById( 'frm_loading' ); | |
| 1349 | - if ( ! loading ) { | |
| 1350 | - return; | |
| 1264 | + let fileval, | |
| 1265 | + loading = document.getElementById( 'frm_loading' ); | |
| 1266 | + if ( loading !== null ) { | |
| 1267 | + fileval = jQuery( object ).find( 'input[type=file]' ).val(); | |
| 1268 | + if ( typeof fileval !== 'undefined' && fileval !== '' ) { | |
| 1269 | + setTimeout( function() { | |
| 1270 | + jQuery( loading ).fadeIn( 'slow' ); | |
| 1271 | + }, 2000 ); | |
| 1272 | + } | |
| 1351 | 1273 | } |
| 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 | 1274 | } |
| 1361 | 1275 | |
| 1362 | 1276 | /********************************************** |
| 1363 | 1277 | * General Helpers |
| @@ -1364,9 +1278,9 @@ | ||
| 1364 | 1278 | *********************************************/ |
| 1365 | 1279 | |
| 1366 | 1280 | function confirmClick() { |
| 1367 | 1281 | /*jshint validthis:true */ |
| 1368 | - const message = this.dataset.frmconfirm; | |
| 1282 | + const message = jQuery( this ).data( 'frmconfirm' ); | |
| 1369 | 1283 | return confirm( message ); |
| 1370 | 1284 | } |
| 1371 | 1285 | |
| 1372 | 1286 | /** |
| @@ -1374,12 +1288,11 @@ | ||
| 1374 | 1288 | * If this is a match, the User is autofilling the input on a Webkit browser. |
| 1375 | 1289 | * We want to delete the Honeypot field, otherwise it will get triggered as spam on autocomplete. |
| 1376 | 1290 | */ |
| 1377 | 1291 | function onHoneypotFieldChange() { |
| 1378 | - /*jshint validthis:true */ | |
| 1379 | - const css = window.getComputedStyle( this ).boxShadow; | |
| 1380 | - if ( css?.match( /inset/ ) ) { | |
| 1381 | - this.remove(); | |
| 1292 | + const css = jQuery( this ).css( 'box-shadow' ); | |
| 1293 | + if ( css.match( /inset/ ) ) { | |
| 1294 | + this.parentNode.removeChild( this ); | |
| 1382 | 1295 | } |
| 1383 | 1296 | } |
| 1384 | 1297 | |
| 1385 | 1298 | /** |
| @@ -1402,10 +1315,10 @@ | ||
| 1402 | 1315 | } |
| 1403 | 1316 | |
| 1404 | 1317 | label.addEventListener( 'click', function() { |
| 1405 | 1318 | inputsContainer.querySelector( '.frm_form_field:first-child input, .frm_form_field:first-child select, .frm_form_field:first-child textarea' ).focus(); |
| 1406 | - } ); | |
| 1407 | - } ); | |
| 1319 | + }); | |
| 1320 | + }); | |
| 1408 | 1321 | } |
| 1409 | 1322 | |
| 1410 | 1323 | /** |
| 1411 | 1324 | * Sets focus on a the first subfield of a combo field that has an error. |
| @@ -1430,22 +1343,23 @@ | ||
| 1430 | 1343 | return false; |
| 1431 | 1344 | } |
| 1432 | 1345 | |
| 1433 | 1346 | function checkForErrorsAndMaybeSetFocus() { |
| 1434 | - if ( ! frm_js.focus_first_error ) { | |
| 1347 | + let errors, element, timeoutCallback; | |
| 1348 | + | |
| 1349 | + if ( ! frm_js.focus_first_error ) { // eslint-disable-line camelcase | |
| 1435 | 1350 | return; |
| 1436 | 1351 | } |
| 1437 | 1352 | |
| 1438 | - const errors = document.querySelectorAll( '.frm_form_field .frm_error' ); | |
| 1353 | + errors = document.querySelectorAll( '.frm_form_field .frm_error' ); | |
| 1439 | 1354 | if ( ! errors.length ) { |
| 1440 | 1355 | return; |
| 1441 | 1356 | } |
| 1442 | 1357 | |
| 1443 | - let element = errors[ 0 ]; | |
| 1444 | - let timeoutCallback; | |
| 1358 | + element = errors[0]; | |
| 1445 | 1359 | do { |
| 1446 | 1360 | element = element.previousSibling; |
| 1447 | - if ( [ 'input', 'select', 'textarea' ].includes( element.nodeName.toLowerCase() ) ) { | |
| 1361 | + if ( -1 !== [ 'input', 'select', 'textarea' ].indexOf( element.nodeName.toLowerCase() ) ) { | |
| 1448 | 1362 | focusInput( element ); |
| 1449 | 1363 | break; |
| 1450 | 1364 | } |
| 1451 | 1365 | |
| @@ -1452,9 +1366,9 @@ | ||
| 1452 | 1366 | if ( maybeFocusOnComboSubField( element ) ) { |
| 1453 | 1367 | break; |
| 1454 | 1368 | } |
| 1455 | 1369 | |
| 1456 | - if ( element.classList !== undefined ) { | |
| 1370 | + if ( 'undefined' !== typeof element.classList ) { | |
| 1457 | 1371 | if ( element.classList.contains( 'html-active' ) ) { |
| 1458 | 1372 | timeoutCallback = function() { |
| 1459 | 1373 | const textarea = element.querySelector( 'textarea' ); |
| 1460 | 1374 | if ( null !== textarea ) { |
| @@ -1492,9 +1406,9 @@ | ||
| 1492 | 1406 | function focusInput( input ) { |
| 1493 | 1407 | if ( input.offsetParent !== null ) { |
| 1494 | 1408 | input.focus(); |
| 1495 | 1409 | } else { |
| 1496 | - triggerCustomEvent( document, 'frmMaybeDelayFocus', { input } ); | |
| 1410 | + triggerCustomEvent( document, 'frmMaybeDelayFocus', { input }); | |
| 1497 | 1411 | } |
| 1498 | 1412 | } |
| 1499 | 1413 | |
| 1500 | 1414 | /** |
| @@ -1507,9 +1421,9 @@ | ||
| 1507 | 1421 | * @param {Function} handler Handler. |
| 1508 | 1422 | * @param {boolean | Object} options Options to be added to `addEventListener()` method. Default is `false`. |
| 1509 | 1423 | */ |
| 1510 | 1424 | function documentOn( event, selector, handler, options ) { |
| 1511 | - if ( options === undefined ) { | |
| 1425 | + if ( 'undefined' === typeof options ) { | |
| 1512 | 1426 | options = false; |
| 1513 | 1427 | } |
| 1514 | 1428 | |
| 1515 | 1429 | document.addEventListener( event, function( e ) { |
| @@ -1516,9 +1430,9 @@ | ||
| 1516 | 1430 | let target; |
| 1517 | 1431 | |
| 1518 | 1432 | // loop parent nodes from the target to the delegation node. |
| 1519 | 1433 | for ( target = e.target; target && target != this; target = target.parentNode ) { |
| 1520 | - if ( target.matches && target.matches( selector ) ) { | |
| 1434 | + if ( target && target.matches && target.matches( selector ) ) { | |
| 1521 | 1435 | handler.call( target, e ); |
| 1522 | 1436 | break; |
| 1523 | 1437 | } |
| 1524 | 1438 | } |
| @@ -1525,23 +1439,27 @@ | ||
| 1525 | 1439 | }, options ); |
| 1526 | 1440 | } |
| 1527 | 1441 | |
| 1528 | 1442 | 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'; | |
| 1443 | + let checkFloatLabel, checkDropdownLabel, runOnLoad, selector, floatClass; | |
| 1531 | 1444 | |
| 1532 | - const checkFloatLabel = function( input ) { | |
| 1533 | - const container = input.closest( '.frm_inside_container' ); | |
| 1445 | + selector = '.frm-show-form .frm_inside_container input, .frm-show-form .frm_inside_container select, .frm-show-form .frm_inside_container textarea'; | |
| 1446 | + floatClass = 'frm_label_float_top'; | |
| 1447 | + | |
| 1448 | + checkFloatLabel = function( input ) { | |
| 1449 | + let container, shouldFloatTop, firstOpt; | |
| 1450 | + | |
| 1451 | + container = input.closest( '.frm_inside_container' ); | |
| 1534 | 1452 | if ( ! container ) { |
| 1535 | 1453 | return; |
| 1536 | 1454 | } |
| 1537 | 1455 | |
| 1538 | - const shouldFloatTop = input.value || document.activeElement === input; | |
| 1456 | + shouldFloatTop = input.value || document.activeElement === input; | |
| 1539 | 1457 | |
| 1540 | 1458 | container.classList.toggle( floatClass, shouldFloatTop ); |
| 1541 | 1459 | |
| 1542 | 1460 | if ( 'SELECT' === input.tagName ) { |
| 1543 | - const firstOpt = input.querySelector( 'option:first-child' ); | |
| 1461 | + firstOpt = input.querySelector( 'option:first-child' ); | |
| 1544 | 1462 | |
| 1545 | 1463 | if ( shouldFloatTop ) { |
| 1546 | 1464 | if ( firstOpt.hasAttribute( 'data-label' ) ) { |
| 1547 | 1465 | firstOpt.textContent = firstOpt.getAttribute( 'data-label' ); |
| @@ -1553,10 +1471,10 @@ | ||
| 1553 | 1471 | } |
| 1554 | 1472 | } |
| 1555 | 1473 | }; |
| 1556 | 1474 | |
| 1557 | - const checkDropdownLabel = function() { | |
| 1558 | - document.querySelectorAll( `.frm-show-form .frm_inside_container:not(.${ floatClass }) select` ).forEach( function( input ) { | |
| 1475 | + checkDropdownLabel = function() { | |
| 1476 | + document.querySelectorAll( '.frm-show-form .frm_inside_container:not(.' + floatClass + ') select' ).forEach( function( input ) { | |
| 1559 | 1477 | const firstOpt = input.querySelector( 'option:first-child' ); |
| 1560 | 1478 | |
| 1561 | 1479 | if ( firstOpt.textContent ) { |
| 1562 | 1480 | firstOpt.setAttribute( 'data-label', firstOpt.textContent ); |
| @@ -1561,9 +1479,9 @@ | ||
| 1561 | 1479 | if ( firstOpt.textContent ) { |
| 1562 | 1480 | firstOpt.setAttribute( 'data-label', firstOpt.textContent ); |
| 1563 | 1481 | firstOpt.textContent = ''; |
| 1564 | 1482 | } |
| 1565 | - } ); | |
| 1483 | + }); | |
| 1566 | 1484 | }; |
| 1567 | 1485 | |
| 1568 | 1486 | [ 'focus', 'blur', 'change' ].forEach( function( eventName ) { |
| 1569 | 1487 | documentOn( |
| @@ -1573,12 +1491,16 @@ | ||
| 1573 | 1491 | checkFloatLabel( event.target ); |
| 1574 | 1492 | }, |
| 1575 | 1493 | true |
| 1576 | 1494 | ); |
| 1577 | - } ); | |
| 1495 | + }); | |
| 1578 | 1496 | |
| 1579 | - const runOnLoad = function( firstLoad ) { | |
| 1580 | - if ( firstLoad && document.activeElement && [ 'INPUT', 'SELECT', 'TEXTAREA' ].includes( document.activeElement.tagName ) ) { | |
| 1497 | + jQuery( document ).on( 'change', selector, function( event ) { | |
| 1498 | + checkFloatLabel( event.target ); | |
| 1499 | + }); | |
| 1500 | + | |
| 1501 | + runOnLoad = function( firstLoad ) { | |
| 1502 | + if ( firstLoad && document.activeElement && -1 !== [ 'INPUT', 'SELECT', 'TEXTAREA' ].indexOf( document.activeElement.tagName ) ) { | |
| 1581 | 1503 | checkFloatLabel( document.activeElement ); |
| 1582 | 1504 | } else if ( firstLoad ) { |
| 1583 | 1505 | document.querySelectorAll( '.frm_inside_container' ).forEach( |
| 1584 | 1506 | function( container ) { |
| @@ -1590,9 +1512,8 @@ | ||
| 1590 | 1512 | ); |
| 1591 | 1513 | } |
| 1592 | 1514 | |
| 1593 | 1515 | checkDropdownLabel(); |
| 1594 | - calcProductsTotal(); | |
| 1595 | 1516 | }; |
| 1596 | 1517 | |
| 1597 | 1518 | runOnLoad( true ); |
| 1598 | 1519 | |
| @@ -1597,13 +1518,13 @@ | ||
| 1597 | 1518 | runOnLoad( true ); |
| 1598 | 1519 | |
| 1599 | 1520 | jQuery( document ).on( 'frmPageChanged', function( event ) { |
| 1600 | 1521 | runOnLoad(); |
| 1601 | - } ); | |
| 1522 | + }); | |
| 1602 | 1523 | |
| 1603 | 1524 | document.addEventListener( 'frm_after_start_over', function( event ) { |
| 1604 | 1525 | runOnLoad(); |
| 1605 | - } ); | |
| 1526 | + }); | |
| 1606 | 1527 | } |
| 1607 | 1528 | |
| 1608 | 1529 | function shouldUpdateValidityMessage( target ) { |
| 1609 | 1530 | if ( 'INPUT' !== target.nodeName ) { |
| @@ -1625,10 +1546,10 @@ | ||
| 1625 | 1546 | return true; |
| 1626 | 1547 | } |
| 1627 | 1548 | |
| 1628 | 1549 | function maybeClearCustomValidityMessage( event, field ) { |
| 1629 | - let key; | |
| 1630 | - let isInvalid = false; | |
| 1550 | + let key, | |
| 1551 | + isInvalid = false; | |
| 1631 | 1552 | |
| 1632 | 1553 | if ( ! shouldUpdateValidityMessage( field ) ) { |
| 1633 | 1554 | return; |
| 1634 | 1555 | } |
| @@ -1640,9 +1561,9 @@ | ||
| 1640 | 1561 | if ( 'valid' !== key && field.validity[ key ] === true ) { |
| 1641 | 1562 | isInvalid = true; |
| 1642 | 1563 | break; |
| 1643 | 1564 | } |
| 1644 | - } | |
| 1565 | + }; | |
| 1645 | 1566 | |
| 1646 | 1567 | if ( ! isInvalid ) { |
| 1647 | 1568 | field.setCustomValidity( '' ); |
| 1648 | 1569 | } |
| @@ -1648,29 +1569,33 @@ | ||
| 1648 | 1569 | } |
| 1649 | 1570 | } |
| 1650 | 1571 | |
| 1651 | 1572 | function maybeShowNewTabFallbackMessage() { |
| 1573 | + let messageEl; | |
| 1574 | + | |
| 1652 | 1575 | if ( ! window.frmShowNewTabFallback ) { |
| 1653 | 1576 | return; |
| 1654 | 1577 | } |
| 1655 | 1578 | |
| 1656 | - const messageEl = document.querySelector( `#frm_form_${ frmShowNewTabFallback.formId }_container .frm_message` ); | |
| 1579 | + messageEl = document.querySelector( '#frm_form_' + frmShowNewTabFallback.formId + '_container .frm_message' ); | |
| 1657 | 1580 | if ( ! messageEl ) { |
| 1658 | 1581 | return; |
| 1659 | 1582 | } |
| 1660 | 1583 | |
| 1661 | - messageEl.insertAdjacentHTML( 'beforeend', ` ${ frmShowNewTabFallback.message }` ); | |
| 1584 | + messageEl.insertAdjacentHTML( 'beforeend', ' ' + frmShowNewTabFallback.message ); | |
| 1662 | 1585 | } |
| 1663 | 1586 | |
| 1664 | 1587 | function setCustomValidityMessage() { |
| 1665 | - const forms = document.getElementsByClassName( 'frm-show-form' ); | |
| 1666 | - const { length } = forms; | |
| 1588 | + let forms, length, index; | |
| 1667 | 1589 | |
| 1668 | - for ( let index = 0; index < length; ++index ) { | |
| 1590 | + forms = document.getElementsByClassName( 'frm-show-form' ); | |
| 1591 | + length = forms.length; | |
| 1592 | + | |
| 1593 | + for ( index = 0; index < length; ++index ) { | |
| 1669 | 1594 | forms[ index ].addEventListener( |
| 1670 | 1595 | 'invalid', |
| 1671 | 1596 | function( event ) { |
| 1672 | - const { target } = event; | |
| 1597 | + const target = event.target; | |
| 1673 | 1598 | |
| 1674 | 1599 | if ( shouldUpdateValidityMessage( target ) ) { |
| 1675 | 1600 | target.setCustomValidity( target.dataset.invmsg ); |
| 1676 | 1601 | } |
| @@ -1684,14 +1609,14 @@ | ||
| 1684 | 1609 | window.addEventListener( 'pageshow', function( event ) { |
| 1685 | 1610 | if ( event.persisted ) { |
| 1686 | 1611 | document.querySelectorAll( '.frm_loading_form' ).forEach( |
| 1687 | 1612 | function( form ) { |
| 1688 | - enableSubmitButton( form ); | |
| 1613 | + enableSubmitButton( jQuery( form ) ); | |
| 1689 | 1614 | } |
| 1690 | 1615 | ); |
| 1691 | 1616 | removeSubmitLoading(); |
| 1692 | 1617 | } |
| 1693 | - } ); | |
| 1618 | + }); | |
| 1694 | 1619 | } |
| 1695 | 1620 | |
| 1696 | 1621 | /** |
| 1697 | 1622 | * Destroys the formidable generated global hcaptcha object since it wouldn't otherwise render. |
| @@ -1712,9 +1637,9 @@ | ||
| 1712 | 1637 | const uniqueKey = Array.from( window.crypto.getRandomValues( new Uint8Array( 8 ) ) ) |
| 1713 | 1638 | .map( b => b.toString( 16 ).padStart( 2, '0' ) ) |
| 1714 | 1639 | .join( '' ); |
| 1715 | 1640 | const timestamp = Date.now().toString( 16 ); |
| 1716 | - return `${ uniqueKey }-${ timestamp }`; | |
| 1641 | + return uniqueKey + '-' + timestamp; | |
| 1717 | 1642 | } |
| 1718 | 1643 | |
| 1719 | 1644 | /** |
| 1720 | 1645 | * Animates the scroll position of the document. |
| @@ -1731,471 +1656,32 @@ | ||
| 1731 | 1656 | document.documentElement.scrollTop = end; |
| 1732 | 1657 | return; |
| 1733 | 1658 | } |
| 1734 | 1659 | |
| 1660 | + /* eslint-disable compat/compat */ | |
| 1735 | 1661 | const startTime = performance.now(); |
| 1736 | - const step = currentTime => { | |
| 1662 | + const step = ( currentTime ) => { | |
| 1737 | 1663 | const progress = Math.min( ( currentTime - startTime ) / duration, 1 ); |
| 1738 | - document.documentElement.scrollTop = start + ( ( end - start ) * progress ); | |
| 1664 | + document.documentElement.scrollTop = start + ( end - start ) * progress; | |
| 1739 | 1665 | if ( progress < 1 ) { |
| 1740 | 1666 | requestAnimationFrame( step ); |
| 1741 | 1667 | } |
| 1742 | 1668 | }; |
| 1743 | 1669 | requestAnimationFrame( step ); |
| 1670 | + /* eslint-enable compat/compat */ | |
| 1744 | 1671 | } |
| 1745 | 1672 | |
| 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 | 1673 | return { |
| 2194 | - init() { | |
| 1674 | + init: function() { | |
| 2195 | 1675 | jQuery( document ).off( 'submit.formidable', '.frm-show-form' ); |
| 2196 | 1676 | jQuery( document ).on( 'submit.formidable', '.frm-show-form', frmFrontForm.submitForm ); |
| 2197 | 1677 | |
| 1678 | + jQuery( '.frm-show-form input[onblur], .frm-show-form textarea[onblur]' ).each( function() { | |
| 1679 | + if ( jQuery( this ).val() === '' ) { | |
| 1680 | + jQuery( this ).trigger( 'blur' ); | |
| 1681 | + } | |
| 1682 | + }); | |
| 1683 | + | |
| 2198 | 1684 | 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 | 1685 | |
| 2200 | 1686 | jQuery( document ).on( 'change', '.frm_verify[id^=field_]', onHoneypotFieldChange ); |
| 2201 | 1687 | |
| @@ -2222,13 +1708,8 @@ | ||
| 2222 | 1708 | jQuery( document ).on( |
| 2223 | 1709 | 'frmPageChanged', |
| 2224 | 1710 | destroyhCaptcha |
| 2225 | 1711 | ); |
| 2226 | - | |
| 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(); | |
| 2231 | 1712 | }, |
| 2232 | 1713 | |
| 2233 | 1714 | getFieldId, |
| 2234 | 1715 | |
| @@ -2238,18 +1719,18 @@ | ||
| 2238 | 1719 | * @param {HTMLElement} captcha |
| 2239 | 1720 | * @param {string} captchaSelector |
| 2240 | 1721 | * @return {void} |
| 2241 | 1722 | */ |
| 2242 | - renderCaptcha( captcha, captchaSelector ) { | |
| 1723 | + renderCaptcha: function( captcha, captchaSelector ) { | |
| 2243 | 1724 | const rendered = captcha.getAttribute( 'data-rid' ) !== null; |
| 2244 | 1725 | if ( rendered ) { |
| 2245 | 1726 | return; |
| 2246 | 1727 | } |
| 2247 | 1728 | |
| 2248 | - const size = captcha.getAttribute( 'data-size' ); | |
| 1729 | + const size = captcha.getAttribute( 'data-size' ); | |
| 2249 | 1730 | const params = { |
| 2250 | 1731 | sitekey: captcha.getAttribute( 'data-sitekey' ), |
| 2251 | - size, | |
| 1732 | + size: size, | |
| 2252 | 1733 | theme: captcha.getAttribute( 'data-theme' ) |
| 2253 | 1734 | }; |
| 2254 | 1735 | |
| 2255 | 1736 | if ( size === 'invisible' ) { |
| @@ -2264,29 +1745,26 @@ | ||
| 2264 | 1745 | frmFrontForm.afterRecaptcha( token, formID ); |
| 2265 | 1746 | }; |
| 2266 | 1747 | } |
| 2267 | 1748 | |
| 2268 | - const activeCaptcha = getSelectedCaptcha( captchaSelector ); | |
| 2269 | - const captchaContainer = typeof turnstile !== 'undefined' && turnstile === activeCaptcha ? `#${ captcha.id }` : captcha.id; | |
| 2270 | - const captchaID = activeCaptcha.render( captchaContainer, params ); | |
| 1749 | + const activeCaptcha = getSelectedCaptcha( captchaSelector ); | |
| 1750 | + const captchaContainer = typeof turnstile !== 'undefined' && turnstile === activeCaptcha ? '#' + captcha.id : captcha.id; | |
| 1751 | + const captchaID = activeCaptcha.render( captchaContainer, params ); | |
| 2271 | 1752 | |
| 2272 | 1753 | captcha.setAttribute( 'data-rid', captchaID ); |
| 2273 | - | |
| 2274 | - maybeFixCaptchaLabel( captcha ); | |
| 2275 | 1754 | }, |
| 2276 | 1755 | |
| 2277 | - afterSingleRecaptcha() { | |
| 2278 | - const recaptcha = document.querySelector( '.frm-show-form .g-recaptcha' ); | |
| 2279 | - const object = recaptcha ? recaptcha.closest( 'form' ) : null; | |
| 1756 | + afterSingleRecaptcha: function() { | |
| 1757 | + const object = jQuery( '.frm-show-form .g-recaptcha' ).closest( 'form' )[0]; | |
| 2280 | 1758 | frmFrontForm.submitFormNow( object ); |
| 2281 | 1759 | }, |
| 2282 | 1760 | |
| 2283 | - afterRecaptcha( _, formID ) { | |
| 2284 | - const object = document.querySelector( `#frm_form_${ formID }_container form` ); | |
| 1761 | + afterRecaptcha: function( _, formID ) { | |
| 1762 | + const object = jQuery( '#frm_form_' + formID + '_container form' )[0]; | |
| 2285 | 1763 | frmFrontForm.submitFormNow( object ); |
| 2286 | 1764 | }, |
| 2287 | 1765 | |
| 2288 | - submitForm( e ) { | |
| 1766 | + submitForm: function( e ) { | |
| 2289 | 1767 | frmFrontForm.submitFormManual( e, this ); |
| 2290 | 1768 | }, |
| 2291 | 1769 | |
| 2292 | 1770 | /** |
| @@ -2293,10 +1771,21 @@ | ||
| 2293 | 1771 | * @param {Event} e |
| 2294 | 1772 | * @param {HTMLElement} object The form object that is being submitted. |
| 2295 | 1773 | * @return {void} |
| 2296 | 1774 | */ |
| 2297 | - submitFormManual( e, object ) { | |
| 2298 | - if ( document.body.classList.contains( 'wp-admin' ) && ! object.closest( '.frmapi-form' ) ) { | |
| 1775 | + submitFormManual: function( e, object ) { | |
| 1776 | + let isPro, errors, | |
| 1777 | + invisibleRecaptcha = hasInvisibleRecaptcha( object ), | |
| 1778 | + classList = object.className.trim().split( /\s+/gi ); | |
| 1779 | + | |
| 1780 | + if ( classList && invisibleRecaptcha.length < 1 ) { | |
| 1781 | + isPro = classList.indexOf( 'frm_pro_form' ) > -1; | |
| 1782 | + if ( ! isPro ) { | |
| 1783 | + return; | |
| 1784 | + } | |
| 1785 | + } | |
| 1786 | + | |
| 1787 | + if ( jQuery( 'body' ).hasClass( 'wp-admin' ) && jQuery( object ).closest( '.frmapi-form' ).length < 1 ) { | |
| 2299 | 1788 | return; |
| 2300 | 1789 | } |
| 2301 | 1790 | |
| 2302 | 1791 | e.preventDefault(); |
| @@ -2304,29 +1793,27 @@ | ||
| 2304 | 1793 | if ( typeof frmProForm !== 'undefined' && typeof frmProForm.submitAllowed === 'function' && ! frmProForm.submitAllowed( object ) ) { |
| 2305 | 1794 | return; |
| 2306 | 1795 | } |
| 2307 | 1796 | |
| 2308 | - const errors = frmFrontForm.validateFormSubmit( object ); | |
| 1797 | + errors = frmFrontForm.validateFormSubmit( object ); | |
| 2309 | 1798 | if ( Object.keys( errors ).length !== 0 ) { |
| 2310 | 1799 | return; |
| 2311 | 1800 | } |
| 2312 | 1801 | |
| 2313 | - const invisibleRecaptcha = hasInvisibleRecaptcha( object ); | |
| 2314 | - | |
| 2315 | - if ( invisibleRecaptcha ) { | |
| 1802 | + if ( invisibleRecaptcha.length ) { | |
| 2316 | 1803 | showLoadingIndicator( jQuery( object ) ); |
| 2317 | 1804 | executeInvisibleRecaptcha( invisibleRecaptcha ); |
| 2318 | 1805 | } else { |
| 1806 | + | |
| 2319 | 1807 | showSubmitLoading( jQuery( object ) ); |
| 2320 | 1808 | |
| 2321 | - frmFrontForm.submitFormNow( object ); | |
| 1809 | + frmFrontForm.submitFormNow( object, classList ); | |
| 2322 | 1810 | } |
| 2323 | 1811 | }, |
| 2324 | 1812 | |
| 2325 | - submitFormNow( object ) { | |
| 2326 | - let hasFileFields; | |
| 2327 | - let antispamInput; | |
| 2328 | - const classList = object.className.trim().split( /\s+/gi ); | |
| 1813 | + submitFormNow: function( object ) { | |
| 1814 | + let hasFileFields, antispamInput, | |
| 1815 | + classList = object.className.trim().split( /\s+/gi ); | |
| 2329 | 1816 | |
| 2330 | 1817 | if ( object.hasAttribute( 'data-token' ) && null === object.querySelector( '[name="antispam_token"]' ) ) { |
| 2331 | 1818 | // include the antispam token on form submit. |
| 2332 | 1819 | antispamInput = document.createElement( 'input' ); |
| @@ -2332,24 +1819,24 @@ | ||
| 2332 | 1819 | antispamInput = document.createElement( 'input' ); |
| 2333 | 1820 | antispamInput.type = 'hidden'; |
| 2334 | 1821 | antispamInput.name = 'antispam_token'; |
| 2335 | 1822 | antispamInput.value = object.getAttribute( 'data-token' ); |
| 2336 | - object.append( antispamInput ); | |
| 1823 | + object.appendChild( antispamInput ); | |
| 2337 | 1824 | } |
| 2338 | 1825 | |
| 2339 | 1826 | // Add a unique ID, used for duplicate checks. |
| 2340 | 1827 | const uniqueIDInput = document.createElement( 'input' ); |
| 2341 | - uniqueIDInput.type = 'hidden'; | |
| 2342 | - uniqueIDInput.name = 'unique_id'; | |
| 1828 | + uniqueIDInput.type = 'hidden'; | |
| 1829 | + uniqueIDInput.name = 'unique_id'; | |
| 2343 | 1830 | uniqueIDInput.value = getUniqueKey(); |
| 2344 | - object.append( uniqueIDInput ); | |
| 1831 | + object.appendChild( uniqueIDInput ); | |
| 2345 | 1832 | |
| 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; | |
| 1833 | + if ( classList.indexOf( 'frm_ajax_submit' ) > -1 ) { | |
| 1834 | + hasFileFields = jQuery( object ).find( 'input[type="file"]' ).filter( function() { | |
| 1835 | + return !! this.value; | |
| 1836 | + }).length; | |
| 2349 | 1837 | if ( hasFileFields < 1 ) { |
| 2350 | - const actionInput = object.querySelector( 'input[name="frm_action"]' ); | |
| 2351 | - const action = actionInput ? actionInput.value : ''; | |
| 1838 | + const action = jQuery( object ).find( 'input[name="frm_action"]' ).val(); | |
| 2352 | 1839 | frmFrontForm.checkFormErrors( object, action ); |
| 2353 | 1840 | } else { |
| 2354 | 1841 | object.submit(); |
| 2355 | 1842 | } |
| @@ -2362,11 +1849,10 @@ | ||
| 2362 | 1849 | * @param {HTMLElement|Object} object Form object. This might be a jQuery object. |
| 2363 | 1850 | * |
| 2364 | 1851 | * @return {Array} List of errors. |
| 2365 | 1852 | */ |
| 2366 | - validateFormSubmit( object ) { | |
| 2367 | - const form = object instanceof jQuery ? object.get( 0 ) : object; | |
| 2368 | - if ( typeof tinyMCE !== 'undefined' && form?.querySelector( '.wp-editor-wrap' ) ) { | |
| 1853 | + validateFormSubmit: function( object ) { | |
| 1854 | + if ( typeof tinyMCE !== 'undefined' && jQuery( object ).find( '.wp-editor-wrap' ).length ) { | |
| 2369 | 1855 | tinyMCE.triggerSave(); |
| 2370 | 1856 | } |
| 2371 | 1857 | |
| 2372 | 1858 | jsErrors = []; |
| @@ -2385,19 +1871,16 @@ | ||
| 2385 | 1871 | /** |
| 2386 | 1872 | * @param {HTMLElement|Object} object Form object. This might be a jQuery object. |
| 2387 | 1873 | * @return {Array} List of errors. |
| 2388 | 1874 | */ |
| 2389 | - getAjaxFormErrors( object ) { | |
| 2390 | - let customErrors; | |
| 2391 | - let key; | |
| 2392 | - const form = object instanceof jQuery ? object.get( 0 ) : object; | |
| 1875 | + getAjaxFormErrors: function( object ) { | |
| 1876 | + let customErrors, key; | |
| 2393 | 1877 | |
| 2394 | 1878 | jsErrors = validateForm( object ); |
| 2395 | 1879 | 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 : ''; | |
| 1880 | + const action = jQuery( object ).find( 'input[name="frm_action"]' ).val(); | |
| 2398 | 1881 | customErrors = frmThemeOverride_jsErrors( action, object ); |
| 2399 | - if ( Object.keys( customErrors ).length ) { | |
| 1882 | + if ( Object.keys( customErrors ).length ) { | |
| 2400 | 1883 | for ( key in customErrors ) { |
| 2401 | 1884 | jsErrors[ key ] = customErrors[ key ]; |
| 2402 | 1885 | } |
| 2403 | 1886 | } |
| @@ -2405,9 +1888,9 @@ | ||
| 2405 | 1888 | |
| 2406 | 1889 | triggerCustomEvent( document, 'frm_get_ajax_form_errors', { |
| 2407 | 1890 | formEl: object, |
| 2408 | 1891 | errors: jsErrors |
| 2409 | - } ); | |
| 1892 | + }); | |
| 2410 | 1893 | |
| 2411 | 1894 | return jsErrors; |
| 2412 | 1895 | }, |
| 2413 | 1896 | |
| @@ -2414,18 +1897,17 @@ | ||
| 2414 | 1897 | /** |
| 2415 | 1898 | * @param {HTMLElement|Object} object Form object. This might be a jQuery object. |
| 2416 | 1899 | * @return {void} |
| 2417 | 1900 | */ |
| 2418 | - addAjaxFormErrors( object ) { | |
| 2419 | - let key; | |
| 2420 | - const form = object instanceof jQuery ? object.get( 0 ) : object; | |
| 1901 | + addAjaxFormErrors: function( object ) { | |
| 1902 | + let key, $fieldCont; | |
| 2421 | 1903 | removeAllErrors(); |
| 2422 | 1904 | |
| 2423 | 1905 | for ( key in jsErrors ) { |
| 2424 | - const fieldCont = form ? form.querySelector( `#frm_field_${ key }_container` ) : null; | |
| 1906 | + $fieldCont = jQuery( object ).find( '#frm_field_' + key + '_container' ); | |
| 2425 | 1907 | |
| 2426 | - if ( fieldCont ) { | |
| 2427 | - addFieldError( fieldCont, key, jsErrors ); | |
| 1908 | + if ( $fieldCont.length ) { | |
| 1909 | + addFieldError( $fieldCont, key, jsErrors ); | |
| 2428 | 1910 | } else { |
| 2429 | 1911 | // we are unable to show the error, so remove it |
| 2430 | 1912 | delete jsErrors[ key ]; |
| 2431 | 1913 | } |
| @@ -2439,28 +1921,23 @@ | ||
| 2439 | 1921 | checkRequiredField, |
| 2440 | 1922 | showSubmitLoading, |
| 2441 | 1923 | removeSubmitLoading, |
| 2442 | 1924 | |
| 2443 | - scrollToID( id ) { | |
| 1925 | + scrollToID: function( id ) { | |
| 2444 | 1926 | const object = jQuery( document.getElementById( id ) ); |
| 2445 | 1927 | frmFrontForm.scrollMsg( object, false ); |
| 2446 | 1928 | }, |
| 2447 | 1929 | |
| 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` ) ); | |
| 1930 | + scrollMsg: function( id, object, animate ) { | |
| 1931 | + let newPos, m, b, screenTop, screenBottom, | |
| 1932 | + scrollObj = ''; | |
| 1933 | + if ( typeof object === 'undefined' ) { | |
| 1934 | + scrollObj = jQuery( document.getElementById( 'frm_form_' + id + '_container' ) ); | |
| 2456 | 1935 | if ( scrollObj.length < 1 ) { |
| 2457 | 1936 | return; |
| 2458 | 1937 | } |
| 2459 | 1938 | } 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(); | |
| 1939 | + scrollObj = jQuery( object ).find( '#frm_field_' + id + '_container' ); | |
| 2463 | 1940 | } else { |
| 2464 | 1941 | scrollObj = id; |
| 2465 | 1942 | } |
| 2466 | 1943 | |
| @@ -2465,17 +1942,17 @@ | ||
| 2465 | 1942 | } |
| 2466 | 1943 | |
| 2467 | 1944 | jQuery( scrollObj ).trigger( 'focus' ); |
| 2468 | 1945 | newPos = scrollObj.offset().top; |
| 2469 | - if ( ! newPos || frm_js.offset === '-1' ) { | |
| 1946 | + if ( ! newPos || frm_js.offset === '-1' ) { // eslint-disable-line camelcase | |
| 2470 | 1947 | return; |
| 2471 | 1948 | } |
| 2472 | - newPos = newPos - frm_js.offset; | |
| 1949 | + newPos = newPos - frm_js.offset; // eslint-disable-line camelcase | |
| 2473 | 1950 | |
| 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 ); | |
| 1951 | + m = jQuery( 'html' ).css( 'margin-top' ); | |
| 1952 | + b = jQuery( 'body' ).css( 'margin-top' ); | |
| 1953 | + if ( m || b ) { | |
| 1954 | + newPos = newPos - parseInt( m ) - parseInt( b ); | |
| 2478 | 1955 | } |
| 2479 | 1956 | |
| 2480 | 1957 | if ( newPos && window.innerHeight ) { |
| 2481 | 1958 | screenTop = document.documentElement.scrollTop || document.body.scrollTop; |
| @@ -2482,9 +1959,9 @@ | ||
| 2482 | 1959 | screenBottom = screenTop + window.innerHeight; |
| 2483 | 1960 | |
| 2484 | 1961 | if ( newPos > screenBottom || newPos < screenTop ) { |
| 2485 | 1962 | // Not in view |
| 2486 | - if ( animate === undefined ) { | |
| 1963 | + if ( typeof animate === 'undefined' ) { | |
| 2487 | 1964 | document.documentElement.scrollTop = newPos; |
| 2488 | 1965 | } else { |
| 2489 | 1966 | animateScroll( screenTop, newPos, 500 ); |
| 2490 | 1967 | } |
| @@ -2492,13 +1969,13 @@ | ||
| 2492 | 1969 | } |
| 2493 | 1970 | } |
| 2494 | 1971 | }, |
| 2495 | 1972 | |
| 2496 | - fieldValueChanged( e ) { | |
| 1973 | + fieldValueChanged: function( e ) { | |
| 2497 | 1974 | /*jshint validthis:true */ |
| 2498 | 1975 | |
| 2499 | 1976 | const fieldId = frmFrontForm.getFieldId( this, false ); |
| 2500 | - if ( ! fieldId ) { | |
| 1977 | + if ( ! fieldId || typeof fieldId === 'undefined' ) { | |
| 2501 | 1978 | return; |
| 2502 | 1979 | } |
| 2503 | 1980 | |
| 2504 | 1981 | if ( e.frmTriggered && e.frmTriggered == fieldId ) { |
| @@ -2504,9 +1981,9 @@ | ||
| 2504 | 1981 | if ( e.frmTriggered && e.frmTriggered == fieldId ) { |
| 2505 | 1982 | return; |
| 2506 | 1983 | } |
| 2507 | 1984 | |
| 2508 | - jQuery( document ).trigger( 'frmFieldChanged', [ this, fieldId, e ] ); | |
| 1985 | + jQuery( document ).trigger( 'frmFieldChanged', [ this, fieldId, e ]); | |
| 2509 | 1986 | |
| 2510 | 1987 | if ( e.selfTriggered !== true ) { |
| 2511 | 1988 | maybeValidateChange( this ); |
| 2512 | 1989 | } |
| @@ -2511,9 +1988,9 @@ | ||
| 2511 | 1988 | maybeValidateChange( this ); |
| 2512 | 1989 | } |
| 2513 | 1990 | }, |
| 2514 | 1991 | |
| 2515 | - escapeHtml( text ) { | |
| 1992 | + escapeHtml: function( text ) { | |
| 2516 | 1993 | console.warn( 'DEPRECATED: function frmFrontForm.escapeHtml in v6.17' ); |
| 2517 | 1994 | return text |
| 2518 | 1995 | .replace( /&/g, '&' ) |
| 2519 | 1996 | .replace( /</g, '<' ) |
| @@ -2521,9 +1998,31 @@ | ||
| 2521 | 1998 | .replace( /"/g, '"' ) |
| 2522 | 1999 | .replace( /'/g, ''' ); |
| 2523 | 2000 | }, |
| 2524 | 2001 | |
| 2525 | - triggerCustomEvent, | |
| 2002 | + /** | |
| 2003 | + * This function was used in old back end code in v2.0. | |
| 2004 | + * | |
| 2005 | + * @param {string} classes | |
| 2006 | + * @return {void} | |
| 2007 | + */ | |
| 2008 | + invisible: function( classes ) { | |
| 2009 | + console.warn( 'DEPRECATED: function frmFrontForm.invisible in v6.16.3' ); | |
| 2010 | + jQuery( classes ).css( 'visibility', 'hidden' ); | |
| 2011 | + }, | |
| 2012 | + | |
| 2013 | + /** | |
| 2014 | + * This function was used in old back end code in v2.0. | |
| 2015 | + * | |
| 2016 | + * @param {string} classes | |
| 2017 | + * @return {void} | |
| 2018 | + */ | |
| 2019 | + visible: function( classes ) { | |
| 2020 | + console.warn( 'DEPRECATED: function frmFrontForm.visible in v6.16.3' ); | |
| 2021 | + jQuery( classes ).css( 'visibility', 'visible' ); | |
| 2022 | + }, | |
| 2023 | + | |
| 2024 | + triggerCustomEvent: triggerCustomEvent, | |
| 2526 | 2025 | documentOn |
| 2527 | 2026 | }; |
| 2528 | 2027 | } |
| 2529 | 2028 | |
| @@ -2530,49 +2029,30 @@ | ||
| 2530 | 2029 | window.frmFrontForm = frmFrontFormJS(); |
| 2531 | 2030 | |
| 2532 | 2031 | jQuery( document ).ready( function() { |
| 2533 | 2032 | frmFrontForm.init(); |
| 2534 | -} ); | |
| 2033 | +}); | |
| 2535 | 2034 | |
| 2536 | 2035 | function frmRecaptcha() { |
| 2537 | 2036 | frmCaptcha( '.frm-g-recaptcha' ); |
| 2538 | 2037 | } |
| 2539 | 2038 | |
| 2540 | -function frmHcaptcha() { | |
| 2541 | - frmCaptcha( '.h-captcha' ); | |
| 2542 | -} | |
| 2543 | - | |
| 2544 | 2039 | function frmTurnstile() { |
| 2545 | - frmCaptcha( '.frm-cf-turnstile' ); | |
| 2040 | + frmCaptcha( '.cf-turnstile' ); | |
| 2546 | 2041 | } |
| 2547 | 2042 | |
| 2548 | 2043 | 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 | 2044 | let c; |
| 2565 | 2045 | const captchas = document.querySelectorAll( captchaSelector ); |
| 2566 | - const cl = captchas.length; | |
| 2046 | + const cl = captchas.length; | |
| 2567 | 2047 | for ( c = 0; c < cl; c++ ) { |
| 2568 | - const closestForm = captchas[ c ].closest( 'form' ); | |
| 2048 | + const closestForm = captchas[c].closest( 'form' ); | |
| 2569 | 2049 | const formIsVisible = closestForm && closestForm.offsetParent !== null; |
| 2570 | - const captcha = captchas[ c ]; | |
| 2050 | + const captcha = captchas[c]; | |
| 2571 | 2051 | if ( ! formIsVisible ) { |
| 2572 | 2052 | // If the form is not visible, try again later in 400ms. |
| 2573 | 2053 | // This fixes issues where the form fades visible on page load. |
| 2574 | - // Or when the form is inside of a modal. | |
| 2054 | + // Or whne the form is inside of a modal. | |
| 2575 | 2055 | const interval = setInterval( |
| 2576 | 2056 | function() { |
| 2577 | 2057 | if ( closestForm && closestForm.offsetParent !== null ) { |
| 2578 | 2058 | frmFrontForm.renderCaptcha( captcha, captchaSelector ); |
| @@ -2590,9 +2070,9 @@ | ||
| 2590 | 2070 | function getSelectedCaptcha( captchaSelector ) { |
| 2591 | 2071 | if ( captchaSelector === '.frm-g-recaptcha' ) { |
| 2592 | 2072 | return grecaptcha; |
| 2593 | 2073 | } |
| 2594 | - if ( document.querySelector( '.frm-cf-turnstile' ) ) { | |
| 2074 | + if ( document.querySelector( '.cf-turnstile' ) ) { | |
| 2595 | 2075 | return turnstile; |
| 2596 | 2076 | } |
| 2597 | 2077 | return hcaptcha; |
| 2598 | 2078 | } |