UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 5.67 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../core/Accessor.js";import{isSome as s}from"../../core/arrayUtils.js";import{createTask as i}from"../../core/asyncUtils.js";import r from"../../core/Collection.js";import{property as a}from"../../core/accessorSupport/decorators/property.js";import"../../core/has.js";import"../../core/Logger.js";import{subclass as o}from"../../core/accessorSupport/decorators/subclass.js";import{featureHasFields as l,extractSubstitutionTemplatesFromString as n}from"./fieldUtils.js";import{loadArcade as c}from"../../support/loadArcade.js";const d="relationships/",p="expression/";let u=class extends t{constructor(e){super(e),this._featureUtils=null,this.effectivePopupTemplate=null}get _arcadeTask(){if(this.expressionsUsedInTitle.length>0){return this._get("_arcadeTask")||i((()=>c()))}return null}get featureUtilsPromise(){return this._get("featureUtilsPromise")??import("../../widgets/Feature/support/featureUtils.js").then((e=>this._featureUtils=e))}get calculatedExpressions(){const e=new r;if(!this.expressionsUsedInTitle.length)return e;if(!this._arcadeTask?.value){for(const t of this.expressionsUsedInTitle??[])e.push({name:t.name,invalid:!0});return e}for(const t of this.expressionsUsedInTitle)try{const s=this._arcadeTask.value.arcade.parseScript(t.expression,["$layer","$map","$datastore"]);if(s.isAsync){e.push({name:t.name,invalid:!0});break}e.push({name:t.name,syntax:s,invalid:!1,func:this._arcadeTask.value.arcade.compileScript(s,{vars:{$feature:"any"}})})}catch{e.push({name:t.name,invalid:!0});break}return e}get expressionsUsedInTitle(){let e=this.effectivePopupTemplate?.title??"";return"string"!=typeof e?[]:(e=e.toLowerCase(),this.effectivePopupTemplate?.expressionInfos?.filter((t=>e.includes(`{expression/${t.name.toLowerCase()}}`)))??[])}get fieldInfoMap(){return this._featureUtils?this._createFieldInfoMap(this._featureUtils.getAllFieldInfos(this.effectivePopupTemplate)):null}get hasBadExpressions(){return this.calculatedExpressions.some((e=>!0===e.invalid))}get requiredFields(){const e=new Set;if(this._arcadeTask?.value&&!this.hasBadExpressions)for(const s of this.calculatedExpressions?.toArray()??[])try{const t=this._arcadeTask.value.arcade.extractFieldLiterals(s.syntax);for(const s of t){const t=s.split("."),i=this.fieldsIndex.get(t.at(-1)??"");i&&e.add(i.name)}}catch{}const t=this._extractFieldNames(this.workingTitle);for(const s of t){const t=this.fieldsIndex.get(s);t&&e.add(t.name)}return null!=this.objectIdField&&e.add(this.objectIdField),e}get titleFromDisplayField(){let e="";return this.displayField&&(e=this.fieldsIndex.get(this.displayField)?.name??""),e||(e=this.fieldsIndex.get(this.objectIdField)?.name??""),e?`{${e}}`:""}get workingTitle(){const e=this.effectivePopupTemplate?this.effectivePopupTemplate.title:"";return""===e||null==e||this.hasBadExpressions||"string"!=typeof e?this.titleFromDisplayField:e}async getTitle(e,t,s){const i=t.getObjectId()??t.attributes[e.objectIdField];return(await this.getTitles(e,[t],s)).get(i)??""}async getTitles(e,t,s){const i=new Map,r=s?.timeZone??"system";try{const[{substituteFieldsInLinksAndAttributes:a}]=await Promise.all([this.featureUtilsPromise,this._arcadeTask?.promise]);s?.fetchMissingFields&&(t=await this._checkAndReQueryGraphics(e,t));const{fieldInfoMap:o,workingTitle:l}=this,n=l&&o;t.forEach((t=>{const s=t.getObjectId()??t.attributes[e.objectIdField],c=n?a({attributes:t.attributes,expressionAttributes:null,fieldInfoMap:o,globalAttributes:this._createFormattedAttributes(e,t,r).global,layer:e,text:l}):"";i.set(s,c)}))}catch{}return i}async _checkAndReQueryGraphics(e,t){const i=t.map((t=>t.getObjectId()??t.attributes[e.objectIdField])).filter(s);if(i.length!==t.length)return t;if(t.some((e=>!l(e,this.requiredFields)))){const s=e.createQuery();s.where="1=1",s.outFields=[...this.requiredFields],s.objectIds=i;const r=await e.queryFeatures(s);if(r?.features.length===t.length)return r.features}return t}_createFieldInfoMap(e){const t=new Map;if(!e)return t;for(const s of e){if(!s.fieldName)continue;const e=this.fieldsIndex.get(s.fieldName),i=e?.name??s.fieldName;s.fieldName=i,t.set(i.toLowerCase(),s)}return t}_createFormattedAttributes(e,t,s="system"){const i=this.effectivePopupTemplate?.fieldInfos??[],r={};if(!this._featureUtils)return{};if(!this.hasBadExpressions&&this.calculatedExpressions.length>0&&this._arcadeTask?.value){const s=this._arcadeTask.value.Feature.createFromGraphicLikeObject(t.geometry,t.attributes,e,null);for(const e of this.calculatedExpressions)try{r[`expression/${e.name}`]=e.func({vars:{$feature:s}})}catch{}}const a={...t.attributes,...r};return{global:this._featureUtils.formatAttributes({fieldInfos:i,attributes:a,graphic:t,timeZone:s,layer:e,fieldInfoMap:this.fieldInfoMap}),content:[]}}_extractFieldNames(e){return n(e).filter((e=>!(0===e.indexOf(d)||0===e.indexOf(p))))}};e([a({readOnly:!0})],u.prototype,"_arcadeTask",null),e([a()],u.prototype,"_featureUtils",void 0),e([a({readOnly:!0})],u.prototype,"featureUtilsPromise",null),e([a({readOnly:!0})],u.prototype,"calculatedExpressions",null),e([a()],u.prototype,"displayField",void 0),e([a()],u.prototype,"effectivePopupTemplate",void 0),e([a()],u.prototype,"expressionsUsedInTitle",null),e([a()],u.prototype,"fieldsIndex",void 0),e([a()],u.prototype,"fieldInfoMap",null),e([a()],u.prototype,"fields",void 0),e([a()],u.prototype,"objectIdField",void 0),e([a()],u.prototype,"requiredFields",null),u=e([o("esri.layers.support.TitleCreator")],u);export{u as default};