PageSourceSearch

https://us.dollarshaveclub.com/cdn/shop/t/387/assets/predictive-search.js?v=156345040199937375791790201869

js dollarshaveclub.com collected 2026-09-24 15:15:48 UTC 9,992 bytes, 1 lines download raw bytes

1class PredictiveSearch extends SearchForm{constructor(){super(),this.cachedResults={},this.predictiveSearchResults=this.querySelector("[data-predictive-search]"),this.predictiveSearchPlaceholder=this.querySelector("[data-predictive-search-placeholder]"),this.statusElement=this.querySelector(".predictive-search-status"),this.allPredictiveSearchInstances=document.querySelectorAll("predictive-search"),this.sectionId=this.querySelector("[data-predictive-search-section-id]")?.dataset?.predictiveSearchSectionId||"predictive-search",this.isOpen=!1,this.abortController=new AbortController,this.searchTerm="",this.searchInput=this.querySelector("#Search-In-Modal"),this.tabsetSelector=".predictive-search-modal__tabset",this.lastCheckedId=null,this.defaultResultsId=this.input.getAttribute("aria-owns")||"predictive-search-results",this.setupEventListeners(),this.initPlaceholderAndTabs()}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)),this.searchInput&&this.predictiveSearchPlaceholder&&this.searchInput.addEventListener("input",this.togglePlaceholder.bind(this)),this.searchInput&&this.searchInput.addEventListener("blur",(()=>{setTimeout((()=>{""!==this.searchInput.value.trim()&&(this.predictiveSearchResults.style.display="block"),this.restoreTab()}),10)})),document.addEventListener("change",(e=>{if(e.target.matches(`${this.tabsetSelector} input[type="radio"]`)){this.lastCheckedId=e.target.id;const t=e.target.getAttribute("aria-controls"),s=document.getElementById(t),i=e.target.closest(".tabset");s&&i&&(i.querySelectorAll(".tab-panel").forEach((e=>{if(e!==s){const t=e.querySelector('[aria-selected="true"]');t&&t.setAttribute("aria-selected","false")}e.style.display="none"})),s.style.display="block",this.input.removeAttribute("aria-activedescendant"),this.syncComboboxOwnership())}}))}togglePlaceholder(){this.predictiveSearchPlaceholder&&(this.predictiveSearchPlaceholder.style.display=""===this.searchInput.value.trim()?"block":"none",this.syncComboboxOwnership())}restorePlaceholder(){if(!this.predictiveSearchPlaceholder)return;this.predictiveSearchPlaceholder.style.display="block",this.predictiveSearchPlaceholder.classList.remove("visually-hidden");const e=this.predictiveSearchPlaceholder.querySelector('input[type="radio"]');e&&(e.checked=!0,e.dispatchEvent(new Event("change",{bubbles:!0})));this.predictiveSearchPlaceholder.querySelectorAll(".tab-panel").forEach(((e,t)=>{e.style.display=0===t?"block":"none"})),this.syncComboboxOwnership()}getTabset(){return document.querySelector(this.tabsetSelector)}rememberTab(){const e=this.getTabset()?.querySelector('input[type="radio"]:checked');e&&(this.lastCheckedId=e.id)}restoreTab(){const e=this.getTabset();if(e&&!e.querySelector('input[type="radio"]:checked')){let t=this.lastCheckedId?e.querySelector("#"+CSS.escape(this.lastCheckedId)):null;t||(t=e.querySelector("#tab1")||e.querySelector('input[type="radio"]')),t&&(t.checked=!0)}}syncComboboxOwnership(){if(!this.predictiveSearchPlaceholder)return;if(null===this.predictiveSearchPlaceholder.offsetParent)return this.input.setAttribute("aria-owns",this.defaultResultsId),this.input.setAttribute("aria-controls",this.defaultResultsId),void(this.isOpen||this.input.setAttribute("aria-expanded","false"));const e=Array.from(this.predictiveSearchPlaceholder.querySelectorAll(".tab-panel")).find((e=>null!==e.offsetParent)),t=e?.querySelector('ul[role="listbox"]');t&&(this.input.setAttribute("aria-owns",t.id),this.input.setAttribute("aria-controls",t.id),this.input.setAttribute("aria-expanded","true"))}initPlaceholderAndTabs(){if(this.togglePlaceholder(),this.rememberTab(),this.predictiveSearchResults){new MutationObserver((()=>{this.restoreTab()})).observe(this.predictiveSearchResults,{childList:!0,subtree:!0})}}getQuery(){return this.input.value.trim()}onChange(){super.onChange();const e=this.getQuery();this.searchTerm&&e.startsWith(this.searchTerm)||this.querySelector("#predictive-search-results-groups-wrapper")?.remove(),this.updateSearchForTerm(this.searchTerm,e),this.searchTerm=e,this.searchTerm.length?this.get
1SearchResults(this.searchTerm):this.close(!0)}onFormSubmit(e){this.getQuery().length&&!this.querySelector('[aria-selected="true"] a')||e.preventDefault()}onFormReset(e){super.onFormReset(e),super.shouldResetForm()&&(this.searchTerm="",this.abortController.abort(),this.abortController=new AbortController,this.closeResults(!0))}onFocus(){const e=this.getQuery();e.length&&(this.searchTerm!==e?this.onChange():"true"===this.getAttribute("results")?this.open():this.getSearchResults(this.searchTerm))}onFocusOut(){setTimeout((()=>{this.contains(document.activeElement)||this.close()}))}onKeyup(e){const t=this.isOpen,s=this.hasAttribute("loading");switch(e.code){case"ArrowUp":e.preventDefault(),this.switchOption("up");break;case"ArrowDown":e.preventDefault(),this.switchOption("down");break;case"Enter":e.preventDefault(),this.selectOption();break;case"Escape":e.preventDefault(),(t||s)&&e.stopPropagation(),s&&!t&&(this.abortController.abort(),this.abortController=new AbortController,this.removeAttribute("loading")),this.close();break;default:this.getQuery().length||this.close(!0)}}onKeydown(e){"ArrowUp"!==e.code&&"ArrowDown"!==e.code||e.preventDefault()}updateSearchForTerm(e,t){const s=this.querySelector("[data-predictive-search-search-for-text]"),i=s?.innerText;if(i){if(i.match(new RegExp(e,"g")).length>1)return;const r=i.replace(e,t);s.innerText=r}}switchOption(e){const t="up"===e,s=Array.from(this.querySelectorAll('li, button.predictive-search__item, a.predictive-search__item[role="button"]')).filter((e=>null!==e.offsetParent));if(!s.length)return;let i=this.querySelector('[aria-selected="true"]');if(i&&!s.includes(i)&&(i.setAttribute("aria-selected",!1),i=null),t&&!i)return;this.statusElement.textContent="";const r=i?s.indexOf(i):-1;let h=0;if(!t&&i?h=r===s.length-1?0:r+1:t&&(h=0===r?s.length-1:r-1),h===r)return;const a=s[h];a.setAttribute("aria-selected",!0),i&&i.setAttribute("aria-selected",!1),this.input.setAttribute("aria-activedescendant",a.id)}selectOption(){const e=this.querySelector('[aria-selected="true"] a, button[aria-selected="true"], a[aria-selected="true"]');e&&e.click()}getSearchResults(e){const t=e.replace(/\s+/g,"-").toLowerCase();this.setLiveRegionLoadingState(),this.cachedResults[t]?this.renderSearchResults(this.cachedResults[t],t):fetch(`${routes.predictive_search_url}?q=${encodeURIComponent(e)}&section_id=${this.sectionId}`,{signal:this.abortController.signal}).then((e=>{if(!e.ok){var t=new Error(e.status);throw this.close(),t}return e.text()})).then((e=>{const s=(new DOMParser).parseFromString(e,"text/html").querySelector(`#shopify-section-${this.sectionId}`).innerHTML;this.allPredictiveSearchInstances.forEach((e=>{e.cachedResults[t]=s})),this.cachedResults[t]=s,this.renderSearchResults(s,t)})).catch((e=>{if(20!==e?.code)throw this.close(),e}))}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(e){this.statusElement.setAttribute("aria-hidden","false"),this.statusElement.textContent=e,setTimeout((()=>{this.statusElement.setAttribute("aria-hidden","true")}),1e3)}hidePlaceholder(){this.predictiveSearchPlaceholder.classList.add("visually-hidden")}showPlaceholder(){this.predictiveSearchPlaceholder.classList.remove("visually-hidden"),this.predictiveSearchPlaceholder.querySelectorAll('input[type="radio"]').forEach(((e,t)=>{e.checked=!1,0===t&&(e.checked=!0)}))}checkForNoResults(){if(this.querySelector("#predictive-search-no-results")){const e=this.predictiveSearchResults.querySelector('input[type="radio"]');if(e){e.checked=!0,e.dispatchEvent(new Event("change",{bubbles:!0}));this.predictiveSearchResults.querySelectorAll(".tab-panel").forEach(((e,t)=>{e.style.display=0===t?"block":"none"}))}}}renderSearchResults(e,t){const s=this.searchTerm.replace(/\s+/g,"-").toLowerCase();t&&t!==s||(this.predictiveSearchResults.innerHTML=e,void 0!==window.yotpoWidgetsContainer&&window.yotpoWidgetsContainer.initWidgets(),this.setAttribute("results",!0),this.setLiveRegionResults(),this.hidePlaceholder(),this.checkForNoResults(),this.open(),this.fireResultsShownEvent())}fireResultsShownEvent(){"predictive-search-modal"===this.sectionId&&"function"==typeof window.dsc?.pushDscDataLayerEvent&&window.dsc.pushDscDataLayerEvent("dl_custom_search_flyout_results_shown",{search_term:this.searchTerm||null,location:window.location.pathname})}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(e=!1){this.isOpen&&(this.closeResults(e),this.isOpen=!1,this.showPlaceholder(),this.syncComboboxOwnership())}closeResults(e=!1){e&&(this.input.value="",this.removeAttribute("results"),this.restorePlaceholder());const t=this.querySelector('[aria-selected="true"]');t&&t.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);

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.