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