UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) • 7.88 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import e from"../../core/Error.js";import{MultiOriginJSONSupport as t}from"../../core/MultiOriginJSONSupport.js";import{whenOnce as o}from"../../core/reactiveUtils.js";import{hasSameCanonicalPortal as r,urlToObject as a}from"../../core/urlUtils.js";import{updateOrigins as i}from"../../core/accessorSupport/originUtils.js";import n from"../../geometry/SpatialReference.js";import{equals as s}from"../../geometry/support/spatialReferenceUtils.js";import{webMercatorToGeographic as l}from"../../geometry/support/webMercatorUtils.js";import{isServerOrServicesAGOLUrl as p}from"../../layers/support/arcgisLayerUrl.js";import{isLayerWithFeatureLayerSource as c,isFeatureCollectionLayer as u}from"../../layers/support/layerUtils.js";import m from"../../portal/Portal.js";import f from"../../portal/PortalItem.js";import{createForItemWrite as d}from"../../portal/support/jsonContext.js";import{typeKeyword as w,removeTypeKeyword as y,addTypeKeyword as h,hasTypeKeyword as g}from"../../portal/support/portalItemUtils.js";import{project as _}from"../../rest/geometryService/project.js";import b from"../../rest/support/ProjectParameters.js";import{hasDeveloperBasemapLayer as j,findSpatialReference as R,isDeveloperBasemapLayer as v}from"../../support/basemapUtils.js";import{saveResources as W}from"./resourceUtils.js";import{beforeSave as P,evaluateWriteErrors as S}from"./saveUtils.js";const I=["NatGeo_World_Map","Ocean_Basemap","USA_Topo_Maps","World_Imagery","World_Street_Map","World_Terrain_Base","World_Topo_Map","World_Hillshade","Canvas/World_Light_Gray_Base","Canvas/World_Light_Gray_Reference","Canvas/World_Dark_Gray_Base","Canvas/World_Dark_Gray_Reference","Ocean/World_Ocean_Base","Ocean/World_Ocean_Reference","Reference/World_Boundaries_and_Places","Reference/World_Reference_Overlay","Reference/World_Transportation"].map((e=>e.toLowerCase()));async function A(e,t,r){r??={},U(e,t),await o((()=>!t.updatingFromView)),await t.load(),await E(t),await P(t),await M(e,t);const a=t.portalItem,{json:i,jsonContext:n}=await O(t,a,e.origin);return S(n,{errorName:`${e.name}:save`},r),await V(t,a),await ye(e,t,a,i,n),await Promise.all([t.updateItemThumbnail(),W(t.resourceReferences,n)]),a}async function O(e,t,o){const r=d(t,o,!0),a=e.write({},r);return await Promise.all(r.resources.pendingOperations),{json:a,jsonContext:r}}async function T(e,t,r,a){a??={};const i=D(e,r);await o((()=>!t.updatingFromView)),await t.load(),await E(t),await P(t),await M(e,t);const{json:n,jsonContext:s}=await O(t,i,e.origin);S(s,{errorName:`${e.name}:save`},a),await X(t,i);const l=t.getThumbnailState();return await he(e,t,i,n,s,a)&&(t.resourceReferences.portalItem=i),t.restoreThumbnailFromState(l),await Promise.all([t.updateItemThumbnail(),W(t.resourceReferences,s)]),i}function U(t,o){if(!o.portalItem)throw new e(`${t.name}:portal-item-not-set`,"Portal item to save to has not been set on the WebMap");C(t,o.portalItem)}function C(t,o){if(o.type!==t.itemType)throw new e(`${t.name}:portal-item-wrong-type`,`Portal item needs to have type "${t.itemType}"`)}async function M(t,r){if("linkchart"===t.name)return;if(!r.basemap?.baseLayers.length)throw new e(`${t.name}:save`,"Map does not have a valid basemap with a base layer.");let a=null;if(await o((()=>{const e=R(r.initialViewProperties,r.basemap);return a=e.spatialReference,!e.updating})),!s(a,r.initialViewProperties.spatialReference))throw new e(`${t.name}:save`,"The spatial reference of the basemap is not compatible with the one from the map.",{expected:r.initialViewProperties.spatialReference,actual:a})}function D(e,t){let o=f.from(t);return o.id&&(o=o.clone(),o.id=null),o.type||(o.type=e.itemType),o.portal||(o.portal=m.getDefault()),C(e,o),o}function E(e){const t=[];return e.basemap&&t.push(e.basemap.load()),e.ground&&t.push(e.ground.load()),Promise.allSettled(t).then((()=>{}))}async function V(e,t){t.extent=await pe(e.portalItem,e.initialViewProperties.viewpoint.targetGeometry),await Y(e,t)}const k=w.JSAPI,x="CollectorDisabled",G="Collector",L="Data Editing",N="OfflineDisabled",$="Offline",B="Workforce Project",F="Workforce Worker",K="Workforce Dispatcher",H="Offline Map Areas",J="FieldMapsDisabled",q=w.DEVELOPER_BASEMAP,z="UtilityNetwork",Q="IPS";async function X(e,t){y(t,x),y(t,J),y(t,w.METADATA),y(t,N),y(t,H),y(t,K),y(t,B),y(t,F),await V(e,t)}async function Y(e,t){h(t,k),await Z(e),re(e,t),ae(e,t),ie(e,t),ne(e,t),se(e,t),le(e,t),t.typeKeywords&&(t.typeKeywords=t.typeKeywords.filter(((e,t,o)=>o.indexOf(e)===t)))}function Z(e){const t=ee(e).map((e=>e.load())).toArray();return Promise.allSettled(t).then((()=>{}))}function ee(e){return e.allLayers.concat(e.allTables)}function te(e){return ee(e).some((e=>e.loaded&&c(e)&&e.capabilities.operations.supportsEditing&&e.editingEnabled&&("subtype-group"!==e.type||e.sublayers.some((e=>e.editingEnabled)))))}function oe(e){return ee(e).filter((e=>"group"!==e.type)).every((t=>t.loaded&&me(e,t)))}function re(e,t){g(t,x)||g(t,B)||g(t,F)||g(t,K)||!te(e)?y(t,G):h(t,G)}function ae(e,t){te(e)?h(t,L):y(t,L)}function ie(e,t){!g(t,N)&&oe(e)?h(t,$):y(t,$)}function ne(e,t){j(e.basemap)?h(t,q):y(t,q)}function se(e,t){e.utilityNetworks?.length?h(t,z):y(t,z)}function le(e,t){e.ipsInfo?h(t,Q):y(t,Q)}async function pe(e,t){const o=t.clone().normalize();let r;if(o.length>1)for(const a of o)r?a.width>r.width&&(r=a):r=a;else r=o[0];return ce(e,r)}async function ce(e,t){const o=t.spatialReference;if(o.isWGS84)return t.clone();if(o.isWebMercator)return l(t);const{getGeometryServiceURL:r}=await import("../../portal/support/geometryServiceUtils.js"),a=await r(e),i=new b({geometries:[t],outSpatialReference:n.WGS84});return(await _(a,i))[0]}function ue(e){return u(e)||"map-notes"===e.type||"route"===e.type}function me(e,t){return c(t)&&t.capabilities.operations.supportsSync||ue(t)&&!t.portalItem||fe(t)&&!de(t)&&t.spatialReference.equals(e.initialViewProperties.spatialReference)}function fe(e){return("tile"===e.type||"vector-tile"===e.type)&&(e.capabilities.operations.supportsExportTiles||we(e)||v(e))}function de(e){return"vector-tile"===e.type&&Object.keys(e.sourceNameToSource).length>1}function we(e){return"tile"===e.type&&(p(e.url)&&I.some((t=>e.url?.toLowerCase().includes("/"+t+"/"))))}async function ye(e,t,o,r,a){await o.update({data:r}),be(e,t,o,r,a)}async function he(t,o,r,a,i,n){const s=o.portalItem,l={item:s,authenticated:!(!s?.id||!s.portal.user)},p=r.portal;await p.signIn();const{copyAllowed:c,itemReloaded:u}=await ge(l,p);if(l.authenticated||=u,!c)throw new e(`${t.name}:save-as-copy-not-allowed`,"Owner of this map does not allow others to save a copy");const m=await _e(r,l,a,n);return o.portalItem=r,be(t,o,r,a,i),m}async function ge(e,t){const{item:o,authenticated:r}=e;return o?.id&&o.typeKeywords?.includes("useOnly")?o.portal.portalHostname!==t.portalHostname?{copyAllowed:!1,itemReloaded:!1}:(r||await o.reload(),{copyAllowed:"admin"===o.itemControl||"update"===o.itemControl,itemReloaded:!0}):{copyAllowed:!0,itemReloaded:!1}}async function _e(e,t,o,a){const i=e.portal,{item:n}=t,{folder:s,copyAllResources:l}=a;let p=!1;if(l&&n?.id&&r(n.portal.url,i.url)&&parseInt(n.portal.currentVersion,10)>=2023){const{total:e}=await n.fetchResources();p=!!e}if(p){const t=await n.copy({copyResources:"all",folder:s});e.id=t.id,e.portal=t.portal;const r=e.toJSON();await e.load(),e.read(r),await e.update({data:o})}else await i.user.addItem({item:e,folder:s,data:o});return p}function be(e,o,r,n,s){t.prototype.read.call(o,{version:n.version,authoringApp:n.authoringApp,authoringAppVersion:n.authoringAppVersion},{origin:e.origin,ignoreDefaults:!0,url:r.itemUrl?a(r.itemUrl):void 0}),i(s),o.resourceInfo=n}export{O as createJSON,_e as initializeNewItem,ge as isCopyAllowed,A as save,T as saveAs};