UNPKG

@salla.sa/twilight-components

Version:
5 lines 4.85 kB
/*! * Crafted with ❤ by Salla */ import{r as t,c as e,h as s,H as i,g as l}from"./p-bdd00808.js";import{F as a}from"./p-6cf5973f.js";import{H as r}from"./p-cff377ac.js";import"./p-5a803ecc.js";const o=":host{display:block}.s-rating-stars-small{line-height:12px}.s-filters-radio{background-image:none !important}";const n=class{constructor(s){t(this,s);this.changed=e(this,"changed",7);this.isReady=false;this.filters=undefined;this.isSidebarOpen=undefined;this.filtersData={};this.apply=undefined;this.reset=undefined;salla.event.on("filters::hidden",(()=>this.host.style.display="none"));salla.lang.onLoaded((()=>{this.apply=salla.lang.get("pages.checkout.apply");this.reset=salla.lang.get("pages.categories.filters_reset")}));salla.event.on("filters::fetched",(({filters:t})=>{this.host.style.display="";let e={};this.filters=t.map((t=>{t.label={category_id:salla.lang.get("common.titles.categories"),brand_id:salla.lang.get("common.titles.brands"),rating:salla.lang.get("pages.categories.filter_rating"),price:salla.lang.get("pages.categories.filter_price")}[t.key]||t.label;if(t.key=="rating"){t.type=a.VALUES;let{max:e,min:s}=t.values;t.values=[5,4,3,2,1].filter((t=>t>=s||t<=e))}if(this.filtersData.variants&&t.type==a.VARIANTS){e.variants=this.filtersData.variants}if(this.filtersData[t.key]&&t.type!==a.VARIANTS){e[t.key]=this.filtersData[t.key]}let s=new URLSearchParams(window.location.search);let i=false;for(let t of s.keys()){if(t.includes("filters")){i=true;break}}if(salla.config.get("page.slug")=="product.index"&&Object.keys(e).length==0&&!i){let t=window.location.href;let s=salla.config.get("page.id");let i=t.indexOf("?")!==-1?"&":"?";let l=t+i+"filters[category_id]="+s;e.category_id=s;window.history.pushState({path:l},"",l)}return t}));this.filtersData=e;this.host.childNodes.forEach((async t=>t.setWidgetHeight&&await t.setWidgetHeight()))}))}async componentWillLoad(){try{const t=new URLSearchParams(window.location.search);this.filtersData=await r.extractFiltersFromUrl(t)}catch(t){console.warn("failed to get filters from URL",t.message)}}async getFilters(){return this.filtersData}async applyFilters(){if(!this.isReady){return}let t=Object.keys(this.filtersData).length>0;setTimeout((()=>{var e;if(t){let t=new URL(window.location.href);for(const[e]of t.searchParams.entries()){if(e.startsWith("filters")){t.searchParams.delete(e)}}let e=this.encodeFilters(this.filtersData);let s=t.search.split("&").filter((t=>!t.includes("filters"))).join("&");if(s){t.search=t.search.split("&filters")[0]+"&"+e}else{t.search=`?${e}`}window.history.pushState({},"",t.href)}else{let t=new URL(window.location.href);(e=t.searchParams)===null||e===void 0?void 0:e.delete("filters");window.history.pushState({},"",t.toString())}salla.event.emit("salla-filters::changed",this.filtersData);this.changed.emit(this.filtersData)}),300)}encodeFilters(t,e="filters"){const s=[];for(const i in t){if(Object.prototype.hasOwnProperty.call(t,i)){const l=t[i];const a=e?`${e}[${i}]`:i;if(Array.isArray(l)){for(let t=0;t<l.length;t++){s.push(`${a}[${t}]=${l[t]}`)}}else if(typeof l==="object"&&l!==null){s.push(this.encodeFilters(l,a))}else{let t=`${a}=${l}`;s.push(t)}}}return s.join("&")}async resetFilters(){this.removeFiltersQueryParams();this.filtersData={};this.host.childNodes.forEach((t=>t.reset&&t.reset()));salla.event.emit("salla-filters::reset");return this.applyFilters()}removeFiltersQueryParams(){const t=new URLSearchParams(window.location.search);const e=Array.from(t.keys()).filter((t=>!t.startsWith("filters["))).map((e=>`${encodeURIComponent(e)}=${encodeURIComponent(t.get(e))}`)).join("&");const s=e?`${window.location.pathname}?${e}`:window.location.pathname;window.history.pushState({},"",s)}handleOptionChange(t,e,s){if(e.type===a.RANGE){this.filtersData[e.key]=s}let i=t.target.checked;if(e.type===a.VARIANTS){this.filtersData.variants=this.filtersData.variants||{};i&&(this.filtersData.variants[e.key]=s);i||delete this.filtersData.variants[e.key]}else if(t.target.type=="radio"){i&&(this.filtersData[e.key]=s);i||delete this.filtersData[e.key]}else if(t.target.type=="checkbox"){this.filtersData[e.key]=this.filtersData[e.key]||[];if(i){this.filtersData[e.key].push(s)}this.filtersData[e.key]=this.filtersData[e.key].filter((t=>t!=s))}this.applyFilters()}render(){var t,e;return s(i,null,(t=this.filters)===null||t===void 0?void 0:t.map((t=>s("salla-filters-widget",{option:t,filtersData:this.filtersData,onChanged:({detail:{event:t,option:e,value:s}})=>{this.handleOptionChange(t,e,s)}}))),((e=this.filters)===null||e===void 0?void 0:e.length)?s("div",{class:"s-filters-footer"},s("salla-button",{color:"gray",fill:"outline",onClick:()=>this.resetFilters()},this.reset)):"")}componentDidLoad(){this.isReady=true}get host(){return l(this)}};n.style=o;export{n as salla_filters}; //# sourceMappingURL=p-afe8a34e.entry.js.map