|
| 1 | +const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent); |
| 2 | +const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0; |
| 3 | + |
| 4 | +const initSearch = () => { |
| 5 | + const search = document.getElementById("search"); |
| 6 | + const searchButtons = document.querySelectorAll(".o-header__search"); |
| 7 | + const isHome = document.querySelector(".o-startpage"); |
| 8 | + const overlay = document.getElementById("overlay"); |
| 9 | + let placeholderText = search.dataset.placeholder; |
| 10 | + let searchLabelText = search.dataset.label; |
| 11 | + |
| 12 | + if (!isMobile) { |
| 13 | + if (isMac) { |
| 14 | + placeholderText += " (⌘ + K)"; |
| 15 | + } else { |
| 16 | + placeholderText += " (CTRL + K)"; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + new PagefindUI({ |
| 21 | + element: "#search", |
| 22 | + highlightParam: "highlight", |
| 23 | + showSubResults: true, |
| 24 | + translations: { |
| 25 | + placeholder: placeholderText, |
| 26 | + search_label: searchLabelText, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + const searchElement = search.querySelector("input"); |
| 31 | + |
| 32 | + const closeSearch = () => { |
| 33 | + search.querySelector(".pagefind-ui__search-clear").click(); |
| 34 | + overlay.classList.remove("overlay--show", "overlay--show-lv5"); |
| 35 | + search.classList.remove("o-search--show"); |
| 36 | + }; |
| 37 | + |
| 38 | + // Scroll to search on click |
| 39 | + if (search && isHome) { |
| 40 | + search.addEventListener("click", function () { |
| 41 | + overlay.classList.add("overlay--show", "overlay--show-lv5"); |
| 42 | + search.scrollIntoView({ behavior: "smooth", block: "start" }); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + function showSearchOnContentPage() { |
| 47 | + search.classList.add("o-search--show"); |
| 48 | + overlay.classList.add("overlay--show", "overlay--show-lv5"); |
| 49 | + searchElement.focus(); |
| 50 | + } |
| 51 | + |
| 52 | + function showSearchOnStartPage() { |
| 53 | + overlay.classList.add("overlay--show", "overlay--show-lv5"); |
| 54 | + searchElement.focus(); |
| 55 | + search.scrollIntoView({ behavior: "smooth", block: "start" }); |
| 56 | + } |
| 57 | + |
| 58 | + searchButtons.forEach((button) => { |
| 59 | + if (isHome) { |
| 60 | + button.addEventListener("click", showSearchOnStartPage); |
| 61 | + } else { |
| 62 | + button.addEventListener("click", showSearchOnContentPage); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + // Open search on Ctrl + K or Cmd + K |
| 67 | + document.addEventListener("keydown", (e) => { |
| 68 | + if ((e.ctrlKey || e.metaKey) && e.key === "k") { |
| 69 | + e.preventDefault(); |
| 70 | + if (isHome) { |
| 71 | + showSearchOnStartPage(); |
| 72 | + } else { |
| 73 | + showSearchOnContentPage(); |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + // Close search on ESC |
| 79 | + document.addEventListener("keydown", (e) => { |
| 80 | + if (e.key === "Escape") { |
| 81 | + closeSearch(); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + overlay.addEventListener("click", closeSearch); |
| 86 | +}; |
| 87 | + |
| 88 | +if (document.readyState === "interactive") { |
| 89 | + initSearch(); |
| 90 | +} else { |
| 91 | + window.addEventListener("DOMContentLoaded", () => { |
| 92 | + initSearch(); |
| 93 | + }); |
| 94 | +} |
0 commit comments