@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 5.8 kB
JavaScript
import{__decorate as u}from"tslib";import{action as a,computed as c,observable as y}from"mobx";import{t as m}from"../utils/i18n";import{findBooth as b,Route as g}from"./RouteStore";export default class n{constructor(t){this.items=[],this.startPoint=null,this.source=null,this.excludedVisitedCount=0,this.rootStore=t}get resolvedBooths(){const t=new Map;return this.startPoint&&t.set(this.startPoint.id,this.startPoint),this.items.forEach(o=>{o.booth&&t.set(o.booth.id,o.booth)}),Array.from(t.values())}get stopsCount(){return this.items.length}get startPointBoothOptions(){return this.rootStore.boothStore.booths}get startPointAutocompleteOptions(){const{exhibitorStore:t}=this.rootStore,o=this.startPointBoothOptions,r=[],i=new Set;return t.exhibitors.forEach(e=>{e.booths.forEach(l=>{i.add(l.id),r.push({value:l.id.toString(),label:`${e.name} - ${l.name}`,exhibitorId:e.id})})}),o.filter(e=>!i.has(e.id)).forEach(e=>{r.push({value:e.id.toString(),label:e.name})}),r.sort((e,l)=>e.label.localeCompare(l.label,void 0,{sensitivity:"base"}))}get title(){return this.source==="bookmarks"?m("Bookmarks route"):""}get subtitleParts(){const{routeInfo:t}=this.rootStore.routeStore;let o;if(t?.time){const r=t.time;o=r<60?m("< 1 min"):m("~ {{n}} min",{n:Math.round(r/60)})}return{stops:m("{{count}} stops",{count:this.stopsCount}),from:this.startPoint?m("From {{name}}",{name:this.startPoint.name}):void 0,distance:t?t.distance:void 0,travelTime:o}}get routeFloors(){const t=this.rootStore.routeStore.pathLayers;return t.map((o,r)=>{const i=t[r+1],s=o.layer?.basePriority??0,e=i?.layer?.basePriority??0,l=i?e<s?"up":"down":void 0;return{id:o.id,name:o.layer?.shortName,description:o.layer?.description,directionToNext:l}})}get currentRouteFloor(){const t=this.routeFloors,o=this.rootStore.routeStore.currentRouteLayer;if(!t.length)return;if(!o)return t[0]?{id:t[0].id,name:t[0].name}:void 0;const r=t.find(i=>i.description?.toLowerCase()===o.description?.toLowerCase());return r?{id:r.id,name:r.name}:{id:t[0].id,name:t[0].name}}setRouteFloor(t){const o=this.rootStore.routeStore.pathLayers.find(r=>r.id===t.id);o?.layer&&this.rootStore.layerStore.updateVisibility(o.layer,!0)}launch(t,o="custom",r){this.excludedVisitedCount=t.filter(s=>s.type==="exhibitor"&&s.entity.visited).length,this.items=t.filter(s=>s.type==="exhibitor"?!s.entity.visited:!0),this.source=o,this.startPoint=r!==void 0?r:this.detectStartPoint();const{uiState:i}=this.rootStore;i.list={type:"route-planner",source:o},i.details=null,i.menu=!1,this.buildRoute()}launchFromBookmarks(t){const{exhibitorStore:o,eventStore:r,boothStore:i}=this.rootStore,s=[];o.bookmarked.forEach(e=>{s.push({type:"exhibitor",entity:e,booth:e.booths[0]||null})}),i.booths.filter(e=>e.exhibitors.length===0&&e.bookmarkedDirectly).forEach(e=>{s.push({type:"booth",entity:e,booth:e})}),this.launch(s,"bookmarks",t)}launchFromSlugs(t,o){const r=this.resolveSlugs(t);this.launch(r,"custom",o)}resolveSlugs(t){const{exhibitorStore:o,eventStore:r,boothStore:i}=this.rootStore;return t.filter(Boolean).map(s=>{const e=s.toLowerCase(),l=o.exhibitors.find(d=>d.slug?.toLowerCase()===e||d.externalId?.toLowerCase()===e);if(l)return{type:"exhibitor",entity:l,booth:l.booths[0]||null};const p=b(s);return p?{type:"booth",entity:p,booth:p}:null}).filter(s=>s!==null)}buildRoute(){const t=[];if(this.startPoint&&this.startPoint.name&&t.push(this.startPoint.name),this.items.forEach(h=>{h.booth&&h.booth.name&&t.push(h.booth.name)}),t.length<2){this.rootStore.routeStore.updateRoute([]);return}let o;try{const h=this.rootStore.fp?.getOptimizedRoutes?.(t);o=Array.isArray(h)?h.flatMap(S=>S?.waypoints??[]):[]}catch{o=[]}const r=new Set(t),i=new Set(o),s=t.length>0&&[...r].every(h=>i.has(h));(!o.length||!s)&&(o=t.slice()),this.reorderItemsByNames(o);const e=this.resolvedBooths;if(e.length<2){this.rootStore.routeStore.updateRoute([]);return}const l=e[0],p=e[e.length-1],d=e.slice(1,-1),f=new g(l,p,d);this.rootStore.routeStore.selectRoute(f,!1),this.rootStore.uiState.setIsMultiPointRoute(d.length>0)}reorderItemsByNames(t){const o=this.startPoint?this.startPoint.name:null,r=new Map;this.items.forEach(s=>{const e=s.booth?s.booth.name:null;e&&(r.has(e)||r.set(e,[]),r.get(e).push(s))});const i=[];t.forEach(s=>{if(s!==o){const e=r.get(s);e&&e.length>0&&i.push(...e.splice(0))}}),this.items=i}detectStartPoint(){const{boothStore:t,poiTypeStore:o}=this.rootStore,r=["entrance","entrances"],i=o.poiTypes.find(s=>r.includes(s.name.toLowerCase()));return i&&t.booths.find(s=>s.poiTypeId===i.id)||null}setStartPoint(t){this.startPoint=t,this.buildRoute()}removeItem(t){this.items.splice(t,1),this.buildRoute()}reorderItems(t,o){const[r]=this.items.splice(t,1);this.items.splice(o,0,r),this.buildRoute()}close(){this.items=[],this.startPoint=null,this.source=null,this.excludedVisitedCount=0,this.rootStore.uiState.details=null,this.rootStore.uiState.list={type:"search",text:"",focused:!1},this.rootStore.routeStore.selectRoute(null,!1)}}u([y],n.prototype,"items",void 0),u([y],n.prototype,"startPoint",void 0),u([y],n.prototype,"source",void 0),u([y],n.prototype,"excludedVisitedCount",void 0),u([c],n.prototype,"resolvedBooths",null),u([c],n.prototype,"stopsCount",null),u([c],n.prototype,"startPointBoothOptions",null),u([c],n.prototype,"startPointAutocompleteOptions",null),u([c],n.prototype,"title",null),u([c],n.prototype,"subtitleParts",null),u([c],n.prototype,"routeFloors",null),u([c],n.prototype,"currentRouteFloor",null),u([a],n.prototype,"setRouteFloor",null),u([a],n.prototype,"launch",null),u([a],n.prototype,"launchFromBookmarks",null),u([a],n.prototype,"launchFromSlugs",null),u([a],n.prototype,"buildRoute",null),u([a],n.prototype,"setStartPoint",null),u([a],n.prototype,"removeItem",null),u([a],n.prototype,"reorderItems",null),u([a],n.prototype,"close",null);