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