| 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 i(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 a(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,i="";const{value:a}=n,c=t(n,"ep-autosuggest-container").querySelector(".ep-autosuggest"),u=c.querySelector(".autosuggest-list");for(;u.firstChild;)u.removeChild(u.firstChild);e.length>0?c.style="display: block;":c.style="display: none;";const l=e.length;for(o=0;l>o;++o){const t=e[o]._source.post_title,n=e[o]._source.permalink,r=t.replace(/\\([\s\S])|(")/g,"""),c=a.trim().split(" ");let u=r;if(s.highlightingEnabled){const e=new RegExp(`\\b(${c.join("|")})`,"gi");u=u.replace(e,(e=>`<${s.highlightingTag} class="${s.highlightingClass} ep-autosuggest-highlight">${e}</${s.highlightingTag}>`))}let l=`<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${u}\n\t\t\t\t</a>\n\t\t\t</li>`;"undefined"!==typeof window.epAutosuggestItemHTMLFilter&&(l=window.epAutosuggestItemHTMLFilter(l,e[o],o,a)),i+=l}"undefined"!==typeof window.epAutosuggestListItemsHTMLFilter&&(i=window.epAutosuggestListItemsHTMLFilter(i,e,n)),u.innerHTML=i;const d=Array.from(document.querySelectorAll(".autosuggest-link"));return u.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 u(){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 l(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 p=[38,40,13],f=e=>{if(!p.includes(e.keyCode))return;const s=e.target,o=t(s,"ep-autosuggest-container").querySelector(".autosuggest-list").children,i=()=>Array.from(o).findIndex((e=>e.classList.contains("selected"))),a=()=>{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,a();break;case 40:if("undefined"===typeof g)g=0;else{const e=i();o[e+1]&&(g=e+1,a())}break;case 13:o[g]?.classList.contains("selected")&&r(s,o[g].querySelector(".autosuggest-link"))}o[g]&&o[g].classList.contains("autosuggest-item")?c():a(),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){l(!0,e);let r=i(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)),"undefined"!==typeof window.epAutosuggestQueryFilter&&(r=JSON.stringify(window.epAutosuggestQueryFilter(JSON.parse(r),s,e)));const d=await a(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})),i={};Object.keys(s).sort().forEach((e=>{i[e]=s[e]})),Object.keys(i).length>0&&Object.keys(i).forEach((e=>{const t=i[e];r.splice(e-1,0,t)}));return r}(d.hits.hits,s);0===t.length?u():c(t,e)}else u();l(!1,e)}else 0===s.length&&u()}),200),y=e=>{e.preventDefault();const{target:t,key:s,keyCode:o}=e;if("Escape"===s||"Esc"===s||27===o)return u(),function(e,t){t.setAttribute("aria-expanded",e)}(!1,t),void n("",t);if(p.includes(o)&&""!==t.value)return void f(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)},E=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)}let t=d.cloneNode(!0);"undefined"!==typeof window.epAutosuggestElementFilter&&(t=window.epAutosuggestElementFilter(t,e)),e.insertAdjacentElement("afterend",t)},v=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"),E(e.parentElement)):(w(e),E(e));const t=new CustomEvent("elasticpress.input.moved");e.dispatchEvent(t),e.addEventListener("keyup",y),e.addEventListener("blur",(function(){window.setTimeout(u,200)}))},b=e=>{const t=e.querySelectorAll(o);t&&Array.from(t).forEach(v)},A=()=>{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)&&v(s):b(s),n.observe(e,t))}))}))})).observe(e,t)};b(document.body),L=A,"undefined"!==typeof document&&("complete"!==document.readyState&&"interactive"!==document.readyState?document.addEventListener("DOMContentLoaded",L):L());var L}(),window.epasAPI={hideAutosuggestBox:u,updateAutosuggestBox:c,esSearch:a,buildSearchQuery:i})}(); |