@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 8.91 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__decorate as e}from"tslib";import{a2 as t}from"../../chunks/languageUtils.js";import n from"../../core/Accessor.js";import s from"../../core/Collection.js";import{getOrCreateMapValue as o}from"../../core/MapUtils.js";import{destroyMaybe as i}from"../../core/maybe.js";import{EsriPromiseMixin as a}from"../../core/Promise.js";import r from"../../core/ReactiveMap.js";import{on as l}from"../../core/reactiveUtils.js";import{property as c,subclass as u}from"../../core/accessorSupport/decorators.js";import f from"../../layers/support/ContingentValue.js";import d from"../../layers/support/LayerContingentValuesCache.js";import{isFeatureLayer as p,isSubtypeGroupLayer as h}from"../../layers/support/layerUtils.js";import{valueIsValidContingentValue as g}from"../FeatureForm/featureFormUtils.js";const m="#JSAPI_CONTINGENT_VALUE_EMPTY_HASH",y="#JSAPI_CONTINGENT_VALUE_ANY_HASH",_="#JSAPI_CONTINGENT_VALUE_NULL_HASH",v={_any:"any",_code:"code",_null:"null",_range:"range"};let V=class extends(a(n)){constructor(e){super(e),this._cacheMap=new r,this._joinedContingenciesMap=new Map,this._optionsMap=new Map,this._violationsMap=new Map,this.layers=new s}initialize(){this.layers.length&&this._onLayersChange(this.layers.toArray()),this.addHandles([l(()=>this.layers,"change",({added:e})=>this._onLayersChange(e))])}destroy(){this.reset()}get fieldsWithContingentValues(){const e=new r;return this._cacheMap.forEach((t,n)=>{e.set(n,new Set(t.fieldGroups.flatMap(e=>e.fields)))}),e}clear(){this._joinedContingenciesMap.clear(),this.resetOptions(),this.resetViolations()}getViolationForField(e,t,n){const s=this._violationsMap.get(e),o=I(e,t),i=s?.get(o);return i?.get(n)||null}getAllViolations(e,t){const n=this._violationsMap.get(e),s=I(e,t),o=n?.get(s);return o?Array.from(o.values()):[]}getFieldValueOptions(e){const{feature:t,layer:n,fieldName:s,emptyString:o}=e,i=A(n,t,s),a=t.getAttribute(s),l=this._optionsMap.get(n),c=I(n,t),u=l?.get(c);if(u){const e=u.get(s);if(e)return e}const f="coded-value"===i?.type?i.codedValues.map(({name:e,code:t})=>({name:e,value:t})):[],d=null==a||f.some(e=>e.value===a)?[]:[{name:`${a}`,value:a}],p=S(n)&&this.fieldsWithContingentValues.get(n)?.has(s)?this._getContingentValueOptionsForField(n,t,s,o):[];let h=null;if(p.length>0){const e=new Set(p.map(e=>e.value));h={recommended:p,other:f.filter(t=>!e.has(t.value)),unsupported:d}}else h={recommended:f,other:[],unsupported:d};if(u)u.set(s,h);else{const e=new r([[s,h]]);l?l.set(c,e):this._optionsMap.set(n,new r([[c,e]]))}return h}getJoinedContingentValues(e,t){return this._joinContingentValues(e,t)}reset(){this.clear(),this._cacheMap.forEach(i),this._cacheMap.clear()}resetOptions(){this._optionsMap.clear()}resetOptionsForFeature(e,t){this._optionsMap.get(e)?.get(t)?.clear()}resetOptionsForLayer(e){this._optionsMap.get(e)?.clear()}resetViolations(){this._violationsMap.clear()}resetViolationsForFeature(e,t){this._violationsMap.get(e)?.get(t)?.clear()}resetViolationsForLayer(e){this._violationsMap.get(e)?.clear()}validateContingencyConstraints(e,t,n){const s=this._cacheMap.get(e);if(!s)return[];const i=F(t),{invalid:a,incomplete:l}=s.validateContingencyConstraints(i),c=[...a,...n?.includeIncompleteViolations?l:[]],u=new r;for(const o of c)for(const e of o.fieldGroup.fields)u.set(e,o);const f=I(e,t);return o(this._violationsMap,e,()=>new r).set(f,u),c}validateContingenciesForNonNullFields(e,t){const n=t.clone();n.attributes=Object.fromEntries(Object.entries(F(t)).filter(([e,t])=>null!=t)),this.validateContingencyConstraints(e,n)}_onLayersChange(e){e.forEach(e=>{let n=null;t(e)?n=e.parent:(p(e)||h(e))&&(n=e),n&&!this._cacheMap.get(n)&&this.addResolvingPromise(d.createLoadedLayerContingentValuesCache(n).then(e=>{e&&this._cacheMap.set(n,e)}))})}_getContingentValueOptionsForField(e,t,n,s){const o=this.getJoinedContingentValues(e,t),i=new Map,a={code:"",name:s??"No value"},{_code:r,_null:l}=v;for(const c of o){const s=c.values[n];if(s?.objectType===r||s?.objectType===l){const{code:o,name:r}=s.codedValue&&s.objectType!==l?s.codedValue:a;!i.has(o)&&this._isValidContingency(e,t,n,c)&&i.set(o,{name:r,value:o})}}return[...i.values()]}_isValidContingency(e,t,n,s){const o={};for(const[i,a]of Object.entries(F(t)))this.fieldsWithContingentValues.get(e)?.has(i)&&null!=a&&i!==n&&(o[i]=a);return Object.entries(o).every(([e,t])=>!s.values.hasOwnProperty(e)||g(t,s.values[e]))}_joinContingentValues(e,t){const n=this._cacheMap.get(e);if(!t||!n)return[];const s=this._joinedContingenciesMap.get(e),o=I(e,t),i=s?.get(o);if(i)return i;const a=n.subtypeFieldName,l=a?t.attributes[a]:null,c=[];for(const r of n.fieldGroups){const{contingencies:e,fields:t,name:n}=r,s=[];for(const o of e)o.isRetired||null!=o.subtype&&o.subtype.code!==l||s.push({values:o.values});s.length>0&&c.push({name:n,fields:t,contingencies:s})}const[u,f]=C(c),d=u.flatMap(n=>L(e,t,n)),p=f.flatMap(e=>e.contingencies),h=[...d,...p];return s?s.set(o,h):this._joinedContingenciesMap.set(e,new r([[o,h]])),[...d,...p]}};function M(e,t){const{_any:n,_null:s,_range:o}=v;return{leftIsNull:e===s,rightIsNull:t===s,bothAreNull:e===s&&t===s,leftIsAny:e===n,rightIsAny:t===n,bothAreAny:e===n&&t===n,leftIsRange:e===o,rightIsRange:t===o}}function j(e,t,n){const{_any:s}=v,o=Array.from(n);return t.filter(t=>o.every(n=>{const o=t.values[n],i=e.values[n];return o.objectType===s||i.objectType===s||o.codedValue?.code===i.codedValue?.code}))}function b(e,t){const n=new RegExp(`${t}$`),s=[];for(const[o,i]of e){if(o===y)break;n.test(o)&&s.push(...i)}return s}function w(e,t,n,s){const o=Array.isArray(n)?new Set(n):n,i=Array.isArray(s)?new Set(s):s,a=o.size<i.size?o:i,r=a===o?i:o,l=new Set,c=new Set;for(const u of a)if(r.has(u)){const n=A(e,t,u,!0);if(null==n)continue;switch(n.type){case"coded-value":l.add(u);break;case"range":c.add(u)}}return{codedValueFields:l,rangeFields:c}}function C(e){const t=new Map,n=[],s=new Set;for(const l of e)for(const e of l.fields)if(t.has(e)){const o=t.get(e),i=l,a=[o.name,i.name].sort().join("|");if(s.has(a))continue;n.push([o,i]),s.add(a),t.set(e,i)}else t.set(e,l);const o=new Set(n.flat()),i=new Set(e);for(const l of o)i.delete(l);const a=new Map,r=new Map;for(const l of new Set(n.flat())){const e=Symbol(l.name);a.set(e,new Set([l])),r.set(l,e)}for(const[l,c]of n){const e=r.get(l),t=r.get(c);if(e===t)continue;const n=a.get(e),s=a.get(t);for(const o of s)n.add(o),r.set(o,e);a.delete(t)}return[[...a.values()].map(e=>[...e]),[...i]]}function F(e){return e.attributes}function A(e,t,n,s=!1){return e.getFieldDomain(n,{feature:t,excludeImpliedDomains:s})}function I(e,t){return t.getObjectId()??t.attributes[e.objectIdField]??t}function N(e,t,n=!1){if(t.size<1)return[m,!1];const{_any:s,_null:o}=v,i=[];let a=!1;for(const r of t.values()){const t=e.values[r];if(t.objectType===s){if(n)return[y,!0];a=!0,i.push(`${r}:.*`)}else t.objectType===o?i.push(`${r}:${_}`):i.push(`${r}:${t.codedValue?.code}`)}return[i.sort().join("&"),a]}function S(e){return p(e)||h(e)}function T(e,t){const n={values:{...e.values,...t.values}};for(const[s,o]of Object.entries(n.values))o.objectType===v._any&&null!=e.values[s]&&(n.values[s]=e.values[s]);return n}function O(e,t,n){const s=T(e,t),{_null:o,_range:i}=v;for(const a of n.values()){const n=e.values[a],r=t.values[a],{leftIsNull:l,rightIsNull:c,bothAreNull:u,leftIsAny:d,rightIsAny:p,bothAreAny:h,leftIsRange:g,rightIsRange:m}=M(n.objectType,r.objectType);if(u||h)continue;if(l&&p||c&&d){s.values[a]=new f({objectType:o});continue}if(l&&m||c&&g)return null;d?(n.minValue=-1/0,n.maxValue=1/0):p&&(r.minValue=-1/0,r.maxValue=1/0);const y=Math.max(n.minValue,r.minValue),_=Math.min(n.maxValue,r.maxValue);if(y>_)return null;s.values[a]=new f({objectType:i,minValue:y,maxValue:_})}return s}function L(e,t,n){const s=n.slice(),o=s.shift();let i=s.shift(),a=w(e,t,o.fields,i.fields),r=new Set([...o.fields,...i.fields]),l=new Map;for(const u of o.contingencies){const[e]=N(u,a.codedValueFields,!0);l.has(e)?l.get(e)?.push(u):l.set(e,[u])}for(;s.length>0;){const n=new Map,o=s.shift(),c=w(e,t,r,o.fields);for(const e of i.contingencies){const[t,s]=N(e,a.codedValueFields),o=s?b(l,t):l.get(t);if(l.has(y)&&o?.push(...j(e,l.get(y),a.codedValueFields)),null!=o)for(const i of o){const t=a.rangeFields.size>0?O(i,e,a.rangeFields):T(i,e);if(null==t)break;const[s]=N(t,c.codedValueFields,!0);n.has(s)?n.get(s)?.push(t):n.set(s,[t])}}l=n,i=o,a=c,r=new Set([...r,...i.fields])}const c=[];for(const u of i.contingencies){const[e,t]=N(u,a.codedValueFields),n=t?b(l,e):l.get(e);if(l.has(y)&&n.push(...j(u,l.get(y),a.codedValueFields)),null!=n)for(const s of n){const e=a.rangeFields.size>0?O(s,u,a.rangeFields):T(s,u);null!=e&&c.push(e)}}return c}e([c()],V.prototype,"fieldsWithContingentValues",null),e([c()],V.prototype,"layers",void 0),V=e([u("esri.widgets.support.ContingentValuesManager")],V);const E=V;export{E as default};