arcade.js 5.1 KB

12345
  1. /*
  2. All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. See https://js.arcgis.com/4.25/esri/copyright.txt for details.
  4. */
  5. import e from"./Graphic.js";import t from"./arcade/ImmutableArray.js";import r from"./core/Error.js";import{clone as a}from"./core/lang.js";import n from"./layers/FeatureLayer.js";import o from"./rest/support/FeatureSet.js";import{loadArcade as s}from"./support/arcadeOnDemand.js";let c=null;function i(e,r,a,n={}){const o=r.elementType,s="value",c="array"===o.type?[{name:s,type:o.type,elementType:o.elementType}]:"dictionary"===o.type?[{name:s,type:o.type,properties:o.properties}]:[{name:s,type:o.type}];return new t(e.map((e=>{const t={};return f(t,c,{[s]:e},a,n),t[s]})))}function u(e,t,r={}){const a=e instanceof o?new n({source:e.features,geometryType:e.geometryType,fields:e.fields,spatialReference:e.spatialReference}):e;return t.constructFeatureSet(a,r.spatialReference,null,!0,r.lruCache,r.interceptor)}function p(e,t,r={}){const{spatialReference:a,interceptor:n,lruCache:o}=r;return"string"==typeof e?t.createFeatureSetCollectionFromService(e,a,o,n):t.createFeatureSetCollectionFromMap(e,a,o,n)}function l(e,t,r,a={}){const n={};return f(n,t.properties,e,r,a),new c.Dictionary(n)}function f(e,t,r,a,n={}){const o={};for(const s of Object.keys(r))o[s.toLowerCase()]=r[s];for(const s of t){const t=s.name.toLowerCase();if(n.variablesPreProcessed)e[t]=o[t];else switch(s.type){case"array":{const r=o[t];e[t]=i(r,s,a,n);break}case"feature":{const r=o[t];e[t]=null==r?null:c.Feature.createFromGraphic(r);break}case"featureSet":{const r=o[t];e[t]=a?u(r,a,n):null;break}case"featureSetCollection":{const r=o[t];e[t]=a?p(r,a,n):null;break}case"dictionary":{const r=o[t];e[t]=l(r,s,a,n);break}case"geometry":case"boolean":case"date":case"text":case"number":e[t]=o[t]}}}function y(e,t){for(const r of e)t.push(r),"dictionary"===r.type&&y(r.properties,t);return t}function m(e,t,r,a,n){const{spatialReference:o,interceptor:s,lruCache:c,services:i,console:u,abortSignal:p}=r,l={vars:{},spatialReference:o,interceptor:s,lrucache:c,useAsync:n,services:i,console:u,abortSignal:p};return t?(f(l.vars,e.variables,t,a,r),l):l}function d(t,r){switch(r.getArcadeType(t)){case"number":case"text":case"boolean":case"point":case"polygon":case"polyline":case"multipoint":case"extent":return t;case"date":return new Date(t.getTime());case"feature":{const r=(t.type,t),a="geometry"in r?r.geometry():null,n="readAttributes"in r?r.readAttributes():r.attributes;return new e({geometry:a,attributes:n})}case"dictionary":{const e=t,a=e.attributes,n={};for(const t of Object.keys(a))n[t]=d(e.field(t),r);return n}case"array":return("toArray"in t?t.toArray():t).map((e=>d(e,r)))}return t}const b={variables:[{name:"$feature",type:"feature"},{name:"$layer",type:"featureSet"},{name:"$datastore",type:"featureSetCollection"},{name:"$map",type:"featureSetCollection"}]},v={variables:[{name:"$feature",type:"feature"},{name:"$aggregatedFeatures",type:"featureSet"}]},S=new Map([["constraint",{variables:[{name:"$feature",type:"feature"}]}],["feature-z",{variables:[{name:"$feature",type:"feature"}]}],["field-calculation",{variables:[{name:"$feature",type:"feature"},{name:"$datastore",type:"featureSetCollection"}]}],["form-calculation",{variables:[{name:"$feature",type:"feature"},{name:"$originalFeature",type:"feature"},{name:"$layer",type:"featureSet"},{name:"$featureSet",type:"featureSet"},{name:"$datastore",type:"featureSetCollection"},{name:"$map",type:"featureSetCollection"},{name:"$editContext",type:"dictionary",properties:[{name:"editType",type:"text"}]}]}],["labeling",{variables:[{name:"$feature",type:"feature"}]}],["popup",b],["popup-element",b],["feature-reduction-popup",v],["feature-reduction-popup-element",v],["visualization",{variables:[{name:"$feature",type:"feature"},{name:"$view",type:"dictionary",properties:[{name:"scale",type:"number"}]}]}]]);function w(e){const t=S.get(e);if(!t){const t=Array.from(S.keys()).map((e=>`'${e}'`)).join(", ");throw new r("createArcadeProfile:invalid-profile-name",`Invalid profile name '${e}'. You must specify one of the following values: ${t}`)}return a(t)}async function $(e,t,a={}){c||(c=await s());const{arcade:n,arcadeUtils:o}=c,{loadScriptDependencies:i,referencesMember:u,scriptIsAsync:p}=n,l=y(t.variables,[]),f=l.filter((e=>"featureSet"===e.type||"featureSetCollection"===e.type)).map((e=>e.name.toLowerCase())),b=n.parseScript(e,f);if(!b)throw new r("arcade:invalid-script","Unable to create SyntaxTree");const v=o.extractFieldNames(b),S=n.scriptTouchesGeometry(b),w=l.map((e=>e.name.toLowerCase())).filter((e=>u(b,e))),$=p(b,f);await i(b,$,f);const g={vars:{},spatialReference:null,useAsync:$};for(const r of w)g.vars[r]="any";const{lruCache:C}=a,h=n.compileScript(b,g),x=n.featureSetUtils();return{execute:(e,a={})=>{if($)throw new r("arcade:invalid-execution-mode","Cannot execute the script in synchronous mode");const n=h(m(t,e,{lruCache:C,...a},x,$));return a.rawOutput?n:d(n,o)},executeAsync:async(e,r={})=>{const a=await h(m(t,e,{lruCache:C,...r},x,$));return r.rawOutput?a:d(a,o)},isAsync:$,variablesUsed:w,fieldsUsed:v,geometryUsed:S,syntaxTree:b}}export{$ as createArcadeExecutor,w as createArcadeProfile};