UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) 7.95 kB
import{reaction as E}from"mobx";import{FONT_WEIGHT_PRIMARY_BOOTH as N,FONT_WEIGHT_PRIMARY_EXHIBITOR as U,FONT_WEIGHT_SECONDARY_BOOTH as V,FONT_WEIGHT_SECONDARY_EXHIBITOR as Z}from"../../../../constants";import p from"../../../../data";import{createShapeDot as X,createTextDef as G,createTextDefLines as Y,DEFAULT_FONT_LIGHT_URL as $,DEFAULT_FONT_URL as W,getRollValue as k,getRotation as q,updateLinesScale as j,updateRectRotation as J,updateTextDefRotation as K}from"../../../../renderer";import{layersStore as B,uiState as f}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 oe}from"./BoothDrawerBase";import{getFont as M}from"./canvases";import{Rect as _}from"./Rect";let D=x.boothLabelColor;x.EXPO==="tqs2021"&&(D="#000");const b=te||ie,z=r=>r.replace(" ","").substring(0,4).replace(/[0-9]/g,"3").replace(/[A-Z]/gi,"A"),C=document.createElement("canvas").getContext("2d");export default function ne(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=()=>!!(f.selectedRoute?.from&&f.selectedRoute?.to);export function getFontUrlByWeight(r,e=[],t="normal"){return[...e,{weight:500,url:W,style:"normal"},{weight:300,url:$,style:"normal"}].find(i=>+i.weight===r&&i.style===t)?.url||W}function S(r,e){return Number(getComputedStyle(document.body).getPropertyValue(r))||e}export function getBoothLabelWeights(){return{main:S(N,500),details:S(V,300)}}export function getExhibitorLabelWeights(){return{main:S(U,500),details:S(Z,300)}}export function getBadgeFontWeight(){return getExhibitorLabelWeights().details}const R="Details";export class BoothLabelDrawer extends oe{rendererService;canvasRect;paddingScaled;padding;minZoomDotVisible;maxZoomDotVisible;exh;shapeLabel;shapeDot;fontSizes;lines;topLeftAlignment;centerAlignment;prefixes;fontUrls;constructor(e,t){if(super(t),this.rendererService=e,this.canvasRect=_.fromCoreRect(t.rect,t.rotate),this.paddingScaled=[4,4],this.padding=(t.borderWidth||x.boothBorderWidth)/2,this.topLeftAlignment={horizontal:f.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:R,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:R,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||D,0,this.fontUrls),_.fromCoreRect(this.booth.rect,this.booth.rotate),[...this.paddingScaled],{horizontal:"left",vertical:"top"}),this.getShapeDot();const n=i=>{const o=this.lines[1]??[],s=this.fontSizes[1]??[];if(x.EXPO!=="wineparis2026"){const m=this.exh.length?this.topLeftAlignment:o.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 o=i>=this.minZoomDotVisible&&i<=this.maxZoomDotVisible,s=this.shapeDot.source.width*i,a=this.shapeDot.source.height*i;if(!(!o&&this.shapeDot.hidden))return this.shapeDot.bounds.size={x:s,y:a},this.shapeDot.hidden=!o,this.shapeDot};E(()=>[this.booth.skipDim,f.selectedRoute,B.layersLoaded],()=>{if(!B.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)}),E(()=>f.mapSettings,({roll:i})=>{k(i)&&this.rendererService.onRoll(`booth-roll-${t.id}`,o=>{const s=q(o,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(),n(this.rendererService.scale),h(this.rendererService.scale))),[this.shapeLabel,this.shapeDot]})}),this.rendererService.onPtScale(`booth-label-${t.id}`,n),this.rendererService.onPtScale(`booth-dot-${t.id}`,h)}getShape(){return[this.shapeLabel,this.shapeDot]}getShapeDot(){this.shapeDot=X(this.canvasRect,this.booth.labelColor||D),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=[],n=[],h=b&&this.optimizationLevel()>=3?1:3;if(this.exh.length){const i=e.exhibitors.filter(s=>s.order===0),o=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(o.length>0){t.push(...o.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));n.push(e.title||e.name)}else{const i=!!e.exhibitors.length;t.push(e.title||e.name),e.onHold?n.push(v("On Hold")):e.reserved?n.push(v("Reserved")):i||n.push(...e.exhibitors.map(o=>o.name).sort((o,s)=>o>s?1:-1)),e.size&&n.push(e.size.indexOf("/")>-1?e.size.substring(0,e.size.indexOf("/")).trim():e.size),e.price&&e.price!=="0"&&!f.previewMode&&n.push(e.price)}f.rtl&&t.forEach((i,o)=>{(i.endsWith(".")||i.endsWith("!")||i.endsWith("?"))&&(t[o]=t[o]+"\u200F"),se(i)&&(t[o]=t[o]+" ")}),this.lines=[t,n]}calculateFontSizeFactors(){let[e,t]=this.lines;const n=this.canvasRect,h=[],i=[],o=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 o){const m=l*devicePixelRatio,F=.9*l*devicePixelRatio,u=M(m,s),T=M(F,a),y=p.hideExhibitorBoothNumber||d;let A,w,L,g;if(this.exh.length)d?t=[]:t=this.lines[1],L=Math.max(...e.map(c=>this.measureText(c,u).width),...t.map(c=>this.measureText(c,T).width)),A=Math.max(...e.map(c=>this.exh.length?this.measureText(z(c),u).width:this.measureText(z(c),u).width+3+3),...t.map(c=>this.measureText(z(c),T).width)),g=m*e.length+F*t.length+devicePixelRatio*(e.length+t.length)+3,w=y?g/e.length:null;else{const c=e[0].replace(/[0-9]/g,"3").replace(/[A-Z]/g,"A"),P=4;L=this.measureText(c,u).width+3+3,g=m+P}const H=n.size.x/(A||L),I=n.size.y/(w||g),O=Math.min(H,I);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===R);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 C.font=t,C.measureText(e)}}