@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 14.8 kB
JavaScript
import{jsx as g,Fragment as L,jsxs as C}from"react/jsx-runtime";import{ErrorBoundary as T}from"@sentry/react";import R from"debug";import{reaction as I}from"mobx";import{createRoot as V}from"react-dom/client";import{install as $}from"resize-observer";import M from"./components/Layout";import{LANG_KEY as _,MAX_ROUTE_WAYPOINTS as d,PATHWAY_KEY as j}from"./constants";import m from"./core/Rect";import{fpGeo as b}from"./data/fpGeo";import z from"./floorplan.loader";import{shouldLoadIntercom as G}from"./intercom";import{BoundsSchema as q,POISelectorsSchema as x,ZoomToOptionsSchema as E}from"./schemas/sdk.schemas";import{applyParameters as N,destroyHistory as Y,initRouting as H}from"./services/routing";import t from"./store";import{destroyUiHandlers as O}from"./store/init/init-ui";import{findBooth as P,Route as w,separateExternalIds as B}from"./store/RouteStore";import{destroyGtag as X,GaEventActions as v,sendEventToGa as F,setConsentSettings as U}from"./tools/gtag";import D from"./tools/report-error";import{resetGlobalVariables as K}from"./tools/reset";import W from"./tools/track-event";import{getStorage as A}from"./utils/entity-storage";import{convertLocalToGps as Q,isValidPointCoords as y}from"./utils/gps";import{getLocales as Z,hasLocale as J}from"./utils/i18n";import{mapEntity as p}from"./utils/mapEntity";import{optimizeWaypointOrder as ee}from"./wayfinding/core/routing/optimizeWaypointOrder";const S=R("efp:api");$();export default class te extends z{root;init(){this.ignoreQuery||H(this.offHistory),W("load"),t.fp=this,U(this.allowConsent),F(v.Load,""),this.root=V(this.renderTarget),this.root.render(g(T,{children:g(M,{offHistory:this.offHistory,allowConsent:this.allowConsent})})),F(v.Rendered,""),I(()=>t.layerStore.layersLoaded,()=>{this.resolveReady(),t.initialized||this.onInit?.(this),t.initialized=!0});const e=G(window.__data);e&&import("./intercom/bootIntercom").then(({bootIntercom:o})=>o(e)).catch(()=>{})}selectBooth(e){if(typeof e!="string"&&!Array.isArray(e)){a("selectBooth","Parameter must be a string or an array of strings.","EFP000000");return}const o=t.boothStore.booths.filter(i=>typeof e=="string"?i.name===e||i.externalId===e:e.includes(i.name)||e.includes(i.externalId));t.selectBooth(o)}selectExhibitor(e){if(typeof e!="string"&&!Array.isArray(e)){a("selectExhibitor","Parameter must be a string or an array of strings.","EFP000100");return}if(!e?.length){t.uiState.menu=!1,t.searchStore.resetSearchBox(),t.uiState.details=null;return}const o=t.exhibitorStore.exhibitors.filter(s=>typeof e=="string"?s.name===e||s.externalId===e:e.includes(s.name)||e.includes(s.externalId));if(!o?.length)return;if(typeof e=="string"){t.selectExhibitor(o[0]),t.moveToList([o[0]]);return}const i=o.flatMap(s=>s.booths.map(r=>r.layer)),{description:n}=i.reduce((s,r)=>(s.freq[r.description]=(s.freq[r.description]||0)+1,s.freq[r.description]>s.maxCount&&(s.maxCount=s.freq[r.description],s.mostFrequent=r),s),{freq:{},mostFrequent:i[0],maxCount:0}).mostFrequent;t.layerStore.updateVisibility(n,!0),t.uiState.menu=!1,t.searchStore.resetSearchBox(),t.uiState.details=null,t.uiState.list={type:"filter",items:o,query:{key:"exhibitors",value:o.map(s=>s.externalId).join(",")}},t.moveToList()}highlightExhibitors(e){Array.isArray(e)||a("highlightExhibitors","Parameter must be an array of strings.","EFP000200"),t.boothStore.highlightedByExternalIds=[],t.exhibitorStore.highlightedByExternalIds=[...e]}highlightBooths(e){Array.isArray(e)||a("highlightBooths","Parameter must be an array of strings.","EFP000300"),t.exhibitorStore.highlightedByExternalIds=[],t.boothStore.highlightedByExternalIds=[...e]}highlightEntities(e){Array.isArray(e)||a("highlightEntities","Parameter must be an array of strings.","EFP000400");const{boothExternalIds:o,exhibitorExternalIds:i}=B(e);t.boothStore.highlightedByExternalIds=[...o],t.exhibitorStore.highlightedByExternalIds=[...i]}selectRoute(e,o){if(Array.isArray(e)){let i=[...e];const n=i.shift(),s=i.pop();if(i.length>d)throw new Error(`The maximum number of waypoints is ${d}.`);if(!n||!s){a("selectRoute","Invalid route format: When providing an array, it must include at least two points: a start and a destination.","EFP000401");return}t.routeStore.selectRoute(new w(f(n),f(s),i.map(f)));return}e||a("selectRoute","At a minimum, the starting point must be passed.","EFP000402"),t.routeStore.selectRoute(new w(f(e),f(o)))}getOptimizedRoutes(e){if(!Array.isArray(e))return a("getOptimizedRoutes","Parameter must be an array.","EFP000500"),[];if(e.length>d)throw new Error(`The maximum number of waypoints is ${d}.`);const o=e.map(f).filter(n=>!!n);if(!o.length)throw new Error("No booths found for the provided waypoints.");const i=new Map;for(const n of o){const s=n.layer?.name??"",r=i.get(s);r?r.push(n):i.set(s,[n])}return Array.from(i.values()).map(n=>({waypoints:ee(n.map(s=>[s.name,{cx:s.rect.cx,cy:s.rect.cy}]))}))}selectCurrentPosition(e,o,i){const n=y(e.x)&&y(e.y),s=y(e.lng)&&y(e.lat);!n&&!s&&a("selectCurrentPosition","A point must be passed. Either (x,y) or (lat,lng) are required.","EFP000600"),t.routeStore.selectCurrentPosition(e,o,i),this.onCurrentPositionChanged?.(e)}setBookmarks(e){(!Array.isArray(e)||!e.length)&&a("setBookmarks","Parameter must be an array.","EFP000700"),e.forEach(o=>{const i=t.exhibitorStore.exhibitors.find(n=>n.name===o.name||n.externalId===o.externalId);i&&(i.bookmarked=o.bookmarked)})}setEntitiesBookmarks(e){if(!Array.isArray(e)||!e.length){a("setEntitiesBookmarks","Parameter must be an array.","EFP000701");return}const o=["exhibitor","event","speaker","booth"],i=e.find(r=>!r.type||!o.includes(r.type));if(i){a("setEntitiesBookmarks",`Invalid entity type: ${i.type}. Must be one of: ${o.join(", ")}.`,"EFP000702");return}const n=A("bookmarked"),s={exhibitor:{existingIds:new Set(n.exhibitors||[]),finder:r=>t.exhibitorStore.findExhibitor(r),replacer:r=>t.exhibitorStore.replaceBookmarked(r)},event:{existingIds:new Set(n.events||[]),finder:r=>t.eventStore.findByNameOrSlug(r),replacer:r=>t.eventStore.replaceBookmarked(r)},speaker:{existingIds:new Set(n.speakers||[]),finder:r=>t.speakerStore.speakers.find(l=>l.name===r||l.externalId===r||l.slug===r),replacer:r=>t.speakerStore.replaceBookmarked(r)},booth:{existingIds:new Set(n.booths||[]),finder:r=>{const l=t.boothStore.findBooth(r);return l?.exhibitors.length===0?l:void 0},replacer:r=>t.boothStore.replaceBookmarked(r)}};e.forEach(r=>{const l=s[r.type],h=r.name||r.externalId||"",c=l.finder(h);c?r.bookmarked?l.existingIds.add(c.id):l.existingIds.delete(c.id):console.warn(`Entity not found: ${r.type} with query "${h}"`)}),Object.values(s).forEach(r=>{r.replacer(Array.from(r.existingIds))})}setEntitiesVisited(e){if(!Array.isArray(e)||!e.length){a("setEntitiesVisited","Parameter must be an array.","EFP000703");return}const o=["exhibitor","event"],i=e.find(r=>!r.type||!o.includes(r.type));if(i){a("setEntitiesVisited",`Invalid entity type: ${i.type}. Must be one of: ${o.join(", ")}.`,"EFP000704");return}const n=A("visited"),s={exhibitor:{existingIds:new Set(n.exhibitors||[]),finder:r=>t.exhibitorStore.findExhibitor(r),replacer:r=>t.exhibitorStore.replaceVisited(r)},event:{existingIds:new Set(n.events||[]),finder:r=>t.eventStore.findByNameOrSlug(r),replacer:r=>t.eventStore.replaceVisited(r)}};e.forEach(r=>{const l=s[r.type],h=r.name||r.externalId||"",c=l.finder(h);c?r.visited?l.existingIds.add(c.id):l.existingIds.delete(c.id):console.warn(`Entity not found: ${r.type} with query "${h}"`)}),Object.values(s).forEach(r=>{r.replacer(Array.from(r.existingIds))})}setMarkers(e){if(!e||!Array.isArray(e.icons)||!Array.isArray(e.markers)){a("setMarkers","Invalid input.","EFP000800");return}t.routeStore.setMarkers(e)}selectMarker(e,o=!0){return typeof e!="string"&&a("selectMarker","The identifier must be passed.","EFP000900"),t.routeStore.selectMarker(e,o)}drawCircles(e){Array.isArray(e)||a("drawCircles","Invalid input.","EFP000A00"),t.uiState.debugCircles=e}checkRoutes(){t.routeStore.checkRoutes()}updateLayerVisibility(e,o){typeof e!="string"&&a("updateLayerVisibility","The layer name (string) must be passed.","EFP000B00"),o||a("updateLayerVisibility","Passing a false value to visible will have no effect.","EFP000B01"),t.layerStore.updateVisibility(e,o)}getCenterCoordinates(){return t.fp.getCenterCoordinates()}exhibitorsList(){return t.exhibitorStore.exhibitors.map(e=>p(e))}boothsList(){return t.boothStore.booths.map(e=>p(e))}categoriesList(){return t.categoryStore.categories.map(e=>p(e))}selectCategory(e){if(e==null||typeof e!="string"){t.searchStore.selectSearch();return}const o=e?.toLowerCase(),i=t.categoryStore.categories.find(({name:n,slug:s})=>n?.toLowerCase()===o||s?.toLowerCase()===o);if(!i){a("selectCategory",`Category ${e} not found.`,"EFP000C01");return}t.selectCategory(i)}applyParameters(e){typeof e!="string"&&a("applyParameters","A query string must be passed.","EFP000D00"),N(e)}getVisibility(){return t.uiState.visibility}setVisibility(e){(typeof e!="object"||!Object.keys(e).length)&&a("setVisibility","The parameter must be passed.","EFP000E00"),t.uiState.setVisibility(e)}findLocation(){t.routeStore.findLocation()}zoomIn(){t.uiState.zoomIn()}zoomOut(){t.uiState.zoomOut()}switchView(){t.maplibreStore.activateMaplibre()}zoomTo(e,o){S("zoomTo called with selectors=%O options=%O",e,o);const i=x.safeParse(e);if(!i.success){a("zoomTo",i.error.message,"EFP001600");return}const n=E.safeParse(o);if(!n.success){a("zoomTo",n.error.message,"EFP001601");return}const s=this.getBounds(e);s&&this.fitBounds(s,o)}getBounds(e){S("getBounds called with selectors=%O",e);const o=x.safeParse(e);if(!o.success){a("getBounds",o.error.message,"EFP001400");return}const i=[];if(e.booths?.forEach(s=>{let r;s.externalId?r=t.boothStore.boothByExternalId.get(s.externalId):s.name&&(r=t.boothStore.boothByName.get(s.name.toLowerCase())),r?.rect&&i.push(r.rect)}),e.exhibitors?.forEach(s=>{let r;s.externalId?r=t.exhibitorStore.exhibitorByExternalId.get(s.externalId):s.name&&(r=t.exhibitorStore.exhibitorByName.get(s.name.toLowerCase())),r?.booths?.forEach(l=>{l.rect&&i.push(l.rect)})}),i.length===0)return;const n=m.fromMultiple(i);return{leftTop:{x:n.x1,y:n.y1},rightBottom:{x:n.x2,y:n.y2}}}fitBounds(e,o){if(S("fitBounds called with bounds=%O options=%O",e,o),!e){t.uiState.fitBounds();return}const i=q.safeParse(e);if(!i.success){a("fitBounds",i.error.message,"EFP001500");return}const n=E.safeParse(o);if(!n.success){a("fitBounds",n.error.message,"EFP001501");return}let s;if(o?.padding){const r=o.padding;s=m.fromX1y1x2y2(e.leftTop.x-r,e.leftTop.y-r,e.rightBottom.x+r,e.rightBottom.y+r)}else s=m.fromX1y1x2y2(e.leftTop.x,e.leftTop.y,e.rightBottom.x,e.rightBottom.y);t.uiState.moveToRect=s}getBoothRect(e){return typeof e!="string"&&a("getBoothRect","A booth name must be passed.","EFP000F00"),P(e)?.rect}convertToGeo(e,o){if(!b?.properties?.config){a("convertToGeo","The coordinates cannot be converted because the GPS configuration is not defined.","EFP001000");return}if(!e||!o){a("convertToGeo","Both coordinates must be passed.","EFP001001");return}return Q(e,o,b.properties.config)}getGeoConfig(){const e=b?.properties?.config;return e?{bearing:e.bearing??0,p0:e.p0,p1:e.p1,p2:e.p2}:null}unstable_destroy(){let e=window.__efpElement.firstChild;K(),window.removeEventListener("__efpStyleLoad",this.efpStyleLoadHandler),window.removeEventListener("error",D),t.maplibreStore.dispose(),Y(),O(),X(),[...document.getElementsByTagName("script")].filter(i=>i.src.indexOf("/fp.svg")>-1||i.src.indexOf("/wf.data.js")>-1||i.src.indexOf("/data.js")>-1).forEach(i=>i.remove()),this.root.unmount(),e.remove()}search(e){return typeof e!="string"&&a("search","A search string must be passed.","EFP001100"),t.fuzzySearchEngineStore.loadEngine().then(()=>(t.searchStore.selectSearch(e),t.uiState.fuzzySearchItems.map(o=>({item:p(o.item),score:o.score}))))}getFloors(){return t.layerStore.floors.map(e=>({name:e.name,shortName:e.shortName,description:e.description,active:e.active,disabled:e.disabled,index:e.index}))}activateFloor(e){if(!e?.name&&e?.index==null){a("activateFloor","Invalid floorId. It must contain either a name or an index.","EFP001200");return}const o=this.getFloors();if(e.name){const i=o.find(n=>n.name===e.name||n.shortName===e.name);if(!i){a("activateFloor",`Floor with name "${e.name}" not found.`,"EFP001201");return}if(i.active)return;this.updateLayerVisibility(i.name,!0);return}if(e.index!==null){const i=o.find(s=>s.index===e.index),n=i?.name;if(!n){a("activateFloor",`Floor at index ${e.index} does not exist.`,"EFP001203");return}if(i.active)return;this.updateLayerVisibility(n,!0);return}}showPathway(e,o){const i=new URLSearchParams;if(i.set(j,e),o?.length){const{boothExternalIds:n,exhibitorExternalIds:s}=B(o);n.length>0&&i.set("booths",n.join(",")),s.length>0&&i.set("exhibitors",s.join(","))}this.applyParameters(i.toString())}showPathwayOnly(e,o){this.activateFloor({name:o}),t.layerStore.showPathWay(e),t.uiState.setPathwayOnlyMode(!0);const i=t.layerStore.findLayer(e),n=t.layerStore.findLayer(o),s=i?.rect||i?.viewbox?i:n;if(s?.rect||s?.viewbox){const r=s.viewbox??s.rect;t.uiState.moveToRect=m.fromX1y1x2y2(r.x1,r.y1,r.x2,r.y2)}}hidePathways(){this.highlightBooths([]),t.layerStore.hidePathways(),t.uiState.setPathwayOnlyMode(!1)}showSearch(){t.selectSearch("")}changeLanguage(e){const o=e?.toLowerCase?.();if(!J(o)){a("changeLanguage",`Language "${e}" was not found. Available languages: ${Z().map(n=>n.id).join(",")}`,"EFP001300");return}const i=new URLSearchParams;i.set(_,o),this.applyParameters(i.toString())}isGpsTrackingEnabled(){return t.routeStore.gpsEnabled}setGpsTrackingEnabled(e){t.routeStore.setGpsTrackingEnabled(e)}deselectCurrentPosition(){t.routeStore.selectCurrentPosition(null,!1)}deselectRoute(){t.routeStore.selectRoute(null),t.routeStore.setOnlyAccessible(!1)}reset(){this.selectBooth(""),this.selectExhibitor(""),this.selectCategory(),this.deselectCurrentPosition(),this.deselectRoute(),this.highlightEntities([]),t.uiState.kioskData&&t.resetCameraFn?t.resetCameraFn?.():this.fitBounds()}selectAccessibleRoute(e){t.routeStore.setOnlyAccessible(!0),this.selectRoute(e)}}function f(u){return typeof u=="string"?P(u):t.routeStore.getNearestBooth(u)}let k=0;function a(u,e,o){const i=`Error calling "${u}" SDK method.`,n=`${e.endsWith(".")?e:e+"."} More information at`;let s="https://js-sdk.expofp.com/api/packages";if(o&&(s+=`#error-${o}`),console.error(`${i} ${n} ${s}`),t.fp.disableRuntimeAlerts)return;t.uiState.setGlobalErrorMessage({title:i,body:C(L,{children:[n," ",g("a",{href:s,target:"_blank",rel:"noreferrer",children:s})]})}),clearTimeout(k);const r=8e3;k=window.setTimeout(()=>{const l=async()=>{t.uiState.globalErrorMessages.length>0&&(t.uiState.popGlobalErrorMessage(),await new Promise(h=>setTimeout(h,r/2)),l())};l()},r)}