UNPKG

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) 4.1 kB
import{throttle as e}from"throttle-debounce";import{getStructureAndFloorAtPoint as t}from"../../../src/utils/geom.js";import o from"../../../src/utils/observable.js";import{headlessCommands as n,handleHeadless as a}from"./sdkHeadless.js";let s=null;function r(e){const t=(e,t)=>{const o={payload:e,type:"LL-server"};t&&(o.clientMsgId=t);try{window.postMessage(o,"*")}catch(e){window.postMessage((e=>JSON.parse(JSON.stringify(e)))(o),"*")}};function o(o){const n=o.data;n&&"LL-client"===n.type&&e.bus.get("clientAPI/execute",n.payload).then((e=>t(e,n.msgId))).catch((t=>{e.config.debug&&console.error(t),((e,t)=>{const o={error:!0,payload:e,type:"LL-server"};t&&(o.clientMsgId=t),window.postMessage(o,"*")})(t.message,n.msgId)}))}return s&&s(),window.addEventListener("message",o),s=()=>window.removeEventListener("message",o),(e,t)=>{const o={event:e,payload:t,type:"LL-server"};window.postMessage(o,"*")}}async function i(s,i){const l=s.env.isBrowser?r(s):function(e){const t=o();return e.eventListener=t,(e,o)=>t.fire(e,o)}(s);return function(o,n){o.bus.monitor("map/userMoveStart",(async({pitch:e,zoom:t,bearing:a})=>{const{lat:s,lng:r,floorId:i,ordinal:l,structureId:d}=await o.bus.get("map/getMapCenter");n("userMoveStart",{lat:s,lng:r,floorId:i,ord:l,structureId:d,pitch:e,zoom:t,bearing:a})})),o.bus.monitor("map/userMoving",e(500,(async({pitch:e,zoom:t,bearing:a})=>{const{lat:s,lng:r,floorId:i,ordinal:l,structureId:d}=await o.bus.get("map/getMapCenter");n("userMoving",{lat:s,lng:r,floorId:i,ord:l,structureId:d,pitch:e,zoom:t,bearing:a})}))),o.bus.monitor("map/moveEnd",(async({pitch:e,zoom:t,bearing:a})=>{const{lat:s,lng:r,floorId:i,ordinal:l,structureId:d}=await o.bus.get("map/getMapCenter");n("moveEnd",{lat:s,lng:r,floorId:i,ord:l,structureId:d,pitch:e,zoom:t,bearing:a})})),o.bus.monitor("map/floorChanged",(({structure:e,floor:t})=>n("levelChange",{floorId:t?t.id:null,floorName:t?t.name:null,ord:t?t.ordinal:null,structureId:e?e.id:null,structureName:e?e.name:null}))),o.bus.monitor("map/poiClicked",(({poi:e})=>n("poiSelected",e))),o.bus.monitor("poiDetails/showPoi",(({poi:e})=>n("poiShown",e))),o.bus.monitor("map/click",(async({lat:e,lng:a,ord:s})=>{const r=await o.bus.get("venueData/getStructures"),i=await o.bus.get("map/getViewBBox"),{building:l,floor:d}=t(r,e,a,s,i,!0);n("mapClicked",{lat:e,lng:a,ord:s,building:l,floor:d})}))}(s,l),{init:async()=>{!function(e){e.bus.send("clientAPI/registerCustomType",{name:"latLngOrdLocation",spec:{type:"object",props:[{name:"lat",type:"float"},{name:"lng",type:"float"},{name:"ord",type:"integer"}]}}),e.bus.send("clientAPI/registerCustomType",{name:"latLngFloorLocation",spec:{type:"object",props:[{name:"lat",type:"float"},{name:"lng",type:"float"},{name:"floorId",type:"string"}]}}),e.bus.send("clientAPI/registerCustomType",{name:"poiIdLocation",spec:{type:"object",props:[{name:"poiId",type:"integer",min:0}]}}),e.bus.send("clientAPI/registerCustomType",{name:"location",spec:{type:"multi",types:[{type:"poiIdLocation"},{type:"latLngOrdLocation"},{type:"latLngFloorLocation"}]}}),e.bus.send("clientAPI/registerCustomType",{name:"viewSettings",spec:{type:"object",props:[{name:"zoom",type:"float",optional:!0},{name:"pitch",type:"float",optional:!0},{name:"bearing",type:"float",optional:!0}]}})}(s),n.forEach((e=>s.bus.send("clientAPI/registerCommand",e))),a(s),i.headless||await import("../../../_virtual/_empty_module_placeholder.js").then((e=>{e.visualCommands.forEach((e=>s.bus.send("clientAPI/registerCommand",e))),e.handleVisual(s,l)}));const e=async()=>{await s.bus.send("system/readywhenyouare"),s.bus.get("clientAPI/execute",{command:"getCommandJSON"}).then((e=>l("ready",{commandJSON:e}))),!i.headless&&s.config.uiHide&&s.config.uiHide.sidebar&&s.env.isDesktop()&&s.bus.send("map/changePadding",{padding:{left:55,right:55,top:72,bottom:22}})};i.headless?Promise.all([new Promise((e=>s.bus.monitor("venueData/navGraphLoaded",e))),new Promise((e=>s.bus.monitor("venueData/poiDataLoaded",e)))]).then(e):s.bus.on("map/mapReadyToShow",e),s.bus.on("sdkServer/sendEvent",(({eventName:e,...t})=>l(e,t)))}}}export{i as create};