UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) • 6.15 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{_ as e}from"../chunks/tslib.es6.js";import t from"../request.js";import s from"../core/JSONSupport.js";import o from"../core/Loadable.js";import{sqlIn as r,sqlAnd as n,sqlOr as i}from"../core/sql.js";import{property as a}from"../core/accessorSupport/decorators/property.js";import"../core/has.js";import"../core/Logger.js";import"../core/RandomLCG.js";import{subclass as c}from"../core/accessorSupport/decorators/subclass.js";import{RuleType as u}from"./support/typeUtils.js";import{getAssetFieldNames as p,getRuleValues as l,getCompatibleRuleElements as d,getFeatureSourceLayerId as y,doesRuleAllowAssociation as h}from"./support/utils.js";import{isGraphic as f}from"../support/guards.js";let m=class extends(s.JSONSupportMixin(o)){constructor(e){super(e),this.rulesCategorized={attachment:[],containment:[],connectivity:[]},this.request=t}initialize(){}async load(e){const t=this.rulesLayer.load(e).then((()=>this._initializeRulesTable()));return this.addResolvingPromise(t),this}getFeaturesCanAssociateWithClause(e,t,s){const o=new Map,[i,a]=p(e.sourceLayer);if(!i||!a)return"";const c=l(e),u=l(t);s.forEach((e=>{const{type:t,direction:s}=e;this._getRulesForAssociationType(t).forEach((e=>{d(e,c,s).filter((e=>e.networkSource?.layerId===u.layerId)).forEach((e=>{const t=e.assetGroup?.assetGroupCode,s=e.assetType?.assetTypeCode;if(null!=t&&null!=s){const e=o.get(t)??new Set;e.add(s),o.set(t,e)}}))}))}));let y=[];return this._mergeAssetCodes(o).forEach(((e,t)=>{const s=`${i} IN (${t})`,o=r(a,[...e]);s&&o&&y.push(n(s,o))})),y.length>1&&(y=y.map((e=>`(${e})`))),y.join(" OR ")}getFeaturesCanAssociateWith(e,t,s){return null==y(e)?[]:t.filter((t=>this.canAssociateFeatures(e,t,s)))}canAssociateFeatures(e,t,s){if(!this._canSupportAssociations([e,t]))return!1;const o=l(e),r=l(t);return s.some((e=>{const{type:t,direction:s}=e;return this._getRulesForAssociationType(t).some((e=>"to"===s?h(e,r,o):h(e,o,r)))}))}getLayersCanAssociateWith(e,t,s){return null==y(e)?[]:t.filter((t=>this.canAssociateFeatureToLayer(e,t,s)))}canAssociateFeatureToLayer(e,t,s){if(!this._canSupportAssociations([e,t]))return!1;const o=l(e),r=l(t);return s.some((e=>{const{type:t,direction:s}=e;return this._getRulesForAssociationType(t).some((e=>"to"===s?h(e,r,o):h(e,o,r)))}))}getFeatureSQL(e,t){const s=e.layerId.toString(),o=e.fieldsIndex?.normalizeFieldName("assetGroup"),r=e.fieldsIndex?.normalizeFieldName("assetType"),n=o?t.attributes[o]:null,i=r?t.attributes[r]:null,a=this.rulesHash[s];if(a){const e=a.assetGroupHash[n];if(e){return e.assetTypeHash[i]||null}}return null}async _initializeRulesTable(){const e={};let t;!function(e){e[e.from=0]="from",e[e.to=1]="to",e[e.via=2]="via"}(t||(t={}));const s=[{networkSourceId:"fromNetworkSource",assetGroupId:"fromAssetGroup",assetTypeId:"fromAssetType"},{networkSourceId:"toNetworkSource",assetGroupId:"toAssetGroup",assetTypeId:"toAssetType"},{networkSourceId:"viaNetworkSource",assetGroupId:"viaAssetGroup",assetTypeId:"viaAssetType"}];this.rulesCategorized={attachment:[],containment:[],connectivity:[]};for(const o of this.rules){if(o.ruleType===u.RTAttachment){this.rulesCategorized.attachment.push(o);continue}if(o.ruleType===u.RTContainment){this.rulesCategorized.containment.push(o);continue}if(o.ruleType===u.RTJunctionJunctionConnectivity){this.rulesCategorized.connectivity.push(o);continue}this.rulesCategorized.connectivity.push(o);let r=[[t.from,t.to],[t.to,t.from]];o.ruleType===u.RTEdgeJunctionEdgeConnectivity&&(r=[[t.from,t.via],[t.via,t.from],[t.to,t.via],[t.via,t.to]]);for(const a of r){const r=a.shift(),c=a.shift();let p=!1;switch(o.ruleType){case u.RTEdgeJunctionEdgeConnectivity:p=r===t.from||r===t.to;break;case u.RTJunctionEdgeConnectivity:p=r===t.to}const l=s[r],d=o[l.networkSourceId]?.layerId.toString()??"",y=o[l.assetGroupId]?.assetGroupCode?.toString(),h=o[l.assetTypeId],f=h?.assetTypeCode?.toString(),m=s[c],g=o[m.networkSourceId]?.layerId.toString()??"",T=o[m.assetGroupId]?.assetGroupCode?.toString(),v=o[m.assetTypeId],C=v?.assetTypeCode?.toString(),S=e[d]??{assetGroupHash:{}};if(!(y&&f&&T&&C))continue;const A=S.assetGroupHash[y]??{assetTypeHash:{}},I=A.assetTypeHash[f]??{};if(I[g]=I[g]??{},p){I[d]=I[d]??{};const e=n(`assetgroup = ${y}`,`assettype = ${f}`);"esriNECPEndVertex"===v?.connectivityPolicy?I[d].endVertex=I[d]?.endVertex?`${I[d].endVertex}`:`(${e})`:I[d].anyVertex=I[d].anyVertex?`${I[d].anyVertex}`:`(${e})`}const j=n(`assetgroup = ${T}`,`assettype = ${C}`);"esriNECPEndVertex"===v?.connectivityPolicy?I[g].endVertex=I[g]?.endVertex?i(I[g].endVertex,j):`(${j})`:I[g].anyVertex=I[g]?.anyVertex?i(I[g].anyVertex,j):`(${j})`,A.assetTypeHash[f]=I,S.assetGroupHash[y]=A,e[d]=S}}this._set("rulesHash",e)}_getRulesForAssociationType(e){const{rulesCategorized:t}=this;switch(e){case"attachment":return t.attachment;case"containment":return t.containment;case"connectivity":case"junction-junction-connectivity":return t.connectivity.filter((e=>e.ruleType===u.RTJunctionJunctionConnectivity));case"junction-edge-from-connectivity":case"junction-edge-midspan-connectivity":case"junction-edge-to-connectivity":return t.connectivity.filter((e=>e.ruleType===u.RTJunctionEdgeConnectivity||e.ruleType===u.RTEdgeJunctionEdgeConnectivity));default:return[]}}_areSetsEqual(e,t){if(e.size!==t.size)return!1;for(const s of e)if(!t.has(s))return!1;return!0}_mergeAssetCodes(e){const t=new Map,s=new Set;for(const[o,r]of e){const n=new Set([o]);for(const[t,a]of e)o!==t&&!s.has(t)&&this._areSetsEqual(r,a)&&(n.add(t),s.add(t));const i=Array.from(n).sort().join(",");t.set(i,r)}return t}_canSupportAssociations(e){return e.every((e=>{const t=f(e)?e.sourceLayer:e,[s,o]=p(t);return null!=s&&null!=o}))}};e([a({constructOnly:!0})],m.prototype,"rulesLayer",void 0),e([a({constructOnly:!0})],m.prototype,"rules",void 0),e([a({readOnly:!0})],m.prototype,"rulesHash",void 0),e([a()],m.prototype,"rulesCategorized",void 0),e([a({constructOnly:!0})],m.prototype,"request",void 0),m=e([c("esri.networks.RulesTable")],m);export{m as default};