@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
3 lines (2 loc) • 12.8 kB
JavaScript
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */
import{__decorate as t}from"tslib";import{isSome as i}from"../../core/arrayUtils.js";import{EventedMixin as e}from"../../core/Evented.js";import{JSONSupport as s}from"../../core/JSONSupport.js";import r from"../../core/Logger.js";import{watch as o,initial as a,on as c}from"../../core/reactiveUtils.js";import{property as n,subclass as u}from"../../core/accessorSupport/decorators.js";import{versionMatches as l,onApplyEditsEvent as d}from"../../layers/mixins/EditBusLayer.js";import p from"./CircuitLocation.js";import h from"./CircuitSection.js";import{createSubcircuitJsonFromGraphic as b,createSectionJsonFromGraphic as g}from"./jsonHelpers.js";import f from"./Subcircuit.js";import{CircuitStatusKebabDict as m,CircuitTypeKebabDict as I}from"./typeUtils.js";import{reader as y}from"../../core/accessorSupport/decorators/reader.js";import{writer as _}from"../../core/accessorSupport/decorators/writer.js";const L=new Set(["objectid","name","sectionorder","status","isdeleted","lastverified","lastexported","startlocationsourceid","startlocationid","startlocationfirstunit","startlocationlastunit","stoplocationsourceid","stoplocationid","stoplocationfirstunit","stoplocationlastunit","issectioned","circuittype","conflictcontainerstate","globalid","created_user","created_date","last_edited_user","last_edited_date"]);let S=class extends(e(s)){constructor(t){super(t),this._sectionIdLookup=new Map,this._status="dirty",this._lastVerifiedTime=null,this._lastExportedTime=null,this._isDeleted=!1,this.attributes=null,this.circuitManager=null,this.circuitType="physical",this.globalId=null,this.name=null,this.sections=null,this.startLocation=null,this.stopLocation=null,this.subcircuits=[],this._applyEditsHandler=t=>{const{serviceUrl:i,gdbVersion:e,result:s}=t;if(this.circuitManager){const t=this.circuitManager.featureServiceUrl,s=this.circuitManager.gdbVersion;if(i!==t||!l(i,e,s))return}s.then(s=>{if(!this.circuitManager)return;const r=this.circuitManager.featureServiceUrl,o=this.circuitManager.gdbVersion;if(i!==r||!l(i,e,o))return;const a=this.circuitManager.circuitTable,c=this.circuitManager.circuitSectionTable,n=this.circuitManager.subcircuitTable;if(!a.loaded||!c.loaded||!n.loaded)return void console.warn(`Circuit tables not loaded yet, skipping edit handling for circuit ${this.name}.`);const u=a?.layerId,d=c?.layerId,p=n?.layerId,h=s.editedFeatures?.find(({layerId:t})=>t===u),b=s.editedFeatures?.find(({layerId:t})=>t===d),g=s.editedFeatures?.find(({layerId:t})=>t===p);if(!h&&!b&&!g)return;const f={serviceUrl:t.serviceUrl,layerId:u,gdbVersion:t.gdbVersion,globalId:this.globalId,circuitEdit:null,sectionAdds:[],sectionUpdates:[],sectionDeletes:[],sectionSubcircuits:[],subcircuitAdds:[],subcircuitUpdates:[],subcircuitDeletes:[]};if(h){const{adds:t,updates:i}=h.editedFeatures,e=a.globalIdField,s=t.find(({attributes:t})=>t[e]===this.globalId),r=i.find(({current:t})=>t.attributes[e]===this.globalId);f.circuitEdit=s??r?.current}if(b){const{adds:t,updates:i,deletes:e}=b.editedFeatures,s=c.fieldsIndex.get("circuitId").name,r=t.filter(({attributes:t})=>t[s]===this.globalId),o=i.filter(({original:t})=>t.attributes[s]===this.globalId),a=e.filter(({attributes:t})=>t[s]===this.globalId);f.sectionAdds=r,f.sectionUpdates=o,f.sectionDeletes=a}if(g){const{adds:t,updates:i,deletes:e}=g.editedFeatures,s=n.fieldsIndex.get("providerId").name,r=n.fieldsIndex.get("consumerId").name,o=[],a=[],c=[];t.forEach(t=>{t.attributes[s]===this.globalId?o.push(t):t.attributes[r]===this.globalId&&c.push(t)}),i.forEach(t=>{t.original.attributes[s]===this.globalId?a.push(t):t.current.attributes[r]===this.globalId&&c.push(t.current)});const u=e.filter(({attributes:t})=>t[s]===this.globalId);f.subcircuitAdds=o,f.subcircuitUpdates=a,f.subcircuitDeletes=u,f.sectionSubcircuits=c}this.emit("edits",f)}).catch(t=>{console.error("Error processing apply edits result for circuit edits.",t)})},this._editsHandler=t=>{const{serviceUrl:i,layerId:e,gdbVersion:s,globalId:r,circuitEdit:o}=t;if(!this.circuitManager)return;const a=this.circuitManager.circuitTable,c=a.layerId,n=this.circuitManager.featureServiceUrl,u=this.circuitManager.gdbVersion;if(i!==n||!l(i,s,u)||e!==c||r!==this.globalId)return;this._handleCircuitEdits(o);const d=a.fieldsIndex.get("sectionOrder").name,p=o?.attributes[d],h=p?JSON.parse(p):null;this._handleSectionEdits(t,h),this._handleSubcircuitEdits(t)}}initialize(){this.addHandles([o(()=>this.sections,t=>{this._sectionIdLookup.clear(),null!=t&&Array.from(t.keys()).forEach(t=>this._sectionIdLookup.set(t.sectionId,t))},a),d(this._applyEditsHandler),c(()=>this,"edits",this._editsHandler)])}normalizeCtorArgs(t){if(void 0===t)return t;const{name:i,startLocation:e,stopLocation:s,sections:o}=t;return(e||s)&&o?(delete t.startLocation,delete t.stopLocation,r.getLogger(this).warn("Cannot instantiate Circuit with both start/stop locations and sections.",`Circuit '${i}' was defaulted to having sections only.`)):(e&&!s||!e&&s)&&!o&&r.getLogger(this).warn(`Non-sectioned circuit '${i}' must have both start and stop locations.`,"Create/alter operations involving this circuit will fail unless both are populated."),t}get isDeleted(){return this._isDeleted}readIsDeleted(t,i){const e=i.isDeleted;return this._isDeleted=e??!1,this._isDeleted}get isSectioned(){return null==this.startLocation&&null==this.stopLocation&&null!=this.sections}get lastExportedTime(){return this._lastExportedTime}readLastExportedTime(t,i){const e=i.lastExportedTime;return this._lastExportedTime="number"==typeof e?new Date(e):null,this._lastExportedTime}get lastVerifiedTime(){return this._lastVerifiedTime}readLastVerifiedTime(t,i){const e=i.lastVerifiedTime;return this._lastVerifiedTime="number"==typeof e?new Date(e):null,this._lastVerifiedTime}readSections(t,i){const{sectionOrder:e,sections:s}=i;return null==s?null:(this._sectionIdLookup.clear(),s.forEach(t=>this._sectionIdLookup.set(t.sectionId,h.fromJSON(t))),this._createSectionMap(e))}writeSections(t,i){const e={},s=[];t?.forEach((t,i)=>{const r=i.sectionId.toString(),o=t.map(t=>t.sectionId);e[r]=o,s.push(i.toJSON())}),i.sectionOrder=e,i.sections=s}get status(){return this._status}readStatus(t,i){const e=i.status;return this._status=e?m.fromJSON(e):"dirty",this._status}_createSectionMap(t,e=new Map){if(e.clear(),null==t){for(const t of this._sectionIdLookup.values())e.set(t,[]);return e}for(const[s,r]of Object.entries(t)){const t=Number(s),o=this._sectionIdLookup.get(t);if(null==o)continue;const a=r.map(t=>this._sectionIdLookup.get(t)).filter(i);e.set(o,a)}return e}_handleCircuitEdits(t){t&&(this._handleSystemPropertyEdits(t),this._handleUserDefinedAttributeEdits(t))}_handleSystemPropertyEdits(t){if(!this.circuitManager)return;const i=this.circuitManager.circuitTable.fieldsIndex,e=this._getCircuitAttribute(t,"lastExported");void 0!==e&&(this._lastExportedTime=e?new Date(e):null);const s=this._getCircuitAttribute(t,"lastVerified");void 0!==s&&(this._lastVerifiedTime=s?new Date(s):null);const r=this._getCircuitAttribute(t,"isDeleted");void 0!==r&&(this._isDeleted=0!==r);if(0===this._getCircuitAttribute(t,"isSectioned")){const i=this._getCircuitAttribute(t,"startLocationSourceId"),e=this._getCircuitAttribute(t,"startLocationId"),s=this._getCircuitAttribute(t,"startLocationFirstUnit"),r=this._getCircuitAttribute(t,"startLocationLastUnit"),o=this._getCircuitAttribute(t,"stopLocationSourceId"),a=this._getCircuitAttribute(t,"stopLocationId"),c=this._getCircuitAttribute(t,"stopLocationFirstUnit"),n=this._getCircuitAttribute(t,"stopLocationLastUnit");this.setStartStopLocations(new p({sourceId:i,globalId:e,firstUnit:s,lastUnit:r}),new p({sourceId:o,globalId:a,firstUnit:c,lastUnit:n}))}const o=i.get("circuitType"),a=this._getCircuitAttribute(t,"circuitType");if(null!=a){const t=o.domain.getName(a);this.circuitType=I.read(t)}const c=i.get("status"),n=this._getCircuitAttribute(t,"status");if(null!=n){const t=c.domain.getName(n);this._status=m.fromJSON(t)}const u=i.get("name").name,l=t.attributes[u];void 0!==l&&(this.name=l)}_handleUserDefinedAttributeEdits(t){const i={};for(const[e,s]of Object.entries(t.attributes))L.has(e.toLocaleLowerCase())||(i[e]=s);this.attributes=i}_applySectionEdits(t,i,e){if(!this.circuitManager)return;const{sectionSubcircuits:s}=t,r=this.circuitManager.subcircuitTable,o=this.circuitManager.telecomDomainNetwork.circuitSources?.find(t=>"esriUNFCUTSubcircuit"===t.utilityNetworkFeatureClassUsageType)?.sourceId;if(e.startLocation?.sourceId===o&&!e.stopLocation){const t=r.globalIdField,i=s?.find(i=>i.attributes[t]===e.startLocation.globalId);if(i){const t=b(r,i);e.subcircuit=t,e.startLocation=void 0}}const a=this._sectionIdLookup.get(i);if(a){const s=this.circuitManager.circuitSectionTable.layerId;a.emit("update",{serviceUrl:t.serviceUrl,layerId:s,gdbVersion:t.gdbVersion,circuitGlobalId:this.globalId,sectionId:i,section:e})}else{const t=h.fromJSON(e);this._sectionIdLookup.set(t.sectionId,t)}}_handleSectionEdits(t,i){if(!this.circuitManager)return;const{sectionAdds:e,sectionUpdates:s,sectionDeletes:r}=t,o=this.circuitManager.circuitSectionTable,a=o.fieldsIndex,c=a.get("globalId").name,n=a.get("sectionId").name;r.forEach(t=>{const i=t.attributes[n];if(void 0!==i)return void this._sectionIdLookup.delete(i);const e=t.attributes[c],s=Array.from(this._sectionIdLookup.values()).find(t=>t.globalId===e);s&&this._sectionIdLookup.delete(s.sectionId)}),s.forEach(i=>{const e=i.original,s=i.current,r=g(o,s);this._applySectionEdits(t,e.attributes[n],r)}),e.forEach(i=>{const e=g(o,i);this._applySectionEdits(t,i.attributes[n],e)}),this.sections=this._createSectionMap(i,this.sections??void 0)}_handleSubcircuitEdits(t){if(!this.circuitManager)return;const{subcircuitAdds:i,subcircuitUpdates:e,subcircuitDeletes:s}=t,r=this.circuitManager.subcircuitTable,o=r.layerId,a=r.fieldsIndex,c=r.globalIdField,n=a.get("name").name;s.forEach(t=>{const i=t.attributes[c],e=this.subcircuits?.findIndex(t=>t.globalId===i);-1!==e&&this.subcircuits?.splice(e,1)}),e.forEach(i=>{const e=i.original,s=i.current,a=e.attributes[c],u=e.attributes[n],l=b(r,s),d=this.subcircuits.find(t=>t.globalId===a||t.name.toLocaleLowerCase()===u.toLocaleLowerCase())??this.subcircuits.find(t=>t.name.toLocaleLowerCase()===l.name.toLocaleLowerCase());if(d)d.emit("update",{serviceUrl:t.serviceUrl,layerId:o,gdbVersion:t.gdbVersion,circuitGlobalId:this.globalId,subcircuitGlobalId:a,name:l.name,subcircuit:l});else{const t=f.fromJSON(l);this.subcircuits.push(t)}}),i.forEach(i=>{const e=b(r,i),s=i.attributes[n],a=i.attributes[c],u=this.subcircuits.find(t=>t.name.toLocaleLowerCase()===s.toLocaleLowerCase());if(u)u.emit("update",{serviceUrl:t.serviceUrl,layerId:o,gdbVersion:t.gdbVersion,circuitGlobalId:this.globalId,subcircuitGlobalId:a,name:e.name,subcircuit:e});else{const t=f.fromJSON(e);this.subcircuits.push(t)}})}_getCircuitAttribute(t,i){const e=this.circuitManager?.circuitTable?.fieldsIndex.get(i)?.name;return t.attributes[e??""]}getSectionById(t){return this._sectionIdLookup.get(t)??null}setStartStopLocations(t,i){this.startLocation=t,this.stopLocation=i,this.sections=null}setSections(t){this.sections=t,this.startLocation=null,this.stopLocation=null}getAttribute(t){return this.attributes?.[t]}setAttribute(t,i){this.attributes?this.attributes[t]=i:this.attributes={[t]:i}}};t([n({json:{write:{allowNull:!0,writer:(t,i)=>i.attributes=t??{}}}})],S.prototype,"attributes",void 0),t([n()],S.prototype,"circuitManager",void 0),t([n({type:I.apiValues,json:{type:I.jsonValues,read:I.read,write:I.write}})],S.prototype,"circuitType",void 0),t([n({type:String,json:{write:!0}})],S.prototype,"globalId",void 0),t([n({type:Boolean,readOnly:!0})],S.prototype,"isDeleted",null),t([y("isDeleted")],S.prototype,"readIsDeleted",null),t([n({type:Boolean,json:{write:!0},readOnly:!0})],S.prototype,"isSectioned",null),t([n({type:Date,readOnly:!0})],S.prototype,"lastExportedTime",null),t([y("lastExportedTime")],S.prototype,"readLastExportedTime",null),t([n({type:Date,readOnly:!0})],S.prototype,"lastVerifiedTime",null),t([y("lastVerifiedTime")],S.prototype,"readLastVerifiedTime",null),t([n({type:String,json:{write:!0}})],S.prototype,"name",void 0),t([n({json:{read:{source:["sectionOrder","sections"]},write:!0}})],S.prototype,"sections",void 0),t([y("sections")],S.prototype,"readSections",null),t([_("sections")],S.prototype,"writeSections",null),t([n({type:p,json:{write:!0}})],S.prototype,"startLocation",void 0),t([n({type:m.apiValues,json:{type:m.jsonValues,read:m.read}})],S.prototype,"status",null),t([y("status")],S.prototype,"readStatus",null),t([n({type:p,json:{write:!0}})],S.prototype,"stopLocation",void 0),t([n({type:[f],json:{write:!0}})],S.prototype,"subcircuits",void 0),S=t([u("esri.networks.support.Circuit")],S);export{S as default};