UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) • 6.24 kB
import d from"../../data";import{getContrastTextColor as S}from"../../utils/getContrastTextColor";import{tIfExists as B}from"../../utils/i18n";import{BUTTON_ICONS as a,PREFIXES as p,SearchButtonType as n}from"./constants";import{countScopedCategories as C,deriveSearchButtonId as c,getScopeCategoryIds as T}from"./utils";export class SearchButtonsFactory{constructor(t,e,o){this.rootStore=t,this.state=e,this.actions=o,this.buttonFactories={[n.BOOKMARKS]:r=>this.createBookmarksButton(r),[n.VISITED]:r=>this.createVisitedButton(r),[n.CATEGORIES]:r=>this.createCategoriesButton(r),[n.SESSIONS]:r=>this.createSessionsButton(r),[n.TOURS]:r=>this.createGuidedToursButton(r),[n.POI_TYPE]:r=>this.createPoiTypeButton(r),[n.POI_TYPES]:r=>this.createPoiTypesButton(r),[n.LINK]:r=>this.createLinkButton(r),[n.EXHIBITORS]:r=>this.createExhibitorsButton(r)}}createButtons(t){return this.dedupeButtons(t).filter(e=>e.visible).map(e=>({button:this.createButton(e),order:e.order??Number.MAX_SAFE_INTEGER})).filter(e=>e.button!==null).sort((e,o)=>e.order-o.order).map(e=>e.button)}createButton(t){const e=this.buttonFactories[t.type];return e?e(t):null}createBaseButton(t){const e=t.color&&t.color.toUpperCase()!=="#FFFFFF",o=B(t.name);return{id:c(t),label:o,text:o,enabled:!0,isDimmed:!1,isClickable:!0,backgroundColor:e?t.color:void 0,textColor:e&&t.color?S(t.color):void 0}}createBookmarksButton(t){const{uiState:e}=this.rootStore;return e.kiosk||e.disableBookmarked||d.hideBookmarks?null:{...this.createBaseButton(t),id:n.BOOKMARKS,icon:a.BOOKMARKS,active:e.list.type==="bookmarks",count:this.countBookmarks(),onClick:()=>this.actions.handleSearchButtonClick(n.BOOKMARKS,t.name)}}createVisitedButton(t){const{uiState:e}=this.rootStore;return e.kiosk?null:{...this.createBaseButton(t),id:n.VISITED,icon:a.VISITED,active:e.list.type==="visited",count:this.countVisited(),onClick:()=>this.actions.handleSearchButtonClick(n.VISITED,t.name)}}createCategoriesButton(t){if(!t.children?.length){const l=this.getCategoriesTotalCount();return l>0?this.createSimpleCategoriesButton(t,l):null}const e=C(this.rootStore.categoryStore.categories,T(t));if(e===0)return null;const o=c(t),{categoryFilterStore:r,uiState:s}=this.rootStore,u=r.scopeButtonId===o?s.selectedCategoryFilters.length:0,h=this.isModalOpenFor(o);return{...this.createBaseButton(t),id:o,active:u>0||h,expanded:h,count:u>0?u:e,showZeroCount:!0,variant:"action",onClick:()=>this.actions.handleExtendedSearchButtonClick(o)}}createSimpleCategoriesButton(t,e){const{categoryFilterStore:o,uiState:r}=this.rootStore,i=o.scopeButtonId===null?r.selectedCategoryFilters.length:0,u=i>0?i:e;return{...this.createBaseButton(t),id:n.CATEGORIES,active:i>0&&!this.state.searchShortcutState.activeParentId,expanded:this.isModalOpenFor(null),count:u,showZeroCount:!0,variant:"action",onClick:()=>this.actions.handleSearchButtonClick(n.CATEGORIES,t.name)}}isModalOpenFor(t){const{categoryFilterStore:e}=this.rootStore;return e.state.isOpen&&e.viewScope?.buttonId===t}createSessionsButton(t){const{uiState:e}=this.rootStore,o=this.getSessionsTotalCount();return o===0?null:{...this.createBaseButton(t),id:n.SESSIONS,icon:a.SESSIONS,active:e.list.type==="sessions",count:o,showZeroCount:!0,hasSubButtons:!0,onClick:()=>this.actions.handleSessionsClick(t.name)}}createGuidedToursButton(t){const{uiState:e}=this.rootStore,o=this.getGuidedToursTotalCount();return o===0?null:{...this.createBaseButton(t),id:"guided_tours",active:e.list.type==="tours"||e.list.type==="multi-point-tour",count:o,showZeroCount:!0,onClick:()=>this.actions.handleSearchButtonClick("guided_tours",t.name)}}createPoiTypeButton(t){const e=this.rootStore.poiTypeStore.poiTypes.find(s=>s.name===t.name);if(!e)return null;const o=`${p.POI_TYPE}${e.id}`,r=this.getPoiTypeTotalCount(e.id);return r===0?null:{...this.createBaseButton(t),id:o,active:this.state.activePoiFilterId===o,count:r,showZeroCount:!0,onClick:()=>this.actions.handleSearchButtonClick(o,t.name)}}createPoiTypesButton(t){if(!t.children?.length)return this.createPoiTypeButton(t);const e=t.children.filter(i=>i.visible);if(e.length===0)return null;if(this.shouldFlattenToSinglePoiType(e,t))return this.createPoiTypeButton(t);const o=e.filter(i=>i.type!=="link"),r=this.countPoiTypes(o),s=c(t);return{...this.createBaseButton(t),id:s,active:this.state.searchShortcutState.activeParentId===s,count:r,showZeroCount:!0,onClick:()=>this.actions.handleExtendedSearchButtonClick(s)}}shouldFlattenToSinglePoiType(t,e){return t.length===1&&t[0].type==="poi_types"&&t[0].name===e.name}createLinkButton(t){const e=c(t);return{...this.createBaseButton(t),id:e,active:!1,onClick:()=>this.actions.handleExtendedSearchButtonClick(e)}}createExhibitorsButton(t){const{uiState:e}=this.rootStore,o=this.getExhibitorsTotalCount();return o===0?null:{...this.createBaseButton(t),id:n.EXHIBITORS,icon:a.EXHIBITORS,active:e.list.type==="exhibitors",count:o,showZeroCount:!0,onClick:()=>this.actions.handleSearchButtonClick(n.EXHIBITORS,t.name)}}countPoiTypes(t){return t.reduce((e,o)=>{if(o.type!=="poi_types")return e;if(o.count!==void 0)return e+o.count;const r=Number(o.id),s=this.rootStore.poiTypeStore.poiTypes.find(i=>i.id===r);if(s){const i=this.rootStore.boothStore.booths.filter(u=>u.poiTypeId===s.id).length;return e+i}return e},0)}dedupeButtons(t){const e=new Set;return t.filter(o=>{const r=`${o.type}_${o.name}`;return e.has(r)?!1:(e.add(r),!0)})}countBookmarks(){return this.rootStore.exhibitorStore.bookmarked.length+this.rootStore.eventStore.bookmarked.length+this.rootStore.speakerStore.bookmarked.length+this.rootStore.boothStore.booths.filter(t=>t.exhibitors.length===0&&t.bookmarkedDirectly).length}countVisited(){return this.rootStore.exhibitorStore.visited.length+this.rootStore.eventStore.visited.length}getSessionsTotalCount(){return this.rootStore.eventStore.eventItems.length}getGuidedToursTotalCount(){return this.rootStore.toursStore.hasTours?this.rootStore.toursStore.tourItems.length:0}getCategoriesTotalCount(){return this.rootStore.categoryStore.categories.filter(t=>t.exhibitors.length>0).length}getPoiTypeTotalCount(t){return this.rootStore.boothStore.booths.filter(e=>e.poiTypeId===t).length}getExhibitorsTotalCount(){return this.rootStore.exhibitorStore.exhibitors.length}}