@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 2.67 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
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{isDateField as l,isNumericField as s,isIntegerField as u,isStringField as c}from"./fieldUtils.js";import{getLabelExpression as p,getSingleFieldArcadeExpression as f}from"./labelUtils.js";import{createLabelExpression as m}from"../../support/ArcadeExpression.js";import{getFieldNameFromSimpleExpression as d}from"../../support/arcadeExpressionUtils.js";const g=()=>r.getLogger("esri.layers.support.labelFormatUtils"),w={type:"simple",evaluate:()=>null},y={getAttribute:(e,r)=>e.field(r)};async function b(r,n,o){if(!r||!r.symbol||!n)return w;const a=r.where,i=p(r);let l;if("arcade"===i.type){const r=d(i.expression),t=n.get(r);if(t&&(u(t)||s(t)||c(t)))l={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 m(i.expression,o);if(null==r)return w;l={type:"arcade",evaluate(t,o){try{const e="attributes"in t?r.repurposeFeature(t,n):t;e.contextTimeZone=o??null;const a=r.evaluate(e,{$view:{timeZone:o}});if(null!=a)return a.toString()}catch(a){g().error(new e("arcade-expression-error","Encountered an error when evaluating label expression for feature",{error:a,feature:t,expression:i}))}return null},needsHydrationToEvaluate:()=>null==f(i.expression)}}}else l={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?"":h(a,o)})};if(a){let r;try{r=await t(a,n)}catch(b){return g().error(new e("bad-where-clause","Encountered an error when evaluating where clause, ignoring",{where:a,error:b})),w}const o=l.evaluate;l.evaluate=(t,n)=>{const i="attributes"in t?void 0:y;try{if(r.testFeature(t,i))return o(t,n)}catch(b){g().error(new e("bad-where-clause","Encountered an error when evaluating where clause for feature",{where:a,feature:t,error:b}))}return null}}return l}function h(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 u=e;return l(r)?u=n(u,o("short-date")):s(r)&&(u=a(+u)),u||""}export{b as createLabelFunction,h as formatField};