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) 3.23 kB
import*as e from"ramda";import r from"zousan";import{getLocalized as a}from"../../../src/utils/configUtils.js";import{randomizeArray as s,arrayPick as t}from"../../../src/utils/rand.js";import n from"./poiSearch.js";import o from"./searchTypeahead.js";function i(i,c){const u={poiSearch:null,typeahead:null,indexesCreated:new r,defaultSearchTerms:null,specialQueryTerms:{}},d=async()=>{const e=await i.bus.get("poi/getAll");u.poiSearch=n(e,i.i18n().language),u.typeahead=o(e,u.poiSearch.search,i.i18n().language),u.defaultSearchTerms=a(c,"defaultSearchTerms",i.i18n().language),u.indexesCreated.resolve()};async function y(){const r=await i.bus.getFirst("user/getPhysicalLocation");if(!r?.floorId)return[];const a=await i.bus.get("poi/getByFloorId",{floorId:r?.floorId}),s=Object.values(e.pickBy((e=>-1===e.category.indexOf("portal")&&"element.door"!==e.category),a)),t=await i.bus.get("wayfinder/addPathTimeMultiple",{pois:s,startLocation:r});return e.sortBy(e.prop("distance"),Object.values(t)).slice(0,50)}i.bus.on("search/queryNearby",(async()=>{const e=await y();return i.bus.send("search/showNearby",{pois:e,term:"Nearby"}),e})),i.bus.on("search/queryNearbyAsync",y),i.bus.on("search/queryCategory",(async({category:e,categoryName:r,searchTerm:a})=>{const s=await u.indexesCreated.then((()=>u.poiSearch.search({query:a||e})));return i.bus.send("search/showCategory",{pois:s,category:e,categoryName:r}),s})),i.bus.on("search/query",(({term:e})=>u.indexesCreated.then((()=>{const r=u.poiSearch.search({query:e});return i.bus.send("search/showSearchResults",{results:r,term:e}),r})))),i.bus.on("search/queryAsync",(({term:e})=>u.indexesCreated.then((()=>u.poiSearch.search({query:e}))))),i.bus.on("search/queryWithSpecial",(({term:e})=>{if(u.specialQueryTerms[e]){const{event:r,params:a}=u.specialQueryTerms[e];return i.bus.send(r,a)}return i.bus.get("search/query",{term:e})})),i.bus.on("search/getDefaultSearchTerms",(async({limit:e=5}={})=>{const r=u.defaultSearchTerms,a=r&&r.length?r:await async function(e){const r=(await i.bus.send("poi/getAllCategories"))[0],a=Array.from(new Set(r));return s(a).slice(0,e)}(e);return i.bus.send("search/showDefaultSearchKeywords",{keywords:a}),a})),i.bus.on("search/getDefaultSearchPois",(async({limit:r=5}={})=>{const a=await i.bus.get("poi/getAll"),s=e.pickBy(((e,r)=>e.isNavigable),a);return t(Object.values(s),r)})),i.bus.on("search/registerSpecialQuery",(({term:e,event:r,params:a,addKeyword:s=!0})=>{u.indexesCreated.then((()=>{s&&u.typeahead.addKeyword(e),u.specialQueryTerms[e]={event:r,params:a}}))})),i.bus.on("search/addKeywords",(({keywords:e})=>u.indexesCreated.then((()=>e.forEach((e=>u.typeahead.addKeyword(e))))))),i.bus.on("search/typeahead",(({term:e,limit:r})=>u.indexesCreated.then((()=>{const{keywords:a,pois:s}=u.typeahead.query(e,r);return{keywords:a,pois:s,term:e}})))),i.bus.on("venueData/loadNewVenue",(()=>{u.indexesCreated=new r,d()})),i.bus.on("poi/setDynamicData",(async({plugin:e,idValuesMap:r})=>{if("grab"!==e)return;const a=Object.keys(r).map((e=>i.bus.get("poi/getById",{id:e})));return Promise.all(a).then((e=>u.indexesCreated.then((()=>u.poiSearch.updateMultiple(e)))))}));return{init:d,runTest:async(e,r)=>(await r(),u)}}export{i as create};