@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 7.83 kB
JavaScript
import{Rect as E}from"@expofp/geometry";import{reaction as W}from"mobx";import{FONT_WEIGHT_PRIMARY_BOOTH as N,FONT_WEIGHT_PRIMARY_EXHIBITOR as U,FONT_WEIGHT_SECONDARY_BOOTH as X,FONT_WEIGHT_SECONDARY_EXHIBITOR as V}from"../../../../constants";import p from"../../../../data";import{createShapeDot as Z,createTextDef as G,createTextDefLines as Y,DEFAULT_FONT_LIGHT_URL as $,DEFAULT_FONT_URL as B,getRollValue as k,getRotation as q,updateLinesScale as j,updateRectRotation as J,updateTextDefRotation as K}from"../../../../renderer";import{layersStore as M,uiState as u}from"../../../../store";import{RegularBooth as Q,SpecialBooth as ee}from"../../../../store/BoothStore";import x from"../../../../tools/settings";import{t as v}from"../../../../utils/i18n";import te from"../../../../utils/is-mobile";import ie from"../../../../utils/is-webview";import{isArabicText as se}from"../../../../utils/rtl";import{BoothDrawerBaseWithoutPainter as ne}from"./BoothDrawerBase";import{getFont as _}from"./canvases";let z=x.boothLabelColor;x.EXPO==="tqs2021"&&(z="#000");const b=te||ie,D=r=>r.replace(" ","").substring(0,4).replace(/[0-9]/g,"3").replace(/[A-Z]/gi,"A"),I=document.createElement("canvas").getContext("2d");export default function oe(r,e){if(!(!(e instanceof Q)||e.noLabels))return new BoothLabelDrawer(r,e)}export function getMinZoomFactorForDot(r){return Array.isArray(r)&&r.length>0?Math.max(...r.map(e=>e)):typeof r=="number"?r:-0}export const isDirectionsMode=()=>!!(u.selectedRoute?.from&&u.selectedRoute?.to);export function getFontUrlByWeight(r,e=[],t="normal"){return[...e,{weight:500,url:B,style:"normal"},{weight:300,url:$,style:"normal"}].find(i=>+i.weight===r&&i.style===t)?.url||B}function S(r,e){return Number(getComputedStyle(document.body).getPropertyValue(r))||e}export function getBoothLabelWeights(){return{main:S(N,500),details:S(X,300)}}export function getExhibitorLabelWeights(){return{main:S(U,500),details:S(V,300)}}export function getBadgeFontWeight(){return getExhibitorLabelWeights().details}const F="Details";export class BoothLabelDrawer extends ne{constructor(e,t){if(super(t),this.rendererService=e,this.canvasRect=new E(t.rect.center,t.rect.size,t.rotate),this.paddingScaled=[4,4],this.padding=(t.borderWidth||x.boothBorderWidth)/2,this.topLeftAlignment={horizontal:u.rtl?"right":"left",vertical:"top"},this.centerAlignment={horizontal:"center",vertical:"center"},this.prefixes=b?[{fontSize:7,name:"XS",short:!0},{fontSize:10,name:"S",short:!0},{fontSize:16,name:F,short:!1}]:[{fontSize:7,name:"XS",short:!0},{fontSize:10,name:"S",short:!0},{fontSize:12,name:"M",short:!0},{fontSize:14,name:"L",short:!0},{fontSize:18,name:F,short:!1}],t instanceof ee)return;this.exh=p.hideExhibitors?[]:p.onlyFeaturedExhibitors?t.exhibitors.filter(i=>i.featured):t.exhibitors,b&&this.optimizationLevel()>=3&&(this.prefixes=this.prefixes.filter(i=>i.name!=="S")),this.createLines(),this.calculateFontSizeFactors(),this.shapeLabel=G(Y(this.lines,this.booth.labelColor||z,0,this.fontUrls),new E(this.booth.rect.center,this.booth.rect.size,this.booth.rotate),[...this.paddingScaled],{horizontal:"left",vertical:"top"}),this.getShapeDot();const o=i=>{const n=this.lines[1]??[],s=this.fontSizes[1]??[];if(x.EXPO!=="wineparis2026"){const m=this.exh.length?this.topLeftAlignment:n.length?i<s[s.length-1].scaleFactor?this.topLeftAlignment:this.centerAlignment:this.topLeftAlignment;this.shapeLabel.alignment=m}if(!j(this.shapeLabel.lines,i,this.fontSizes,this.booth.name))return;const l=this.paddingScaled[0]*i+this.padding,d=this.paddingScaled[1]*i+this.padding;return this.shapeLabel.padding[0]=l,this.shapeLabel.padding[1]=d,this.shapeLabel},h=i=>{const n=i>=this.minZoomDotVisible&&i<=this.maxZoomDotVisible,s=this.shapeDot.source.width*i,a=this.shapeDot.source.height*i;if(!(!n&&this.shapeDot.hidden))return this.shapeDot.bounds.size={x:s,y:a},this.shapeDot.hidden=!n,this.shapeDot};W(()=>[this.booth.skipDim,u.selectedRoute,M.layersLoaded],()=>{if(!M.layersLoaded)return;const i=isDirectionsMode();this.shapeLabel.dim=i||this.booth.skipDim?!1:void 0,this.shapeDot.dim=i||this.booth.skipDim?!1:void 0,this.rendererService.update(this.shapeLabel,this.shapeDot)}),W(()=>u.mapSettings,({roll:i})=>{k(i)&&this.rendererService.onRoll(`booth-roll-${t.id}`,n=>{const s=q(n,this.shapeLabel.bounds.rotation),{size:a}=this.canvasRect;return s!==void 0&&(this.shapeLabel=K(this.shapeLabel,s),!this.shapeLabel.hidden&&!this.exh.length&&a.x!==a.y&&(this.canvasRect=J(this.canvasRect,s),this.calculateFontSizeFactors(),o(this.rendererService.scale),h(this.rendererService.scale))),[this.shapeLabel,this.shapeDot]})},{fireImmediately:!0}),this.rendererService.onPtScale(`booth-label-${t.id}`,o),this.rendererService.onPtScale(`booth-dot-${t.id}`,h)}getShape(){return[this.shapeLabel,this.shapeDot]}getShapeDot(){this.shapeDot=Z(this.canvasRect,this.booth.labelColor||z),this.shapeDot.hidden=!0,this.maxZoomDotVisible=Math.min(this.canvasRect.size.x/this.shapeDot.source.width,this.canvasRect.size.y/this.shapeDot.source.height)}createLines(){const e=this.booth,t=[],o=[],h=b&&this.optimizationLevel()>=3?1:3;if(this.exh.length){const i=e.exhibitors.filter(s=>s.order===0),n=e.exhibitors.filter(s=>s.order);if(i.length>0)t.push(...i.map(s=>s.name));else if(p.onlyFeaturedExhibitors)t.push(...e.exhibitors.filter(s=>s.featured).map(s=>s.name));else if(n.length>0){t.push(...n.map(a=>a.name));const s=e.exhibitors.filter(a=>a.order===void 0);s.length>0&&t.push(v("and {{moreCount}} more",{moreCount:s.length}))}else e.exhibitors.length>h?t.push(`${e.exhibitors.length} ${p.exhibitorTermPlural}`):t.push(...e.exhibitors.map(s=>s.name));o.push(e.title||e.name)}else{const i=!!e.exhibitors.length;t.push(e.title||e.name),e.onHold?o.push(v("On Hold")):e.reserved?o.push(v("Reserved")):i||o.push(...e.exhibitors.map(n=>n.name).sort((n,s)=>n>s?1:-1)),e.size&&o.push(e.size.indexOf("/")>-1?e.size.substring(0,e.size.indexOf("/")).trim():e.size),e.price&&e.price!=="0"&&!u.previewMode&&o.push(e.price)}u.rtl&&t.forEach((i,n)=>{(i.endsWith(".")||i.endsWith("!")||i.endsWith("?"))&&(t[n]=t[n]+"\u200F"),se(i)&&(t[n]=t[n]+" ")}),this.lines=[t,o]}calculateFontSizeFactors(){let[e,t]=this.lines;const o=this.canvasRect,h=[],i=[],n=this.prefixes.slice(0,this.exh.length?this.prefixes.length:this.prefixes.length-1),{main:s,details:a}=this.exh.length?getExhibitorLabelWeights():getBoothLabelWeights();for(const{fontSize:l,short:d}of n){const m=l*devicePixelRatio,R=.9*l*devicePixelRatio,f=_(m,s),T=_(R,a),y=p.hideExhibitorBoothNumber||d;let w,A,L,g;if(this.exh.length)d?t=[]:t=this.lines[1],L=Math.max(...e.map(c=>this.measureText(c,f).width),...t.map(c=>this.measureText(c,T).width)),w=Math.max(...e.map(c=>this.exh.length?this.measureText(D(c),f).width:this.measureText(D(c),f).width+3+3),...t.map(c=>this.measureText(D(c),T).width)),g=m*e.length+R*t.length+devicePixelRatio*(e.length+t.length)+3,A=y?g/e.length:null;else{const c=e[0].replace(/[0-9]/g,"3").replace(/[A-Z]/g,"A"),C=4;L=this.measureText(c,f).width+3+3,g=m+C}const H=o.size.x/(w||L),P=o.size.y/(A||g),O=Math.min(H,P);h.push({scaleFactor:O,value:l}),!y&&i.push({scaleFactor:O,value:.9*l})}if(!this.exh.length){const l=h[h.length-1],d=this.prefixes.find(m=>m.name===F);h.push({scaleFactor:l.scaleFactor/1.8,value:d.fontSize}),i.push({scaleFactor:l.scaleFactor/1.8,value:d.fontSize*(b?1:.9)})}this.fontUrls=[getFontUrlByWeight(s,this.rendererService.fontUrls),getFontUrlByWeight(a,this.rendererService.fontUrls)],this.minZoomDotVisible=getMinZoomFactorForDot(h.map(l=>l.scaleFactor)),this.fontSizes=[h,i]}calcArea(e,t){return Math.floor(Math.max(0,e)*Math.max(0,t)/1e3)}optimizationLevel(){return Math.min(p.viewOptimizationLevel?p.viewOptimizationLevel:Math.floor(this.calcArea(this.canvasRect.size.x,this.canvasRect.size.y)/5e3),3)}measureText(e,t){return I.font=t,I.measureText(e)}}