UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) 12.8 kB
import{createBrowserHistory as oe}from"history";import{autorun as O,reaction as k,runInAction as A}from"mobx";import{recoverKioskFromUrlPrams as se}from"../components/Kiosk/recoverKioskFromUrlPrams";import{BUILD_ROUTE_KEY as re,KIOSK_ENABLE_KEY as F,LANG_KEY as N,LANGUAGE_TYPE as E,MAP_SETTINGS_KEY as ae,PATHWAY_KEY as $,PLANNER_FROM_KEY as Y,PLANNER_KEY as P,PLANNER_SOURCE_KEY as K,PREVIEW_MODE_QUERY as I,PREVIEW_MODE_STORAGE_KEY as W,SEPARATOR as R,UI_SCALE_PARAM_KEY as V,UI_SCALE_RESET_PARAM_KEY as H,VIEWER_MODE_QUERY as x,VIEWER_MODE_STORAGE_KEY as G}from"../constants";import ie from"../core/Rect";import v from"../data";import o,{uiState as n}from"../store";import{BoothBase as D}from"../store/BoothStore";import{CurrentPosition as ne,extractRoute as le,findBooth as ce}from"../store/RouteStore";import{setConsentSettings as z}from"../tools/gtag";import U from"../tools/logger";import{areLayersEnabled as ue}from"../utils/areLayersEnabled";import{handleCustomCommand as he}from"../utils/customCommands";import{getMostFrequent as fe}from"../utils/getMostFrequent";import{hasLocale as de,setLocale as me}from"../utils/i18n";import{isLocalStorageAvailable as L}from"../utils/localStorage";import{stripTrackingParams as pe}from"../utils/trackingParams";let m=!1,C=!1,j=null,q=null,Q=null,J;const h=oe(),B=window.location.pathname,w=[];export function getLocationHistory(){return w}function b(e){return B+e}function ge(e){m||h.push(b(e))}export function historyReplace(e){m||h.push(b(e))}function d(e){return e.replace(/=&/g,"&").replace(/=$/,"")}function X(){let e="";const l=n.selectedExhibitor,i=n.selectedBooth,s=n.selectedEventItem,t=n.selectedRoute,r=n.selectedSpeaker,a=n.setKioskModeEnabled;if(n.list.type==="route-planner"&&!l&&!i&&!s){const u=o.routePlannerStore;if(u.source==="bookmarks")e=`${P}&${K}=bookmarks`;else{const g=u.items.map(S=>S.entity.slug).filter(Boolean).join(R);e=`${P}=${g}`}const p=u.startPoint?.slug;p&&(e+=`&${Y}=${p}`)}else if(t&&!n.isMultiPointRoute){const u=t.from instanceof D?`:${t.from.slug}`:"",p=t.to instanceof D?`:${t.to.slug}`:"",g=o.routeStore.onlyAccessible?":true":":false",S=t.waypoints?.map(f=>`:${f.slug}`).join("");e=`route${p}${u}${g}${S||""}`}else if(l)e=l.slug;else if(i)e=i.slug;else if(s)e=s.slug;else if(r)e=r.slug;else if(a)e="";else switch(n.list.type){case"bookmarks":e="bookmarks";break;case"visited":e="visited";break;case"category":e=n.selectedCategory?.slug??"";break;case"search":e=n.list.text||"";break;case E:e=E;break;case"filter":e=`${n.list.query.key}=${n.list.query.value}`;break;case"sessions":e="sessions";break;case"exhibitors":e="exhibitors";break;case"speakers":e="speakers";break;case"tours":e="tours";break;case"multi-point-tour":e=`tour=${n.list.tourId}`;break;default:throw new Error("Unkown list.type")}if(C)return;const c=e?"?"+encodeURIComponent(e):"";h.location.search!==c&&(l!==j||i!==q||s!==Q?ge(c):historyReplace(c),j=l,q=i,Q=s)}function Z(){const e=n.selectedExhibitor,l=n.selectedEventItem,i=n.selectedSpeaker;let s="";e?s=e.name:l?s=l.name:i?s=i.name:n.list.type==="search"&&n.list.text?s="`"+(n.list.text||"")+"`":n.list.type==="multi-point-tour"&&n.tourTitle&&(s=n.tourTitle),s.length&&(s+=" \u2013 "),s+=v.title,v.subtitle&&(s+=" \u2013 "+v.subtitle),s+=" \u2013 Expo Floor Plan by ExpoFP",document.title=s}function T(){const e=h.location.search.length>1?decodeURIComponent(h.location.search.substring(1)):"";return he(e)}function y(){const e=h.location.search.length>1?decodeURIComponent(h.location.search.substring(1)):"";C=!0;const l=o.boothStore.booths.find(s=>s.slug?.toLowerCase()===e?.toLowerCase()||s.externalId?.toLowerCase()===e?.toLowerCase()||s.externalId?.toLowerCase()?.replace(/\s+/g,"")===e?.toLowerCase()),i=new URLSearchParams(decodeURIComponent(window.location.search));if(se(i),Se(i),!T())if(i.has(F)){const s=i.get(F);s==="1"?n.kiosk=!0:s==="0"&&(n.kiosk=!1,n.resetUIScale())}else if(e==="tours")o.selectTours();else if(e.startsWith("tour=")){const s=e.split("=")[1],t=o.toursStore.getTourById(s);t?o.toursStore.launchTour(t,t.name,t.description):(console.warn(`Route with id "${s}" not found`),o.selectTours())}else if(i.has(P)){const s=i.get(K),t=i.get(Y),r=t?ce(t):null;if(s==="bookmarks")o.routePlannerStore.launchFromBookmarks(r??void 0);else{const a=(i.get(P)||"").split(R).filter(Boolean);o.routePlannerStore.launchFromSlugs(a,r??void 0)}}else if(e.startsWith("route")){const s=e.split(R);o.routeStore.setOnlyAccessible(s[3]==="true");const t=le(s[2],s[1],s.slice(4));o.routeStore.selectRoute(t);const a=s.slice(4).length>0;o.uiState.setIsMultiPointRoute(a);const u=new URLSearchParams(window.location.search).get("title");u&&o.uiState.setRouteTitle(u)}else if(e==="bookmarks")o.selectBookmarks();else if(e==="visited")o.selectVisited();else if(e===E)o.selectLanguage();else if(i.has(N)){const s=i.get(N)?.toLowerCase();de(s)&&me(s)}else if(e==="sessions")o.selectSessions();else if(e==="exhibitors")o.selectExhibitors();else if(e==="speakers")o.selectSpeakers();else if(e==="-pdf")o.uiState.printingPdf=!0;else if(e.startsWith("hide"))o.uiState.setVisibility((new URLSearchParams(e).get("hide")??"").split(",").filter(Boolean).reduce((s,t)=>({...s,[t]:!1}),{}));else if(l)setTimeout(()=>o.selectBooth(l),250);else if(e===re)o.uiState.startBuildRoute();else if(i.get($)){const s=i.get($),t=i.get("booths")?.split(",")||[],r=i.get("exhibitors")?.split(",")||[],a=i.get("tour");if(a){const f=o.toursStore.getTourById(a);f&&A(()=>{o.uiState.setTourTitle(f.name),o.uiState.setTourDescription(f.description??""),o.uiState.setIsMultiPointTour(!0),o.uiState.setMultiPointTourList(f.id)})}const c=o.boothStore.booths,u=o.exhibitorStore.exhibitors,p=c.filter(f=>t.includes(f.externalId)),g=u.filter(f=>r.includes(f.externalId)).flatMap(f=>f.booths),S=[...p,...g];if(ue()){const f=S.map(te=>te.layer.name),M=fe(f);M&&o.fp.activateFloor({name:M})}(t.length>0||r.length>0)&&o.fp.highlightEntities([...t,...r]),o.layerStore.showPathWay(s),S.length>0&&A(()=>{o.uiState.moveToRect=ie.fromMultiple(S.map(f=>f.rect).filter(Boolean)),o.uiState.rollReset=!0})}else{const s=o.eventStore.eventItems.find(t=>t.slug?.toLowerCase()===e?.toLowerCase()||t.externalId?.toLowerCase()===e?.toLowerCase());if(s)o.selectEventItem(s,!0);else{const t=o.exhibitorStore.exhibitors.find(r=>r.slug?.toLowerCase()===e?.toLowerCase()||r.externalId?.toLowerCase()===e?.toLowerCase());if(e.startsWith("exhibitors")){const r=e.split("=")[1].split(",");setTimeout(()=>o.fp.selectExhibitor(r),250)}else if(t)setTimeout(()=>o.clickExhibitor(t),250);else if(e.includes("=")&&(e.startsWith("categories=")||/^poiTypes?=/.test(e)))U.log("Skipping filter parameter in dispatchFromUrl:",e);else{const a=o.categoryStore.categories.find(c=>c.slug===e);if(a)o.selectCategory(a);else{const c=o.speakerStore.findBySlug(e);c?o.selectSpeaker(c,!0):e.includes("heatmap=true")||o.searchStore.selectSearch(e)}}}}C=!1,X(),Z()}function _(){const e=h.location.search;if(e.includes("heatmap")){const t=new URL(window.location.href);if(t.searchParams.get("heatmap")==="true"){const a=t.searchParams.get("type")==="yah";a||t.searchParams.delete("heatmap"),m=!0,historyReplace(d(t.search)),a?(o.uiState.heatmapYah=!0,o.uiState.monochrome=!0,o.uiState.hideLogoInBooth=!0,o.uiState.hideHeaderLogo=!0,o.uiState.disableBookmarked=!0):o.uiState.heatmap=!0}}if(e.startsWith("?preview="))historyReplace("?");else if(e.startsWith("?b="))historyReplace("?bookmarks");else if(e.startsWith("?ba=")){const t=new URL(window.location.href),r=parseInt(t.searchParams.get("ba")),a=o.exhibitorStore.exhibitorById.get(r);historyReplace(a?"?"+a.slug:"?bookmarks")}if(e.includes("blue-dot")){const t=new URL(window.location.href),r=t.searchParams.get("blue-dot");t.searchParams.delete("blue-dot");const a=r?.split(",")??[],c=Number(a[0]),u=Number(a[1]);if(a.length>1&&Number.isFinite(c)&&Number.isFinite(u)){const g=o.layerStore.findLayer(a[2])?.name,S=new ne(c,u,g,void 0,Number(a[3])||void 0,Number(a[4])||void 0);o.layerStore.layersLoaded?o.routeStore.selectCurrentPosition(S,!0,1):k(()=>o.layerStore.layersLoaded,()=>{o.routeStore.selectCurrentPosition(S,!0,1)})}const p=d(t.search);historyReplace(p)}if(e.includes("copy_exh")&&historyReplace("?"),e.includes("noOverlay")){const t=new URL(window.location.href),r=t.searchParams.get("noOverlay");t.searchParams.delete("noOverlay");const a=d(t.search);r==="true"?o.uiState.hideOverlay=!0:r==="false"&&(o.uiState.hideOverlay=!1),historyReplace(a)}if(e.includes("allowConsent")){const t=new URL(window.location.href),r=t.searchParams.get("allowConsent");t.searchParams.delete("allowConsent");const a=d(t.search);r==="true"?z(!0):r==="false"&&z(!1),historyReplace(a)}if(e.includes("hideHeaderLogo")){const t=new URL(window.location.href),r=t.searchParams.get("hideHeaderLogo");t.searchParams.delete("hideHeaderLogo");const a=d(t.search);r==="true"&&(n.hideHeaderLogo=!0),historyReplace(a)}if(e.includes("hideLogoInBooth")){const t=new URL(window.location.href),r=t.searchParams.get("hideLogoInBooth");t.searchParams.delete("hideLogoInBooth");const a=d(t.search);r==="true"&&(n.hideLogoInBooth=!0),historyReplace(a)}if(e.includes("disableFeatured")){const t=new URL(window.location.href),r=t.searchParams.get("disableFeatured");t.searchParams.delete("disableFeatured");const a=d(t.search);r==="true"&&o.exhibitorStore.exhibitors.forEach(c=>c.featured=!1),historyReplace(a)}if(e.includes("disableBookmarked")){const t=new URL(window.location.href),r=t.searchParams.get("disableBookmarked");t.searchParams.delete("disableBookmarked");const a=d(t.search);r==="true"&&(n.disableBookmarked=!0),historyReplace(a)}if(e.includes("disableGps")){const t=new URL(window.location.href),r=t.searchParams.get("disableGps");t.searchParams.delete("disableGps");const a=d(t.search);r==="true"&&o.routeStore.setGpsTrackingEnabled(!1),historyReplace(a)}if(e.includes("monochrome")){const t=new URL(window.location.href),r=t.searchParams.get("monochrome");t.searchParams.delete("monochrome");const a=d(t.search);r==="true"&&(n.monochrome=!0),historyReplace(a)}const l=pe(h.location.search);if(l!==null&&historyReplace("?"+l),e.includes(x)){const t=new URL(window.location.href),r=t.searchParams.get(x);r==="1"||r==="true"?L&&localStorage.setItem(G,"1"):(r==="0"||r==="false")&&L&&localStorage.removeItem(G),t.searchParams.delete(x),historyReplace(d(t.search))}if(e.includes(I)){const t=new URL(window.location.href),r=t.searchParams.get(I);r==="true"?L&&localStorage.setItem(W,"1"):r==="false"&&L&&localStorage.removeItem(W),t.searchParams.delete(I),historyReplace(d(t.search))}if(n.previewExhibitor&&historyReplace("?"+n.previewExhibitor.slug),h.location.search.includes("&")||h.location.search.includes("%26")){let t=h.location.search;t=t.startsWith("?")?t.slice(1):t;const r=t.includes("&")?"&":"%26",c=t.split(r).map(u=>{const p=o.exhibitorStore.exhibitors.find(g=>g.slug===u||g.externalId===u);return p?p.name:u}).join("&");historyReplace("?"+c)}const i=new URLSearchParams(window.location.search);if(i.has(H)){o.uiState.resetUIScale();const t=new URL(window.location.href);t.searchParams.delete(H);const r=d(t.search);m=!0,historyReplace(r),m=!1}const s=i.get(V);if(s){const t=s.replace(",","."),r=parseFloat(t);!isNaN(r)&&r>=.8&&r<=1.4?o.uiState.setUIScale(r,!0):U.warn(`Invalid ui-scale parameter: ${s}. Must be a number between 0.8 and 1.4.`);const a=new URL(window.location.href);a.searchParams.delete(V);const c=d(a.search);m=!0,historyReplace(c),m=!1}}export function initRouting(e=!1){m=e,!m&&b(h.location.search)===B&&w.push(b(h.location.search)),J=h.listen((l,i)=>{m||(w.push(b(l.search)),U.log("history",i,l),i==="POP"&&y())}),_(),T(),k(()=>o.layerStore.layersLoaded,()=>{o.urlFilterManager&&!o.urlFilterManager.isInitialized&&o.urlFilterManager.initialize(),y(),O(Z),O(X),n.viewerMode&&(n.menu=!0)})}export function applyParameters(e=""){historyReplace("?"+decodeURIComponent(e.toString())),o.layerStore.layersLoaded?(_(),y()):(T(),k(()=>o.layerStore.layersLoaded,()=>{_(),y()}))}export function destroyHistory(){J()}export function goBack(){if(console.log("goBack"),w.length>1){w.pop();const l=w[w.length-1].replace(B,"");return m=!0,h.replace(l),m=!1,y(),!0}else return h.go(-1),!1}function Se(e){try{let l={};const i=n.defaultMapSettings;if(new Set(e.keys()).size)l=ee(Object.fromEntries(e.entries()),i);else{const s=sessionStorage.getItem(ae);s&&(l=ee(JSON.parse(s),i))}if(n.setMapSettings(l),l.z){const s=o.layerStore.layers.find(t=>t.name===l.z);s&&o.layerStore.updateVisibility(s,!0)}}catch(l){console.error("Failed to process or save map settings.",l)}}function ee(e,l){const i={};for(const s in e){if(!Object.prototype.hasOwnProperty.call(l,s)||!Object.prototype.hasOwnProperty.call(e,s))continue;const t=e[s];typeof l[s]=="number"?i[s]=parseFloat(t):i[s]=t}return i}