class FacetFiltersForm extends HTMLElement{constructor(){super(),this.onActiveFilterClick=this.onActiveFilterClick.bind(this),this.debouncedOnSubmit=debounce(event=>{this.onSubmitHandler(event)},500),this.querySelector("form").addEventListener("input",this.debouncedOnSubmit.bind(this));const facetWrapper=this.querySelector("#FacetsWrapperDesktop");facetWrapper&&facetWrapper.addEventListener("keyup",onKeyUpEscape)}static setListeners(){const onHistoryChange=event=>{const searchParams=event.state?event.state.searchParams:FacetFiltersForm.searchParamsInitial;searchParams!==FacetFiltersForm.searchParamsPrev&&FacetFiltersForm.renderPage(searchParams,null,!1)};window.addEventListener("popstate",onHistoryChange)}static toggleActiveFacets(disable=!0){document.querySelectorAll(".js-facet-remove").forEach(element=>{element.classList.toggle("disabled",disable)})}static renderPage(searchParams,event,updateURLHash=!0){FacetFiltersForm.searchParamsPrev=searchParams;const sections=FacetFiltersForm.getSections(),countContainer=document.getElementById("ProductCount"),countContainerDesktop=document.getElementById("ProductCountDesktop");document.getElementById("ProductGridContainer").querySelector(".collection").classList.add("loading"),countContainer&&countContainer.classList.add("loading"),countContainerDesktop&&countContainerDesktop.classList.add("loading"),sections.forEach(section=>{const url=`${window.location.pathname}?section_id=${section.section}&${searchParams}`,filterDataUrl=element=>element.url===url;FacetFiltersForm.filterData.some(filterDataUrl)?FacetFiltersForm.renderSectionFromCache(filterDataUrl,event):FacetFiltersForm.renderSectionFromFetch(url,event)}),updateURLHash&&FacetFiltersForm.updateURLHash(searchParams)}static renderSectionFromFetch(url,event){fetch(url).then(response=>response.text()).then(responseText=>{const html=responseText;FacetFiltersForm.filterData=[...FacetFiltersForm.filterData,{html,url}],FacetFiltersForm.renderFilters(html,event),FacetFiltersForm.renderProductGridContainer(html),FacetFiltersForm.renderProductCount(html),typeof initializeScrollAnimationTrigger=="function"&&initializeScrollAnimationTrigger(html.innerHTML)})}static renderSectionFromCache(filterDataUrl,event){const html=FacetFiltersForm.filterData.find(filterDataUrl).html;FacetFiltersForm.renderFilters(html,event),FacetFiltersForm.renderProductGridContainer(html),FacetFiltersForm.renderProductCount(html),typeof initializeScrollAnimationTrigger=="function"&&initializeScrollAnimationTrigger(html.innerHTML)}static renderProductGridContainer(html){document.getElementById("ProductGridContainer").innerHTML=new DOMParser().parseFromString(html,"text/html").getElementById("ProductGridContainer").innerHTML,document.getElementById("ProductGridContainer").querySelectorAll(".scroll-trigger").forEach(element=>{element.classList.add("scroll-trigger--cancel")})}static renderProductCount(html){document.body.classList.contains("overflow-hidden-mobile")&&document.body.classList.remove("overflow-hidden-mobile");const count=new DOMParser().parseFromString(html,"text/html").getElementById("ProductCount").innerHTML,container=document.getElementById("ProductCount"),containerDesktop=document.getElementById("ProductCountDesktop");container.innerHTML=count,container.classList.remove("loading"),containerDesktop&&(containerDesktop.innerHTML=count,containerDesktop.classList.remove("loading"))}static renderFilters(html,event){const parsedHTML=new DOMParser().parseFromString(html,"text/html"),facetDetailsElements=parsedHTML.querySelectorAll("#FacetFiltersForm .js-filter, #FacetFiltersFormMobile .js-filter, #FacetFiltersPillsForm .js-filter"),matchesIndex=element=>{const jsFilter=event?event.target.closest(".js-filter"):void 0;return jsFilter?element.dataset.index===jsFilter.dataset.index:!1},facetsToRender=Array.from(facetDetailsElements).filter(element=>!matchesIndex(element)),countsToRender=Array.from(facetDetailsElements).find(matchesIndex);facetsToRender.forEach(element=>{document.querySelector(`.js-filter[data-index="${element.dataset.index}"]`).innerHTML=element.innerHTML}),FacetFiltersForm.renderActiveFacets(parsedHTML),FacetFiltersForm.renderAdditionalElements(parsedHTML),countsToRender&&FacetFiltersForm.renderCounts(countsToRender,event.target.closest(".js-filter"))}static renderActiveFacets(html){[".active-facets-mobile",".active-facets-desktop"].forEach(selector=>{const activeFacetsElement=html.querySelector(selector);activeFacetsElement&&(document.querySelector(selector).innerHTML=activeFacetsElement.innerHTML)}),FacetFiltersForm.toggleActiveFacets(!1)}static renderAdditionalElements(html){[".mobile-facets__open",".mobile-facets__count",".sorting"].forEach(selector=>{html.querySelector(selector)&&(document.querySelector(selector).innerHTML=html.querySelector(selector).innerHTML)}),document.getElementById("FacetFiltersFormMobile").closest("menu-drawer").bindEvents()}static renderCounts(source,target){const targetElement=target.querySelector(".facets__selected"),sourceElement=source.querySelector(".facets__selected"),targetElementAccessibility=target.querySelector(".facets__summary"),sourceElementAccessibility=source.querySelector(".facets__summary");sourceElement&&targetElement&&(target.querySelector(".facets__selected").outerHTML=source.querySelector(".facets__selected").outerHTML),targetElementAccessibility&&sourceElementAccessibility&&(target.querySelector(".facets__summary").outerHTML=source.querySelector(".facets__summary").outerHTML)}static updateURLHash(searchParams){history.pushState({searchParams},"",`${window.location.pathname}${searchParams&&"?".concat(searchParams)}`)}static getSections(){return[{section:document.getElementById("product-grid").dataset.id}]}createSearchParams(form){const formData=new FormData(form);return new URLSearchParams(formData).toString()}onSubmitForm(searchParams,event){FacetFiltersForm.renderPage(searchParams,event)}onSubmitHandler(event){event.preventDefault();const sortFilterForms=document.querySelectorAll("facet-filters-form form");if(event.srcElement.className=="mobile-facets__checkbox"){const searchParams=this.createSearchParams(event.target.closest("form"));this.onSubmitForm(searchParams,event)}else{const forms=[],isMobile=event.target.closest("form").id==="FacetFiltersFormMobile";sortFilterForms.forEach(form=>{isMobile?form.id==="FacetFiltersFormMobile"&&forms.push(this.createSearchParams(form)):(form.id==="FacetSortForm"||form.id==="FacetFiltersForm"||form.id==="FacetSortDrawerForm")&&(document.querySelectorAll(".no-js-list").forEach(el=>el.remove()),forms.push(this.createSearchParams(form)))}),this.onSubmitForm(forms.join("&"),event)}}onActiveFilterClick(event){event.preventDefault(),FacetFiltersForm.toggleActiveFacets();const url=event.currentTarget.href.indexOf("?")==-1?"":event.currentTarget.href.slice(event.currentTarget.href.indexOf("?")+1);FacetFiltersForm.renderPage(url)}}FacetFiltersForm.filterData=[],FacetFiltersForm.searchParamsInitial=window.location.search.slice(1),FacetFiltersForm.searchParamsPrev=window.location.search.slice(1),customElements.define("facet-filters-form",FacetFiltersForm),FacetFiltersForm.setListeners();class PriceRange extends HTMLElement{constructor(){super(),this.querySelectorAll("input").forEach(element=>element.addEventListener("change",this.onRangeChange.bind(this))),this.setMinAndMaxValues()}onRangeChange(event){this.adjustToValidValues(event.currentTarget),this.setMinAndMaxValues()}setMinAndMaxValues(){const inputs=this.querySelectorAll("input"),minInput=inputs[0],maxInput=inputs[1];maxInput.value&&minInput.setAttribute("max",maxInput.value),minInput.value&&maxInput.setAttribute("min",minInput.value),minInput.value===""&&maxInput.setAttribute("min",0),maxInput.value===""&&minInput.setAttribute("max",maxInput.getAttribute("max"))}adjustToValidValues(input){const value=Number(input.value),min=Number(input.getAttribute("min")),max=Number(input.getAttribute("max"));valuemax&&(input.value=max)}}customElements.define("price-range",PriceRange);class FacetRemove extends HTMLElement{constructor(){super();const facetLink=this.querySelector("a");facetLink.setAttribute("role","button"),facetLink.addEventListener("click",this.closeFilter.bind(this)),facetLink.addEventListener("keyup",event=>{event.preventDefault(),event.code.toUpperCase()==="SPACE"&&this.closeFilter(event)})}closeFilter(event){event.preventDefault(),(this.closest("facet-filters-form")||document.querySelector("facet-filters-form")).onActiveFilterClick(event)}}customElements.define("facet-remove",FacetRemove); //# sourceMappingURL=/cdn/shop/t/25/assets/facets.js.map?ignore_redirect=1