1/*-----------------------------------------------------------------------------------*/ 2/* Submission Handler 3/*-----------------------------------------------------------------------------------*/ 4 5let contact_labels = document.getElementsByClassName("contact-label"); 6Array.from(contact_labels).forEach(function(element) { 7 element.addEventListener('click', changeContactMethod); 8}); 9window.addEventListener('load', changeContactMethod); 10 11async function changeContactMethod(event) { 12 // event.preventDefault(); 13 const url = AUDIRE.homeUri + "/wp-json/audire/v1/contact-forms/form/"; 14 15 if ( event.type == "click" && event.target.classList.contains("contact-label--active") ) { 16 return; 17 } 18 19 let cid = 0; 20 if ( event.type !== "load" ) { 21 cid = event.target.getAttribute('data-id'); 22 } 23 24 else if ( window.location.hash ) { 25 console.log('Found hash: ' + window.location.hash); 26 let element = document.querySelector('[data-name="' + window.location.hash.slice(1) + '"]'); 27 if ( element ) { 28 cid = element.getAttribute('data-id'); 29 } 30 } 31 32 if ( event.type == "click" || cid > 0 ) { 33 Array.from(contact_labels).forEach(function(element) { 34 element.classList.remove("contact-label--active"); 35 }); 36 37 document.querySelector('[data-id="' + cid + '"]').classList.add("contact-label--active"); 38 } 39 40 try { 41 let pid = getWordpressPostId(); 42 let params = "?pid=" + pid + "&cid=" + cid; 43 let contactWrapper = document.querySelector("#contact-methods__contact"); 44 let contactContent = ''; 45 let sycleScript = ''; 46 47 // Show loader 48 contactWrapper.innerHTML += '<div class="loader--overlay">' + 49 '<span class="loader"></span>' + 50 '</div>'; 51 52 const response = await fetch(url + params); 53 54 if (!response.ok) { 55 throw new Error(`Response status: ${response.status}`); 56 } 57 58 const json = await response.json(); 59 60 if ( json.type == "cf7" ) { 61 let wrapperCls = ["wpcf7--wrapper", "max-width-xs", "text-component", "text-center", "margin-auto"]; 62 63 if ( json.options.background !== "white" ) { 64 wrapperCls.push("wpcf7--wrapper--" + json.options.background); 65 wrapperCls.push("fields-white"); 66 } 67 68 if ( json.options.text == "white" || json.options.text == "default" && json.options.background !== "white" ) { 69 wrapperCls.push("text-white"); 70 } 71 72 contactContent += '<div class="' + wrapperCls.join(' ') + '">'; 73 contactContent += json.introduction; 74 contactContent += updateFormAction( json.data.replace("no-js", "js") ); 75 contactContent += '</div>'; 76 } else if ( json.type == "sycle" ) { 77 sycleScript = document.createElement('script'); 78 sycleScript.textContent = '!function(){window.__ct = window.__ct || [];__ct.token="' + json.data + '";__ct.adSource="1";var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src="https://sycle.audiologydesign.com/assets/js/sycle_script.js";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e)}();'; 79 contactContent += '<div id="ad-sycle-form"></div>'; 80 } else { 81 let wrapperCls = []; 82 83 if ( ! json.options || json.options.media_wrapper !== "none" ) { 84 wrapperCls = ["media-wrapper", "media-wrapper--4:3", "media-wrapper--scroll"]; 85 } 86 87 contactContent += wrapperCls.length 88 ? '<div class="' + wrapperCls.join(' ') + '">' 89 : '<div>'; 90 contactContent += json.data; 91 contactContent += '</div>'; 92 } 93 94 contactWrapper.innerHTML = contactContent; 95 96 if ( json.type == "cf7" ) { 97 initContactForm(); 98 wad_enable_optgroups(); 99 adjustMultilineLabels(); 100 } 101 102 if ( json.type == "sycle" ) { 103 document.body.appendChild(sycleScript); 104 } 105 106 // Custom scripts 107 Array.from(json.scripts).forEach(function(element) { 108 scriptElement = document.createElement('script'); 109 110 for ( var key in element ) { 111 if ( element.hasOwnProperty(key) ) { 112 scriptElement.setAttribute(key, element[key]); 113 } 114 } 115 116 // document.body.appendChild(scriptElement); 117 contactWrapper.appendChild(scriptElement); 118 }); 119
120 // Fire custom event so we know when a form has loaded 121 const contactLoaded = new CustomEvent('loadcontact', { 122 detail: json, 123 bubbles: true, 124 cancelable: true 125 }); 126 document.dispatchEvent(contactLoaded); 127 128 // Generate Contact Content 129 // let message = document.createElement("div"); 130 // message.classList.add("wpcf7--response-message"); 131 // 132 // let responseContainer = document.createElement("div"); 133 // responseContainer.classList.add("wpcf7--response-message--container"); 134 // message.appendChild(responseContainer); 135 // 136 // let headline = document.createElement("p"); 137 // headline.classList.add("font-headline", "weight-500", "text-lg"); 138 // headline.textContent = json.headline; 139 // responseContainer.appendChild(headline); 140 // 141 // let body = document.createElement("p"); 142 // body.textContent = json.body; 143 // responseContainer.appendChild(body); 144 // 145 // if ( json.link_to_homepage === true) { 146 // let btns = document.createElement("p"); 147 // responseContainer.appendChild(btns); 148 // 149 // let homeBtn = document.createElement("a"); 150 // homeBtn.classList.add("btn", "form__button", "form__button--home"); 151 // homeBtn.textContent = "Back to Homepage"; 152 // homeBtn.href = AUDIRE.homeUri; 153 // btns.appendChild(homeBtn); 154 // } 155 // 156 // if ( json.hasOwnProperty("custom_link") && json.custom_link !== false ) { 157 // let btns = document.createElement("p"); 158 // responseContainer.appendChild(btns); 159 // 160 // let theBtn = document.createElement("a"); 161 // theBtn.classList.add("btn", "form__button", "form__button--" + json.custom_link.class); 162 // theBtn.textContent = json.custom_link.text; 163 // 164 // if ( json.custom_link.external == true) { 165 // theBtn.href = json.custom_link.url; 166 // theBtn.target = "_blank"; 167 // } else { 168 // theBtn.href = AUDIRE.homeUri + json.custom_link.url; 169 // } 170 // 171 // btns.appendChild(theBtn); 172 // } 173 } catch (error) { 174 console.error(error.message); 175 } 176} 177 178/*-----------------------------------------------------------------------------------*/ 179/* Utility Functions 180/*-----------------------------------------------------------------------------------*/ 181 182function updateFormAction(htmlString) { 183 const actionMatch = htmlString.match(/action="([^"]*?)"/); 184 const idMatch = htmlString.match(/id="(wpcf7-[^"]*?)"/); 185 186 if (actionMatch && idMatch) { 187 const newActionValue = `${window.location.pathname}#${idMatch[1]}`; 188 const updatedHtml = htmlString.replace(actionMatch[0], `action="${newActionValue}"`); 189 return updatedHtml; 190 } 191 192 // If either value wasn't found, return original 193 return htmlString; 194} 195 196function initContactForm() { 197 let form = document.querySelector('form.wpcf7-form'); 198 wpcf7.init( form ); 199} 200
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.