1class ResultsStateLayer{constructor(){this.eventBus=window.theme.nosto.eventBus,this.events=window.theme.results.events;const sortOptions=[{field:"score",order:"desc",label:this.pageType==="search"?window.theme.strings.sections.collectionTemplate.sortBy.relevant:window.theme.strings.sections.collectionTemplate.sortBy.featured,selected:!0},{field:"stats.buys",order:"desc",label:window.theme.strings.sections.collectionTemplate.sortBy.bestsellers,selected:!1},{field:"customFields.product_details-strapline",order:"asc",label:window.theme.strings.sections.collectionTemplate.sortBy.nameAsc,selected:!1},{field:"customFields.product_details-strapline",order:"desc",label:window.theme.strings.sections.collectionTemplate.sortBy.nameDesc,selected:!1},{field:"price",order:"asc",label:window.theme.strings.sections.collectionTemplate.sortBy.priceAsc,selected:!1},{field:"price",order:"desc",label:window.theme.strings.sections.collectionTemplate.sortBy.priceDesc,selected:!1},{field:"datePublished",order:"desc",label:window.theme.strings.sections.collectionTemplate.sortBy.dateDesc,selected:!1}];this.state={activeTerm:this.activeTerm,activeFacets:[],activeSort:{field:"score",order:"desc"},defaultSort:"score~desc",facets:[],pagination:{actualFrom:0,actualPage:1,showing:0},products:[],productsPerPage:window.theme.results.config.productsPerPage,productsPerFirstPage:window.theme.results.config.productsPerFirstPage,sort:sortOptions.map(option=>({...option})),lastInteraction:null},this.subscribeToEvents(),this.initializeStateFromUrl(),queueMicrotask(()=>{this.eventBus.publish(this.events.API.FETCH_PRODUCTS,{})})}subscribeToEvents(){this.eventBus.subscribe(this.events.API.PRODUCTS_FETCHED,response=>this.onProductsFetched(response)),this.eventBus.subscribe(this.events.API.FACETS_FETCHED,facets=>this.updateFacets(facets)),this.eventBus.subscribe(this.events.UI.FACET_CLEAR_ALL,()=>this.onFacetClearAll()),this.eventBus.subscribe(this.events.UI.SORT_CHANGED,sort=>this.onSortChanged(sort)),this.eventBus.subscribe(this.events.UI.PRICE_CHANGED,({min,max})=>this.onPriceChanged({min,max})),this.eventBus.subscribe(this.events.UI.FACET_ADDED,({field,value})=>this.onFacetAdded({field,value})),this.eventBus.subscribe(this.events.UI.FACET_REMOVED,({field,value})=>this.onFacetRemoved({field,value})),this.eventBus.subscribe(this.events.STATE.ACTIVE_SORT_UPDATED,activeSort=>this.onActiveSortUpdated(activeSort)),this.eventBus.subscribe(this.events.UI.PAGINATION_NEXT,()=>this.onPaginationNext()),this.eventBus.subscribe(this.events.UI.PAGINATION_PREVIOUS,payload=>this.onPaginationPrevious(payload))}async onProductsFetched(response){const pagination={total:response.products.total,size:response.products.size,from:response.products.from,hits:response.products.hits.length,totalPages:Math.ceil(response.products.total/response.products.size),currentPage:Math.ceil((response.products.from+1)/response.products.size),showNextButton:!1,showPreviousButton:!1};this.state.pagination.actualPage>1&&(pagination.currentPage=this.state.pagination.actualPage,pagination.from=this.state.pagination.actualFrom),pagination.showing=this.state.pagination.showing+Math.min(pagination.size,pagination.hits),pagination.showing!==pagination.total&&(pagination.showNextButton=response.products.from+response.products.size<response.products.total,pagination.showPreviousButton=pagination.currentPage>1&&pagination.showing<pagination.from+pagination.size),this.updatePagination({...this.state.pagination,...pagination}),this.updateProducts(response.products.hits),this.activeTerm.length>0&&this.eventBus.publish(this.events.STATE.SEARCH_TOTAL_UPDATED,{total:this.state.pagination.total,activeTerm:this.activeTerm})}onFacetClearAll(){this.state.lastInteraction="filter",this.resetPagination(),this.updateActiveFacets([]),this.updateActiveSort({field:"score",order:"desc"}),this.syncUrlAndFetch()}onSortChanged({field,order}){this.state.lastInteraction="sort",this.resetPagination(),this.updateActiveSort({field,order}),this.syncUrlAndFetch()}onPriceChanged({min,max}){this.state.lastInteraction="filter",this.resetPagination();const field="price",bounds=this.getPriceStatBoundsFromFacets(this.state.facets);let outMin=min,outMax=max;const mn=parseFloat(min),mx=parseFloat(max);if(bounds&&Number.isFinite(mn)&&Number.isFinite(mx)){let lo=Math.min(Math.max(mn,bounds.min),bounds.max),hi=Math.min(Math.max(mx,bounds.min),bounds.max);if(lo>hi){const t=lo;lo=hi,hi=t}outMin=String(lo),outMax=String(hi)}const value=[`${outMin}~${outMax}`];this.state.activeFacets.find(facet=>facet.field===field)?this.updateActiveFacets(this.state.activeFacets.map(facet=>facet.field===field?{field,value}:facet)):this.updateActiveFacets([...this.state.activeFacets,{field,value}]),this.syncUrlAndFetch(void 0,{debounceMs:300})}onFacetAdded({field,value}){this.state.lastInteraction="filter",this.resetPagination();const existing=this.state.activeFacets.find(facet=>facet.field===field);if(existing){const merged=[...new Set([...existing.value,...value])];this.updateActiveFacets(this.state.activeFacets.map(facet=>facet.field===field?{field,value:merged}:facet))}else this.updateActiveFacets([...this.state.activeFacets,{field,value}]);this.syncUrlAndFetch()}onFacetRemoved({field,value}){this.state.lastInteraction="filter",this.resetPagination();const existing=this.state.activeFacets.find(facet=>facet.field===field);if(existing){const filtered=existing.value.filter(v=>!value.includes(v));filtered.length>0?this.updateActiveFacets(this.state.activeFacets.map(facet=>facet.field===field?{field,value:filtered}:facet)):this.updateActiveFacets(this.state.activeFacets.filter(facet=>facet.field!==field))}this.syncUrlAndFetch()}onActiveSortUpdated(activeSort){this.state.sort.forEach(sort=>{sort.selected=sort.field===activeSort.field&&sort.order===activeSort.order})}onPaginationChanged(cursor){this.state.lastInteraction="pagination",this.state.pagination.from=Number(cursor.current),cursor.direction==="next"?this.state.pagination.currentPage+=1:cursor.direction==="previous"&&(this.state.pagination.currentPage-=1),this.syncUrlAndFetch(cursor.direction)}onPaginationNext(){this.state.lastInteraction="paginationNext",this.state.pagination.from+=this.state.pagination.size,this.state.pagination.actualFrom+=this.state.pagination.size,this.state.pagination.currentPage+=1,this.state.pagination.actualPage+=1,this.syncUrlAndFetch("next")}onPaginationPrevious({from,page}={}){this.state.lastInteraction="paginationPrevious",this.state.pagination.actualPage===1&&(this.state.pagination.actualPage=this.state.pagination.currentPage,this.state.pagination.actualFrom=this.state.pagination.from),this.state.pagination.from=from,this.state.pagination.currentPage=page,this.syncUrlAndFetch("previous")}initializeStateFromUrl(){const searchParams=new URLSearchParams(window.location.search),activeFacets=[];
1searchParams.forEach((value,key)=>{const values=value.includes(",")?value.split(","):[value];switch(key){case"sort":{const[field,order]=values[0].split("~");this.updateActiveSort({field,order});break}case"page":{const currentPage=parseInt(value,10);this.state.pagination.currentPage=currentPage,this.state.pagination.actualPage=currentPage;const from=(currentPage-1)*this.state.productsPerPage;this.state.pagination.from=from,this.state.pagination.actualFrom=from;break}case"q":this.state.activeTerm=value;break;default:(key==="price"||key==="available"||key.includes("customFields.")||key.includes("extra."))&&activeFacets.push({field:key,value:values})}}),this.updateActiveFacets(activeFacets)}buildSearchParams(){const params=new URLSearchParams;if(this.state.activeTerm&¶ms.set("q",this.state.activeTerm),this.state.activeFacets.forEach(facet=>{facet.value.length>0&¶ms.set(facet.field,facet.value.join(","))}),this.state.activeSort){const activeSort=`${this.state.activeSort.field}~${this.state.activeSort.order}`;activeSort!==this.state.defaultSort&¶ms.set("sort",activeSort)}const page=this.state.pagination.actualPage;return page>1&¶ms.set("page",page),params}syncUrlAndFetch(direction,{debounceMs=0}={}){const runSync=()=>{const query=this.buildSearchParams().toString(),url=query?`${window.location.pathname}?${query}`:window.location.pathname;window.history.replaceState({},"",url),this.eventBus.publish(this.events.API.FETCH_PRODUCTS,{targetPage:this.state.pagination.currentPage,direction})};if(clearTimeout(this.syncTimeout),debounceMs>0){this.syncTimeout=setTimeout(runSync,debounceMs);return}runSync()}updateProducts(products){this.state.products=products,this.eventBus.publish(this.events.STATE.PRODUCTS_UPDATED,{products,interaction:this.state.lastInteraction||"initial",state:this.state}),this.state.lastInteraction=null}updateFacets(facets){this.state.facets=facets,this.clampActivePriceToCurrentFacetStats(facets),this.eventBus.publish(this.events.STATE.FACETS_UPDATED,{facets,activeFacets:this.state.activeFacets})}getPriceStatBoundsFromFacets(facets){if(!Array.isArray(facets))return null;const f=facets.find(x=>x&&x.field==="price"&&typeof x.max=="number"&&Number.isFinite(x.max));if(!f)return null;const low=Number.isFinite(f.min)?Math.floor(f.min):0,high=Math.ceil(f.max);return high<=0?null:{min:Math.max(0,low),max:high}}replaceSearchUrlFromState(){const query=this.buildSearchParams().toString(),url=query?`${window.location.pathname}?${query}`:window.location.pathname;window.history.replaceState({},"",url)}clampActivePriceToCurrentFacetStats(facets){const bounds=this.getPriceStatBoundsFromFacets(facets);if(!bounds)return;const idx=this.state.activeFacets.findIndex(x=>x.field==="price");if(idx===-1)return;const raw=this.state.activeFacets[idx].value?.[0];if(typeof raw!="string"||!raw.includes("~"))return;const parts=raw.split("~");if(parts.length!==2)return;const lo=parseFloat(parts[0]),hi=parseFloat(parts[1]);if(!Number.isFinite(lo)||!Number.isFinite(hi))return;let loC=Math.min(Math.max(lo,bounds.min),bounds.max),hiC=Math.min(Math.max(hi,bounds.min),bounds.max);if(loC>hiC){const t=loC;loC=hiC,hiC=t}if(loC===lo&&hiC===hi)return;const next=this.state.activeFacets.map((facet,i)=>i===idx?{field:"price",value:[`${loC}~${hiC}`]}:facet);this.updateActiveFacets(next),this.replaceSearchUrlFromState()}resetPagination(){this.state.pagination.actualPage=1,this.state.pagination.currentPage=1,this.state.pagination.actualFrom=0,this.state.pagination.from=0,this.state.pagination.showing=0}updateActiveFacets(activeFacets){this.state.activeFacets=activeFacets,this.eventBus.publish(this.events.STATE.ACTIVE_FACETS_UPDATED,{activeFacets})}updateActiveSort(activeSort){this.state.activeSort=activeSort,this.eventBus.publish(this.events.STATE.ACTIVE_SORT_UPDATED,activeSort)}updatePagination(pagination){this.state.pagination=pagination,this.eventBus.publish(this.events.STATE.PAGINATION_UPDATED,pagination)}get activeTerm(){return new URLSearchParams(window.location.search).get("q")||""}get pageType(){return document.body.dataset.pageType}}window.theme.results.stateLayer=new ResultsStateLayer; 2//# sourceMappingURL=/cdn/shop/t/1735/assets/results-state-layer.js.map?v=30589708876539377821787233677
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.