esphome-docs/_templates/searchbox.html
Clyde Stubbs 542a91a42d
Be less aggressive in maintaining focus on search box (#4395)
* Be less aggressive in maintaining focus on search box

* Also focus when clear clicked
2024-10-28 12:32:09 +01:00

222 lines
7.3 KiB
HTML

<script src="/pagefind/pagefind-modular-ui.js"></script>
<div class="pagefind-ui__form" id="search"></div>
<script>
class El {
constructor(tagname) {
this.element = document.createElement(tagname);
}
id(s) {
this.element.id = s;
return this;
}
class(s) {
this.element.classList.add(s);
return this;
}
attrs(obj) {
for (const [k,v] of Object.entries(obj)) {
this.element.setAttribute(k, v);
}
return this;
}
text(t) {
this.element.innerText = t;
return this;
}
html(t) {
this.element.innerHTML = t;
return this;
}
handle(e, f) {
this.element.addEventListener(e, f);
return this;
}
addTo(el) {
if (el instanceof El) {
el.element.appendChild(this.element);
} else {
el.appendChild(this.element);
}
return this.element;
}
}
window.addEventListener('DOMContentLoaded', (event) => {
const targetClass = "search-results";
const docel = document.getElementsByClassName("document");
if (docel.length === 0)
return;
const inpel = document.getElementById("search");
if (!inpel)
return;
const mobileWidth = getComputedStyle(document.body).getPropertyValue("--mobile-width-stop");
function isMobile() {
return window.innerWidth <= mobileWidth;
}
const target = document.createElement("div");
target.classList.add(targetClass);
target.id = targetClass;
target.style.display = "none";
docel.item(0).appendChild(target);
const margin = 20;
function getLink(location, anchors, url) {
if (!anchors || !anchors.length)
return null;
// find the closest anchor at or before the current location
const anchor = anchors.sort((a, b) => b.location - a.location).find(a => a.location <= location);
if (anchor) {
return url + "#" + anchor.id;
}
return null;
}
const resultTemplate = (result) => {
const wrapper = new El("li").class("pagefind-modular-list-result");
const thumb = new El("div").class("pagefind-modular-list-thumb").addTo(wrapper);
let image = result?.meta?.image;
if (image) {
if (image.includes("/_images/"))
image = image.substring(image.indexOf("/_images/"));
new El("img").class("pagefind-modular-list-image").attrs({
src: image,
alt: result.meta.image_alt || result.meta.title
}).addTo(thumb);
}
const inner = new El("div").class("pagefind-modular-list-inner").addTo(wrapper);
const title = new El("p").class("pagefind-modular-list-title").addTo(inner);
new El("a").class("pagefind-modular-list-link").text(result.meta?.title).attrs({
href: result.meta?.url || result.url
}).addTo(title);
const excerpt = new El("p").class("pagefind-modular-list-excerpt").addTo(inner);
const locations = result.weighted_locations.sort((a, b) => b.weight - a.weight);
const url = getLink(locations[0]?.location, result.anchors, result.url) || result.meta?.url || result.url;
new El("a").class("pagefind-modular-list-link").html(result.excerpt).attrs({
href: url
}).addTo(excerpt);
return wrapper.element;
}
function resizeTarget() {
const searchPos = inpel.getBoundingClientRect();
let leftPos;
let topPos;
const maxWidth = 650;
target.style.width = "auto;"
target.style.height = "fit-content";
target.style.maxWidth = maxWidth + "px";
let rightPos = margin;
if (isMobile()) {
// position search results left aligned with the search box, and below.
leftPos = margin / 2;
topPos = searchPos.bottom + margin / 2;
rightPos = margin / 2;
} else {
// position search results top aligned with the search box, and to its right.
leftPos = searchPos.right + margin * 2;
topPos = searchPos.top;
if (rightPos - leftPos > maxWidth)
rightPos = leftPos + maxWidth;
}
target.style.right = rightPos + "px";
target.style.top = topPos + "px";
target.style.left = leftPos + "px";
let twidth = window.innerWidth - margin - leftPos;
if (twidth > maxWidth)
twidth = maxWidth;
target.style.width = twidth + "px";
target.style.maxHeight = (window.innerHeight - margin - topPos) + "px";
}
window.addEventListener("resize", (event) => { resizeTarget(); });
function showTarget() {
if (target.style.display !== "block") {
target.style.display = "block";
document.addEventListener('click', clickCallback);
}
resizeTarget();
}
function search_focus() {
const input_field = document.getElementById("pfmod-input-0");
if (input_field) {
requestAnimationFrame(() => input_field.focus());
}
}
function hideTargets() {
if (target.style.display !== "none") {
target.style.display = "none";
document.removeEventListener('click', clickCallback);
// keep focus on search box after clicking on search result
search_focus();
}
}
const instance = new PagefindModularUI.Instance({
showSubResults: true,
showImages: false,
ranking: {
pageLength: 0.0,
termSaturation: 1.6,
termFrequency: 0.4,
termSimilarity: 6.0
}
});
instance.add(new PagefindModularUI.Input({
containerElement: "#search"
}));
instance.add(new PagefindModularUI.ResultList({
containerElement: "#search-results",
resultTemplate: resultTemplate
}));
const clickCallback = (event) => {
const path = event.composedPath();
if (path.includes(inpel))
return;
hideTargets();
};
if (target) {
instance.on("results", (results) => {
if (results.results.length) {
showTarget();
} else {
hideTargets();
}
});
}
// focus the search field on page load.
search_focus();
// focus when clicking on clear button
const clears = document.getElementsByClassName("pagefind-modular-list-clear");
for (let i = 0; i !== clears.length; i++) {
clears[i].addEventListener("click", search_focus);
}
});
</script>