UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) 2.35 kB
import{Box as h}from"@expofp/geometry";import*as a from"d3-selection";import{STRING_META_DELIMITER as x}from"../constants";import M from"../tools/logger";import E from"../tools/settings";import{extractMetaFromString as b}from"../utils/extractMetaFromString";import _ from".";import{getFpPaths as A,getFpSvg as c,getFpSvgJs as P,getWfData as V}from"./fp-payloads";const n=new Map;function p(e,r=""){if(n.has(r))return n.get(r);const s=new DOMParser().parseFromString(e,"image/svg+xml").documentElement;s.firstChild.tagName==="parsererror"&&M.error("Parsed svg with error: ",l);const i=new Map;return a.select(s).selectAll("style").each(function(){const t=this.textContent,S=/\.([a-z0-9.]+)\s*{[^}]*fill\s*:\s*([^};]+);[^}]*}/gi;let v;for(;(v=S.exec(t))!==null;){const B=v[1],F=v[2];i.set(B,F)}}),a.select(s).selectAll("*[class]").each(function(){const t=this;t.style.fill=i.get(t.className.baseVal)}),N(s),n.set(r,s),s}let l=p(c());const m=P().__viewbox,d=a.select(l).select("rect#VIEWBOX").node(),u=l.viewBox.baseVal,g=new h(u.x,u.y,u.width,u.height);let f,w=!1;m?(f=new h(m.x,m.y,m.width,m.height),w=!0,d?.remove()):d?(f=h.fromSvg(d),w=!0,d.remove()):f=g.scale(1.1),a.select(l).attr("width",g.size.width),a.select(l).attr("height",g.size.height),M.log("svgArea",f,"svgViewBox",g),E.wayfinding=!!(!_.hideDirections&&V());let I=P().__fpLayersMode?a.select(l).selectAll("[data-floor]").nodes().map(e=>(e.remove(),{name:e.dataset.floor,rect:h.fromSvg(e)})).sort():[];export{I as floors,f as svgArea,w as svgAreaExplicit,g as svgViewBox};export function getTrianglesFromFpPaths(e,r){const o=gtePathByIndex(e,r),s=o.positions.map(t=>[t[0],Math.abs(t[1])]),i=[];for(const t of o.cells)i.push([s[t[0]],s[t[1]],s[t[2]]]);return i}export let gtePathByIndex=(e,r="")=>(A(r)??A())[e],getLayerSvg=(e="")=>typeof e=="string"?n.has(e)?n.get(e):c(e)?p(c(e),e):n.get(""):e.rootParent?n.has(e.rootParent.name)?n.get(e.rootParent.name):c(e.rootParent.name)?p(c(e.rootParent.name),e.rootParent.name):n.get(""):c(e.name)?p(c(e.name),e.name):n.get("");function N(e){a.select(e).selectAll("[id], [data-name]").each(function(){const r=this;let o={};const s=r.getAttribute("data-name");if(s&&s.includes(x)){const{meta:t}=b(s);o={...o,...t}}const i=r.getAttribute("id");if(i&&i.includes(x)){const{meta:t}=b(i);o={...o,...t}}r.setAttribute("data-meta",JSON.stringify(o))})}