@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 10.7 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../../request.js";import{isSome as n}from"../../core/arrayUtils.js";import{JSONSupportMixin as i}from"../../core/JSONSupport.js";import{Loadable as s}from"../../core/Loadable.js";import{property as o,subclass as r}from"../../core/accessorSupport/decorators.js";import{parse as a}from"./arcgisLayerUrl.js";import l from"./Contingency.js";import u from"./ContingencyConstraintViolation.js";import c from"./ContingentValue.js";import p from"./ContingentValuesResult.js";import d from"./FieldGroup.js";import{isStringField as f,isDateField as y}from"./fieldUtils.js";import m from"./Subtype.js";import{getNormalizedFeatureTypeInfo as g}from"../../widgets/FeatureForm/featureFormUtils.js";var h;let V=h=class extends(i(s)){constructor(e){super(e),this.request=t,this.fieldGroups=null,this.fieldGroupDefinitions=null}initialize(){}get loaded(){return super.loaded}get subtypeFieldName(){return g(this.layer).typeFieldName}static async createLoadedLayerContingentValuesCache(e){await e.load();const t=e.sourceJSON;if(void 0===e.layerId)return null;const n=t?.hasContingentValuesDefinition;if(n)return new h({layer:e}).load();if(void 0===n){const t=a(e.url);if(null==t)return null;const n=t.url.path;if((await C(n)).supportsQueryDataElements){const t=e.layerId.toString(),i=await x(n,t);if(i){const t=i.map(t=>({name:t.name,isEditingRestrictive:t.isEditingRestrictive,fields:t.fieldNames.names.map(t=>e.fieldsIndex.normalizeFieldName(t))}));return new h({layer:e,fieldGroupDefinitions:t}).load()}}}return null}async load(e){const t=this.layer.load(e).then(()=>this._initializeContingentValues(this.fieldGroupDefinitions,e));return this.addResolvingPromise(t),this}validateContingencyConstraints(e,t){const n=Object.keys(e),i=[],s=[];for(const o of this.fieldGroups){const r=o.isEditingRestrictive?"error":"warning";if(t&&!o.fields.some(e=>t.includes(e)))continue;if(!o.fields.every(e=>n.includes(e))){s.push(new u({fieldGroup:o,type:r}));continue}let a=!1;const l=e[this.subtypeFieldName],c=o.contingencies.filter(e=>!(!e.isRetired&&e.subtype)||e.subtype.code===l);if(0!==c.length){for(const t of c){let n=!0;for(const i in t.values){const s=t.values[i];if("any"!==s.objectType)if("null"===s.objectType){if(null!=e[i]){n=!1;break}}else if("code"===s.objectType){if(e[i]!==s.codedValue.code){n=!1;break}}else if("range"===s.objectType){const t=e[i];if(t<s.minValue||t>s.maxValue){n=!1;break}}}if(n){a=!0;break}}a||i.push(new u({fieldGroup:o,type:r}))}}return{invalid:i,incomplete:s}}getContingentValues(e,t,n=!1){const i=e[this.subtypeFieldName],s=null!=i,o={};let r=[];const a=this.fieldGroups.filter(e=>e.fields.includes(t));r.push(new c({objectType:"any"}));for(const l of a){let a=[];const u=l.contingencies.filter(e=>!(e.isRetired||e.subtype&&s&&e.subtype.code!==i));let c=!1;const p={};for(const n of u){let i,s=!0;for(const o in n.values){const r=n.values[o];if(t!==o){if(void 0!==e[o]&&"any"!==r.objectType)if("null"===r.objectType)null!==e[o]&&(s=!1);else if("code"===r.objectType)e[o]!==r.codedValue.code&&(s=!1);else if("range"===r.objectType){const t=e[o];(t<r.minValue||t>r.maxValue)&&(s=!1)}}else i=r,c=c||"range"===r.objectType}if(i&&s){if("any"===i.objectType){a.length=0,a.push(i);break}const e=b(i);p[e]||a.push(i),p[e]=!0}}c&&(a=j(a)),o[l.name]=a,r=n?G(r,a):[]}return 1===a.length&&n&&(r=[]),new p({contingentValuesAllGroups:r,contingentValuesByFieldGroup:o})}async _initializeContingentValues(e,t){const n=e??await this._fetchFieldGroupDefs(t);if(0===n.length)return void(this.fieldGroups=[]);const i=await this._fetchContingentValues(n,t);this.fieldGroups=i}async _fetchFieldGroupDefs(e){if(void 0===this.layer.layerId)return[];const t=this.layer.sourceJSON,n=this.layer.layerId.toString(),i=a(this.layer.url).url.path;if(t?.hasContingentValuesDefinition){return(await v(i,n,e)).map(e=>({name:e.name,isEditingRestrictive:e.restrictive,fields:e.fields.map(e=>this.layer.fieldsIndex.normalizeFieldName(e))}))}return void 0!==t?.hasContingentValuesDefinition?[]:C(i,e).then(async t=>{if(t.supportsQueryDataElements){return(await x(i,n,e)).map(e=>({name:e.name,isEditingRestrictive:e.isEditingRestrictive,fields:e.fieldNames.names.map(e=>this.layer.fieldsIndex.normalizeFieldName(e))}))}return[]})}async _fetchContingentValues(e,t){if(void 0===this.layer.layerId)return[];const n=this.layer.sourceJSON,i=this.layer.layerId.toString(),s=a(this.layer.url).url.path;if(n?.hasContingentValuesDefinition){const n=await S(s,i,t);return this._constructFieldGroupsAGOL(e,n)}const o=await F(s,i,t);return this._constructFieldGroupsEnterprise(e,o)}_constructFieldGroupsAGOL(e,t){return e.map(e=>{const n=t.contingentValuesDefinition.fieldGroups.find(t=>t.name===e.name);if(n){let i=[];return i=t.contingentValuesDefinition.hasSubType?this._parseAGOLFieldGroupSubtype(e,t,n.subTypes):this._parseAGOLFieldGroup(e,t,n.contingentValues),new d({name:e.name,isEditingRestrictive:e.isEditingRestrictive,fields:e.fields,contingencies:i})}return null}).filter(n)}_parseAGOLFieldGroupSubtype(e,t,n){let i=[];return n?.forEach(n=>{const s=this._getSubtypeAGOL(n.name);i=i.concat(this._parseAGOLFieldGroup(e,t,n.contingentValues,s))}),i}_parseAGOLFieldGroup(e,t,n,i=null){const s=[];for(const o of n??[]){const n=this._parseAGOLContingency(o,e,t,i);s.push(n)}return s}_parseAGOLContingency(e,t,n,i){const s=e.id,o=!!e.retired&&1===e.retired,r={};for(let a=0,l=0;a<t.fields.length;a++){const s=t.fields[a],o=n.typeCodes[e.types[a]];if("Code"===o){let t=e.values[l];l++;const o=this._getDomain(i,s),a=this.layer.getField(s);if("string"===a?.type){const e=n.stringDicts.find(e=>e.domain===o?.name);e&&(t=e.entries[t])}const u=o?.codedValues.find(e=>e.code===t),p=new c({codedValue:u,objectType:"code"});r[s]=p}else if("Range"===o){const t=e.values[l];l++;const n=t.range[0],i=t.range[1],o=new c({minValue:n,maxValue:i,objectType:"range"});r[s]=o}else if("Any"===o){const e=new c({objectType:"any"});r[s]=e}else{const e=new c({objectType:"null"});r[s]=e}}return new l({id:s,isRetired:o,subtype:i,values:r})}_constructFieldGroupsEnterprise(e,t){const i=t.fieldGroups;return e.map(e=>{const n=i.find(t=>t.name===e.name);if(n){const i=n.contingencies.map(n=>{const i=n.id,s=n.isRetired||!1,o=this._getSubtypeEnterprise(n.subtypeCode),r={};for(let a=0;a<e.fields.length;a++){const i=e.fields[a];let s=n.values[a];if("number"==typeof s||"string"==typeof s){const e=this._getDomain(o,i),n=this.layer.getField(i);if(f(n))s=t.stringDictionary[s];else if(y(n)){const e=new Date(`${s}+00:00`);s=e.getTime()}const a=e?.codedValues.find(e=>e.code===s),l=new c({codedValue:a,objectType:"code"});r[i]=l}else if("object"==typeof s){const e=s.minValue,t=s.maxValue,n=new c({minValue:e,maxValue:t,objectType:"range"});r[i]=n}else if(s){const e=new c({objectType:"any"});r[i]=e}else{const e=new c({objectType:"null"});r[i]=e}}return new l({id:i,isRetired:s,subtype:o,values:r})});return new d({name:e.name,isEditingRestrictive:e.isEditingRestrictive,fields:e.fields,contingencies:i})}return null}).filter(n)}_getSubtypeEnterprise(e){const t=this.layer.sourceJSON;let n;if(t?.subtypes){const i=t.subtypes.find(t=>t.code===e);n=m.fromJSON(i)}return n||null}_getSubtypeAGOL(e){const t=this.layer.sourceJSON;let n;if(t?.subtypes){const i=t.subtypes.find(t=>t.name===e);n=m.fromJSON(i)}return n||null}_getDomain(e,t){const n=e?O(e,t):this.layer.getFieldDomain(t);return"inherited"===n?.type?this.layer.getFieldDomain(t):n}};function b(e){switch(e.objectType){case"any":return"@any@";case"null":return"@null@";case"code":return e.codedValue.name+e.codedValue.code.toString();case"range":return e.minValue.toString()+"-"+e.maxValue.toString()}}function j(e){let t,n;e.sort((e,t)=>"null"===e.objectType?-1:"null"===t.objectType?1:e.minValue-t.minValue);const i=[];for(const s of e)"null"!==s.objectType?null!=t&&null!=n?n<s.minValue?(i.push(new c({objectType:"range",minValue:t,maxValue:n})),t=s.minValue,n=s.maxValue):n<s.maxValue&&(n=s.maxValue):(t=s.minValue,n=s.maxValue):i.push(s);return i.push(new c({objectType:"range",minValue:t,maxValue:n})),i}function T(e,t){const n=[];let i,s=0;for(const o of e)for(;s<t.length;s++){const e=t[s];if("null"===o.objectType&&"null"===e.objectType)n.push(new c({objectType:"null"}));else{if("null"===o.objectType)break;if("null"===e.objectType)continue}if(o.maxValue<e.minValue)break;if(o.maxValue===e.minValue){n.push(new c({objectType:"range",minValue:o.maxValue,maxValue:e.minValue}));break}if(!(o.minValue>e.maxValue))if(o.minValue!==e.maxValue){if(i=o.minValue>e.minValue?o.minValue:e.minValue,o.maxValue<e.maxValue){n.push(new c({objectType:"range",minValue:i,maxValue:o.maxValue}));break}n.push(new c({objectType:"range",minValue:i,maxValue:e.maxValue}))}else n.push(new c({objectType:"range",minValue:o.minValue,maxValue:e.maxValue}))}return n}function w(e,t){const n=[];for(const i of e)t.some(e=>{if(i.objectType===e.objectType)switch(i.objectType){case"any":case"null":return!0;case"code":return i.codedValue.code===e.codedValue.code&&i.codedValue.name===e.codedValue.name}return!1})&&n.push(i);return n}function G(e,t){if(0===e.length||0===t.length)return[];if("any"===e[0].objectType)return t;if("any"===t[0].objectType)return e;const n="range"===e[0].objectType||"range"===e[1]?.objectType,i="range"===t[0].objectType||"range"===t[1]?.objectType;return n||i?T(e,t):w(e,t)}async function v(e,n,i){return t(`${e}/${n}/fieldgroups`,{responseType:"json",query:{f:"json"},...i}).then(e=>e.data.fieldGroups??[])}async function x(e,n,i){return t(`${e}/queryDataElements`,{responseType:"json",query:{layers:JSON.stringify([n]),f:"json"},...i}).then(e=>e.data.layerDataElements?.[0].dataElement.fieldGroups??[])}async function C(e,n){return t(e,{responseType:"json",query:{f:"json"},...n}).then(e=>e.data)}async function F(e,n,i){return t(`${e}/queryContingentValues`,{responseType:"json",query:{layers:JSON.stringify([n]),f:"json"},...i}).then(e=>e.data.contingentValueSets?.[0])}async function S(e,n,i){return t(`${e}/${n}/contingentValues`,{responseType:"json",query:{f:"json"},...i}).then(e=>e.data)}function O(e,t){const n=e.domains;if(n){const[e,i]=Object.entries(n).find(([e])=>e.trim().toLowerCase()===t.trim().toLowerCase())||[];return i||null}return null}e([o({constructOnly:!0})],V.prototype,"layer",void 0),e([o({constructOnly:!0})],V.prototype,"request",void 0),e([o({type:[d]})],V.prototype,"fieldGroups",void 0),e([o({constructOnly:!0})],V.prototype,"fieldGroupDefinitions",void 0),e([o()],V.prototype,"subtypeFieldName",null),V=h=e([r("esri.layers.support.LayerContingentValuesCache")],V);const D=V;export{D as default};