pages/media/plugins/staticSearch/static.search.min.js
2024-01-04 22:34:43 +01:00

63 lines
No EOL
11 KiB
JavaScript

window.publiiStaticSearchInitCallback=function(a){window.publiiStaticSearch.indexLoaded||(window.publiiStaticSearch.cachedItems=a,window.publiiStaticSearch.initIndex())},window.publiiStaticSearch={cachedItems:null,searchIndex:null,indexLoaded:!1,htmlLoaded:!1,UI:{customTrigger:null,smallSearchInput:null,smallSearchForm:null,input:null,overlay:null,popupContent:null,emptyState:null,tooShortPhraseState:null,noResultsState:null,listWithResults:null,listWithResultsContent:null},config:{minCharCount:window.publiiStaticSearchConfig.minCharCount,maxResultsCount:window.publiiStaticSearchConfig.maxResultsCount,engineSettings:{tokenize:window.publiiStaticSearchConfig.engineSettings.tokenize,charset:window.publiiStaticSearchConfig.engineSettings.charset,language:window.publiiStaticSearchConfig.engineSettings.language,cache:window.publiiStaticSearchConfig.engineSettings.cache,suggest:window.publiiStaticSearchConfig.engineSettings.suggest},indexSettings:{resultsDescription:window.publiiStaticSearchConfig.indexSettings.resultsDescription,indexPostTitles:window.publiiStaticSearchConfig.indexSettings.indexPostTitles,indexPostExcerpts:window.publiiStaticSearchConfig.indexSettings.indexPostExcerpts,indexPostHeadings:window.publiiStaticSearchConfig.indexSettings.indexPostHeadings,indexPostAuthors:window.publiiStaticSearchConfig.indexSettings.indexPostAuthors,indexPostMetaDescription:window.publiiStaticSearchConfig.indexSettings.indexPostMetaDescription,indexTagNames:window.publiiStaticSearchConfig.indexSettings.indexTagNames,indexTagDescription:window.publiiStaticSearchConfig.indexSettings.indexTagDescription,indexTagMetaDescription:window.publiiStaticSearchConfig.indexSettings.indexTagMetaDescription}},templates:{searchPopup:`
<div class="pss-results__wrapper">
<div class="pss-results__searchbar">
<input
class="pss-results__searchbar__input"
type="search"
placeholder="${window.publiiStaticSearchConfig.translations.inputPlaceholder}" />
<svg
class="pss-results__searchbar__icon"
width="24"
height="24"
viewBox="0 0 24 24">
<path d="M21.71,20.29,18,16.61A9,9,0,1,0,16.61,18l3.68,3.68a1,1,0,0,0,1.42,0A1,1,0,0,0,21.71,20.29ZM4,11a7,7,0,1,1,12,4.93h0s0,0,0,0A7,7,0,0,1,4,11Z"/>
</svg>
<button
class="pss-results__searchbar__btn"
type="button">
${window.publiiStaticSearchConfig.translations.buttonClose}
</button>
</div>
<div class="pss-results__msg pss-results__msg--empty">
${window.publiiStaticSearchConfig.translations.searchEmptyState}
</div>
<div
class="pss-results__msg pss-results__msg--too-short"
hidden>
${window.publiiStaticSearchConfig.translations.tooShortPhraseState}
</div>
<div
class="pss-results__msg pss-results__msg--no-results"
hidden>
${window.publiiStaticSearchConfig.translations.noResults}
</div>
<ol class="pss-results__list" hidden></ol>
</div>
`,searchItem:`
<li class="pss-results__list__item">
<a href="{PSS_URL}" class="pss-results__list__link">
{PSS_ICON}
<strong class="pss-results__list__title">
{PSS_TITLE}
</strong>
<small class="pss-results__list__text">
{PSS_TEXT}
</small>
</a>
</li>
`,searchItemWithoutText:`
<li class="pss-results__list__item">
<a href="{PSS_URL}" class="pss-results__list__link">
{PSS_ICON}
<strong class="pss-results__list__title">
{PSS_TITLE}
</strong>
</a>
</li>
`,postIcon:'<svg width="20" height="20" viewBox="0 0 24 24"><path d="M20.92,7.62a.93.93,0,0,0-.21-.33l-6-6a1,1,0,0,0-.32-.21A1.09,1.09,0,0,0,14,1H6A3,3,0,0,0,3,4V20a3,3,0,0,0,3,3H18a3,3,0,0,0,3-3V8A1,1,0,0,0,20.92,7.62ZM15,4.41,17.59,7H15ZM18,21H6a1,1,0,0,1-1-1V4A1,1,0,0,1,6,3h7V8a1,1,0,0,0,1,1h5V20A1,1,0,0,1,18,21Zm-1-8a1,1,0,0,1-1,1H8a1,1,0,0,1,0-2h8A1,1,0,0,1,17,13Zm0,4a1,1,0,0,1-1,1H8a1,1,0,0,1,0-2h8A1,1,0,0,1,17,17ZM7,9A1,1,0,0,1,8,8h2a1,1,0,0,1,0,2H8A1,1,0,0,1,7,9Z"/></svg>',tagIcon:'<svg width="20" height="20" viewBox="0 0 24 24"> <path d="M20,14H15.78l.45-4H20a1,1,0,0,0,0-2H16.45L17,3.11A1,1,0,0,0,16.11,2a1,1,0,0,0-1.1.88L14.44,8h-4L11,3.11a1,1,0,0,0-2-.22L8.44,8H4a1,1,0,0,0,0,2H8.22l-.45,4H4a1,1,0,0,0,0,2H7.55L7,20.89A1,1,0,0,0,7.89,22H8a1,1,0,0,0,1-.89L9.56,16h4L13,20.89a1,1,0,0,0,.88,1.1H14a1,1,0,0,0,1-.89L15.56,16H20a1,1,0,0,0,0-2ZM9.78,14l.45-4h4l-.45,4Z"/></svg>'},init(){if(this.UI.smallSearchForm=document.querySelector("form.search__form"),window.publiiStaticSearchConfig.customTriggerSelector&&(this.UI.customTrigger=document.querySelectorAll(window.publiiStaticSearchConfig.customTriggerSelector)),this.UI.smallSearchForm&&(this.UI.smallSearchInput=this.UI.smallSearchForm.querySelector(".search__input"),window.publiiStaticSearchConfig.showPopupOnInputClick?(this.UI.smallSearchForm.addEventListener("submit",a=>{a.preventDefault()},!1),this.UI.smallSearchInput.addEventListener("click",a=>{this.showPopup("")},!1)):this.UI.smallSearchForm.addEventListener("submit",a=>{a.preventDefault(),this.showPopup(this.UI.smallSearchInput.value)},!1)),this.UI.customTrigger)for(let a=0;a<this.UI.customTrigger.length;a++)this.UI.customTrigger[a].addEventListener("click",a=>{a.preventDefault(),this.showPopup("")},!1)},showPopup(a){this.htmlLoaded?(this.UI.overlay.classList.remove("is-hidden"),this.UI.input.value=a,this.UI.input.focus(),this.triggerSearchEvent()):(this.htmlLoaded=!0,this.appendHtmlStructure(),setTimeout(()=>{this.initPopupEvents(a),this.indexLoaded||this.loadIndex()},0))},loadIndex(){let b=1e5*parseInt(Date.now()/1e5,10),c=window.publiiStaticSearchConfig.baseURL+"/media/plugins/staticSearch/index.js?v="+b,a=document.createElement("script");a.setAttribute("type","text/javascript"),a.setAttribute("src",c),document.body.appendChild(a)},initIndex(){let d={charset:this.config.engineSettings.charset,language:this.config.engineSettings.language,preset:"match",tokenize:this.config.engineSettings.tokenize,cache:this.config.engineSettings.cache,suggest:this.config.engineSettings.suggest};this.searchIndex=new FlexSearch.Index(d);for(let c=0;c<this.cachedItems.length;c++){let a=this.cachedItems[c],b=[];0===a.id.indexOf("post-")?(this.config.indexSettings.indexPostTitles&&b.push(a.title),this.config.indexSettings.indexPostExcerpts&&b.push(a.excerpt),this.config.indexSettings.indexPostHeadings&&b.push(a.headings),this.config.indexSettings.indexPostAuthors&&b.push(a.author),this.config.indexSettings.indexPostMetaDescription&&b.push(a.metaDescription)):0===a.id.indexOf("tag-")&&(this.config.indexSettings.indexTagNames&&b.push(a.title),this.config.indexSettings.indexTagDescription&&b.push(a.excerpt),this.config.indexSettings.indexTagMetaDescription&&b.push(a.metaDescription)),this.searchIndex.add(a.id,b.join(" "))}this.indexLoaded=!0,this.UI.overlay.classList.remove("is-loading"),setTimeout(()=>{this.triggerSearchEvent()},0)},appendHtmlStructure(){let a=document.createElement("div");a.setAttribute("class","pss-results is-hidden is-loading"),a.innerHTML=this.templates.searchPopup,document.body.appendChild(a)},initPopupEvents(a){this.UI.input=document.querySelector(".pss-results__searchbar__input"),this.UI.overlay=document.querySelector(".pss-results"),this.UI.popupContent=document.querySelector(".pss-results__wrapper"),this.UI.emptyState=document.querySelector(".pss-results__msg--empty"),this.UI.noResultsState=document.querySelector(".pss-results__msg--no-results"),this.UI.tooShortPhraseState=document.querySelector(".pss-results__msg--too-short"),this.UI.listWithResultsContent=document.querySelector(".pss-results__list"),this.UI.popupCloseBtn=document.querySelector(".pss-results__searchbar__btn"),this.UI.input.addEventListener("input",()=>{this.triggerSearchEvent()},!1),this.UI.overlay.addEventListener("click",()=>{this.UI.input.value="",this.showEmptyState(),this.UI.overlay.classList.add("is-hidden")},!1),this.UI.popupCloseBtn.addEventListener("click",()=>{this.UI.input.value="",this.showEmptyState(),this.UI.overlay.classList.add("is-hidden")},!1),this.UI.popupContent.addEventListener("click",a=>{a.stopPropagation()},!1);let b=null;this.UI.listWithResultsContent.addEventListener("scroll",()=>{clearTimeout(b),this.UI.listWithResultsContent.classList.contains("is-scrolled")||this.UI.listWithResultsContent.classList.add("is-scrolled"),b=setTimeout(()=>{this.UI.listWithResultsContent.classList.remove("is-scrolled")},350)},!1),setTimeout(()=>{this.UI.overlay.classList.remove("is-hidden"),this.UI.input.value=a,this.UI.input.focus(),this.triggerSearchEvent()},0),document.addEventListener("keydown",a=>{"Escape"!==a.key&&"Esc"!==a.key||this.UI.overlay.classList.contains("is-hidden")||(this.UI.input.value="",this.showEmptyState(),this.UI.overlay.classList.add("is-hidden"))},!1)},search(a){if(null===this.searchIndex)return!1;let b=this.searchIndex.search(a,this.config.maxResultsCount);return this.cachedItems.filter(a=>b.includes(a.id))},showEmptyState(){this.UI.emptyState.removeAttribute("hidden"),this.UI.tooShortPhraseState.setAttribute("hidden",!0),this.UI.noResultsState.setAttribute("hidden",!0),this.UI.listWithResultsContent.setAttribute("hidden",!0)},showTooShortState(){this.UI.emptyState.setAttribute("hidden",!0),this.UI.tooShortPhraseState.removeAttribute("hidden"),this.UI.noResultsState.setAttribute("hidden",!0),this.UI.listWithResultsContent.setAttribute("hidden",!0)},showNoResults(){this.UI.emptyState.setAttribute("hidden",!0),this.UI.tooShortPhraseState.setAttribute("hidden",!0),this.UI.noResultsState.removeAttribute("hidden"),this.UI.listWithResultsContent.setAttribute("hidden",!0)},showResults(){this.UI.emptyState.setAttribute("hidden",!0),this.UI.tooShortPhraseState.setAttribute("hidden",!0),this.UI.noResultsState.setAttribute("hidden",!0),this.UI.listWithResultsContent.removeAttribute("hidden")},renderResults(b){let e="";for(let a=0;a<b.length;a++){let c="",f=window.publiiStaticSearch.templates.postIcon,g="searchItem",d=b[a].url,h=b[a].title;0===b[a].id.indexOf("tag-")&&(f=window.publiiStaticSearch.templates.tagIcon),"excerpt"===this.config.indexSettings.resultsDescription?c=b[a].excerpt:"metadescription"===this.config.indexSettings.resultsDescription&&(c=b[a].metaDescription),""===c&&(g="searchItemWithoutText"),d.indexOf("#PUBLII_RELATIVE_URL_BASE#")> -1&&(d=d.replace("#PUBLII_RELATIVE_URL_BASE#",""));let i=this.templates[g].replace(/\{PSS_ICON\}/gmi,f).replace(/\{PSS_URL\}/gmi,d).replace(/\{PSS_TITLE\}/gmi,h).replace(/\{PSS_TEXT\}/gmi,c);e+=i}this.UI.listWithResultsContent.innerHTML=e},triggerSearchEvent(){if(this.indexLoaded){if(this.UI.input.value.trim().length>=this.config.minCharCount){let a=this.search(this.UI.input.value.trim());!1!==a&&a.length?(this.showResults(),this.renderResults(a)):this.showNoResults()}else this.UI.input.value.trim().length>0?this.showTooShortState():this.showEmptyState()}}},window.publiiStaticSearch.init()