UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) • 12.3 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import{createTask as t}from"../../core/asyncUtils.js";import s from"../../core/Collection.js";import r from"../../core/Error.js";import{EventedAccessor as i}from"../../core/Evented.js";import a from"../../core/Logger.js";import{abortMaybe as o}from"../../core/maybe.js";import{throwIfAborted as n,isAbortError as p}from"../../core/promiseUtils.js";import l from"../../core/ReactiveMap.js";import{on as u,watch as h,sync as m,whenOnce as d}from"../../core/reactiveUtils.js";import{property as c,subclass as f}from"../../core/accessorSupport/decorators.js";import{UpdatingHandles as y}from"../../core/support/UpdatingHandles.js";import v from"../../geometry/SpatialReference.js";import{system as g}from"../../time/constants.js";import{createReactiveGraphic as F}from"./batchAttributeFormUtils.js";import{ArcadeExecutorProvider as _}from"./expressions/ArcadeExecutorProvider.js";import{ExpressionsManager as x}from"./expressions/ExpressionsManager.js";import{ExpressionsModel as w}from"./expressions/ExpressionsModel.js";import I from"./inputs/BatchFormInputs.js";import{createBatchFormInputsFromBatchFormTemplate as b}from"./inputs/support/createBatchFormInputs.js";import{isGroupInput as M,isFieldInput as A}from"./inputs/support/inputUtils.js";import E from"./templates/BatchFormTemplate.js";import{getLayerContingentValuesMetaData as k}from"./templates/support/contingentValuesUtils.js";import{createBatchFormTemplate as T}from"./templates/support/createBatchFormTemplate.js";import{isExcludedFromSharedForm as C,isGroupElementTemplate as V}from"./templates/support/templateUtils.js";function j(e,t){return e?.find(e=>e.layer===t)}let H=class extends i{constructor(e){super(e),this.activeFeatureIndex=-1,this.disabled=!1,this.editType="NA",this.features=new s,this._hasAsyncArcadeExpressions=!1,this.maximumFeatureCount=2e3,this.maximumFeatureCountWithComplexForms=50,this.map=null,this.readOnly=!1,this.spatialReference=null,this.submitHasBeenAttempted=!1,this.timeZone=null,this.userHasChangedValues=!1,this.layerInfos=null,this._arcadeExecutorProvider=new _,this._activeFormInputsByElementId=new Map,this._emptyForm=new I({inputs:[]}),this._emptyFormTemplate=new E({elements:[]}),this._featureFormMap=new Map,this._prepareTask=null,this._reactiveGraphicLookup=new l,this._updatingHandles=new y,this._layerTemplateMap=new l,this._layerContingentValuesMap=new l,this._workingFeatures=new s,this._expressionsManager=new x({arcadeContext:{editType:"NA",spatialReference:null,map:null,timeZone:g}}),this.sharedForm=this._emptyForm,this.sharedFormTemplate=this._emptyFormTemplate}initialize(){this.addHandles([u(()=>this.features,"after-changes",()=>this._prepare(),{sync:!0}),h(()=>[this.features,this.map,this.timeZone,this.editType],()=>this._prepare(),m),h(()=>this.activeForm,()=>this._activeFormInputsByElementId.clear(),m)]),this._prepare()}destroy(){this._prepareTask=o(this._prepareTask),this._workingFeatures.destroyAll(),this._expressionsManager&&this._expressionsManager.abort(),this._updatingHandles.destroy(),this._reactiveGraphicLookup=new l,this._emptyForm.destroy(),this._emptyFormTemplate.destroy()}get _effectiveTimeZone(){return this.timeZone??"system"}get activeFeature(){const e=this.activeFeatureIndex;return e<0?null:this.features.at(e)}get activeForm(){if("batch"===this.mode)return this.sharedForm;const e=this._workingFeatures.at(this.activeFeatureIndex);if(!e)return this._emptyForm;const t=this._featureFormMap.get(e);if(t)return t;const s=this._makeBatchFormInputsForFeature(e);return s!==this._emptyForm&&this._featureFormMap.set(e,s),s}get calculating(){return this._expressionsManager.calculating}get expressionEvaluationFailed(){return this._expressionsManager.expressionEvaluationFailed}get hasNonActiveInvalidFeatures(){if("batch"===this.mode)return!1;const{activeFeature:e}=this;return this.invalidFeatures.some(t=>t!==e)}get hasAsyncArcadeExpressions(){return this._hasAsyncArcadeExpressions}get hasTooManyFeatures(){return this._workingFeatures.length>this.maximumFeatureCount}get hasTooManyComplexFeatures(){return this._workingFeatures.length>this.maximumFeatureCountWithComplexForms&&this.hasAsyncArcadeExpressions}get invalidFeatures(){return this.sharedForm.invalidFeatures}get hasVisibleInputs(){return this.visibleInputs.length>0}get hasLayersWithContingentValues(){for(const e of this.layers){const t=this._layerContingentValuesMap.get(e);if(t&&t.size>0)return!0}return!1}get visibleInputs(){return this.activeForm.inputs.filter(e=>e.visible)}get noVisibleElementsReason(){if(this.hasVisibleInputs)return null;if(0===this.activeForm.inputs.length)return"noElements";const e=new Set;for(const t of this.activeForm.inputs)switch(t.visibilityCode){case"hidden:not-in-all-layers":case"hidden:no-domain-in-common":e.add("noElementsInCommon");break;case"hidden:field-definition":case"hidden:group-visibility-expression:all-features":case"hidden:visibility-expression:all-features":e.add("allElementsHidden");break;case"hidden:group-visibility-expression:some-features":case"hidden:visibility-expression:some-features":return"elementsHiddenInSome"}return e.has("allElementsHidden")?"allElementsHidden":"noElementsInCommon"}get status(){const e=this._prepareTask;return null==e?"not-loaded":e.finished?null!=e.error?"failed":"loaded":"loading"}get submittable(){return this.valid,!0}get updating(){return this._updatingHandles.updating||this.calculating}get valid(){return this.sharedForm.valid}get layers(){const e=new Set;return this.features.forEach(t=>{const s=t.sourceLayer??t.layer;e.add(s)}),Array.from(e)}get mode(){return this.activeFeatureIndex>-1?"single":"batch"}submit(){this.submitHasBeenAttempted=!0,this.emit("submit",{name:"submit",results:this._makeSubmitResults(),valid:this.valid})}findFieldInput(e){if(null==e)return;const t=this._activeFormInputsByElementId;if(t.has(e))return t.get(e);const s=this.activeForm.allFieldInputs.find(t=>t.template.elementId===e);return void 0!==s?(t.set(e,s),s):void 0}getFirstVisibleInvalidFieldInput(){if(this.hasVisibleInputs&&!this.activeForm.valid)for(const e of this.visibleInputs){if(M(e)){const t=e.inputs.find(e=>A(e)&&!e.valid);if(t)return{input:t,groupInput:e}}if(A(e)&&!e.valid)return{input:e}}}getFieldInputValue(e){return this.findFieldInput(e)?.value}getValues(e){const t=this._reactiveGraphicLookup.get(e);if(!t)throw new r("feature-not-found","The given feature is not present in the BatchAttributeForm");return{...t.attributes}}notifyGeometriesChanged(e){const t=[];for(const s of e){const e=this._reactiveGraphicLookup.get(s);e&&(t.push(e),e.geometry=s.geometry)}this._expressionsManager.runGeometryDependentExpressions(t)}async setFieldInputValue(e,t){const s=new Set(this.invalidFeatures);await e.setValueFromUser(t),this.userHasChangedValues=!0,this._trackValidityChange(s),this.emit("value-change",{features:e.features.toArray().map(e=>e.source),fieldName:e.fieldName,name:"value-change",value:t})}async setValue(e,t){const s=this.findFieldInput(e);if(null==s)throw new r("no-FieldInput-found",`Cannot set the value of FieldInput with ID: ${e} because none was found in the active form`);await this.setFieldInputValue(s,t)}userChangesHaveMadeFeatureInvalid(e){return!!this._reactiveGraphicLookup.get(e)?.userChangesHaveMadeInvalid}async _trackValidityChange(e){await d(()=>!1===this.calculating);const t=this.invalidFeatures;for(const s of t)!1===e.has(s)&&this._reactiveGraphicLookup.has(s)&&(this._reactiveGraphicLookup.get(s).userChangesHaveMadeInvalid=!0)}validate(){return!1}_makeBatchFormInputsForFeature(e){const t=this._layerTemplateMap.get(e.layer);return t?b(t,new s([e]),this._expressionsManager):this._emptyForm}_makeSubmitResults(){const e=new Map;for(const[t,s]of this._reactiveGraphicLookup)e.set(t,{feature:t,values:{...s.attributes},invalidFields:[]});for(const t of this.sharedForm.allFieldInputs)for(const s of t.invalidFeatures)e.get(s)?.invalidFields.push(t.fieldName);return Array.from(e.values())}_prepare(){this._hasAsyncArcadeExpressions=!1,this._prepareTask=o(this._prepareTask),this._updateWorkingFeatures(),o(this._expressionsManager),this.userHasChangedValues=!1,this._expressionsManager=new x({arcadeContext:{editType:this.editType,spatialReference:this.spatialReference??v.WebMercator,map:this.map,timeZone:this._effectiveTimeZone}});const{layers:e}=this;if(0===e.length)return;const s=t(async t=>{try{n(t);const s=new Map;for(const r of e){const e=await k(r);if(n(t),e){const t=e.fieldGroups.flatMap(e=>e.fields);this._layerContingentValuesMap.set(r,new Set(t))}else this._layerContingentValuesMap.set(r,new Set);const i=j(this.layerInfos,r),a=await T(r,{arcadeExecutorProvider:this._arcadeExecutorProvider,formTimeZone:this._effectiveTimeZone},i?.formTemplate);n(t),this._layerTemplateMap.set(r,a);const o=a.getExpressionExecutorsForLayer(r);if(this._expressionsManager.layerExpressionsModelMap.set(r,new w({preserveFieldValuesWhenHidden:a.preserveFieldValuesWhenHidden,executorMap:o})),!this._hasAsyncArcadeExpressions)for(const t of o.keys()){const e=o.get(t);if(this._hasAsyncArcadeExpressions=!0===(e.editableExpression?.isAsync||e.requiredExpression?.isAsync||e.valueExpression?.isAsync||e.visibilityExpression?.isAsync),this._hasAsyncArcadeExpressions)break}for(const t of a.elements){if(C(t))continue;const e=t.clone();V(e)&&(e.elements=e.elements.filter(e=>!C(e)));const{elementId:r}=e;s.has(r)?s.get(r).foldIn(e):s.set(r,e)}}if(this.hasTooManyComplexFeatures)throw new r("too-many-features-with-complex-forms","There are too many features to load into the form with the configured complexity");if(this.hasTooManyFeatures)throw new r("too-many-features","There are too many features to load into the form.");const i=new E({elements:Array.from(s.values())});this.sharedFormTemplate=i,this.sharedForm=b(i,this._workingFeatures,this._expressionsManager),await this._expressionsManager.runAllExpressions(this._workingFeatures.toArray())}catch(s){throw p(s)||a.getLogger(this).error("Failed preparing form",s),s}});this._updatingHandles.addPromise(s.promise),this._prepareTask=s}_updateWorkingFeatures(){this._workingFeatures.destroyAll();const{features:e}=this;if(this._reactiveGraphicLookup=new l,0!==e.length){this._workingFeatures.addMany(e.map(F));for(const e of this._workingFeatures)this._reactiveGraphicLookup.set(e.source,e)}}};e([c()],H.prototype,"_effectiveTimeZone",null),e([c({readOnly:!0})],H.prototype,"activeFeature",null),e([c()],H.prototype,"activeFeatureIndex",void 0),e([c({readOnly:!0})],H.prototype,"activeForm",null),e([c()],H.prototype,"disabled",void 0),e([c()],H.prototype,"calculating",null),e([c()],H.prototype,"editType",void 0),e([c()],H.prototype,"features",void 0),e([c()],H.prototype,"expressionEvaluationFailed",null),e([c()],H.prototype,"hasNonActiveInvalidFeatures",null),e([c()],H.prototype,"hasAsyncArcadeExpressions",null),e([c()],H.prototype,"_hasAsyncArcadeExpressions",void 0),e([c()],H.prototype,"invalidFeatures",null),e([c()],H.prototype,"hasVisibleInputs",null),e([c()],H.prototype,"hasLayersWithContingentValues",null),e([c()],H.prototype,"maximumFeatureCount",void 0),e([c()],H.prototype,"maximumFeatureCountWithComplexForms",void 0),e([c()],H.prototype,"visibleInputs",null),e([c()],H.prototype,"map",void 0),e([c()],H.prototype,"noVisibleElementsReason",null),e([c()],H.prototype,"readOnly",void 0),e([c()],H.prototype,"spatialReference",void 0),e([c()],H.prototype,"submitHasBeenAttempted",void 0),e([c()],H.prototype,"timeZone",void 0),e([c()],H.prototype,"updating",null),e([c()],H.prototype,"valid",null),e([c()],H.prototype,"layers",null),e([c()],H.prototype,"mode",null),e([c()],H.prototype,"sharedForm",void 0),e([c()],H.prototype,"sharedFormTemplate",void 0),e([c()],H.prototype,"userHasChangedValues",void 0),e([c()],H.prototype,"layerInfos",void 0),e([c()],H.prototype,"_arcadeExecutorProvider",void 0),e([c()],H.prototype,"_prepareTask",void 0),e([c()],H.prototype,"_reactiveGraphicLookup",void 0),e([c()],H.prototype,"_layerTemplateMap",void 0),e([c()],H.prototype,"_layerContingentValuesMap",void 0),e([c()],H.prototype,"_workingFeatures",void 0),e([c()],H.prototype,"_expressionsManager",void 0),H=e([f("esri.widgets.BatchAttributeForm.BatchAttributeFormViewModel")],H);const L=H;export{L as default};