Searching...
';showResults();const apiBase =
window.location.origin &&
window.location.origin !== "null"? window.location.origin: "";fetch(apiBase +
"/wp-json/wp/v2/search?search=" +
encodeURIComponent(query) +
"&per_page=8&_fields=title,url,type",{credentials: "same-origin",signal: currentController.signal}).then(function (response) {if (!response.ok) {throw new Error(
"Search request failed"
);}return response.json();}).then(function (results) {searchResults.innerHTML = "";if (
!Array.isArray(results) ||
results.length === 0
) {searchResults.innerHTML =
'No matching results found.
';return;}/* =====================================================
RESULTS
===================================================== */results.forEach(function (item) {const resultItem =
document.createElement("a");resultItem.className =
"phs-search-result-item";resultItem.href =
item.url || "#";resultItem.textContent =
item.title || "View Result";resultItem.setAttribute(
"role",
"option"
);searchResults.appendChild(
resultItem
);});/* =====================================================
VIEW ALL
===================================================== */const viewAll =
document.createElement("a");viewAll.className =
"phs-search-view-all";viewAll.href =
"/?s=" +
encodeURIComponent(query);viewAll.textContent =
'View all results for "' +
query +
'"';searchResults.appendChild(
viewAll
);}).catch(function (error) {if (
error.name === "AbortError"
) {return;}searchResults.innerHTML =
'Unable to search. Please try again.
';});};/* =========================================================
LIVE SEARCH
========================================================= */searchInput.addEventListener(
"input",
function () {const query =
this.value.trim();clearTimeout(searchTimer);if (query.length = 2 &&
searchResults.innerHTML.trim() !== ""
) {showResults();}});/* =========================================================
CLOSE WHEN CLICKING OUTSIDE
========================================================= */document.addEventListener(
"click",
function (event) {if (
!event.target.closest(
".phs-search-wrapper"
)
) {hideResults();}});/* =========================================================
ESCAPE KEY
========================================================= */document.addEventListener(
"keydown",
function (event) {if (event.key === "Escape") {hideResults();searchInput.blur();}});/* =========================================================
FORM SUBMIT
========================================================= */const searchForm =
document.getElementById(
"phs-live-search-form"
);if (searchForm) {searchForm.addEventListener(
"submit",
function (event) {const query =
searchInput.value.trim();if (!query) {event.preventDefault();searchInput.focus();return;}/*
* For a valid query we allow the normal
* WordPress search request to continue.
*/});}});




