UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 5.29 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{ArcadeDate as e}from"../arcade/ArcadeDate.js";import t from"../core/Error.js";import"../core/has.js";import{DateOnly as r}from"../core/sql/DateOnly.js";import{TimeOnly as i}from"../core/sql/TimeOnly.js";import{fromJSON as n}from"../geometry/support/jsonUtils.js";import s from"../layers/support/FieldsIndex.js";import{loadArcade as a}from"./loadArcade.js";import{unknown as o,system as c}from"../time/constants.js";const l=["geometry","scale","timeProperties"];function u(e,t){if(null!=t)for(const r of l)t.hasArcadeDependency(r)&&e.add(r);return e}function d(e,t){return p.create(e,t,null,["$feature","$view"])}function h(e,t){return p.create(e,t,null,["$feature","$view"])}function m(e,t,r){return p.create(e,t,r,["$feature","$view","$config"])}class p{static async create(e,r,i,n){const{arcade:s,Dictionary:o}=await a();let c;try{c=s.parseScript(e)}catch(f){throw new t("arcade-bad-expression","Failed to parse arcade script",{script:e,error:f})}const l=s.scriptUsesGeometryEngine(c);l&&await s.enableGeometrySupport(),await s.loadDependentModules(new Set,c,null,!1,l);const u={vars:n.reduce((e,t)=>({...e,[t]:null}),{}),spatialReference:r,useAsync:!1},d=s.compileScript(c,u);let h=null;null!=i&&(h=new o(i),h.immutable=!0);const m=new o;return m.immutable=!1,m.setField("scale",0),new p(e,s,c,d,r,m,h,o)}constructor(e,t,r,i,n,s,a,o){this.script=e,this._arcade=t,this._syntaxTree=r,this._compiled=i,this._spatialReference=n,this._viewDict=s,this._configDict=a,this._dictionaryCtor=o,this._dependencies=new Map,this._featureReader=new f,this._dependencies.set("geometry",t.scriptTouchesGeometry(this._syntaxTree)),this._dependencies.set("scale",this._arcade.referencesMember(this._syntaxTree,"scale")),this._dependencies.set("timeProperties",this._arcade.scriptUsesViewProperties(this._syntaxTree,["timeProperties"]))}evaluate(t,r){const i=r.$view?.timeZone;if(r.$view){let t;if(this._viewDict.setField("scale",r.$view.scale),null!=r.$view.timeProperties){const{currentStart:n,currentEnd:s}=r.$view.timeProperties;t=new this._dictionaryCtor({currentStart:null!=n?null!=i?e.epochToArcadeDate(n,i):e.unknownEpochToArcadeDate(n):void 0,currentEnd:null!=s?null!=i?e.epochToArcadeDate(s,i):e.unknownEpochToArcadeDate(s):void 0,startIncluded:!0,endIncluded:!0})}this._viewDict.setField("timeProperties",t)}return this._compiled({vars:{$view:this._viewDict,$config:this._configDict,$feature:t},spatialReference:this._spatialReference,timeZone:i})}repurposeFeature(e,t){return this._featureReader.bind(e,t,this._spatialReference),this._featureReader}references(e){return this._dependencies.get(e)??!1}}class f{constructor(){this._boundTarget=null,this._boundSchema={fields:null,fieldsIndex:null,spatialReference:null,get geometryType(){return null},get objectIdField(){return null}},this.arcadeDeclaredClass="esri.arcade.Feature",this._contextTimeZone=null}bind(e,t,r){const i=t??new s(_(e.attributes));this._boundTarget=e,this._boundSchema.fields=i.fields,this._boundSchema.fieldsIndex=i,this._boundSchema.spatialReference=r}_getField(e){return this._boundSchema.fieldsIndex.get(e)}get contextTimeZone(){return this._contextTimeZone}set contextTimeZone(e){this._contextTimeZone=e}readArcadeFeature(){return this}hasField(e){return this._boundSchema.fieldsIndex.has(e)}geometry(){if("fromJSON"in this._boundTarget)return this._boundTarget.geometry;const e=n(this._boundTarget.geometry);if(e){if(!this._boundSchema.spatialReference)throw new Error("InternalError: Expected spatial reference to be defined");e.spatialReference=this._boundSchema.spatialReference}return e}_hasGeometry(){return null!=this._boundTarget.geometry}isUnknownDateTimeField(e){return this._boundSchema.fieldsIndex.getTimeZone(e)===o}field(t,n=!0){const s=this._getField(t);if(s){const n=this._boundTarget.attributes[s.name];if(null==n)return null;switch(s.type){case"date-only":case"esriFieldTypeDateOnly":return r.fromReader(n);case"time-only":case"esriFieldTypeTimeOnly":return i.fromReader(n);case"esriFieldTypeTimestampOffset":case"timestamp-offset":return e.fromReaderAsTimeStampOffset(n);case"date":case"esriFieldTypeDate":return this.isUnknownDateTimeField(t)?e.unknownEpochToArcadeDate(n):e.epochToArcadeDate(n,this.contextTimeZone??c);default:return n}}if(n)throw new Error(`Field ${t} does not exist`);return null}setField(e,t){throw new Error("Unable to update feature attribute values, feature is readonly")}keys(){return this._boundSchema.fieldsIndex.fields.map(e=>e.name)}isEmpty(){return this._boundSchema.fields.length<=0&&!this._hasGeometry()}castToText(e=!1){return JSON.stringify(this._boundTarget)}gdbVersion(){return null}fullSchema(){return this._boundSchema}castAsJson(e=null){return{attributes:this._boundTarget.attributes,geometry:!0===e?.keepGeometryType?this.geometry():this.geometry()?.toJSON()??null}}castAsJsonAsync(e=null,t=null){return Promise.resolve(this.castAsJson(t))}}function _(e){const t=[];for(const r in e)t.push({name:r,alias:r,type:"string"==typeof e[r]?"esriFieldTypeString":"esriFieldTypeDouble"});return t}export{p as ArcadeExpression,f as ArcadeFeatureReader,u as collectExpressionDependencies,m as createDictionaryExpression,d as createLabelExpression,h as createRendererExpression,_ as deriveFields};