@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 10.6 kB
JavaScript
import{__decorate as n}from"tslib";import{action as f,computed as g,observable as u}from"mobx";import{Box as R,Point as _,boxContainsPoint as O,pointDistance as T}from"@expofp/geometry";import c,{layersStore as x}from".";import{fpGeo as S}from"../data/fpGeo";import{getName as F}from"../components/Kiosk/SetKiosk";import{DEFAULT_UNITS as L,ORIENTATION_GRANTED_KEY as G}from"../constants";import E from"../data";import{getLayerSvg as B,svgArea as m}from"../data/svg";import{GaEventActions as U,sendEventToGa as $}from"../tools/gtag";import P from"../tools/logger";import{areLayersEnabled as V}from"../utils/areLayersEnabled";import{calcSpeed as D}from"../utils/calcSpeed";import{calcTravelTime as w}from"../utils/calcTravelTime";import{calculateRelativeBearing as K,convertGpsToLocal as A,isValidPointCoords as I}from"../utils/gps";import{boothToEndpoint as C,buildRoute as W,CURRENT_LOCATION_NAME as Z,CURRENT_POSITION_POINT_ID as q,isWayfindingReady as j,KIOSK_ANCHOR_POINT_ID as X,toArbitraryPoint as M}from"../wayfinding";import{BoothBase as y}from"./BoothStore";import{uiState as h}from"./index";import{LayersMode as Y}from"./LayerStore";const d=o=>typeof o=="string"?Number(o.replace(",",".")):o;export default class i{constructor(t){this.routeLines=[],this.routeDistance=null,this.currentPosition=null,this.requestCompass=!1,this.iconType=0,this.tempToBooth=null,this.focusEnabled=!0,this.prevZ=null,this.markersData={icons:[],markers:[]},this.prevMarkers=[],this.showAccessible=!1,this.onlyAccessible=!1,this.currentRouteLayer=null,this.currentRouteExhibitor=null,this.routeUnits=L,this.isGpsTrackingEnabled=!0,this.rootStore=t,this.focusEnabled=!window.location.search}get canFindLocation(){return!!h.kioskData||!!this.currentPosition}selectRoute(t,e=!0,r=!1){if(e&&(h.list={type:"search",text:"",focused:!1}),t&&!t.from&&t.to){const l=this.resolveFallbackFrom();l&&(t=new Route(l,t.to,t.waypoints))}t?.from&&t?.to&&t.from===t.to&&(t=null);let s=[];t?.from&&t?.to&&window.setTimeout(()=>{this.rootStore.showMap()},navigator.userAgent.toLowerCase().indexOf("android")>-1?400:50),t?.from instanceof y&&t.from.visible&&s.push(t.from),t?.to instanceof y&&t.to.visible&&(this.tempToBooth=null,s.push(t.to)),!t&&c.fp.onDirection&&c.fp.onDirection(null);const a=()=>{if(this.rootStore.moveToList(s),h.details=t,t&&(!t.from||!t.to)&&c.showOverlay(),t?.to&&t?.from){const l=H(t.from);l&&this.rootStore.layerStore.updateVisibility(l,!0)}if(!this.currentRouteLayer){const l=N(this.rootStore.layerStore,t?.from),p=N(this.rootStore.layerStore,t?.to);l?this.currentRouteLayer=l:p&&(this.currentRouteLayer=p)}e&&t?.from&&t?.to&&$(U.ClickDirections,`${t?.from?"From "+t.from.name:""} ${t?.to?"To "+t.to.name:""}`)};r?a():setTimeout(a,200)}rebuildRouteFromPosition(t){const e=h.selectedRoute;e?.to&&this.selectRoute(new Route(t,e.to,e.waypoints),!1,!0)}resolveFallbackFrom(){if(this.currentPosition)return M(this.currentPosition,Z,q);const t=h.kioskData;return t?M({x:t.x,y:t.y,z:t.z},F(),X):null}setStartPoint(t){const e=h.selectedRoute;if(e?.to&&!e?.from&&t){const r=new Route(t,e.to,e.waypoints);this.selectRoute(r)}}get pathLayers(){return c.routeStore.routeLines?.map(t=>t.p0.layer)?.filter((t,e,r)=>r.indexOf(t)===e).map((t,e)=>({id:e+1,layer:c.layerStore.findLayer(t)}))}getNearestBooth(t){if(!t)return null;let e=this.rootStore.layerStore.findLayer(t.z);const r=t.lat&&t.lng?A(t,S.properties.config):t;return this.rootStore.boothStore.booths.filter(s=>x.mode===Y.Default||!e?s.visible&&s.rect:s.rect&&(typeof t.z!="number"&&!t.z&&s.visible||e.name===s.layer?.name)).sort((s,a)=>T(r,s.rect.center)-T(r,a.rect.center))[0]||null}setMarkers(t){this.markersData.markers=(t?.markers??[]).map(J),this.markersData.icons=t?.icons??[]}selectMarker(t,e){const r=this.markersData.markers.find(a=>a.id===t);this.markersData.markers.forEach(a=>a.active=!1),r&&(r.active=!0);let s=c.layerStore.findLayer(r?.z);if(e&&r){const a=c.rendererService.renderer;return s&&!s?.visible&&x.updateVisibility(s,!0),a?a.controls.panTo(r.x,r.y):(P.warn("Renderer is not initialized. Cannot pan to marker."),Promise.resolve())}return Promise.resolve()}get selectedMarkers(){return this.markersData.markers.filter(t=>t.active)}get layers(){var t=[];return c.routeStore.routeLines?.map(e=>e.p0.layer).forEach(e=>{t.indexOf(e)===-1&&t.push(e)}),t.map(e=>c.layerStore.layers.find(r=>r.name===e))}clickRoute(t,e){window.__resett&&window.__resett(),this.rootStore.uiState.menu=null,h.list.type==="route-planner"&&(h.list={type:"search",text:"",focused:!1}),this.selectRoute(new Route(t,e))}selectCurrentPosition(t,e=!0,r){clearTimeout(this.cpTimeout),t&&(t.x=d(t.x),t.y=d(t.y),t.lat=d(t.lat),t.lng=d(t.lng),V()||(t.z=null));const s=e&&this.rootStore.uiState.details&&!(this.rootStore.uiState.details instanceof Route),a=e&&!s&&(this.focusEnabled||this.prevZ!=t?.z);this.focusEnabled&&(this.focusEnabled=!1),this.prevZ=t?.z?.toString(),this.iconType=r?1:0;const l=t?Q(t):null;if(!l){this.currentPosition=null;return}const p=c.layerStore.findLayer(t.z),b=new CurrentPosition(l.x,l.y,p?.name||this.currentRouteLayer?.name,K(l),t.lat,t.lng);let v=!this.routeLines?.length;if(p&&this.routeLines?.length&&(v=!!this.routeLines.find(k=>k.p0.layer===p.name||k.p1.layer===p.name)),a&&v){const k=c.rendererService.renderer;if(p&&!p?.visible&&x.updateVisibility(p,!0),!k){P.warn("Renderer is not initialized. Cannot pan to current position.");return}k.controls.panTo(b.x,b.y)}this.currentPosition=b,this.gpsEnabled||(this.cpTimeout=setTimeout(()=>{this.currentPosition&&this.selectCurrentPosition(null,!1)},30*1e3))}findLocation(){if(!this.canFindLocation)return;if(c.maplibreStore.showMaplibre){h.moveToLocation=!0;return}const t=this.currentPosition??h.kioskData;if(!t)return;this.currentPosition&&localStorage.getItem(G)==="false"&&(c.routeStore.requestCompass=!0);const e=R.fromCenter(t,{x:100,y:100});let r;if(e.intersects(m))r=e;else{const a=Math.max(m.min.x,Math.min(t.x,m.max.x)),l=Math.max(m.min.y,Math.min(t.y,m.max.y)),p=Math.min(m.size.width,m.size.height)*.3,b=R.fromCenter({x:a,y:l},{x:p,y:p}).intersection(m);r=b?R.fromUnion(e,b):e}h.moveToRect=r;const s=c.layerStore.findLayer(t.z);s&&x.updateVisibility(s,!0)}get routeInfo(){const t=this.routeDistance,e=this.routeUnits;return t?{distance:`${t}${e}`,units:e,time:w(t,D(e))}:null}updateRoute(t,e){!t?.length&&!this.routeLines.length||(this.routeLines=t,this.updateRoutePoints())}updateRouteDistance(t){if(t==null)return;const e=B(),r=(e.getAttribute("units")||L).toLowerCase?.().trim(),s=e.getAttribute("fp-ver")?.startsWith("5")??!1;this.routeDistance=Math.round(t/(s?1:10)),this.routeUnits=r,this.updateRoutePoints()}updateRoutePoints(){const t=h.selectedRoute,e=this.routeDistance,r=this.routeUnits;c.fp.onDirection&&setTimeout(()=>{c.fp.onDirection({from:z(t?.from??null),to:z(t?.to??null),lines:this.routeLines,distance:`${e}${r}`,units:r,time:w(e,D(r))})},200)}checkRoutes(){if(!j()){console.warn("checkRoutes: wayfinding is not initialized \u2014 call createWayfinding() first. Skipping route check.");return}const t=c.boothStore.booths;console.info(`Route check started ${t.length}.... `);for(let e=0;e<t.length;e++){const r=t[e];for(let s=e+1;s<t.length;s++){const a=t[s];W(C(r),C(a)).lines.length||console.warn(`No route found from ${r.name} to ${a.name}`)}}console.info("Route check done....")}setGpsTrackingEnabled(t){this.isGpsTrackingEnabled=t}get gpsEnabled(){return E.autoTrackingGps&&this.isGpsTrackingEnabled&&!E.enableIPS}setOnlyAccessible(t){this.onlyAccessible=t}}n([u],i.prototype,"routeLines",void 0),n([u],i.prototype,"routeDistance",void 0),n([u],i.prototype,"currentPosition",void 0),n([u],i.prototype,"requestCompass",void 0),n([u],i.prototype,"iconType",void 0),n([u],i.prototype,"tempToBooth",void 0),n([u],i.prototype,"focusEnabled",void 0),n([u],i.prototype,"prevZ",void 0),n([u],i.prototype,"markersData",void 0),n([u],i.prototype,"prevMarkers",void 0),n([u],i.prototype,"showAccessible",void 0),n([u],i.prototype,"onlyAccessible",void 0),n([u],i.prototype,"currentRouteLayer",void 0),n([u],i.prototype,"currentRouteExhibitor",void 0),n([g],i.prototype,"canFindLocation",null),n([f],i.prototype,"selectRoute",null),n([f],i.prototype,"rebuildRouteFromPosition",null),n([f],i.prototype,"setStartPoint",null),n([g({keepAlive:!0})],i.prototype,"pathLayers",null),n([f],i.prototype,"setMarkers",null),n([f],i.prototype,"selectMarker",null),n([g({keepAlive:!0})],i.prototype,"selectedMarkers",null),n([g({keepAlive:!0})],i.prototype,"layers",null),n([f],i.prototype,"clickRoute",null),n([f],i.prototype,"selectCurrentPosition",null),n([f],i.prototype,"findLocation",null),n([u],i.prototype,"routeUnits",void 0),n([g],i.prototype,"routeInfo",null),n([f],i.prototype,"updateRoute",null),n([f],i.prototype,"updateRouteDistance",null),n([f],i.prototype,"updateRoutePoints",null),n([f],i.prototype,"checkRoutes",null),n([u],i.prototype,"isGpsTrackingEnabled",void 0),n([f],i.prototype,"setGpsTrackingEnabled",null),n([g({keepAlive:!0})],i.prototype,"gpsEnabled",null),n([f],i.prototype,"setOnlyAccessible",null);function H(o){if(o)return o instanceof y?o.layer?.name:o.layer}function N(o,t){return t?t instanceof y?t.layer??null:o.findLayer(t.layer):null}function J(o){return{...o,x:d(o.x),y:d(o.y),lat:d(o.lat),lng:d(o.lng)}}function Q(o){let t=null,e=null;return S&&(t=S.properties.config),t&&O({min:t.p0,max:t.p2},o)?e={...o}:t&&I(o.lat)&&I(o.lng)?e=A(o,t):t||(e=o),e||null}export function findBooth(o){return c.boothStore.findBooth(o)||c.exhibitorStore.findExhibitor(o)?.booths[0]}export function findEntityByExternalId(o){const t=c.boothStore.boothByExternalId.get(o);if(t)return{type:"booth",entity:t};const e=c.exhibitorStore.exhibitorByExternalId.get(o);if(e)return{type:"exhibitor",entity:e}}export function separateExternalIds(o){const t=[],e=[];for(const r of o){const s=findEntityByExternalId(r);s&&(s.type==="booth"?t.push(r):e.push(r))}return{boothExternalIds:t,exhibitorExternalIds:e}}export function extractRoute(o,t,e){return new Route(findBooth(o)??null,findBooth(t)??null,e?.map(r=>findBooth(r)))}export class Route{constructor(t,e,r){this.from=t,this.to=e,this.waypoints=r;const s=t instanceof y?t.id:void 0,a=e instanceof y?e.id:void 0;this.waypoints=r?.filter(l=>l&&l.id!==s&&l.id!==a)}}function z(o){return o?o instanceof y?{id:o.id,name:o.name,externalId:o.externalId,layer:{name:o.layer?.name,description:o.layer?.description}}:{id:Number.MAX_SAFE_INTEGER,name:o.name,externalId:void 0,layer:{name:o.layer,description:void 0}}:null}export class CurrentPosition extends _{constructor(t,e,r,s,a,l){super(t,e),this.x=t,this.y=e,this.z=r,this.angle=s,this.lat=a,this.lng=l}}