| 1 |
!function(){"use strict";const e=(e,t)=>{let s=null;return function(...n){const o=this;window.clearTimeout(s),s=window.setTimeout((()=>{e.apply(o,n)}),t)}},t=(e,t)=>{for(;(e=e.parentElement)&&!e.classList.contains(t););return e},{epas:s}=window;function n(e,t){t.setAttribute("aria-activedescendant",e)}function o(e,t){!function(e){const t=new CustomEvent("ep-autosuggest-click",{detail:e});if(window.dispatchEvent(t),e.searchTerm&&1===parseInt(s.triggerAnalytics,10)&&"function"===typeof gtag){const t=`click - ${e.searchTerm}`;gtag("event",t,{event_category:"EP :: Autosuggest",event_label:e.url,transport_type:"beacon"})}}({searchTerm:e,url:t}),window.location.href=t}function r(e,t){return"navigate"===s.action?o(e.value,t.dataset.url):(function(e,t){e.value=t}(e,t.innerText),function(e){e.closest("form").submit()}(e))}function a(e,t,{query:s}){const n=(o=t,r=e,s.replace(new RegExp((e=>e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"))(o),"g"),r));var o,r;return n}async function i(e,t){const n={body:e,method:"POST",mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};s?.http_headers&&"object"===typeof s.http_headers&&Object.keys(s.http_headers).forEach((e=>{n.headers[e]=s.http_headers[e]})),s.addSearchTermHeader&&(n.headers["EP-Search-Term"]=encodeURI(t));try{const e=await fetch(s.endpointUrl,n);if(!e.ok)throw Error(e.statusText);const o=await e.json();return"undefined"!==typeof window.epDataFilter?window.epDataFilter(o,t):o}catch(e){return console.error(e),e}}function c(e,n){let o,a="";const{value:i}=n,c=t(n,"ep-autosuggest-container").querySelector(".ep-autosuggest"),l=c.querySelector(".autosuggest-list");for(;l.firstChild;)l.removeChild(l.firstChild);e.length>0?c.style="display: block;":c.style="display: none;";const u=e.length;for(o=0;u>o;++o){const t=e[o]._source.post_title,n=e[o]._source.permalink,r=t.replace(/\\([\s\S])|(")/g,"""),c=i.trim().split(" ");let l=r;if(s.highlightingEnabled){const e=new RegExp(`\\b(${c.join("|")})`,"gi");l=l.replace(e,(e=>`<${s.highlightingTag} class="${s.highlightingClass} ep-autosuggest-highlight">${e}</${s.highlightingTag}>`))}let u=`<li class="autosuggest-item" role="option" aria-selected="false" id="autosuggest-option-${o}">\n\t\t\t\t<a href="${n}" class="autosuggest-link" data-search="${r}" data-url="${n}" tabindex="-1">\n\t\t\t\t\t${l}\n\t\t\t\t</a>\n\t\t\t</li>`;"undefined"!==typeof window.epAutosuggestItemHTMLFilter&&(u=window.epAutosuggestItemHTMLFilter(u,e[o],o,i)),a+=u}l.innerHTML=a;const d=Array.from(document.querySelectorAll(".autosuggest-link"));return l.addEventListener("click",(e=>{e.preventDefault();const t=e.target.tagName===s.highlightingTag?.toUpperCase()?e.target.parentElement:e.target;d.includes(t)&&r(n,t)})),!0}function l(){const e=document.querySelectorAll(".autosuggest-list"),t=document.querySelectorAll(".ep-autosuggest");return e.forEach((e=>{for(;e.firstChild;)e.removeChild(e.firstChild)})),t.forEach((e=>{e.style="display: none;"})),!0}function u(e,t){const s=t.closest("form");e?s.classList.add("is-loading"):s.classList.remove("is-loading")}s.endpointUrl&&""!==s.endpointUrl&&(!function(){const o=[s.defaultSelectors,s.selector].filter(Boolean).join(",");if(!o)return;let d,g;const f=[38,40,13],p=e=>{if(!f.includes(e.keyCode))return;const s=e.target,o=t(s,"ep-autosuggest-container").querySelector(".autosuggest-list").children,a=()=>Array.from(o).findIndex((e=>e.classList.contains("selected"))),i=()=>{Array.from(o).forEach((e=>{e.classList.remove("selected"),e.setAttribute("aria-selected","false")}))},c=()=>{if(g>=0){const e=o[g];e.classList.add("selected"),e.setAttribute("aria-selected","true"),n(e.id,s)}};switch(e.keyCode){case 38:g=g-1>=0?g-1:0,i();break;case 40:if("undefined"===typeof g)g=0;else{const e=a();o[e+1]&&(g=e+1,i())}break;case 13:o[g]?.classList.contains("selected")&&r(s,o[g].querySelector(".autosuggest-link"))}o[g]&&o[g].classList.contains("autosuggest-item")?c():i(),38===e.keyCode&&e.preventDefault()};function h(e){const t=new FormData(e);return t.has("post_type")?t.getAll("post_type").slice(-1):t.has("post_type[]")?t.getAll("post_type[]"):[]}const m=e((async e=>{const t=function(){if("undefined"===typeof window.epas){const e="No epas object defined";return console.warn(e),{error:e}}return window.epas}();if(t.error)return;const s=e.value,n="ep_autosuggest_placeholder",o=h(e.form);if(s.length>=2){u(!0,e);let r=a(s,n,t);o.length>0&&(r=JSON.parse(r),"undefined"!==typeof r.post_filter.bool.must&&r.post_filter.bool.must.push({terms:{"post_type.raw":o}}),r=JSON.stringify(r));const d=await i(r,s);if(d&&d._shards&&d._shards.successful>0){const t=function(e,t){const s={},n="ep_custom_result",o=t.toLowerCase(),r=e.filter((e=>{let t=!0;return void 0!==e._source.terms&&void 0!==e._source.terms[n]&&e._source.terms[n].forEach((n=>{n.name.toLowerCase()===o&&(s[n.term_order]=e,t=!1)})),t})),a={};Object.keys(s).sort().forEach((e=>{a[e]=s[e]})),Object.keys(a).length>0&&Object.keys(a).forEach((e=>{const t=a[e];r.splice(e-1,0,t)}));return r}(d.hits.hits,s);0===t.length?l():c(t,e)}else l();u(!1,e)}else 0===s.length&&l()}),200),y=e=>{e.preventDefault();const{target:t,key:s,keyCode:o}=e;if("Escape"===s||"Esc"===s||27===o)return l(),function(e,t){t.setAttribute("aria-expanded",e)}(!1,t),void n("",t);if(f.includes(o)&&""!==t.value)return void p(e);const r=e.target;m(r)},w=e=>{const t=document.createElement("div");t.classList.add("ep-autosuggest-container"),e.insertAdjacentElement("afterend",t),t.appendChild(e)},v=e=>{if(!d){d=document.createElement("div"),d.classList.add("ep-autosuggest");const e=document.createElement("ul");e.classList.add("autosuggest-list"),e.setAttribute("role","listbox"),d.appendChild(e)}const t=d.cloneNode(!0);e.insertAdjacentElement("afterend",t)},E=e=>{if(e.classList.contains("facet-search"))return;e.setAttribute("autocomplete","off"),e.classList.contains("wp-block-search__input")?(e.form.classList.add("ep-autosuggest-container"),v(e.parentElement)):(w(e),v(e));const t=new CustomEvent("elasticpress.input.moved");e.dispatchEvent(t),e.addEventListener("keyup",y),e.addEventListener("blur",(function(){window.setTimeout(l,200)}))},b=e=>{const t=e.querySelectorAll(o);t&&Array.from(t).forEach(E)},_=()=>{const e=document.body,t={subtree:!0,childList:!0};new MutationObserver(((s,n)=>{s.forEach((s=>{Array.from(s.addedNodes).forEach((s=>{s.nodeType===Node.ELEMENT_NODE&&(n.disconnect(),"INPUT"===s.tagName?s.matches(o)&&E(s):b(s),n.observe(e,t))}))}))})).observe(e,t)};b(document.body),A=_,"undefined"!==typeof document&&("complete"!==document.readyState&&"interactive"!==document.readyState?document.addEventListener("DOMContentLoaded",A):A());var A}(),window.epasAPI={hideAutosuggestBox:l,updateAutosuggestBox:c,esSearch:i,buildSearchQuery:a})}(); |