UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 7.8 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import t from"../../../core/Accessor.js";import r from"../../../core/Error.js";import a from"../../../core/Logger.js";import{getOrCreateMapValue as s}from"../../../core/MapUtils.js";import{whenOrAbort as o}from"../../../core/promiseUtils.js";import i from"../../../core/ReactiveMap.js";import{property as n,subclass as l}from"../../../core/accessorSupport/decorators.js";import{DateOnly as u}from"../../../core/sql/DateOnly.js";import{TimeOnly as c}from"../../../core/sql/TimeOnly.js";import{makeUseId as p}from"../inputs/support/inputUtils.js";import{isGroupElementTemplate as d,isFieldElementTemplate as f}from"../templates/support/templateUtils.js";let h=null,m=class extends t{constructor(e){super(e),this.calculating=!1,this.arcadeContext=null,this._abortController=new AbortController,this.layerExpressionsModelMap=new Map,this.evaluatedExpressions=new i,this.expressionEvaluationFailed=!1}abort(){this._abortController.abort()}async runAllExpressions(e,t=!1){return this._evaluateSpecifiedComponentsForEachFeature(e,v,t)}async runGeometryDependentExpressions(e,t=!1){return this._evaluateSpecifiedComponentsForEachFeature(e,x,t)}async valueChanged(e,t,r=!0){try{this.calculating=!0;const a=t.map(t=>this.evaluateValueChangedForFeature(e,t,r));await Promise.all(a)}catch(s){a.getLogger(this).error(s),this.expressionEvaluationFailed=!0}finally{this.calculating=!1}}async evaluateValueChangedForFeature(e,t,r){if(this._abortController.signal.aborted)return;const a=this.layerExpressionsModelMap.get(t.layer);if(!a||0===a.components.length)return;const s=new Set,o=new Set([e]),i=new Set;for(;o.size>0;){const[e]=o;if(o.delete(e),s.has(e))continue;s.add(e);const n=a.fieldReferencesMap.get(e.toLowerCase());if(!n)continue;for(const t of n)i.has(t)&&n.delete(t);if(0===n.size)continue;const l=new Set;await this.evaluateAffectedComponents([...n],t,a,r,l);for(const t of n)i.add(t);for(const t of l)s.has(t)||o.add(t)}}async evaluateAffectedComponents(e,t,r,a,s){const o=new Map;for(const n of e)o.has(n)||o.set(n,this.createSccEvaluator(n,t,o,r));if(0===o.size)return;const i=[...o.values()].map(e=>e.evaluate(a,s));await Promise.all(i)}createSccEvaluator(e,t,r,a){const o=new g(e,t,this.arcadeContext,a,this.evaluatedExpressions,this._abortController);for(const i of e.adjacencyList)o.dependentEvaluators.push(s(r,i,()=>this.createSccEvaluator(i,t,r,a)));return o}async _evaluateSpecifiedComponentsForEachFeature(e,t,r=!1){this.calculating=!0;try{const a=[];for(const s of e){const e=this.layerExpressionsModelMap.get(s.layer);if(!e||0===e.components.length)continue;const o=new Set;a.push(this.evaluateAffectedComponents(t(e),s,e,r,o))}await o(Promise.all(a),this._abortController)}catch(s){a.getLogger(this).error(s),this.expressionEvaluationFailed=!0}finally{this.calculating=!1}}};e([n()],m.prototype,"calculating",void 0),e([n({constructOnly:!0})],m.prototype,"arcadeContext",void 0),e([n()],m.prototype,"_abortController",void 0),e([n()],m.prototype,"layerExpressionsModelMap",void 0),e([n()],m.prototype,"evaluatedExpressions",void 0),e([n()],m.prototype,"expressionEvaluationFailed",void 0),m=e([l("esri.widgets.BatchAttributeForm.expressions.ExpressionsManager")],m);class g{constructor(e,t,r,a,s,o){this.scc=e,this.feature=t,this.arcadeContextInfo=r,this.expressionsModel=a,this.evaluatedExpressions=s,this._abortController=o,this.dependentEvaluators=[],this._evaluatorPromise=null}get baseContext(){const{editType:e,map:t,spatialReference:r,timeZone:a}=this.arcadeContextInfo,s=this.feature.layer,o="scene"===s?.type&&null!=s.associatedLayer?s.associatedLayer:s;return{variables:{$originalfeature:this.feature.original,$editcontext:{editType:e},$layer:o,$featureset:o,$datastore:o?.url,$feature:this.feature.plainGraphic,$map:t},executeContext:{rawOutput:!0,spatialReference:r??void 0,timeZone:a,abortSignal:this._abortController.signal}}}evaluate(e,t){return null==this._evaluatorPromise&&(this._evaluatorPromise=this._evaluate(e,t)),this._evaluatorPromise}async _evaluate(e,t){null===h&&(h=await import("../../../arcade/languageUtils.js"));const a=this.dependentEvaluators.map(r=>r.evaluate(e,t));await o(Promise.all(a),this._abortController);for(const o of this.scc.executors){const a=this.baseContext;if(this._abortController.signal.aborted)break;const l=this.expressionsModel.arcadeExecutorUses.get(o)??[],u=s(this.evaluatedExpressions,this.feature,()=>new i);let c=null,h=null;try{c=o.isAsync?await o.executeAsync(a.variables,a.executeContext):o.execute(a.variables,a.executeContext),h={result:c,status:"success"}}catch(n){h={error:n,status:"error"}}for(const r of l){const a=this.expressionsModel.elementTemplateMap.get(r.elementId);switch(r.executorPurpose){case"editable":case"required":case"text":u.set(p(r),h);break;case"value":{if("error"===h?.status)continue;const e=a.getExpressionExecutorsForLayer(this.feature.layer);let r=!0;if(e?.editableExpression&&(r=!1===u.get(p({elementId:a.elementId,executorPurpose:"editable"}))?.result),r){const e=this.feature.getAttribute(a.fieldName)??null,r=y(h.result,this.feature,a.fieldName,this.arcadeContextInfo.timeZone);r!==e&&(this.feature.setAttribute(a.fieldName,r),t.add(a.fieldName))}}break;case"visibility":if(u.set(p(r),h),!this.expressionsModel.preserveFieldValuesWhenHidden&&!1===h.result&&e)if(d(a)){for(const e of a.elements)if(f(e)){null!==this.feature.getAttribute(e.fieldName)&&t.add(e.fieldName),this.feature.setAttribute(e.fieldName,null)}}else if(f(a)){null!==this.feature.getAttribute(a.fieldName)&&t.add(a.fieldName),this.feature.setAttribute(a.fieldName,null)}}}if("error"===h?.status){const e=new Set;for(const t of l){const r=this.expressionsModel.elementTemplateMap.get(t.elementId);if(r)switch(t.executorPurpose){case"editable":e.add(r.label+" - Editable expression");break;case"required":e.add(r.label+" - Required expression");break;case"value":e.add(r.label+" - Value expression");break;case"visibility":e.add(r.label+" - Visibility expression")}}if(e.size>0)throw new r("expression-evaluation-failed","Arcade evaluation failed ("+[...e].join(",")+")");throw new r("expression-evaluation-failed","Arcade evaluation failed ")}}}}function y(e,t,a,s){if(null===e)return null;const o=t.layer.fieldsIndex.get(a);if(!o)throw new r("field-not-found","Unable set field value as the field cannot be found. ("+a+")");switch(o.type){case"big-integer":case"integer":case"long":case"oid":case"small-integer":case"single":case"double":return h.toNumber(e);case"global-id":case"guid":case"string":return h.toString(e);case"date":{if(h.isDate(e))return e.getTime();const t=h.toDate(e,s);if(!t)throw new r("unsupported-value-type","Arcade return type is not a supported field type. ("+a+")");return t.getTime()}case"date-only":{if(h.isDateOnly(e))return e.toStorageFormat();if(h.isDate(e))return u.fromDateTime(e.toDateTime()).toStorageFormat();const t=u.fromString(h.toString(e));if(!t)throw new r("unsupported-value-type","Arcade return type is not a supported field type.");return t.toStorageFormat()}case"time-only":{if(h.isTime(e))return e.toStorageString();if(h.isDate(e)){return c.fromDateTime(e.toDateTime()).toStorageString()}const t=c.fromString(h.toString(e));if(!t)throw new r("unsupported-value-type","Arcade return type is not a supported field type.");return t.toStorageString()}case"timestamp-offset":{if(h.isDate(e))return e.toISOString(!1);const t=h.toDate(e,s);if(!t)throw new r("unsupported-value-type","Arcade return type is not a supported field type. ("+a+")");return t.toISOString(!1)}}throw new r("unsupported-value-type","Arcade return type is not a supported field type. ("+a+")")}function v(e){return e.components}function x(e){return Array.from(e.geometryReferences)}export{m as ExpressionsManager};