@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 2.77 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import e from"../../core/Error.js";import r from"../../core/Logger.js";import{parseWhereClause as t}from"../../core/sql.js";import{formatDate as n,convertDateFormatToIntlOptions as o}from"../../intl/date.js";import{formatNumber as a}from"../../intl/number.js";import{getDomainRange as i}from"./domainUtils.js";import{isIntegerField as l,isNumericField as s,isStringField as u,isDateField as c}from"./fieldUtils.js";import{getLabelExpression as f,getSingleFieldArcadeExpression as m}from"./labelUtils.js";import{getFieldNameFromSimpleExpression as p}from"../../support/arcadeExpressionUtils.js";import{createLabelExpression as d}from"../../support/arcadeOnDemand.js";const g=()=>r.getLogger("esri.layers.support.labelFormatUtils"),w={type:"simple",evaluate:()=>null},v={getAttribute:(e,r)=>e.field(r)};async function y(r,n,o){if(!r||!r.symbol||!n)return w;const a=r.where,i=f(r);let c;if("arcade"===i.type){const r=p(i.expression),t=n.get(r);if(t&&(l(t)||s(t)||u(t)))c={type:"simple",evaluate(e){const r="attributes"in e?e.attributes?.[t.name]:e.field(t.name);return null==r?"":r.toString()}};else{const r=await d(i.expression,o,n);if(null==r)return w;c={type:"arcade",evaluate(t,n){try{const e="attributes"in t?r.repurposeFeature(t):t;e.contextTimeZone=n??null;const o=r.evaluate({$view:{timeZone:n},$feature:e},r.services);if(null!=o)return o.toString()}catch(o){g().error(new e("arcade-expression-error","Encountered an error when evaluating label expression for feature",{error:o,feature:t,expression:i}))}return null},needsHydrationToEvaluate:()=>null==m(i.expression)}}}else c={type:"simple",evaluate:e=>i.expression.replaceAll(/{[^}]*}/g,(r=>{const t=r.slice(1,-1),o=n.get(t);if(!o)return r;let a=null;return a="attributes"in e?e?.attributes?.[o.name]:e.field(o.name),null==a?"":b(a,o)}))};if(a){let r;try{r=await t(a,n)}catch(y){return g().error(new e("bad-where-clause","Encountered an error when evaluating where clause, ignoring",{where:a,error:y})),w}const o=c.evaluate;c.evaluate=(t,n)=>{const i="attributes"in t?void 0:v;try{if(r.testFeature(t,i))return o(t,n)}catch(y){g().error(new e("bad-where-clause","Encountered an error when evaluating where clause for feature",{where:a,feature:t,error:y}))}return null}}return c}function b(e,r){if(null==e)return"";const t=r.domain;if(t)if("codedValue"===t.type||"coded-value"===t.type){const r=e;for(const e of t.codedValues)if(e.code===r)return e.name}else if("range"===t.type){const{max:n,min:o}=i(r),a=+e;if(null!=o&&null!=n&&o<=a&&a<=n)return t.name}let l=e;return c(r)?l=n(l,o("short-date")):s(r)&&(l=a(+l)),l||""}export{y as createLabelFunction,b as formatField};