@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 9.38 kB
JavaScript
import{__decorate as p}from"tslib";import{getConfig as m,toQueryString as S}from"@expofp/config";import{action as c,observable as d}from"mobx";import{PREFIXES as P}from"../store/search-buttons/constants";import{getButtonConfig as I,getScopeCategoryIds as C}from"../store/search-buttons/utils";import l from"../tools/logger";import{planReaction as g}from"../utils/plan-reactions.js";import{decodeString as L,decodeStringArray as F,encodeString as w,encodeStringArray as T,toLowerCaseArray as y,toLowerCaseString as f}from"../utils/urlEncoding";import{replaceQuery as R}from"./url-sync";const h={CATEGORIES:"categories",POI_TYPE:"poiType",POI_TYPES:"poiTypes",EXHIBITORS:"exhibitors"};export class URLFilterManager{constructor(e){this.rootStore=e,this.currentFilters={categories:[],poiType:null,poiTypes:[],exhibitors:!1,searchButton:null},this.isInitialized=!1,this.setupReactions()}initialize(){this.parseURLFilters(),this.applyFiltersToStore(),this.isInitialized=!0,l.log("URLFilterManager initialized with filters:",this.currentFilters)}parseURLFilters(){const{categories:e,poiType:t,poiTypes:s,exhibitors:r,searchButton:i}=this.readRawFilters(),o=s.filter(n=>{if(!n)return!1;const a=this.rootStore.poiTypeStore.poiTypes.some(u=>u.name.toLowerCase()===n.toLowerCase());return a||l.warn(`POI type not found in data: ${n}`),a});e.length>0&&(t||o.length>0)?(l.warn("URL contains both categories and POI types. Clearing POI types."),this.currentFilters={categories:e,poiType:null,poiTypes:[],exhibitors:r,searchButton:i}):this.currentFilters={categories:e,poiType:t,poiTypes:o,exhibitors:r,searchButton:i},l.log("URLFilterManager parsed filters:",{categories:e,poiType:t,poiTypes:s,exhibitors:r}),this.validateFilters(this.currentFilters)}readRawFilters(){const e=m().filters;if(Object.keys(e).length>0)return{categories:[...e.categories??[]],poiType:e.poiType??null,poiTypes:[...e.poiTypes??[]],exhibitors:e.exhibitors??!1,searchButton:e.searchButton??null};const t=new URLSearchParams(window.location.search),s=t.get(h.CATEGORIES),r=s?F(s):[],i=t.get(h.POI_TYPE),o=i?L(i):null,n=t.get(h.POI_TYPES),a=n?F(n):[],u=t.get(h.EXHIBITORS);return{categories:r,poiType:o,poiTypes:a,exhibitors:u==="true"||u==="",searchButton:null}}applyFiltersToStore(){const{uiState:e,categoryFilterStore:t,searchButtonsStore:s}=this.rootStore,r=this.currentFilters.searchButton;if(r){const i=I(r),o=i?C(i):[];i?.type==="categories"&&o.length>0?(this.currentFilters.categories=this.currentFilters.categories.filter(n=>o.includes(this.getCategoryIdByName(n)??-1)),t.setOwner(r)):(l.warn(`URLFilterManager: unknown categories search button: ${r}`),this.currentFilters.searchButton=null)}if(this.currentFilters.categories.length>0){const i=this.currentFilters.categories.map(o=>this.rootStore.categoryStore.categories.find(n=>n.name.toLowerCase()===o.toLowerCase())).filter(o=>o!==void 0);i.length>0?(e.selectedCategoryFilters=i,t.state.selectedItems=i.map(o=>({id:o.id,name:o.name})),e.categoryFilterOpen=!0,l.log("URLFilterManager applied categories:",i.map(o=>o.name)),setTimeout(()=>{this.focusOnFilteredBooths(),this.rootStore.searchStore.setFocused(!0)},100)):l.warn("URLFilterManager: no categories found for names:",this.currentFilters.categories)}if(this.currentFilters.poiType){const i=this.rootStore.poiTypeStore.poiTypes.find(o=>o.name.toLowerCase()===this.currentFilters.poiType.toLowerCase());i?(e.selectedPoiTypeFilters=[i],s.setPoiFilterId(`${P.POI_TYPE}${i.id}`),l.log("URLFilterManager applied POI type:",i.name),setTimeout(()=>{this.focusOnFilteredBooths(),this.rootStore.searchStore.setFocused(!0)},100)):l.warn("URLFilterManager: no POI type found for name:",this.currentFilters.poiType)}if(this.currentFilters.poiTypes.length>0){const i=this.currentFilters.poiTypes.map(o=>o?this.rootStore.poiTypeStore.poiTypes.find(n=>n.name.toLowerCase()===o.toLowerCase()):null).filter(o=>o!=null);i.length>0?(e.selectedPoiTypeFilters=i,l.log("URLFilterManager applied POI types:",i.map(o=>o.name)),setTimeout(()=>{this.focusOnFilteredBooths(),this.rootStore.searchStore.setFocused(!0)},100)):(l.warn("URLFilterManager: no POI types found for names:",this.currentFilters.poiTypes),this.currentFilters.poiTypes=[])}this.currentFilters.exhibitors&&this.openExhibitorsList()}filterSearch(){const{uiState:e}=this.rootStore,t=this.owningSearchButtonId();if(t)return`?${S({filters:{searchButton:t,categories:e.selectedCategoryFilters.map(r=>r.name.toLowerCase())}})}`;const s=new URLSearchParams;if(e.selectedCategoryFilters.length>0){const r=e.selectedCategoryFilters.map(o=>o.name.toLowerCase()),i=T(r);i&&s.set(h.CATEGORIES,i)}if(e.selectedPoiTypeFilters.length>0&&e.selectedCategoryFilters.length===0)if(e.selectedPoiTypeFilters.length===1){const r=e.selectedPoiTypeFilters[0].name.toLowerCase();if(r){const i=w(r);i&&s.set(h.POI_TYPE,i)}}else{const r=e.selectedPoiTypeFilters.map(o=>o.name.toLowerCase()),i=T(r);i&&s.set(h.POI_TYPES,i)}return this.currentFilters.exhibitors&&e.selectedCategoryFilters.length===0&&e.selectedPoiTypeFilters.length===0&&s.set(h.EXHIBITORS,"true"),s.toString()?`?${s.toString()}`:""}updateURLFromFilters(){const{uiState:e}=this.rootStore;R(this.filterSearch()),this.currentFilters={categories:e.selectedCategoryFilters.length>0?e.selectedCategoryFilters.map(t=>t.name):[],poiType:e.selectedPoiTypeFilters.length===1&&e.selectedCategoryFilters.length===0?e.selectedPoiTypeFilters[0].name:null,poiTypes:e.selectedPoiTypeFilters.length>1&&e.selectedCategoryFilters.length===0?e.selectedPoiTypeFilters.map(t=>t.name):[],exhibitors:this.currentFilters.exhibitors,searchButton:this.owningSearchButtonId()}}owningSearchButtonId(){return this.rootStore.uiState.selectedCategoryFilters.length===0?null:this.rootStore.categoryFilterStore.scopeButtonId}setCategories(e){this.currentFilters.categories=y(e),this.currentFilters.poiType=null,this.isInitialized&&this.updateURLFromFilters()}setPoiType(e){this.currentFilters.poiType=f(e),this.currentFilters.poiTypes=[],this.currentFilters.categories=[],this.isInitialized&&this.updateURLFromFilters()}setPoiTypes(e){this.currentFilters.poiTypes=y(e),this.currentFilters.poiType=null,this.currentFilters.categories=[],this.isInitialized&&this.updateURLFromFilters()}clearAllFilters(){this.currentFilters={categories:[],poiType:null,poiTypes:[],exhibitors:!1,searchButton:null};const{uiState:e}=this.rootStore;e.categoryFilterOpen=!1,this.updateURLFromFilters()}getCurrentFilters(){return{...this.currentFilters}}setupReactions(){g(()=>this.rootStore.uiState.selectedCategoryFilters,e=>{if(this.isInitialized&&e.length>0){const t=y(e.map(s=>s.name));this.setCategories(t)}}),g(()=>this.rootStore.uiState.selectedPoiTypeFilters,e=>{if(this.isInitialized&&e.length>0)if(e.length===1){const t=f(e[0].name);t&&this.setPoiType(t)}else{const t=y(e.map(s=>s.name));this.setPoiTypes(t)}})}validateFilters(e){if(e.categories.length>0&&(e.poiType||e.poiTypes.length>0))return l.warn("Categories and POI types cannot be active simultaneously"),!1;if(e.poiType&&e.poiTypes.length>0)return l.warn("Both single POI type and multiple POI types cannot be active simultaneously"),!1;for(const t of e.categories)if(!this.getCategoryIdByName(t))return l.warn(`Category not found: ${t}`),!1;if(e.poiType&&!this.getPoiTypeIdByName(e.poiType))return l.warn(`POI type not found: ${e.poiType}`),!1;for(const t of e.poiTypes)if(!this.getPoiTypeIdByName(t))return l.warn(`POI type not found: ${t}`),!1;return!0}getCategoryIdByName(e){const t=this.rootStore.categoryStore.categories.find(s=>s.name.toLowerCase()===e.toLowerCase());return t?t.id:null}getPoiTypeIdByName(e){const t=this.rootStore.poiTypeStore.poiTypes.find(s=>s.name.toLowerCase()===e.toLowerCase());return t?t.id:null}focusOnFilteredBooths(){if(!this.rootStore.layerStore.layersLoaded){g(()=>this.rootStore.layerStore.layersLoaded,()=>{this.focusOnFilteredBooths()},{fireImmediately:!1});return}const{uiState:e,exhibitorStore:t,boothStore:s}=this.rootStore;let r=[];if(e.selectedCategoryFilters.length>0){const o=t.exhibitors.filter(n=>e.selectedCategoryFilters.some(a=>n.categories.some(u=>u.id===a.id)));r=s.booths.filter(n=>o.some(a=>a.id===n.exhibitors[0]?.id))}if(e.selectedPoiTypeFilters.length>0){const o=s.booths.filter(n=>e.selectedPoiTypeFilters.some(a=>n.poiTypeId===a.id));r.length>0?r=r.filter(n=>o.some(a=>a.id===n.id)):r=o}const i=r.filter(o=>o.visible);i.length>0?(e.moveToBooths=i,this.rootStore.moveToList(i)):r.length>0&&(this.rootStore.showMap(),this.rootStore.moveToList(r))}openExhibitorsList(){const{uiState:e}=this.rootStore;e.list={type:"exhibitors",text:"",focused:!1},setTimeout(()=>{this.rootStore.searchStore.setFocused(!0)},100),l.log("URLFilterManager opened exhibitors list")}}p([d],URLFilterManager.prototype,"currentFilters",void 0),p([d],URLFilterManager.prototype,"isInitialized",void 0),p([c],URLFilterManager.prototype,"initialize",null),p([c],URLFilterManager.prototype,"parseURLFilters",null),p([c],URLFilterManager.prototype,"applyFiltersToStore",null),p([c],URLFilterManager.prototype,"updateURLFromFilters",null),p([c],URLFilterManager.prototype,"setCategories",null),p([c],URLFilterManager.prototype,"setPoiType",null),p([c],URLFilterManager.prototype,"setPoiTypes",null),p([c],URLFilterManager.prototype,"clearAllFilters",null),p([c],URLFilterManager.prototype,"focusOnFilteredBooths",null),p([c],URLFilterManager.prototype,"openExhibitorsList",null);