@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 6.24 kB
JavaScript
import h from"../../data";import{getContrastTextColor as l}from"../../utils/getContrastTextColor";import{BUTTON_ICONS as u,PREFIXES as a,SearchButtonType as i}from"./constants";export class SearchButtonsFactory{rootStore;state;actions;buttonFactories;constructor(t,e,o){this.rootStore=t,this.state=e,this.actions=o,this.buttonFactories={[i.BOOKMARKS]:r=>this.createBookmarksButton(r),[i.VISITED]:r=>this.createVisitedButton(r),[i.CATEGORIES]:r=>this.createCategoriesButton(r),[i.SESSIONS]:r=>this.createSessionsButton(r),[i.TOURS]:r=>this.createGuidedToursButton(r),[i.POI_TYPE]:r=>this.createPoiTypeButton(r),[i.POI_TYPES]:r=>this.createPoiTypesButton(r),[i.LINK]:r=>this.createLinkButton(r),[i.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";return{id:t.id||`${t.type}_${t.name}`,label:t.name,text:t.name,enabled:!0,isDimmed:!1,isClickable:!0,backgroundColor:e?t.color:void 0,textColor:e&&t.color?l(t.color):void 0}}createButtonId(t,e){return e.id||`${t}${e.name}`}createBookmarksButton(t){const{uiState:e}=this.rootStore;return e.kiosk||e.disableBookmarked||h.hideBookmarks?null:{...this.createBaseButton(t),id:i.BOOKMARKS,icon:u.BOOKMARKS,active:e.list.type==="bookmarks",count:this.countBookmarks(),onClick:()=>this.actions.handleSearchButtonClick(i.BOOKMARKS)}}createVisitedButton(t){const{uiState:e}=this.rootStore;return e.kiosk?null:{...this.createBaseButton(t),id:i.VISITED,icon:u.VISITED,active:e.list.type==="visited",count:this.countVisited(),onClick:()=>this.actions.handleSearchButtonClick(i.VISITED)}}createCategoriesButton(t){if(!t.children?.length){const n=this.getCategoriesTotalCount();return n>0?this.createSimpleCategoriesButton(t,n):null}const e=(t.children||[]).filter(n=>n.visible);if(e.length===0)return null;const o=this.createButtonId(a.CATEGORIES,t),r=e.filter(n=>n.type==="categories"),s=this.countCategories(r);return{...this.createBaseButton(t),id:o,active:this.state.searchShortcutState.activeParentId===o,count:s,showZeroCount:!0,onClick:()=>this.actions.handleExtendedSearchButtonClick(o)}}createSimpleCategoriesButton(t,e){const{uiState:o}=this.rootStore,r=o.selectedCategoryFilters.length>0?o.selectedCategoryFilters.length:e;return{...this.createBaseButton(t),id:i.CATEGORIES,active:o.selectedCategoryFilters.length>0&&!this.state.searchShortcutState.activeParentId,count:r,showZeroCount:!0,onClick:()=>this.actions.handleSearchButtonClick(i.CATEGORIES)}}createSessionsButton(t){const{uiState:e}=this.rootStore,o=this.getSessionsTotalCount();return o===0?null:{...this.createBaseButton(t),id:i.SESSIONS,icon:u.SESSIONS,active:e.list.type==="sessions",count:o,showZeroCount:!0,hasSubButtons:!0,onClick:()=>this.actions.handleSessionsClick()}}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")}}createPoiTypeButton(t){const e=this.rootStore.poiTypeStore.poiTypes.find(s=>s.name===t.name);if(!e)return null;const o=`${a.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)}}createPoiTypesButton(t){if(!t.children?.length)return this.createPoiTypeButton(t);const e=t.children.filter(n=>n.visible);if(e.length===0)return null;if(this.shouldFlattenToSinglePoiType(e,t))return this.createPoiTypeButton(t);const o=e.filter(n=>n.type!=="link"),r=this.countPoiTypes(o),s=this.createButtonId(a.POI_TYPES,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=this.createButtonId(a.LINK,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:i.EXHIBITORS,icon:u.EXHIBITORS,active:e.list.type==="exhibitors",count:o,showZeroCount:!0,onClick:()=>this.actions.handleSearchButtonClick(i.EXHIBITORS)}}countCategories(t){const{categoryById:e}=this.rootStore.categoryStore,o=new Set;return t.forEach(r=>{if(r.type!=="categories")return;const s=e.get(Number(r.id));s&&s.exhibitors.forEach(n=>o.add(n.id))}),o.size}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(n=>n.id===r);if(s){const n=this.rootStore.boothStore.booths.filter(c=>c.poiTypeId===s.id).length;return e+n}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}}