UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) • 17 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import r from"../../core/Accessor.js";import o from"../../core/Collection.js";import t from"../../core/Error.js";import i from"../../core/Logger.js";import{watch as n,on as s}from"../../core/reactiveUtils.js";import{removeTrailingSlash as a}from"../../core/urlUtils.js";import{property as d}from"../../core/accessorSupport/decorators/property.js";import"../../core/has.js";import"../../core/RandomLCG.js";import{subclass as u}from"../../core/accessorSupport/decorators/subclass.js";import{UpdatingHandles as v}from"../../core/support/UpdatingHandles.js";import{getOwningPortalUrl as p}from"../../layers/support/layerUtils.js";import l from"../../portal/Portal.js";import{hasUserTypeExtension as h,hasPrivilege as m}from"../../portal/support/utils.js";import c from"../../rest/featureService/FeatureService.js";import{createFeatureServices as g}from"../../rest/featureService/utils.js";import{getVersioningStates as w}from"../../versionManagement/utils.js";import f from"../../versionManagement/VersioningState.js";import E from"../../versionManagement/VersionManagementService.js";import{createVersionAdapters as L}from"../../versionManagement/versionAdapters/utils.js";let _=class extends r{constructor(e){super(e),this._portalLookup=new Map,this._updatingHandlesLoad=new v,this._updatingHandlesExecute=new v,this.advancedEditingUserTypeExtensionLookup=new Map,this.executionError=void 0,this.featureServiceLookup=new Map,this.loadError=void 0,this.serverVersionLookup=new Map,this.serviceNameLookup=new Map,this.userLookup=new Map,this.versionIdentifierLookup=new Map,this.versionInfoLookup=new Map,this.versionAdministratorLookup=new Map,this.versionManagementServiceLookup=new Map,this.versioningStateLookup=new Map,this.view=null,this.versioningStates=null}initialize(){this._viewChangeHandle(),this.addHandles([n((()=>this.view),(()=>{this._resetAllLookupProperties(),this._viewChangeHandle()})),n((()=>this.versioningStates),(()=>{this._resetVersioningLookupProperties(),this._setVersioningStatesLookups()}))])}destroy(){this._updatingHandlesLoad.destroy(),this._updatingHandlesExecute.destroy()}get state(){return this._updatingHandlesLoad.updating?"loading":this.loadError?"disabled":this._updatingHandlesExecute.updating?"executing":this.executionError?"failed":"ready"}async alterVersion(e){return this._updatingHandlesExecute.addPromise(this._alterVersionInternal(e))}async changeVersion(e,r,o){return this._updatingHandlesExecute.addPromise(this._changeVersionInternal(e,r,o))}async createVersion(e){return this._updatingHandlesExecute.addPromise(this._createVersionInternal(e))}async deleteVersion(e,r,o){return this._updatingHandlesExecute.addPromise(this._deleteVersionInternal(e,r,o))}async getVersionInfos(e){return this._updatingHandlesExecute.addPromise(this._getVersionInfosInternal(e))}async _alterVersionInternal(e){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError);this._setExecutionError();const r=this.versioningStateLookup.get(e.featureServerUrl);if(!r)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError);if(!this.serverVersionLookup.has(e.featureServerUrl)||this.serverVersionLookup.get(e.featureServerUrl)<=11.1)throw this._logError("version-management-view-model:execution-error","no-valid-enterprise-version"),new t("version-management-view-model:execution-error",this.executionError);if(!this.advancedEditingUserTypeExtensionLookup.get(e.featureServerUrl))throw this._logError("version-management-view-model:execution-error","no-advanced-editing-user-type-extension"),new t("version-management-view-model:execution-error",this.executionError);const{featureServerUrl:o,versionIdentifier:i,...n}=e,s=await r.alterVersion(i,n).catch((e=>{throw this._logExecutionError(e),e}));return await this.getVersionInfos(o),s}async _changeVersionInternal(e,r,o){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError);this._setExecutionError();const i=this.versioningStateLookup.get(e);if(!i)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError);const n={name:r,guid:o};return await i.changeVersion(n).catch((e=>{throw this._logExecutionError(e),e}))}_updateVersionableItems(e){this._updatingHandlesLoad.addPromise((async()=>{L(e.added).forEach((e=>{const r=e.featureServiceUrl,o=this.versioningStateLookup.get(r);o&&(o.versionableItems.find((r=>r.versionableItem===e.versionableItem))||o.versionableItems.add(e))}));L(e.removed).forEach((e=>{const{allLayers:r,allTables:o}=this.view.map,t=e.featureServiceUrl,i=this.versioningStateLookup.get(t);if(!i)return void this._logError("version-management-view-model:execution-error","no-version-management-service-found");const n=e=>("feature"===e.type||"subtype-group"===e.type)&&(e.url?.includes(t)??!1);[...r.filter(n),...o.filter(n)].length||(this.featureServiceLookup.delete(t),this.serviceNameLookup.delete(t),this.versioningStateLookup.delete(t),this.versioningStates.remove(i));const s=i.versionableItems.find((r=>r.versionableItem===e.versionableItem));s&&i.versionableItems.remove(s)}))})())}async _createVersionInternal(e){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError);this._setExecutionError();const r=e.featureServerUrl,o=this.versioningStateLookup.get(r);if(!o)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError);const i=this.advancedEditingUserTypeExtensionLookup.get(e.featureServerUrl),n=this.userLookup.get(r).toUpperCase(),s=this._isEmptyString(e.ownerName)?n:e.ownerName?.trim().toUpperCase();if(s!==n){if(this.serverVersionLookup.get(r)<=11.1)throw this._logError("version-management-view-model:execution-error","versioning-api-error"),new t("version-management-view-model:execution-error",this.executionError);if(!i)throw this._logError("version-management-view-model:execution-error","no-advanced-editing-user-type-extension"),new t("version-management-view-model:execution-error",this.executionError)}let a=await(this.versioningStateLookup.get(r)?.getVersionInfos());if("SDE"===s?.toUpperCase()&&"DEFAULT"===e.versionName.toUpperCase()||a?.find((r=>r.versionIdentifier.name.toUpperCase()===(s+"."+e.versionName).toUpperCase()||r.versionIdentifier.name.toUpperCase()===(n+"."+e.versionName).toUpperCase())))throw this._logError("version-management-view-model:execution-error","no-valid-version-name"),new t("version-management-view-model:execution-error",this.executionError);const d=await o.versionManagementService.createVersion({versionName:e.versionName,access:s!==n?"public":e.access,description:e.description}).catch((e=>{throw this._logExecutionError(e),e}));if(s!==n){const{guid:o,name:t}=d.versionIdentifier,i={featureServerUrl:r,versionIdentifier:{guid:o,name:t},access:e.access,ownerName:s};await this.alterVersion(i)||this.deleteVersion(r,n+"."+e.versionName,d.versionIdentifier.guid)}return await this.getVersionInfos(r),a=await(this.versioningStateLookup.get(r)?.getVersionInfos()),a?.find((e=>e.versionIdentifier.guid===d.versionIdentifier.guid))}async _deleteVersionInternal(e,r,o){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError);this._setExecutionError();const i=this.versioningStateLookup.get(e);if(!i)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError);if(this.serverVersionLookup.get(e)<=11.1)throw this._logError("version-management-view-model:execution-error","versioning-api-error"),new t("version-management-view-model:execution-error",this.executionError);if(!this.advancedEditingUserTypeExtensionLookup.get(e))throw this._logError("version-management-view-model:execution-error","no-advanced-editing-user-type-extension"),new t("version-management-view-model:execution-error",this.executionError);const n={name:r,guid:o};return await i.deleteVersion(n).catch((e=>{throw this._logExecutionError(e),new t("version-management-view-model:execution-error",this.executionError)}))}async _getVersionInfosInternal(e){if("disabled"===this.state)throw this._logError("version-management-view-model:load-error",this.loadError),new t("version-management-view-model:load-error",this.loadError);this._setExecutionError();const r=this.featureServiceLookup.get(e)?.featureService;if(!r)throw this._logError("version-management-view-model:execution-error","no-feature-service-found"),new t("version-management-view-model:execution-error",this.executionError);r.loaded||await r.load().catch((e=>{throw this._logExecutionError(e),e}));const o=r.url;this.serverVersionLookup.set(o,r.sourceJSON?.currentVersion??0),this.serverVersionLookup.get(o)<=11.1&&this.advancedEditingUserTypeExtensionLookup.set(o,!1);const i=this.userLookup.get(e);this._portalLookup.get(e)&&i?(this.advancedEditingUserTypeExtensionLookup.set(o,await h(this._portalLookup.get(e),i,"advediting")),this.versionAdministratorLookup.set(o,await m(this._portalLookup.get(e),i,"features:user:manageVersions"))):(this.advancedEditingUserTypeExtensionLookup.set(o,!1),this.versionAdministratorLookup.set(o,!1));const n=this.versioningStateLookup.get(e);if(!n)throw this._logError("version-management-view-model:execution-error","no-versioning-state-found"),new t("version-management-view-model:execution-error",this.executionError);n.loaded||await n.load().catch((e=>{throw this._logExecutionError(e),e}));return await n.getVersionInfos(!0).catch((e=>{throw this._logExecutionError(e),e}))}_isEmptyString(e){return!e||0===e.trim().length}_logError(e,r){switch(i.getLogger(this).error(new t(e,r)),e){case"version-management-view-model:load-error":this._setLoadError(r);break;case"version-management-view-model:execution-error":this._setExecutionError(r)}}_logExecutionError(e){this._logError("version-management-view-model:execution-error",e.message)}async _viewChangeHandle(){this._updatingHandlesLoad.addPromise((async()=>{if(this._setLoadError(),!this.view)return void this._setLoadError("no-view-property");if(this.view&&"2d"!==this.view.type)return void this._logError("version-management-view-model:load-error","sceneView-not-supported");this.removeHandles("map-change-handle"),this.addHandles([n((()=>this.view?.map),(()=>{this._resetAllLookupProperties(),this._mapChangeHandle(this.versioningStates)}))],"map-change-handle");const e=await this._getVersioningStates();await this._mapChangeHandle(e)})())}async _mapChangeHandle(e){this._updatingHandlesLoad.addPromise((async()=>{this._setLoadError(),this.removeHandles("map-layers-tables-change-handle"),this.addHandles([s((()=>this.view?.map.allLayers),"change",(e=>{(e.added.some((({type:e})=>"feature"===e))||e.removed.some((({type:e})=>"feature"===e))||e.added.some((({type:e})=>"subtype-group"===e))||e.removed.some((({type:e})=>"subtype-group"===e)))&&(this._resetServiceRelatedLookupProperties(),this._propertiesChangeInternal(this.versioningStates),this._updateVersionableItems(e))})),s((()=>this.view?.map.allTables),"change",(e=>{(e.added.some((({type:e})=>"feature"===e))||e.removed.some((({type:e})=>"feature"===e))||e.added.some((({type:e})=>"subtype-group"===e))||e.removed.some((({type:e})=>"subtype-group"===e)))&&(this._resetServiceRelatedLookupProperties(),this._propertiesChangeInternal(this.versioningStates),this._updateVersionableItems(e))}))],"map-layers-tables-change-handle"),await this._propertiesChangeInternal(e)})())}async _propertiesChangeInternal(e){this._updatingHandlesLoad.addPromise((async()=>{const r=e=>"feature"===e.type||"subtype-group"===e.type,{allLayers:o,allTables:t}=this.view.map;if(this._setLoadError(),this.featureServiceLookup=g([...o.filter(r),...t.filter(r)]),this.featureServiceLookup.size){this._updateFeatureServiceLookup(e),this.serviceNameLookup=new Map;for(const e of this.featureServiceLookup.values()){const{featureService:r,featureService:{url:o}}=e;if(!this.serviceNameLookup.has(o)){if(r.loaded||await r.load().catch((e=>{i.getLogger(this).error(e)})),!r.versionManagementServiceUrl){this.featureServiceLookup.delete(o);continue}const e=await p(o),t=new l({authMode:"immediate",url:e});await t.load().catch((e=>{i.getLogger(this).error(e)}));const n=t.user?.username;if(!r.loaded||!t.loaded||!n)continue;this.serviceNameLookup.set(a(o),o.split("/").at(-2)),this.userLookup.set(o,n),this._portalLookup.set(o,t)}}this.removeHandles("versioning-states-change-handle"),e.forEach((e=>this._handleVersioningStateLookupUpdate(e))),await this._updateVersioningStates(e),this.versioningStates=e}else this._logError("version-management-view-model:load-error","no-feature-services")})())}_updateFeatureServiceLookup(e){for(const r of e){const e=r.featureServiceUrl;if(!this.featureServiceLookup.has(e)){const o=new c({url:e}),t=r.versionableItems.toArray().flatMap((e=>"network"===e.type?null:e.versionableItem)).filter((e=>!!e));this.featureServiceLookup.set(e,{featureService:o,layers:t})}}}async _updateVersioningStates(e){for(const r of this.featureServiceLookup.values()){const t=r.featureService;if(!t.versionManagementServiceUrl)continue;if(!e.find((e=>e.featureServiceUrl===t.url))&&t.versionManagementServiceUrl){const i=new o(L(r.layers)),n=new f({versionManagementService:new E({url:t.versionManagementServiceUrl}),versionableItems:i});e.add(n),this._handleVersioningStateLookupUpdate(n)}await this.getVersionInfos(t.url)}}async _getVersioningStates(){return this.versioningStates&&this.versioningStates.length?this.versioningStates:this.view?await w(this.view,!1):(this._logError("version-management-view-model:load-error","no-view-property"),new o)}_handleVersioningStateLookupUpdate(e){this.versioningStateLookup.set(e.featureServiceUrl,e),this._addHandlesToVersioningState(e),this.versionIdentifierLookup.set(e.featureServiceUrl,e.currentVersionInfo?.versionIdentifier),this.versionManagementServiceLookup.set(e.featureServiceUrl,e.versionManagementService)}async _setVersioningStatesLookups(){this._updatingHandlesLoad.addPromise((async()=>{this.versioningStates&&(this.removeHandles("versioning-states-change-handle"),this._updateFeatureServiceLookup(this.versioningStates),this.versioningStates.forEach((e=>this._handleVersioningStateLookupUpdate(e))),await this._updateVersioningStates(this.versioningStates))})())}_addHandlesToVersioningState(e){this.addHandles([n((()=>e.versionInfos),(()=>{this.versionInfoLookup.set(e.featureServiceUrl,e.versionInfos)})),n((()=>e.currentVersionInfo?.versionIdentifier),(()=>{this.versionIdentifierLookup.set(e.featureServiceUrl,e.currentVersionInfo?.versionIdentifier)}))],"versioning-states-change-handle")}_resetVersioningLookupProperties(){this.versionIdentifierLookup=new Map,this.versionInfoLookup=new Map,this.versionManagementServiceLookup=new Map,this.versioningStateLookup=new Map}_resetAllLookupProperties(){this._portalLookup=new Map,this.advancedEditingUserTypeExtensionLookup=new Map,this.serverVersionLookup=new Map,this.userLookup=new Map,this.versioningStates=new o,this._resetVersioningLookupProperties(),this._resetServiceRelatedLookupProperties()}_resetServiceRelatedLookupProperties(){this.featureServiceLookup=new Map,this.serviceNameLookup=new Map}_setExecutionError(e){this._set("executionError",e)}_setLoadError(e){this._set("loadError",e)}};e([d()],_.prototype,"_portalLookup",void 0),e([d()],_.prototype,"advancedEditingUserTypeExtensionLookup",void 0),e([d({readOnly:!0})],_.prototype,"executionError",void 0),e([d()],_.prototype,"featureServiceLookup",void 0),e([d({readOnly:!0})],_.prototype,"loadError",void 0),e([d()],_.prototype,"serverVersionLookup",void 0),e([d()],_.prototype,"serviceNameLookup",void 0),e([d({readOnly:!0})],_.prototype,"state",null),e([d()],_.prototype,"userLookup",void 0),e([d()],_.prototype,"versionIdentifierLookup",void 0),e([d()],_.prototype,"versionInfoLookup",void 0),e([d()],_.prototype,"versionAdministratorLookup",void 0),e([d()],_.prototype,"versionManagementServiceLookup",void 0),e([d()],_.prototype,"versioningStateLookup",void 0),e([d()],_.prototype,"view",void 0),e([d()],_.prototype,"versioningStates",void 0),_=e([u("esri.widgets.VersionManagement.VersionManagementViewModel")],_);const S=_;export{S as default};