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