/* eslint-disable */ document.addEventListener( 'DOMContentLoaded', function() { var forms = document.querySelectorAll( '.wp-block-rh-contact-form' ); var formSectionData = []; var limits = document.querySelectorAll( '.wp-block-rh-contact-form-input.is-form-type-file .max-upload-size > .limit' ); window.addEventListener( 'keydown', sectionButtonPressEnter ); createFormOverlays(); initializeAutoForwardSections(); initializeDependencies(); initializeDragndropZones(); initializeSections(); uninvalidateEmptyFields(); for ( var i = 0; i < limits.length; i++ ) { // get the unit and the value of the limit var unitValue = getUnitAndValue( limits[ i ].innerText ); // get the file input var input = limits[ i ].parentNode.parentNode.querySelector( 'input[type="file"]' ); // stop here, if there is no input file if ( ! input ) { return; } input.setAttribute( 'data-max-upload', unitValue.value ); } /** * Create form overlays. */ function createFormOverlays() { for ( const form of forms ) { const formElement = form.querySelector( 'form' ); if ( ! formElement.classList.contains( 'is-ajax-form' ) ) { continue; } let overlay = formElement.querySelector( '.rh-form-overlay' ); let overlayCloseButton; let overlayContainer; const redirectArea = formElement.querySelector( '.wp-block-rh-contact-form-redirect-area' ); if ( ! overlay || ! redirectArea ) { overlayContainer = document.createElement( 'div' ); overlayCloseButton = document.createElement( 'span' ); overlayContainer.classList.add( 'rh-form-overlay-container' ); overlayContainer.innerHTML = '
'; overlayCloseButton.classList.add( 'rh-close-overlay' ); overlayCloseButton.addEventListener( 'click', function( event ) { const currentForm = event.currentTarget.parentNode.parentNode; currentForm.classList.remove( 'is-overlay-open' ); currentForm.querySelector( '.rh-form-overlay-container' ).innerHTML = ''; } ); } if ( ! overlay && ! redirectArea ) { overlay = document.createElement( 'div' ); overlay.classList.add( 'rh-form-overlay' ); overlay.appendChild( overlayCloseButton ); overlay.appendChild( overlayContainer ); formElement.appendChild( overlay ); } else if ( redirectArea ) { redirectArea.insertBefore( overlayContainer, redirectArea.firstChild ); redirectArea.insertBefore( overlayCloseButton, redirectArea.firstChild ); redirectArea.classList.add( 'rh-form-overlay' ); } const overlayBackgroundColor = formElement.getAttribute( 'data-overlay-background-color' ); const overlayColor = formElement.getAttribute( 'data-overlay-color' ); if ( overlayBackgroundColor ) { const overlayId = ( Math.random() + 1 ).toString( 36 ).substring( 2 ); if ( redirectArea ) { redirectArea.id = overlayId; } else { overlay.id = overlayId; } const css = '#' + overlayId + ` { background-color: ` + overlayBackgroundColor + `cc; color: ` + overlayColor + `; } #` + overlayId + ` a, #` + overlayId + ` h1, #` + overlayId + ` h2, #` + overlayId + ` h3, #` + overlayId + ` h4, #` + overlayId + ` h5, #` + overlayId + ` h6 { color: ` + overlayColor + `; }`; const style = document.createElement( 'style' ); document.head.appendChild( style ); if ( style.styleSheet ) { style.styleSheet.cssText = css; } else { style.appendChild( document.createTextNode( css ) ); } } } } /** * Get an available section with matching dependency or no dependency. * * @param {Element} currentSection Current section * @param {String} direction Direction to search, either next or previous * @return {null|Element} Available section element or null */ function getAvailableSection( currentSection, direction ) { if ( direction === 'next' ) { var section = currentSection.nextElementSibling; } else if ( direction === 'previous' ) { var section = currentSection.previousElementSibling; } else { return null; } if ( section && ! section.classList.contains( 'rh-dependency-is-hidden' ) ) { return section; } else if ( section ) { return getAvailableSection( section, direction ); } return null; } /** * Get unit and value of a string. * * @param {string} value The value to get the unit from * @return {{unit: string, value: number}} The unit and value */ function getUnitAndValue( value ) { value = value.replace( /\s/, '' ).toLowerCase(); var unit = 'B'; var valueFloat = parseFloat( value.replace( /[a-z]/g, '' ).replace( ',', '.' ) ); // return empty values immediately if ( valueFloat === 0.00 ) { return { unit: unit, value: valueFloat }; } // get the unit and the value as integer switch ( true ) { case value.includes( 'kb' ): case value.includes( 'kilobyte' ): case value.includes( 'kib' ): case value.includes( 'kibibyte' ): unit = 'KB'; valueFloat = valueFloat * 1024; break; case value.includes( 'mb' ): case value.includes( 'megabyte' ): case value.includes( 'mib' ): case value.includes( 'mibibyte' ): unit = 'MB'; valueFloat = valueFloat * 1024 * 1024; break; case value.includes( 'gb' ): case value.includes( 'gigabyte' ): case value.includes( 'gib' ): case value.includes( 'gibibyte' ): unit = 'GB'; valueFloat = valueFloat * 1024 * 1024 * 1024; break; } return { unit: unit, value: valueFloat }; } /** * Initialize auto-forward a section after radio button click. */ function initializeAutoForwardSections() { for ( var i = 0; i < forms.length; i++ ) { var autoForwardContainers = forms[ i ].querySelectorAll( '.is-auto-forward-section' ); for ( var n = 0; n < autoForwardContainers.length; n++ ) { var section = autoForwardContainers[ n ].parentNode; if ( ! section.classList.contains( 'wp-block-rh-contact-form-section' ) ) { break; } var inputs = autoForwardContainers[ n ].querySelectorAll( '.input-radio-container' ); for ( var j = 0; j < inputs.length; j++ ) { inputs[ j ].addEventListener( 'click', function( event ) { var section = event.currentTarget.closest( '.wp-block-rh-contact-form-section:not(.rh-last-section)' ); if ( ! section ) { return; } var nextButton = section.querySelector( '.rh-block-section-button.button-next' ); if ( ! nextButton ) { return; } nextButton.click(); } ); } } } } /** * Initialize dependencies. */ function initializeDependencies() { for ( var i = 0; i < forms.length; i++ ) { var carverFields = forms[ i ].querySelector( '[name="wp-carver-fields"]' ); var dependentElements = forms[ i ].querySelectorAll( '[data-dependency-condition]' ); var requiredDependentElements = []; if ( dependentElements ) { for ( var n = 0; n < dependentElements.length; n++ ) { var fieldNames = dependentElements[ n ].getAttribute( 'data-dependency-field' ).split( ',' ); // make sure no dependent field is required by default if ( dependentElements[ n ].required ) { dependentElements[ n ].setAttribute( 'data-required', true ); dependentElements[ n ].removeAttribute( 'required' ); requiredDependentElements.push( dependentElements[ n ].name ); } // check sections and radio button containers as well if ( dependentElements[ n ].classList.contains( 'wp-block-rh-contact-form-section' ) || dependentElements[ n ].classList.contains( 'rh-block-contact-form-radio-button-container' ) ) { var requiredFields = dependentElements[ n ].querySelectorAll( '[required]' ); if ( requiredFields ) { for ( var j = 0; j < requiredFields.length; j++ ) { requiredFields[ j ].setAttribute( 'data-required', true ); requiredFields[ j ].removeAttribute( 'required' ); requiredDependentElements.push( requiredFields[ j ].name ); } } } for ( var m = 0; m < fieldNames.length; m++ ) { var conditionalFields = forms[ i ].querySelectorAll( '[name="' + fieldNames[ m ] + '"]' ); if ( conditionalFields ) { for ( var j = 0; j < conditionalFields.length; j++ ) { conditionalFields[ j ].addEventListener( 'input', _checkInputConditions ); } } } } } // remove required dependent fields from carver fields if ( requiredDependentElements.length ) { var requiredFields = carverFields.value.split( ', ' ); for ( var n = 0; n < requiredDependentElements.length; n++ ) { var index = requiredFields.indexOf( requiredDependentElements[ n ] ); if ( index > -1 ) { requiredFields.splice( index, 1 ); } carverFields.value = requiredFields.join( ', ' ); } } forms[ i ].addEventListener( 'submit', __resetHiddenFields ); } } /** * Initialize drag-n-drop zones. */ function initializeDragndropZones() { for ( const form of forms ) { const dropZones = form.querySelectorAll( '.dragndrop-zone__container' ); if ( ! dropZones ) { return; } for ( const dropZone of dropZones ) { initializeDragndropZone( dropZone ); } } } /** * Initialize a drag-and-drop zone. * * @param {HTMLElement} zone The zone */ function initializeDragndropZone( zone ) { const dragInEvents = [ 'dragenter', 'dragover' ]; const dragOutEvents = [ 'dragleave', 'drop' ]; const input = zone.querySelector( '.input-field' ); const uploadButton = zone.querySelector( '.dragndrop-zone__upload-button' ); let zoneFiles = new DataTransfer(); // trigger input click to open local file browser uploadButton.addEventListener( 'click', ( event ) => { event.preventDefault(); input.click(); } ); for ( const dragInEvent of dragInEvents ) { zone.addEventListener( dragInEvent, ( event ) => { event.preventDefault(); event.stopPropagation(); zone.classList.add( 'is-highlighted' ); } ) } for ( const dragOutEvent of dragOutEvents ) { zone.addEventListener( dragOutEvent, ( event ) => { event.preventDefault(); event.stopPropagation(); zone.classList.remove( 'is-highlighted' ); } ) } zone.addEventListener( 'drop', ( event ) => { event.preventDefault(); for ( const file of event.dataTransfer.files ) { zoneFiles.items.add( file ); } if ( ! zoneFiles.files ) { return; } __getSelectedFile( zoneFiles, zone ); input.files = zoneFiles.files; __validateFiles( input, zone ); } ); input.addEventListener( 'change', ( event ) => { for ( const file of event.target.files ) { zoneFiles.items.add( file ); } if ( ! zoneFiles.files ) { return; } __getSelectedFile( zoneFiles, zone ); event.target.files = zoneFiles.files; __validateFiles( event.target, zone ); } ); } /** * Get a selected file. * * @param {DataTransfer} zoneFiles DataTransfer object * @param {HTMLElement} zone The drop zone * @private */ function __getSelectedFile( zoneFiles, zone ) { const files = zoneFiles.files const fileArea = zone.querySelector( '.dragndrop-zone__files' ); // reset list fileArea.innerHTML = ''; let i = 0; for ( const file of files ) { const fileDelete = document.createElement( 'button' ); const fileElement = document.createElement( 'div' ); const fileSize = document.createElement( 'div' ); const fileTitle = document.createElement( 'strong' ); const fileIcon = document.createElement( 'span' ); const fileIconInner = document.createElement( 'span' ); const fileIconSvg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ); const fileIconUse = document.createElementNS( 'http://www.w3.org/2000/svg', 'use' ); fileDelete.classList.add( 'dragndrop-zone__file--delete' ); fileDelete.type = 'button'; fileDelete.ariaLabel = rhBlockContactForm.removeFile.replace( '%s', file.name ); fileElement.classList.add( 'dragndrop-zone__file' ); fileElement.setAttribute( 'data-file-key', i ); fileIcon.classList.add( 'rh-block-icon', 'is-icon-xmark', 'is-inline' ); fileIcon.setAttribute( 'aria-hidden', true ); fileIcon.style.height = '24px'; fileIcon.style.width = '24px'; fileIcon.title = rhBlockContactForm.remove; fileIconInner.classList.add( 'no-link' ); fileIconUse.setAttribute( 'href', '#xmark' ); fileSize.classList.add( 'dragndrop-zone__file--size' ); fileSize.textContent = ( file.size / 1024 / 1024 ).toFixed( 2 ).replace( '.', rhBlockContactForm.decimalSeparator ) + ' MB'; fileTitle.classList.add( 'dragndrop-zone__file--title' ); fileTitle.textContent = file.name; fileIconSvg.appendChild( fileIconUse ); fileIconInner.appendChild( fileIconSvg ); fileIcon.appendChild( fileIconInner ); fileDelete.appendChild( fileIcon ); fileElement.appendChild( fileTitle ); fileElement.appendChild( fileDelete ); fileElement.appendChild( fileSize ); fileArea.appendChild( fileElement ); i++; fileDelete.addEventListener( 'click', ( event ) => { __deleteFile( event, zoneFiles ); } ); } } /** * Delete a file in a drop zone. * * @param {Event} event The event * @param {DataTransfer} zoneFiles DataTransfer object * @private */ function __deleteFile( event, zoneFiles ) { const fileElement = event.currentTarget.parentNode; const filesContainer = fileElement.parentNode; const input = filesContainer.nextElementSibling; const key = parseInt( fileElement.getAttribute( 'data-file-key' ) ); zoneFiles.items.remove( key ); input.files = zoneFiles.files; fileElement.remove(); // re-calculate keys const files = filesContainer.querySelectorAll( '.dragndrop-zone__file' ); files.forEach( ( file, index ) => { file.setAttribute( 'data-file-key', index ); } ); // re-validate field __validateFiles( input, filesContainer.parentNode ); } /** * Validate selected files in an input 'file'. * * @param {HTMLElement} input The input * @param {HTMLElement} zone The drop zone * @private */ function __validateFiles( input, zone ) { // in order to get the validation work, we need a required attribute input.required = true; const result = validator.tests.file.call( validator, input, validator.prepareFieldData( input ) ); if ( ! result.valid ) { validator.mark( input, result.error ); zone.classList.add( 'is-error' ); } else { validator.unmark( input ); zone.classList.remove( 'is-error' ); } input.required = false; } /** * Initialize sections. */ function initializeSections() { // sort sections by their forms for ( var i = 0; i < forms.length; i++ ) { if ( ! forms[ i ].querySelector( '.wp-block-rh-contact-form-section' ) ) { continue; } formSectionData.push( { form: forms[ i ], children: forms[ i ].querySelectorAll( '.wp-block-rh-contact-form-section' ), } ); } for ( var i = 0; i < formSectionData.length; i++ ) { for ( var n = 0; n < formSectionData[ i ].children.length; n++ ) { // set hidden and visible classes for the form sections if ( ! isFirstSection( formSectionData[ i ].children[ n ] ) ) { formSectionData[ i ].children[ n ].classList.add( 'rh-hidden-section' ); } else { formSectionData[ i ].children[ n ].classList.add( 'rh-first-section' ); formSectionData[ i ].children[ n ].classList.add( 'rh-visible-section' ); } if ( n + 1 === formSectionData[ i ].children.length ) { formSectionData[ i ].children[ n ].classList.add( 'rh-last-section' ); } // get the buttons var buttons = formSectionData[ i ].children[ n ].querySelectorAll( '.rh-block-section-button' ); // add listener to the buttons for ( var m = 0; m < buttons.length; m++ ) { buttons[ m ].addEventListener( 'click', sectionButtonClick ); } } } } /** * Check if the current section is the first section. * A first section is defined as a section with no section as previous element. * * @param {Element} section Section to test * @return {boolean} True if section is a first section, false otherwise */ function isFirstSection( section ) { var previousElement = section.previousElementSibling; if ( ! previousElement || ! previousElement.classList.contains( 'wp-block-rh-contact-form-section' ) ) { return true; } return false; } /** * Show or hide sections on button click. * * @param {Element} event The event object */ function sectionButtonClick( event ) { event.preventDefault(); var currentTarget = event.currentTarget; var section = currentTarget.closest( '.wp-block-rh-contact-form-section' ); if ( currentTarget.classList.contains( 'button-next' ) ) { var inputFields = section.querySelectorAll( 'input, select, textarea' ); var invalidFields = []; for ( var n = 0; n < inputFields.length; n++ ) { var check = validator.checkField( inputFields[ n ] ); if ( ! check.valid ) { invalidFields.push( inputFields[ n ] ); } } if ( invalidFields.length ) { return; } } section.classList.remove( 'rh-visible-section' ); section.classList.add( 'rh-hidden-section' ); if ( currentTarget.classList.contains( 'button-next' ) ) { var newSection = getAvailableSection( section, 'next' ); } else { var newSection = getAvailableSection( section, 'previous' ); } if ( newSection ) { newSection.classList.remove( 'rh-hidden-section' ); newSection.classList.add( 'rh-visible-section' ); if ( newSection !== section ) { newSection.scrollIntoView( { behavior: window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches ? 'auto' : 'smooth', block: 'start', inline: 'start', } ); } } else { console.error( '[Definition Error]: No new section found.' ); } } /** * Trigger a button click to next button on Enter press. * * @param {Event} event The keydown event */ function sectionButtonPressEnter( event ) { if ( event.key !== 'Enter' || event.target.tagName !== 'INPUT' ) { return; } var section = event.target.closest( '.rh-visible-section' ); if ( section ) { event.preventDefault(); section.querySelector( '.rh-block-section-button.button-next' ).click(); } } /** * Check for conditional input fields. * * @param {Event} event Current event * @private */ function _checkInputConditions( event ) { var currentTarget = event.currentTarget; var form = currentTarget.closest( 'form' ); var carverField = form.querySelector( '[name="wp-carver-fields"]' ); var carverValue = carverField.value.split( ',' ); // all elements, which depend on the current field var elements = form.querySelectorAll( '[data-dependency-field*="' + currentTarget.name + '"]' ); for ( var i = 0; i < elements.length; i++ ) { // we need to check all fields the current element is dependent on var fields = elements[ i ].getAttribute( 'data-dependency-field' ).split( ',' ); // if any field is not matching, the end result will be false var isMatchingAllConditions = true; for ( var n = 0; n < fields.length; n++ ) { var checkableFields = form.querySelectorAll( '[name="' + fields[ n ] + '"]' ); var elementValue = elements[ i ].getAttribute( 'data-dependency-value' ).split( ',' )[ n ]; var elementValueBetween = elements[ i ].getAttribute( 'data-dependency-value-between' ); var isConditionCorrect = false; // allow checking for the value of the current checkable field // usually it should only be one field except for radio buttons for ( var j = 0; j < checkableFields.length; j++ ) { if ( checkableFields[ j ] === event.currentTarget ) { var value = event.currentTarget.value; break; } else { var value = checkableFields[ j ].value; } } if ( elements[ i ].getAttribute( 'data-dependency-value-between' ).indexOf( '],[' ) !== -1 ) { elementValueBetween = elements[ i ].getAttribute( 'data-dependency-value-between' ).split( '],[' )[ n ].replace( '[', '' ).replace( ']', '' ); } if ( elementValueBetween && elementValueBetween.indexOf( ',' ) !== -1 ) { elementValueBetween = elementValueBetween.split( ',' ).map( function( value ) { return parseFloat( value.replace( '[', '' ).replace( ']', '' ) ); } ); } switch ( elements[ i ].getAttribute( 'data-dependency-condition' ).split( ',' )[ n ] ) { case 'contains': if ( value.includes( elementValue ) ) { isConditionCorrect = true; } else { isConditionCorrect = false; } break; case 'contains-not': if ( ! value.includes( elementValue ) ) { isConditionCorrect = true; } else { isConditionCorrect = false; } break; case 'ends-with': if ( value.endsWith( elementValue ) ) { isConditionCorrect = true; } else { isConditionCorrect = false; } break; case 'is-between': if ( typeof elementValueBetween[0] === 'undefined' || typeof elementValueBetween[1] === 'undefined' ) { isConditionCorrect = false; break; } var floatValue = parseFloat( value ); if ( floatValue >= elementValueBetween[0] && floatValue <= elementValueBetween[1] ) { isConditionCorrect = true; } else { isConditionCorrect = false; } break; case 'is-equal': if ( value === elementValue ) { isConditionCorrect = true; } else { isConditionCorrect = false; } break; case 'is-not-equal': if ( value !== elementValue ) { isConditionCorrect = true; } else { isConditionCorrect = false; } break; case 'is-selected': isConditionCorrect = false; for ( var j = 0; j < checkableFields.length; j++ ) { if ( checkableFields[ j ].checked ) { isConditionCorrect = true; } } break; case 'starts-with': if ( value.startsWith( elementValue ) ) { isConditionCorrect = true; } else { isConditionCorrect = false; } break; } if ( ! isConditionCorrect ) { isMatchingAllConditions = false; break; } } if ( isMatchingAllConditions ) { if ( elements[ i ].classList.contains( 'wp-block-rh-contact-form-section' ) || elements[ i ].classList.contains( 'rh-block-contact-form-radio-button-container' ) ) { elements[ i ].classList.remove( 'rh-dependency-is-hidden' ); elements[ i ].classList.add( 'rh-dependency-was-hidden' ); var requiredFields = elements[ i ].querySelectorAll( '[data-required]' ); if ( requiredFields ) { var carverFieldValue = carverField.value.split( ', ' ); for ( var j = 0; j < requiredFields.length; j++ ) { if ( carverValue.indexOf( requiredFields[ j ].name ) === -1 ) { carverValue.push( requiredFields[ j ].name ); } requiredFields[ j ].setAttribute( 'required', true ); // add field to carver fields carverFieldValue.push( requiredFields[ j ] ); carverField.value = carverFieldValue.join( ', ' ); } } } else if ( elements[ i ].classList.contains( 'select-field' ) ) { const closestSelect = elements[ i ].closest( '.wp-block-rh-contact-form-select' ); closestSelect.classList.remove( 'rh-dependency-is-hidden' ); closestSelect.classList.add( 'rh-dependency-was-hidden' ); if ( elements[ i ].getAttribute( 'data-required' ) ) { if ( carverValue.indexOf( elements[ i ].name ) === -1 ) { carverValue.push( elements[ i ].name ); } elements[ i ].setAttribute( 'required', true ); } } else { elements[ i ].parentNode.classList.remove( 'rh-dependency-is-hidden' ); elements[ i ].parentNode.classList.add( 'rh-dependency-was-hidden' ); if ( elements[ i ].getAttribute( 'data-required' ) ) { if ( carverValue.indexOf( elements[ i ].name ) === -1 ) { carverValue.push( elements[ i ].name ); } elements[ i ].setAttribute( 'required', true ); } } } else { if ( elements[ i ].classList.contains( 'wp-block-rh-contact-form-section' ) || elements[ i ].classList.contains( 'rh-block-contact-form-radio-button-container' ) ) { elements[ i ].classList.add( 'rh-dependency-is-hidden' ); var requiredFields = elements[ i ].querySelectorAll( '[required]' ); if ( requiredFields ) { for ( var j = 0; j < requiredFields.length; j++ ) { var carverIndex = carverValue.indexOf( requiredFields[ j ].name ); if ( carverIndex !== -1 ) { carverValue.splice( carverIndex, 1 ); } requiredFields[ j ].removeAttribute( 'required' ); } } } else if ( elements[ i ].classList.contains( 'select-field' ) ) { elements[ i ].closest( '.wp-block-rh-contact-form-select' ).classList.add( 'rh-dependency-is-hidden' ); if ( elements[ i ].required ) { var carverIndex = carverValue.indexOf( elements[ i ].name ); if ( carverIndex !== -1 ) { carverValue.splice( carverIndex, 1 ); } elements[ i ].removeAttribute( 'required' ); } } else { elements[ i ].parentNode.classList.add( 'rh-dependency-is-hidden' ); if ( elements[ i ].required ) { var carverIndex = carverValue.indexOf( elements[ i ].name ); if ( carverIndex !== -1 ) { carverValue.splice( carverIndex, 1 ); } elements[ i ].removeAttribute( 'required' ); } } } } carverField.value = carverValue.join( ',' ); } /** * Reset values of hidden fields. * * @param {Event} event Current event * @private */ function __resetHiddenFields( event ) { var elements = event.currentTarget.querySelectorAll( 'input, select, textarea' ); if ( elements ) { for ( var i = 0; i < elements.length; i++ ) { if ( elements[ i ].parentNode.classList.contains( 'rh-dependency-is-hidden' ) ) { elements[ i ].value = ''; } else { var section = elements[ i ].closest( '.wp-block-rh-contact-form-section' ) if ( section && section.classList.contains( 'rh-dependency-is-hidden' ) ) { elements[ i ].value = ''; } } } } } } ); /** * Handles Ajax form submits. * * @param {Event} event Current event * @private */ function __formSubmit( event ) { // should already been called, but safe is safe event.preventDefault(); var form = event.currentTarget; const date = new Date(); const dateEndInput = form.querySelector( 'input[name="' + rhBlockContactForm.fieldNames.dateEnd + '"]' ); const overlayContainer = form.querySelector( '.rh-form-overlay-container' ); const redirectArea = form.querySelector( '.wp-block-rh-contact-form-redirect-area > .rh-block-contact-form__redirect-area' ); const submitEvent = new Event( 'rhContactFormSubmit' ); form.dispatchEvent( submitEvent ); if ( window.dataLayer !== undefined ) { window.dataLayer.push( { event: 'rhContactFormSubmit' } ); } if ( dateEndInput ) { dateEndInput.value = date.toLocaleString(); } form.classList.add( 'is-overlay-open' ); // send form data var xhr = new XMLHttpRequest(); xhr.open( 'POST', form.action, true ); xhr.send( new FormData( form ) ); xhr.onreadystatechange = function() { if ( this.readyState === XMLHttpRequest.DONE ) { var responseContainer = document.createElement( 'div' ); responseContainer.innerHTML = this.response; var dieMessage = responseContainer.querySelector( '.wp-die-message' ); var isError = false; if ( dieMessage ) { if ( this.status.toString() === '200' ) { // default message if ( redirectArea ) { overlayContainer.innerHTML = redirectArea.outerHTML; } else { overlayContainer.innerHTML = dieMessage.innerHTML; } } else { // a WordPress error occurred isError = true; overlayContainer.innerHTML = ''; } } else if ( this.status.toString().charAt( 0 ) !== '4' ) { // regular redirect page var main = responseContainer.querySelector( '.site-main' ); if ( main ) { overlayContainer.innerHTML = main.innerHTML; } else { // different page layout isError = true; overlayContainer.innerHTML = ''; } } else { // redirect page cannot be found isError = true; overlayContainer.innerHTML = ''; } // set status icon var statusIcon = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' ); statusIcon.setAttributeNS( 'http://www.w3.org/2000/svg', 'viewBox', '0 0 52 52' ); statusIcon.classList.add( 'rh-overlay-status-icon' ); overlayContainer.prepend( statusIcon ); if ( isError ) { statusIcon.classList.add( 'is-error' ); var circle = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' ); var path1 = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' ); var path2 = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' ); circle.classList.add( 'crossmark__circle' ); circle.setAttributeNS( null, 'cx', 26 ); circle.setAttributeNS( null, 'cy', 26 ); circle.setAttributeNS( null, 'r', 25 ); circle.setAttributeNS( null, 'fill', 'none' ); path1.classList.add( [ 'cross__path', 'cross__path--right' ] ); path1.setAttributeNS( null, 'd', 'M16,16 l20,20' ); path1.setAttributeNS( null, 'fill', 'none' ); path2.classList.add( [ 'cross__path', 'cross__path--left' ] ); path2.setAttributeNS( null, 'd', 'M16,36 l20,-20' ); path2.setAttributeNS( null, 'fill', 'none' ); statusIcon.appendChild( circle ); statusIcon.appendChild( path1 ); statusIcon.appendChild( path2 ); } else { statusIcon.classList.add( 'is-success' ); var circle = document.createElementNS( 'http://www.w3.org/2000/svg', 'circle' ); var path = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' ); circle.classList.add( 'checkmark__circle' ); circle.setAttributeNS( null, 'cx', 26 ); circle.setAttributeNS( null, 'cy', 26 ); circle.setAttributeNS( null, 'r', 25 ); circle.setAttributeNS( null, 'fill', 'none' ); path.classList.add( 'checkmark__check' ); path.setAttributeNS( null, 'd', 'M14.1 27.2l7.1 7.2 16.7-16.8' ); path.setAttributeNS( null, 'fill', 'none' ); statusIcon.appendChild( circle ); statusIcon.appendChild( path ); form.reset(); var checkedInputContainers = form.querySelectorAll( '.is-valid, .is-invalid' ); const date = new Date(); const dateStart = form.querySelector( 'input[name="' + rhBlockContactForm.fieldNames.dateStart + '"]' ); var firstSections = form.querySelectorAll( '.rh-first-section' ); var lastSections = form.querySelectorAll( '.rh-last-section' ); const originalHidden = form.querySelectorAll( '.rh-dependency-was-hidden' ); // reset start date if ( dateStart ) { dateStart.value = date.toLocaleString(); } // reset sections if ( firstSections ) { // mark first section as active for ( var i = 0; i < firstSections.length; i++ ) { firstSections[ i ].classList.add( 'rh-visible-section' ); firstSections[ i ].classList.remove( 'rh-hidden-section' ); } } if ( lastSections ) { // mark last section as inactive for ( var i = 0; i < lastSections.length; i++ ) { lastSections[ i ].classList.add( 'rh-hidden-section' ); lastSections[ i ].classList.remove( 'rh-visible-section' ); } } // reset input containers if ( checkedInputContainers ) { for ( var i = 0; i < checkedInputContainers.length; i++ ) { checkedInputContainers[ i ].classList.remove( 'is-valid' ); checkedInputContainers[ i ].classList.remove( 'is-invalid' ); } } // reset dependencies if ( originalHidden ) { for ( const element of originalHidden ) { element.classList.add( 'rh-dependency-is-hidden' ); element.classList.remove( 'rh-dependency-was-hidden' ); } } } } } } /** * Check whether a number input contains numbers. * * @since 2.10.4 */ function uninvalidateEmptyFields() { const numberInputs = document.querySelectorAll( '.optional[type="number"]' ); for ( const input of numberInputs ) { const parentNode = input.parentNode; const inputObserver = new MutationObserver( function( mutations, observer ) { if ( parentNode.classList.contains( 'is-valid' ) && ! input.value ) { parentNode.classList.remove( 'is-valid' ); if ( validator ) { validator.mark( input, rhBlockContactForm.numberFailure ); } } } ); inputObserver.observe( parentNode, { attributes: true, } ); } } if ( ! Element.prototype.matches ) { Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; } if ( ! Element.prototype.closest ) { Element.prototype.closest = function( s ) { var el = this; do { if ( el.matches( s ) ) return el; el = el.parentElement || el.parentNode; } while ( el !== null && el.nodeType === 1 ); return null; }; } if ( ! String.prototype.endsWith ) { String.prototype.endsWith = function( search, thisLen ) { if ( thisLen === undefined || thisLen > this.length ) { thisLen = this.length; } return this.substring( thisLen - search.length, thisLen ) === search; }; } if ( ! String.prototype.includes ) { String.prototype.includes = function( search, start ) { 'use strict'; if ( search instanceof RegExp ) { throw TypeError( 'first argument must not be a RegExp' ); } if ( start === undefined ) { start = 0; } return this.indexOf( search, start ) !== -1; }; } if ( ! String.prototype.startsWith ) { Object.defineProperty( String.prototype, 'startsWith', { value: function( search, rawPos ) { var pos = rawPos > 0 ? rawPos | 0 : 0; return this.substring( pos, pos + search.length ) === search; }, } ); }