UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) • 15.4 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 t}from"../../../chunks/tslib.es6.js";import e from"../../../Graphic.js";import o from"../../../core/Accessor.js";import r from"../../../core/Clonable.js";import s from"../../../core/Collection.js";import i from"../../../core/Identifiable.js";import{getOrCreateMapValue as a}from"../../../core/MapUtils.js";import{ignoreAbortErrors as n,debounce as c}from"../../../core/promiseUtils.js";import l from"../../../core/ReactiveMap.js";import{watch as u,initial as y}from"../../../core/reactiveUtils.js";import{sqlIn as d}from"../../../core/sql.js";import{property as h}from"../../../core/accessorSupport/decorators/property.js";import"../../../core/has.js";import"../../../core/Logger.js";import"../../../core/RandomLCG.js";import{subclass as p}from"../../../core/accessorSupport/decorators/subclass.js";import{queryAllJSON as m}from"../../../layers/support/featureQueryAll.js";import{isSubtypeSublayer as g,isSubtypeGroupLayer as _}from"../../../layers/support/layerUtils.js";import b from"../../../rest/networks/support/NetworkElement.js";import w from"../../../rest/support/FeatureSet.js";import A from"../../../rest/support/Query.js";import{featureUtilityNetworkFields as F}from"../../Feature/FeatureUtilityNetworkAssociations/resources.js";import{findUtilityNetwork as f}from"../../Feature/support/featureUtils.js";import{getFeatureTitle as C}from"./utils/getFeatureTitle.js";const I=100;let v=class extends(r.ClonableMixin(i.IdentifiableMixin(o))){constructor(t){super(t),this._loaded=!1,this._queryAbortController=null,this._queryPageAbortController=null,this._queryFeatureCountAbortController=null,this.networkSourceIdsInUse=new Set,this.source="popup",this.description=null,this.graphic=null,this.layer=null,this.map=null,this.featureCount=0,this.associationTypes=null,this.showAllEnabled=!1,this.title=null,this.attachmentsFeatureCount=0,this.structureFeatureCount=0,this.contentFeatureCount=0,this.containerFeatureCount=0,this.connectivityFeatureCount=0,this._queryOpenAssociationType=async()=>{this.activeAssociationType&&await this._queryDebounced(this.activeAssociationType)},this._cancelQuery=()=>{const{_queryAbortController:t}=this;t&&t.abort(),this._queryAbortController=null},this._cancelQueryFeatureCount=()=>{const{_queryFeatureCountAbortController:t}=this;t&&t.abort(),this._queryFeatureCountAbortController=null},this._queryController=async t=>{this._cancelQuery();const e=new AbortController;this._queryAbortController=e,await n(this._query(t)),this._queryAbortController===e&&(this._queryAbortController=null)},this._queryFeatureCountController=async()=>{this._loaded=!1,this._cancelQueryFeatureCount();const t=new AbortController;this._queryFeatureCountAbortController=t,await n(this._queryFeatureCount()),this._queryFeatureCountAbortController===t&&(this._queryFeatureCountAbortController=null),this._loaded=!0},this._queryDebounced=c(this._queryController,I),this._queryFeatureCountDebounced=c(this._queryFeatureCountController,I)}initialize(){this.addHandles([u((()=>[this.graphic,this.layer,this.map,this.associationTypes,this.objectId,this.globalId,this.canQuery]),(()=>this.refresh()),y),u((()=>this.activeAssociationType),(t=>this._queryDebounced(t)),y)])}destroy(){this._cancelQuery(),this._cancelQueryFeatureCount(),this._destroyAssociatedFeatureViewModels()}get supportsCacheHint(){return!!this.layer?.capabilities?.query?.supportsCacheHint}get canLoad(){return!!this.map&&!!this.associationTypes&&"string"==typeof this.globalId}get canQuery(){const t=this.layer?.capabilities?.query;return!!this.associationTypes&&"string"==typeof this.globalId&&!!t?.supportsPagination}set displayCount(t){const e=0,o=3;this._set("displayCount",Math.max(t??o,e))}get displayCount(){return this._get("displayCount")}get objectId(){return(this.objectIdField&&this.graphic?.attributes?.[this.objectIdField])??null}get objectIdField(){return this.layer?.objectIdField||null}get globalId(){return(this.globalIdField&&this.graphic?.attributes?.[this.globalIdField])??null}get globalIdField(){const{layer:t}=this;return t?.globalIdField}get activeAssociationType(){return this._get("activeAssociationType")}set activeAssociationType(t){t&&!this.associationTypes.includes(t)||this._set("activeAssociationType",t)}get state(){const{_queryAbortController:t,_queryFeatureCountAbortController:e,_queryPageAbortController:o,canQuery:r,_loaded:s,canLoad:i,source:a}=this;return e||i&&!s?"loading":t||o?"querying":!r||"popup"===a&&0===this.featureCount?"disabled":"ready"}get utilityNetwork(){const{layer:t,map:e}=this;if(!t?.loaded||!e)return null;const o=g(t)?t.parent:t;return f(e,o)}get attachmentsAssociations(){return this._get("attachmentsAssociations")||new s}get structureAssociations(){return this._get("structureAssociations")||new s}get contentAssociations(){return this._get("contentAssociations")||new s}get containerAssociations(){return this._get("containerAssociations")||new s}get connectivityAssociations(){return this._get("connectivityAssociations")||new s}get associationFeatures(){return this._get("associationFeatures")||new l}get associationViewModels(){return this._get("associationViewModels")||new Map}async refresh(){await this._queryFeatureCountDebounced(),await this._queryOpenAssociationType()}getFeatureCountForAssociationType(t){switch(t){case"attachment":return this.attachmentsFeatureCount;case"structure":return this.structureFeatureCount;case"content":return this.contentFeatureCount;case"container":return this.containerFeatureCount;case"connectivity":return this.connectivityFeatureCount}}_destroyAssociatedFeatureViewModels(){this.associationViewModels.forEach((t=>t.destroyAll()))}async _loadUtiltyNetworks(){const t=this.map;if(!t)return;await Promise.allSettled(t.utilityNetworks?.map((async t=>{await t.load()}))??[]);const e=this.utilityNetwork;if(e){const o=t=>{if("layerId"in t&&e.isUtilityLayer(t)){const o=e.getSourceIdByLayerId(t.layerId);null!==o&&this.networkSourceIdsInUse.add(o)}};this._set("networkSourceIdsInUse",new Set),t.allLayers.forEach(o),t.allTables.forEach(o)}}async _findLayersBySourceId(t){const{utilityNetwork:e,map:o}=this,r=t=>{const o=t;if(!t.url)return!1;if(o.layerId===s){return t.url.replace(/\/\d+$/,"")===e?.featureServiceUrl}return!1};await(e?.load());const s=e.getLayerIdBySourceId(t),i=o.allLayers.filter(r),a=o.allTables.filter(r),n=i.concat(a).toArray();return await Promise.allSettled(n.map((t=>t.load()))),n}_clearAssociations(){this.attachmentsAssociations.removeAll(),this.structureAssociations.removeAll(),this.contentAssociations.removeAll(),this.containerAssociations.removeAll(),this.connectivityAssociations.removeAll()}_clearFeatures(){this.associationFeatures.forEach((t=>t.removeAll())),this.associationFeatures.clear()}_getAssociationsByType(t){switch(t){case"attachment":return this.attachmentsAssociations;case"structure":return this.structureAssociations;case"connectivity":return this.connectivityAssociations;case"container":return this.containerAssociations;case"content":return this.contentAssociations}}async _queryLayer(t,e,o,r,s){const i=this._getFeatureQueryWhereClause(t,e,o,r),a=new A({where:i,outFields:["*"],cacheHint:this.supportsCacheHint}),n=w.fromJSON(await m(t,a,s));return n.features.forEach((e=>{e.layer=e.sourceLayer=_(t)?t.findSublayerForFeature(e):t})),n.features}async _createAssociationFeatureObjects(t,e,o,r,s,i){if(0===t.length)return[];const a=new Map;for(const[c,l]of e){const t=await this._findLayersBySourceId(c);for(const e of t){(await this._queryLayer(e,l,r,s,i)).forEach((t=>{if("popup"===this.source?t.sourceLayer&&t.getEffectivePopupTemplate():!!t.sourceLayer){const o=a.get(t.attributes[e.globalIdField])??[];o.push(t),a.set(t.attributes[e.globalIdField],o)}}))}}const n=[];return await Promise.all(t.toArray().map((async t=>{const{fromNetworkElement:e,toNetworkElement:r}=t,s=e.globalId===o?r:e,i=a.get(s.globalId)??[];await Promise.all(i.map((async e=>{const o=this.utilityNetwork?.getTerminalById(s?.terminalId)?.name,r=e.sourceLayer&&"getFeatureTitle"in e.sourceLayer?await e.sourceLayer.getFeatureTitle(e):C(e);n.push({title:r,feature:e,association:t,terminalName:o})})))}))),n}_parseFeatureObjects(t,e){const o=new Map;t.forEach((t=>{const e=t?.feature,r=e.sourceLayer;a(o,r,(()=>new s)).add(t)}));for(const[r,s]of o)this._sortFeatureObjectsByTitle(s),e.set(r,s)}_sortFeatureObjectsByTitle(t){t.sort(this._compareByFeatureTitle)}_compareByFeatureTitle(t,e){return t.title.localeCompare(e.title,void 0,{numeric:!0})}async _queryAssociations(t){const{layer:e,globalId:o,associationTypes:r,utilityNetwork:s,canQuery:i}=this;if(await Promise.allSettled([e?.load(),s?.load()]),this._clearAssociations(),!(i&&e&&r&&s&&o))return;const a=g(e)?e.parent:e,n=new b({globalId:o,networkSourceId:s.getSourceIdByLayerId(a.layerId)}),c=new Set;r.forEach((t=>{switch(t.type){case"attachment":case"structure":c.add("attachment");break;case"container":case"content":c.add("containment");break;case"connectivity":c.add("connectivity"),c.add("junction-junction-connectivity"),c.add("junction-edge-from-connectivity"),c.add("junction-edge-midspan-connectivity"),c.add("junction-edge-to-connectivity")}}));const l=await(s?.queryAssociations({elements:[n],types:Array.from(c)},{signal:t?.signal})),u=new Map,y=new Map;r.forEach((t=>{y.set(t.type,t),u.set(t.type,[])})),l.forEach((t=>{const{toNetworkElement:e,fromNetworkElement:r}=t;switch(t.associationType){case"connectivity":case"junction-junction-connectivity":case"junction-edge-from-connectivity":case"junction-edge-midspan-connectivity":case"junction-edge-to-connectivity":if(r?.globalId===o){if(this._shouldDiscardNetworkElement(e,"connectivity",y))break;u.get("connectivity")?.push(e.globalId)}else{if(this._shouldDiscardNetworkElement(r,"connectivity",y))break;u.get("connectivity")?.push(r.globalId)}this.connectivityAssociations.add(t);break;case"containment":if(r?.globalId===o){if(this._shouldDiscardNetworkElement(e,"content",y))break;u.get("content")?.push(e.globalId),this.contentAssociations.add(t)}else{if(this._shouldDiscardNetworkElement(r,"container",y))break;u.get("container")?.push(r.globalId),this.containerAssociations.add(t)}break;case"attachment":if(r?.globalId===o){if(this._shouldDiscardNetworkElement(e,"attachment",y))break;u.get("attachment")?.push(e.globalId),this.attachmentsAssociations.add(t)}else{if(this._shouldDiscardNetworkElement(r,"structure",y))break;u.get("structure")?.push(r.globalId),this.structureAssociations.add(t)}}}));const d=r.map((async e=>{const{associatedNetworkSourceId:o,associatedAssetGroup:r,associatedAssetType:s}=e,i=u.get(e.type),a=null!=r?await this._countAssociatedFeatures(o,i,r,s,t):i.length;switch(e.type){case"attachment":this._set("attachmentsFeatureCount",a);break;case"structure":this._set("structureFeatureCount",a);break;case"content":this._set("contentFeatureCount",a);break;case"container":this._set("containerFeatureCount",a);break;case"connectivity":this._set("connectivityFeatureCount",a)}}));await Promise.allSettled(d)}async _countAssociatedFeatureCount(t,e,o,r,s){const i=this._getFeatureQueryWhereClause(t,e,o,r);return t.queryFeatureCount({where:i,outFields:["*"],returnGeometry:!1},{signal:s?.signal})}async _countAssociatedFeatures(t,e,o,r,s){if(0===e.length)return 0;const i=(await this._findLayersBySourceId(t)).map((async t=>this._countAssociatedFeatureCount(t,e,o,r,s)));return(await Promise.all(i)).reduce(((t,e)=>t+e),0)}async _queryAssociatedFeatures(t,e){const{layer:o,globalId:r,associationTypes:s,utilityNetwork:i,canQuery:a,associationFeatures:n}=this;if(await Promise.allSettled([o?.load(),i?.load()]),!(a&&o&&s&&i))return;const c=this._getAssociationsByType(t.type),{associatedAssetGroup:l,associatedAssetType:u}=t,y=new Map;c.forEach((t=>{const{fromNetworkElement:e,toNetworkElement:o}=t,{networkSourceId:s,elementGlobalId:i}=e.globalId===r?{networkSourceId:o.networkSourceId,elementGlobalId:o.globalId}:{networkSourceId:e.networkSourceId,elementGlobalId:e.globalId},a=y.get(s)||[];a.push(i),y.set(s,a)}));const d=await this._createAssociationFeatureObjects(c,y,r,l,u,e);this._parseFeatureObjects(d,n)}async _queryFeatureCount(){await this._loadUtiltyNetworks();const{_queryFeatureCountAbortController:t,canQuery:e}=this;e?(await this._queryAssociations(t),this._set("featureCount",this.attachmentsFeatureCount+this.structureFeatureCount+this.contentFeatureCount+this.containerFeatureCount+this.connectivityFeatureCount)):this._set("featureCount",0)}async _query(t){if(!t)return;await this._loadUtiltyNetworks();const{_queryAbortController:e}=this;this._destroyAssociatedFeatureViewModels(),this._clearFeatures(),0!==this.featureCount&&(this.destroyed||await this._queryAssociatedFeatures(t,{signal:e?.signal}))}_shouldDiscardNetworkElement(t,e,o){if(!t)return!1;const{networkSourceIdsInUse:r}=this,{networkSourceId:s}=t,i=o.get(e)?.associatedNetworkSourceId,a=r.has(s);return null!=i&&i!==s||!a}_getFeatureQueryWhereClause(t,e,o,r){const s=t.globalIdField,i=t.fieldsIndex.get(F.assetGroup),a=t.fieldsIndex.get(F.assetType),n=null!=o,c=null!=r;return[s?d(s,e):null,n?`(${i?.name} = ${o})`:null,n&&c?`(${a?.name} = ${r})`:null].filter(Boolean).join(" AND ")}};t([h()],v.prototype,"_loaded",void 0),t([h()],v.prototype,"_queryAbortController",void 0),t([h()],v.prototype,"_queryPageAbortController",void 0),t([h()],v.prototype,"_queryFeatureCountAbortController",void 0),t([h({readOnly:!0})],v.prototype,"supportsCacheHint",null),t([h({readOnly:!0})],v.prototype,"canLoad",null),t([h({readOnly:!0})],v.prototype,"canQuery",null),t([h()],v.prototype,"networkSourceIdsInUse",void 0),t([h({constructOnly:!0})],v.prototype,"source",void 0),t([h()],v.prototype,"description",void 0),t([h({value:3})],v.prototype,"displayCount",null),t([h({type:e})],v.prototype,"graphic",void 0),t([h()],v.prototype,"layer",void 0),t([h()],v.prototype,"map",void 0),t([h({readOnly:!0})],v.prototype,"objectId",null),t([h({readOnly:!0})],v.prototype,"objectIdField",null),t([h({readOnly:!0})],v.prototype,"globalId",null),t([h({readOnly:!0})],v.prototype,"globalIdField",null),t([h()],v.prototype,"featureCount",void 0),t([h()],v.prototype,"associationTypes",void 0),t([h()],v.prototype,"activeAssociationType",null),t([h()],v.prototype,"showAllEnabled",void 0),t([h()],v.prototype,"state",null),t([h()],v.prototype,"title",void 0),t([h({readOnly:!0})],v.prototype,"utilityNetwork",null),t([h({readOnly:!0})],v.prototype,"attachmentsFeatureCount",void 0),t([h({readOnly:!0})],v.prototype,"structureFeatureCount",void 0),t([h({readOnly:!0})],v.prototype,"attachmentsAssociations",null),t([h({readOnly:!0})],v.prototype,"structureAssociations",null),t([h({readOnly:!0})],v.prototype,"contentFeatureCount",void 0),t([h({readOnly:!0})],v.prototype,"containerFeatureCount",void 0),t([h({readOnly:!0})],v.prototype,"contentAssociations",null),t([h({readOnly:!0})],v.prototype,"containerAssociations",null),t([h({readOnly:!0})],v.prototype,"connectivityFeatureCount",void 0),t([h({readOnly:!0})],v.prototype,"connectivityAssociations",null),t([h({readOnly:!0})],v.prototype,"associationFeatures",null),t([h({readOnly:!0})],v.prototype,"associationViewModels",null),v=t([p("esri.widgets.support.UtilityNetworkAssociations.FeatureUtilityNetworkAssociationsViewModel")],v);export{v as default};