PageSourceSearch

https://avpayurveda.com/cdn/shop/t/39/assets/predictive-search.js?v=51607395374279446731789559538

js avpayurveda.com collected 2026-10-02 11:01:59 UTC 10,800 bytes, 12 lines download raw bytes

1class PredictiveSearch extends SearchForm{constructor(){super(),this.cachedResults={},this.predictiveSearchResults=this.querySelector("[data-predictive-search]"),this.allPredictiveSearchInstances=document.querySelectorAll("predictive-search"),this.isOpen=!1,this.abortController=new AbortController,this.searchTerm="",this.setupEventListeners()}setupEventListeners(){this.input.form.addEventListener("submit",this.onFormSubmit.bind(this)),this.input.addEventListener("focus",this.onFocus.bind(this)),this.addEventListener("focusout",this.onFocusOut.bind(this)),this.addEventListener("keyup",this.onKeyup.bind(this)),this.addEventListener("keydown",this.onKeydown.bind(this))}getQuery(){return this.input.value.trim()}getRecentSearches(){try{return JSON.parse(localStorage.getItem("avp_recent_searches"))||[]}catch{return[]}}saveRecentSearch(query){if(!query||query.trim()==="")return;query=query.trim();let searches=this.getRecentSearches();searches=searches.filter(s=>s.toLowerCase()!==query.toLowerCase()),searches.unshift(query),searches.length>5&&(searches=searches.slice(0,5)),localStorage.setItem("avp_recent_searches",JSON.stringify(searches))}clearRecentSearches(){localStorage.removeItem("avp_recent_searches"),this.showRecentSearches()}removeRecentSearch(query,event){event&&(event.preventDefault(),event.stopPropagation());let searches=this.getRecentSearches();searches=searches.filter(s=>s!==query),localStorage.setItem("avp_recent_searches",JSON.stringify(searches)),this.showRecentSearches()}showRecentSearches(){const searches=this.getRecentSearches();if(searches.length===0){this.close(!1);return}let html='<div class="predictive-search__results-groups-wrapper" style="padding: 16px;">';html+='<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px;">',html+=`<h3 style="margin: 0; font-size: 15px; font-weight: 600; color: #1a1a1a; font-family: 'Montserrat', sans-serif;">Recent Searches</h3>`,html+='<button type="button" class="clear-recent-searches" style="background: none; border: none; color: #d32f2f; font-size: 13px; cursor: pointer; text-decoration: underline; padding: 0;">Clear all</button>',html+="</div>",html+='<ul style="list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px;">',searches.forEach(search=>{html+=`
2        <li style="display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; background: #f8f8f8; border-radius: 8px; transition: background 0.2s;">
3          <a href="/search?q=${encodeURIComponent(search)}" class="recent-search-link" style="display: flex; align-items: center; gap: 12px; text-decoration: none; color: #333; flex: 1;" onclick="window.saveRecentSearchGlobal && window.saveRecentSearchGlobal('${search.replace(/'/g,"\\'")}')">
4            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="color: #999;"><circle cx="12" cy="12" r="10"></circle><polyline points="12 6 12 12 16 14"></polyline></svg>
5            <span style="font-size: 15px; font-weight: 500;">${search}</span>
6          </a>
7          <button type="button" class="remove-recent-search" data-query="${search}" style="background: none; border: none; color: #999; cursor: pointer; padding: 4px; display: flex; align-items: center; justify-content: center; border-radius: 50%;" aria-label="Remove recent search">
8            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
9          </button>
10        </li>
11      `}),html+="</ul></div>",this.predictiveSearchResults.innerHTML=html,this.setAttribute("results",!0);const clearAllBtn=this.predictiveSearchResults.querySelector(".clear-recent-searches");clearAllBtn&&clearAllBtn.addEventListener("click",this.clearRecentSearches.bind(this)),this.predictiveSearchResults.querySelectorAll(".remove-recent-search").forEach(btn=>{btn.addEventListener("click",e=>this.removeRecentSearch(btn.dataset.query,e)),btn.addEventListener("mouseenter",()=>btn.style.color="#333"),btn.addEventListener("mouseleave",()=>btn.style.color="#999")}),this.predictiveSearchResults.querySelectorAll(".recent-search-link").forEach(link=>{link.addEventListener("click",()=>{const q=new URL(link.href,window.location.origin).searchParams.get("q");q&&this.saveRecentSearch(q)});const parentLi=link.closest("li");link.addEventListener("mouseenter",()=>parentLi.style.background="#f0f0f0"),link.addEventListener("mouseleave",()=>parentLi.style.background="#f8f8f8")}),this.open()}onChange(){super.onChange();const newSearchTerm=this.getQuery();if((!this.searchTerm||!newSearchTerm.startsWith(this.searchTerm))&&this.querySelector("#predictive-search-results-groups-wrapper")?.remove(),this.updateSearchForTerm(this.searchTerm,newSearchTerm),this.searchTerm=newSearchTerm,!this.searchTerm.length){this.showRecentSearches();return}this.getSearchResults(this.searchTerm)}onFormSubmit(event){!this.getQuery().length||this.querySelector('[aria-selected="true"] a')?event.preventDefault():this.saveRecentSearch(this.getQuery())}onFormReset(event){super.onFormReset(event),super.shouldResetForm()&&(this.searchTerm="",this.abortController.abort(),this.abortController=new AbortController,this.closeResults(!0))}onFocus(){const currentSearchTerm=this.getQuery();if(!currentSearchTerm.length){this.showRecentSearches();return}this.searchTerm!==currentSearchTerm?this.onChange():this.getAttribute("results")==="true"?this.open():this.getSearchResults(this.searchTerm)}onFocusOut(){setTimeout(()=>{this.contains(document.activeElement)||this.close()})}onKeyup(event){switch(this.getQuery().length||this.close(!0),event.preventDefault(),event.code){case"ArrowUp":this.switchOption("up");break;case"ArrowDown":this.switchOption("down");break;case"Enter":this.selectOption();break}}onKeydown(event){(event.code==="ArrowUp"||event.code==="ArrowDown")&&event.preventDefault()}updateSearchForTerm(previousTerm,newTerm){const searchForTextElement=this.querySelector("[data-predictive-search-search-for-text]"),currentButtonText=searchForTextElement?.innerText;if(currentButtonText){if(currentButtonText.match(new RegExp(previousTerm,"g")).length>1)return;const newButtonText=currentButtonText.replace(previousTerm,newTerm);searchForTextElement.innerText=newButtonText}}switchOption(direction){if(!this.getAttribute("open"))return;const moveUp=direction==="up",selectedElement=this.querySelector('[aria-selected="true"]'),allVisibleElements=Array.from(this.querySelectorAll("li, button.predictive-search__item")).filter(element=>element.offsetParent!==null);let activeElementIndex=0;if(moveUp&&!selectedElement)return;let selectedElementIndex=-1,i=0;for(;selectedElementIndex===-1&&i<=allVisibleElements.length;)allVisibleElements[i]===selectedElement&&(selectedElementIndex=i),i++;if(this.statusElement.textContent="",!moveUp&&selectedElement?activeElementIndex=selectedElementIndex===allVisibleElements.length-1?0:selectedElementIndex+1:moveUp&&(activeElementIndex=selectedElementIndex===0?allVisibleElements.length-1:selectedElementIndex-1),activeElementIndex===selectedElementIndex)return;const activeElement=allVisibleElements[activeElementIndex];activeElement.setAttribute("aria-selected",!0),selectedElement&&selectedElement.setAttribute("aria-selected",!1),this.input.setAttribute("aria-activedescendant",activeElement.id)}selectOption(){const selectedOption=this.querySelector('[aria-selected="true"] a, button[aria-selected="true"]');selectedOption&&selectedOption.click()}getSearchResults(searchTerm){const queryKey=searchTerm.replace(" ","-").toLowerCase();if(this.setLiveRegionLoadingState(),this.cachedResults[queryKey]){this.renderSearchResults(this.cachedResults[queryKey]);return}fetch(`${routes.predictive_search_url}?q=${encodeURIComponent(searchTerm)}&section_id=predictive-search`,{signal:this.abortController.signal}).then(response=>{if(!response.ok){var error=new Error(response.status);throw this.close(),error}return response.text()}).then(text=>{const resultsMarkup=new DOMParser().parseFromString(text,"text/html").querySelector("#shopify-section-predictive-search").innerHTML;
11this.allPredictiveSearchInstances.forEach(predictiveSearchInstance=>{predictiveSearchInstance.cachedResults[queryKey]=resultsMarkup}),this.renderSearchResults(resultsMarkup)}).catch(error=>{if(error?.code!==20)throw this.close(),error})}setLiveRegionLoadingState(){this.statusElement=this.statusElement||this.querySelector(".predictive-search-status"),this.loadingText=this.loadingText||this.getAttribute("data-loading-text"),this.setLiveRegionText(this.loadingText),this.setAttribute("loading",!0)}setLiveRegionText(statusText){this.statusElement.setAttribute("aria-hidden","false"),this.statusElement.textContent=statusText,setTimeout(()=>{this.statusElement.setAttribute("aria-hidden","true")},1e3)}renderSearchResults(resultsMarkup){this.predictiveSearchResults.innerHTML=resultsMarkup,this.setAttribute("results",!0),this.setLiveRegionResults(),this.open()}setLiveRegionResults(){this.removeAttribute("loading"),this.setLiveRegionText(this.querySelector("[data-predictive-search-live-region-count-value]").textContent)}getResultsMaxHeight(){return this.resultsMaxHeight=window.innerHeight-document.querySelector(".section-header")?.getBoundingClientRect().bottom,this.resultsMaxHeight}open(){this.predictiveSearchResults.style.maxHeight=this.resultsMaxHeight||`${this.getResultsMaxHeight()}px`,this.setAttribute("open",!0),this.input.setAttribute("aria-expanded",!0),this.isOpen=!0}close(clearSearchTerm=!1){this.closeResults(clearSearchTerm),this.isOpen=!1}closeResults(clearSearchTerm=!1){clearSearchTerm&&(this.input.value="",this.removeAttribute("results"));const selected=this.querySelector('[aria-selected="true"]');selected&&selected.setAttribute("aria-selected",!1),this.input.setAttribute("aria-activedescendant",""),this.removeAttribute("loading"),this.removeAttribute("open"),this.input.setAttribute("aria-expanded",!1),this.resultsMaxHeight=!1,this.predictiveSearchResults.removeAttribute("style")}}customElements.define("predictive-search",PredictiveSearch),window.saveRecentSearchGlobal=function(query){if(!(!query||query.trim()==="")){query=query.trim();try{let searches=JSON.parse(localStorage.getItem("avp_recent_searches"))||[];searches=searches.filter(s=>s.toLowerCase()!==query.toLowerCase()),searches.unshift(query),searches.length>5&&(searches=searches.slice(0,5)),localStorage.setItem("avp_recent_searches",JSON.stringify(searches))}catch{}}};
12//# sourceMappingURL=/cdn/shop/t/39/assets/predictive-search.js.map?v=51607395374279446731789559538

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.