UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) • 7.96 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/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{urlToObject as r,hasSameCanonicalPortal 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{removeTypeKeyword as w,addTypeKeyword as y,typeKeyword as h,hasTypeKeyword as g,toggleTypeKeyword as _}from"../../portal/support/portalItemUtils.js";import{project as b}from"../../rest/geometryService/project.js";import j from"../../rest/support/ProjectParameters.js";import{findSpatialReference as R,hasDeveloperBasemapLayer as v,isDeveloperBasemapLayer as S}from"../../support/basemapUtils.js";import{saveResources as W}from"./resourceUtils.js";import{beforeSave as P,evaluateWriteErrors as I,hasCharts as A}from"./saveUtils.js";const O=["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 T(e,t,r){r??={},M(e,t),await o((()=>!t.updatingFromView)),await t.load(),await k(t),await P(t),await E(e,t);const a=t.portalItem,{json:i,jsonContext:n}=await U(t,a,e.origin);return I(n,{errorName:`${e.name}:save`},r),await x(t,a),await _e(e,t,a,i,n),await Promise.all([t.updateItemThumbnail(),W(t.resourceReferences,n)]),a}async function U(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 C(e,t,r,a){a??={};const i=V(e,r);await o((()=>!t.updatingFromView)),await t.load(),await k(t),await P(t),await E(e,t);const{json:n,jsonContext:s}=await U(t,i,e.origin);I(s,{errorName:`${e.name}:save`},a),await Z(t,i);const l=t.getThumbnailState();return await be(e,t,i,n,s,a)&&(t.resourceReferences.portalItem=i),t.restoreThumbnailFromState(l),await Promise.all([t.updateItemThumbnail(),W(t.resourceReferences,s)]),i}function M(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");D(t,o.portalItem)}function D(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 E(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 V(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()),D(e,o),o}function k(e){const t=[];return e.basemap&&t.push(e.basemap.load()),e.ground&&t.push(e.ground.load()),Promise.allSettled(t).then((()=>{}))}async function x(e,t){t.extent=await me(e.portalItem,e.initialViewProperties.viewpoint.targetGeometry),await ee(e,t)}const G=h.JSAPI,L="CollectorDisabled",N="Collector",$="Data Editing",B="OfflineDisabled",F="Offline",H="Workforce Project",K="Workforce Worker",J="Workforce Dispatcher",q="Offline Map Areas",z="FieldMapsDisabled",Q=h.DEVELOPER_BASEMAP,X="UtilityNetwork",Y="IPS";async function Z(e,t){w(t,L),w(t,z),w(t,h.METADATA),w(t,B),w(t,q),w(t,J),w(t,H),w(t,K),await x(e,t)}async function ee(e,t){y(t,G),await te(e),ie(e,t),ne(e,t),se(e,t),le(e,t),pe(e,t),ce(e,t),ue(e,t),t.typeKeywords&&(t.typeKeywords=t.typeKeywords.filter(((e,t,o)=>o.indexOf(e)===t)))}function te(e){const t=oe(e).map((e=>e.load())).toArray();return Promise.allSettled(t).then((()=>{}))}function oe(e){return e.allLayers.concat(e.allTables)}function re(e){return oe(e).some((e=>e.loaded&&c(e)&&e.capabilities.operations.supportsEditing&&e.editingEnabled&&("subtype-group"!==e.type||e.sublayers.some((e=>e.editingEnabled)))))}function ae(e){return oe(e).filter((e=>"group"!==e.type)).every((t=>t.loaded&&we(e,t)))}function ie(e,t){g(t,L)||g(t,H)||g(t,K)||g(t,J)||!re(e)?w(t,N):y(t,N)}function ne(e,t){re(e)?y(t,$):w(t,$)}function se(e,t){!g(t,B)&&ae(e)?y(t,F):w(t,F)}function le(e,t){v(e.basemap)?y(t,Q):w(t,Q)}function pe(e,t){e.utilityNetworks?.length?y(t,X):w(t,X)}function ce(e,t){e.ipsInfo?y(t,Y):w(t,Y)}function ue(e,t){_(t,h.CHARTS,A(e))}async function me(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 fe(e,r)}async function fe(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 j({geometries:[t],outSpatialReference:n.WGS84});return(await b(a,i))[0]}function de(e){return u(e)||"map-notes"===e.type||"route"===e.type}function we(e,t){return c(t)&&t.capabilities.operations.supportsSync||de(t)&&!t.portalItem||ye(t)&&!he(t)&&t.spatialReference.equals(e.initialViewProperties.spatialReference)}function ye(e){return("tile"===e.type||"vector-tile"===e.type)&&(e.capabilities.operations.supportsExportTiles||ge(e)||S(e))}function he(e){return"vector-tile"===e.type&&Object.keys(e.sourceNameToSource).length>1}function ge(e){return"tile"===e.type&&(p(e.url)&&O.some((t=>e.url?.toLowerCase().includes("/"+t+"/"))))}async function _e(e,t,o,r,a){await o.update({data:r}),ve(e,t,o,r,a)}async function be(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 je(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 Re(r,l,a,n);return o.portalItem=r,ve(t,o,r,a,i),m}async function je(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 Re(e,t,o,r){const i=e.portal,{item:n}=t,{folder:s,copyAllResources:l}=r;let p=!1;if(l&&n?.id&&a(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 ve(e,o,a,n,s){t.prototype.read.call(o,{version:n.version,authoringApp:n.authoringApp,authoringAppVersion:n.authoringAppVersion},{origin:e.origin,ignoreDefaults:!0,url:a.itemUrl?r(a.itemUrl):void 0}),i(s),o.resourceInfo=n}export{U as createJSON,Re as initializeNewItem,je as isCopyAllowed,T as save,C as saveAs};