atriusmaps-node-sdk
Version:
This project provides an API to Atrius Personal Wayfinder maps within a Node environment. See the README.md for more information
2 lines (1 loc) • 8.08 kB
JavaScript
import*as e from"ramda";import t from"zousan";import{normalizeCoords as a,getVenueDataFromUrls as n,buildStructures as s,createFetchJson as r,createFetchText as u}from"./venueLoadingUtils.js";async function o(o,i){const d=o.log.sublog("venueDataLoader");let l=new t,c=new t;const p=t=>t.defaultStructureId||e.path(["structureOrder",0],t)||e.path(["selectorOrder",0],t)||e.pipe(e.prop("structures"),Object.values,e.path([0,"id"]))(t),v=(e,t)=>e.split("").reduce(((a,n,s)=>e.substring(0,s+1)===t.substring(0,s+1)?s:a),-1)+1,g=e=>((e,t)=>Object.values(e.venueList).filter((e=>e.locale===t)).sort(((t,a)=>v(a.id,e.id)-v(t.id,e.id))).at(0))(e,"en")?.id;async function f(t,a){t.assetStage&&"prod"!==t.assetStage&&location.hostname;const i=r(),d=u(),c=await n(t,i,a),{accountId:v,venueId:f}=t;return c.assetStage=t.assetStage,c.defaultOrdinal=c.defaultOrdinal||function(t){const a=p(t),n=Object.values(t.structures).find(e.propEq(a,"id"));return n.levels[n.defaultLevelId].ordinal}(c),c.structures=s(c),c.baseVenueId=g(c),c.getTranslatedContentPath=e=>`https://content.locuslabs.com/${c.category}/${e}/${f}/${v}`,c.fetchJson=i,c.fetchText=d,o.config.debug&&o.env.isBrowser&&(window._venueData=c),c.queueTypes&&(c.securityQueueTypes=(()=>{const e=c.queueTypes.find((e=>"SecurityLane"===e.id));return e?e.subtypes.map((e=>e.id)):[]})()),o.bus.send("venueData/venueDataLoaded",{venueData:c}),l.resolve(c),l}function b(e){const t={id:"venueDataLoader"};return e.id!==i.venueId&&(t.vid=e.id),t.lang=o.i18n().language,"prod"!==e.assetStage&&(t.stage=e.assetStage),o.bus.send("deepLinking/notifyState",t),e}o.bus.on("debugTools/fileDrop",(async({file:e,content:t})=>{if("application/json"===e.type){const e=JSON.parse(t);if(e.basemap&&e["basemap.venue"])return n=JSON.parse(t),void o.bus.send("map/replaceTheme",{theme:n});if(e.metadata&&e.metadata["mapbox:type"])return a=t,void o.bus.send("map/replaceStyle",{styleSrc:a})}var a,n}));async function m(e){const t={...e},a=await o.bus.get("poi/getAll");return Object.values(t).forEach((e=>e.filter((e=>"poi"===e.properties.aiLayer&&"Point"===e.geometry.type)).forEach((e=>{const t=a[e.properties.id];t?t.mapLabel?e.properties.text=t.mapLabel:!1!==i.copyPOINamesToMap&&(e.properties.text=function(e){let t=e.name;return i.poiMapNameXForm?(Object.keys(i.poiMapNameXForm).filter((t=>((e,t)=>e===t||0===e.indexOf(t+"."))(e.category,t))).forEach((e=>{i.poiMapNameXForm[e].forEach((e=>t=t.replace(new RegExp(e.replace),e.with)))})),t):t}(t)):d.warn(`Unknown poi in style: ${e.properties.id}`)})))),t}const h=(t,a,n,s)=>{const r=t.replace(/-[^-]*$/,""),u=r===n?"landscape-background":`ordinal: ${s.find(e.hasPath(["levels",t])).levels[t].ordinal}`;return a.map((a=>{var s,o,i;return s="properties",o={venueId:n,structureId:r,ordinalId:u,levelId:t},i=a,a=e.over(e.lensProp(s),(t=>e.mergeRight(t||{},o)),i),a=e.assoc("id",a.properties.subid,a)}))};o.bus.on("venueData/loadMap",(async()=>{l.then((async a=>{const n=await a.fetchText(a.files.style),s=await a.fetchJson(a.files.theme),r=a.files.spritesheet,u=a.files.glyphs,{id:d,bounds:l,structures:p,venueCenter:v,venueRadius:g,defaultOrdinal:f}=a,b=a.venueList[d].mapTokens?{[d]:[]}:await async function(a){return e.pipe(e.prop("structures"),e.map(e.prop("levels")),e.chain(e.keys),e.prepend(a.id),e.map((e=>a.files.geoJson.replace("${geoJsonId}",e))),e.map(a.fetchJson),e.map(e.andThen((e=>[e.id,h(e.id,e.features,a.id,a.structures)]))),(e=>t.all(e)),e.andThen(e.fromPairs))(a)}(a).then(m),y={mapFeatures:b,mapStyleSource:n,mapTheme:s,badgesSpriteUrl:r,mapGlyphsUrl:u,structures:p,defaultOrdinal:f,venueBounds:{n:l.ne.lat,s:l.sw.lat,e:l.ne.lng,w:l.sw.lng},venueId:d,venueCenter:v,venueRadius:g,accountId:i.accountId,secure:void 0!==i.auth,tileServerAuthInfo:a.tileServerAuthInfo};c.resolve(y),o.bus.send("venueData/mapDataLoaded",y)}))}));const y=e=>null==e.shouldDisplay||e.shouldDisplay;o.bus.on("venueData/loadBuildingSelectorData",(()=>l.then((async t=>{const a={buildings:t.structures.filter(y).map(e.evolve({levels:e.pipe(e.values,e.sortWith([e.descend(e.prop("ordinal"))]))})),structureOrder:t.structureOrder,selectorOrder:t.selectorOrder};return o.bus.send("venueData/buildingSelectorDataLoaded",a),a})))),o.bus.on("venueData/normalizeCoords",(({coords:e})=>l.then((t=>a(e,t.bounds)))));const D={edges:[],nodes:[]};o.bus.on("venueData/loadNavGraph",(async()=>l.then((async e=>{const t={navGraphData:e.files.nav?await e.fetchJson(e.files.nav):D,structures:e.structures};return o.bus.send("venueData/navGraphLoaded",t),t})))),o.bus.on("venueData/loadPoiData",(async()=>l.then((async e=>{const t=i.useOldDataModel?e.files.poisOld||e.files.pois:e.files.pois||e.files.poisOld;if(t){const a=await e.fetchJson(t);o.bus.send("venueData/poiDataLoaded",{pois:a,structures:e.structures})}})))),o.bus.on("venueData/getVenueCenter",(async()=>l.then((async e=>({lat:e.venueCenter[0],lng:e.venueCenter[1],ordinal:0}))))),o.bus.on("venueData/getContentUrl",(({type:e,name:t=""})=>l.then((a=>a.files[e]+t)))),o.bus.on("venueData/getFloorIdToNameMap",(()=>l.then(e.pipe(e.prop("structures"),e.map(e.prop("levels")),e.chain(e.values),e.map(e.props(["id","name"])),e.fromPairs)))),o.bus.on("venueData/getFloorIdName",(({floorId:t})=>l.then((async a=>{const n=e.pipe(e.values,e.find(e.hasPath(["levels",t])))(a.structures);return n?{structureId:n.id,structureName:n.name,floorName:n.levels[t].name}:null}))));const I=(t,a)=>()=>l.then(e.pipe(e.prop(t),e.defaultTo(a)));function w(e,t){const a=S(t);if(a.includes(e))return e;const n=e.split("-").slice(1).join("-");return a.find((e=>new RegExp(`${t.baseVenueId}.?.?-${n}`).test(e)))}o.bus.on("venueData/getVenueData",(()=>l)),o.bus.on("venueData/getVenueName",I("name")),o.bus.on("venueData/getVenueCategory",I("category")),o.bus.on("venueData/getVenueTimezone",I("tz")),o.bus.on("venueData/getAccountId",(()=>i.accountId)),o.bus.on("venueData/getVenueId",I("id")),o.bus.on("venueData/getPositioningSupported",I("positioningSupported")),o.bus.on("venueData/getStructures",I("structures")),o.bus.on("venueData/loadNewVenue",(async({venueId:e,accountId:a,assetStage:n=i.assetStage})=>{l.reject(new Error("loadNewVenue called - previous loading ignored")),c.reject(new Error("loadNewVenue called - previous loading ignored")),l=new t,c=new t,f({...i,venueId:e,accountId:a,assetStage:n},[]).then(b)})),o.bus.on("venueData/getPoiImageUrl",(({imageName:e,size:t})=>`https://img.locuslabs.com/resize/${i.accountId}/png/transparent/${t}contain/poi/${e}`)),o.bus.on("venueData/getDistributionId",(()=>l.then((e=>`${e.baseVenueId}-${i.accountId}`)))),o.bus.on("venueData/getCustomKeywords",(()=>l.then((e=>{const t=i.useOldDataModel&&e.files.searchOld||e.files.search;return e.fetchJson(t)})))),o.bus.on("venueData/isGrabEnabled",I("enableGrab")),o.bus.on("venueData/getGrabPoiIds",I("grabPoiIds",[])),o.bus.on("venueData/getAssetsTimestamp",I("version")),o.bus.on("venueData/getTranslatedFloorId",(async({floorId:e})=>l.then((t=>w(e,t)))));const S=e=>e.structures.reduce(((e,t)=>e.concat(Object.keys(t.levels))),[]);o.bus.on("venueData/getQueueTypes",(()=>l.then((e=>{const t=["tsapre","clear","globalEntry"];return e.queueTypes?e.queueTypes.reduce(((e,a)=>{const{id:n,subtypes:s}=a,r=s.map((e=>{const a=t.includes(e.id)&&`security-logo-${e.id.toLowerCase()}`;return{...e,imageId:a}}));return e[n]=r,e}),{}):{}}))));return{init:async()=>{let e=[];null===new URLSearchParams("undefined"==typeof window?"":window.location.search).get("lang")&&(e="undefined"==typeof window?[]:navigator.languages);const t=i.deepLinkProps||{};t.lang&&e.unshift(t.lang);const a=t.vid||i.venueId,n=i.useDynamicUrlParams&&t.stage?t.stage:i.assetStage,s=t.accountId||("alpha"===n?"A1VPTJKREFJWX5":i.accountId);f({...i,venueId:a,accountId:s,assetStage:n},e).then(b)},runTest:async({testRoutine:e,reset:a=!1,venueData:n=null})=>{let s,r;return(a||n)&&(l=new t,c=new t),n&&(l=t.resolve(n)),await e(),l.v&&(s=await l),c.v&&(r=await c),{venueDataObj:s,mapDataObj:r}},internal:{calculateBaseVenueId:g,getDefaultStructureId:p,getTranslatedFloorId:w,setConfigProperty:(e,t)=>{i[e]=t}}}}export{o as create};