UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

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