UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) • 9.34 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.19/LICENSE.txt */ import e from"../../WebScene.js";import t from"../../core/Error.js";import{getPropertiesMetadata as r}from"../../core/accessorSupport/metadata.js";import{json as n}from"../../portal/schemas/webScene.js";import{ScanContext as s,sorted as i}from"./utils.js";function a(e,t){if(t.properties){if("layerType"in t.properties)return t.properties.layerType.enum[0];if("type"in t.properties&&t.properties.type.enum)return t.properties.type.enum[0]}switch(e){case"multipoint_geometry_schema.json":return"multipoint";case"point_geometry_schema.json":return"point";case"polyline_geometry_schema.json":return"polyline";case"polygon_geometry_schema.json":return"polygon";case"extent_schema.json":return"extent"}}function o(e){return"array"===e.type?`${o(e.items)}[]`:e.type}function c(e){const t={count:e.length,refsCount:0,typesCount:0,distinctTypes:[],type:null},r=new Set;for(const n in e){const s=e[n];s.$ref?t.refsCount++:s.type&&(t.typesCount++,r.add(o(s)))}return r.forEach(e=>t.distinctTypes.push(e)),t.distinctTypes.sort(),t.refsCount===t.count?t.type="refs":2===t.count&&t.refsCount>0&&1===t.distinctTypes.length&&"null"===t.distinctTypes[0]?t.type="refsAndNull":t.typesCount===t.count?(t.type="types",t.distinctTypes=t.distinctTypes):t.type="mix",t}function l(e,t){const r=t.stack[t.stack.length-1],n=1===t.stack.length?t.schemaRoot:t.definitions[r.klass.name];return n?.required&&n.required.includes(e.replace("[]",""))}function u(e){return"array"===e.type||!("properties"in e)}function p(e,t,r){if(!r.hasFilteredProperties)return`${e}--${t}`;return`${e}--${t}--${r.filteredPropertiesArray.join("/")}`}async function f(t,n,s,i,o,c){if("version_schema.json"===t){const t=r(e.prototype),n=t.sourceVersion.json?.type;n&&(s={...s,enum:n})}o.schemaStack.push(t);const f=a(t,s),m=i?i.replace("<?TYPE?>",f?`<${f}>`:""):"";if(u(s)){const e=o.currentClass?null:{type:t,name:t,id:`${t}--${n}`,typeValue:n,properties:[]};return e&&o.push(null,e),await E(s,m,o,c),o.schemaStack.pop(),e}const h=p(t,n,o),y="drawingInfo_schema.json"!==t&&"layer_schema.json"!==t&&o.seen.get(h);if(y&&m)return o.addProperty({name:m,type:y,required:l(m,o),nullable:c?.nullable}),o.schemaStack.pop(),y;const d={type:t,name:t,id:h,typeValue:n,properties:[]};return m&&o.addProperty({name:m,type:d,required:l(m,o),nullable:c?.nullable}),o.push(m,d),await E(s,"",o),o.schemaStack.pop(),o.pop()}async function m(e,t,r,n){const s=await r.requestSchema(e.$ref),i=O(s.schema);if(i){for(const e of i){const i={...s.schema};i.properties={...i.properties,type:{type:"string",enum:[e]}};const a=t&&!t.includes("<?TYPE?>")?`${t}<?TYPE?>`:t;await f(s.schemaId,e,i,a,r,n)}return}const a="layer_schema.json"===s.schemaId&&r.schemaStack.length?r.schemaStack[r.schemaStack.length-1].replace(/_schema\.json/,""):null;await f(s.schemaId,a,s.schema,t,r,n)}function h(e,t){if(!d(e))return!1;const r=e.stack.map(e=>e.klass.type).join("/");return/.*pointCloudLayer_schema\.json\/layerDefinition_schema\.json\/drawingInfo_schema\.json$/.test(r)&&"renderer"===t}function y(e,t){if(!_(e)&&!j(e))return!1;const r=e.stack.map(e=>e.klass.type).join("/");return(/.*(imageServiceLayer|tiledImageServiceLayer|wcsLayer)_schema\.json\/(layerDefinition|rasterLayerDefinition)_schema\.json\/rasterDrawingInfo_schema\.json$/.test(r)||/.*rasterPresetRenderer_schema\.json$/.test(r))&&"renderer"===t}function d(e){return!!e.currentClass&&"drawingInfo_schema.json"===e.currentClass.name}function _(e){return!!e.currentClass&&"rasterDrawingInfo_schema.json"===e.currentClass.name}function j(e){return!!e.currentClass&&"rasterPresetRenderer_schema.json"===e.currentClass.name}function w(e){switch(e.schemaStack[e.schemaStack.length-1]){case"operationalLayers_schema.json":case"elevationLayers_schema.json":case"baseMapLayer_schema.json":return!0}return!1}const $=/raster.*Renderer|vectorFieldRenderer/,g=/(uniqueValueRenderer|classBreaksRenderer|raster.*Renderer|vectorFieldRenderer)/,P=/(rasterUniqueValueRenderer|rasterClassBreaksRenderer)/;function S(e,t,r){if(d(r)||_(r)||j(r)){const n=h(r,t),s=y(r,t);return e.filter(e=>{if(e.$ref.endsWith("uniqueValueFromStyleRenderer_schema.json"))return!1;if(s)return g.test(e.$ref)&&!P.test(e.$ref);if($.test(e.$ref))return!1;const t=/pointCloud.*Renderer/.test(e.$ref);return n===t})}if(w(r)){const t=["kmlLayer","rasterDataElevationLayer","rasterDataLayer","object3DTilesLayer"];return e.filter(e=>!t.some(t=>e.$ref.replace(/.*\//,"")===`${t}_schema.json`))}return e}async function b(e,t,r){await E(e.items,`${t}[]`,r)}async function k(e,t,r){for(const n in e.properties)if(!("webscene_schema.json"===r.currentClass?.name&&"mapRangeInfo"===n||"baseMap_schema.json"===r.currentClass?.name&&"elevationLayers"===n)){if(d(r)){const e=r.stack.map(e=>e.klass.type).join("/");if(/imageServiceLayer|tiledImageServiceLayer/.test(e)&&"transparency"===n)continue}await r.withFilter(n,async()=>{const s=t?`${t}.${n}`:n;await E(e.properties[n],s,r)})}}function R(e,t="",r=new Set){for(const n of e)if("properties"in n)for(const e in n.properties){const s=n.properties[e],i=t?`${t}.${e}`:e,a=Object.keys(s);if(0===a.length||1===a.length&&"$ref"===a[0])r.add(i);else{if(1!==a.length||"allOf"!==a[0])throw new Error(`unexpected allOf filter construct: ${JSON.stringify(s)}`);r.add(i),R(s.allOf,i,r)}}return r}async function C(e,t,r){let n=null;for(const i of e.allOf)if("$ref"in i){if(n)throw new Error("Cannot process more than 1 ref in an allOf construct");n=i}else if(!("properties"in i))throw new Error("allOf construct only allows simple top-level property filters");const s=R(e.allOf);await r.addFilter(s,()=>m(n,t,r))}async function L(e,t,r){const n=c(e.oneOf),s="refsAndNull"===n.type;if("refs"===n.type||s){const n=S(e.oneOf,t,r);for(const e of n)if(s&&"null"===e.type);else{const i=`${t||""}`+(1!==n.length?"<?TYPE?>":"");await E(e,i,r,{nullable:s})}return}if("types"===n.type){const{types:e,nullable:s}=q(n.distinctTypes);return void r.addProperty({name:t,type:i(e).join("|"),required:l(t,r),nullable:s})}for(const i in e.oneOf){const n=`.oneOf[${i}]`;await E(e.oneOf[i],`${t}${n}`,r)}}function q(e){const t=e.indexOf("null");if(-1===t)return{types:e};const r=e.slice();return r.splice(t,1),{types:r,nullable:!0}}function O(e){if("layerDefinition"===e.title)return null;const t=e.properties?.type;return t?.enum?t.enum:null}async function T(e,t,r,n){await m(e,t,r,n)}async function v(e,t,r){let n,s="unknown";if(e.type){const{types:t,nullable:r}=q(Array.isArray(e.type)?e.type:e.type.replaceAll(" ","").split(","));s=i(t).join("|"),n=r}const a={name:t,type:s,default:e.default,required:l(t,r),nullable:n};e.enum&&(a.enum=i(e.enum).map(e=>"string"==typeof e?`"${e}"`:`${e}`).join("|")),r.addProperty(a)}async function E(e,t,r,n){return"array"===e.type?b(e,t,r):"properties"in e?k(e,t,r):"allOf"in e?C(e,t,r):"oneOf"in e?L(e,t,r):"$ref"in e?T(e,t,r,n):v(e,t,r)}const I="#/definitions/";function F(e){return e.startsWith(I)?e.slice(I.length):e}async function D(e,t){const r=await x.create(e,t);return f(`${e||"webscene"}_schema.json`,null,r.schemaRoot,null,r)}class x extends s{constructor(e,t,r){super(),this.definitions=e,this.schemaRoot=t,this.requestSchema=r,this._filteredProperties=null,this.schemaStack=[],this.requestSchema.bind(this)}get hasFilteredProperties(){return(this._filteredProperties?.size??0)>0}get filteredPropertiesArray(){const e=[];return this._filteredProperties?.forEach(t=>e.push(t)),e}async withFilter(e,t){if(!this.hasFilteredProperties)return t();if(!this._filteredProperties?.has(e))return;const r=this._filteredProperties;this._filteredProperties=null;const n=e=>{this._filteredProperties||(this._filteredProperties=new Set),this._filteredProperties.add(e)};r.forEach(t=>{const r=t.split(".",2);r.length>1&&r[0]===e&&n(r[1])}),await t(),this._filteredProperties=r}async addFilter(e,t){const r=this._filteredProperties;this._filteredProperties=null;const n=e=>{this._filteredProperties||(this._filteredProperties=new Set),this._filteredProperties.add(e)};r&&r.forEach(n),e&&e.forEach(n);const s=await t();return this._filteredProperties=r,s}static async create(e,t){return t?.useRemoteSchema?x.createRemote(e,t?.baseUrl):x.createLocal(e)}static createLocal(e){const t=e&&"webscene"!==e?n.definitions[`${e}_schema.json`]:n;return new x(n.definitions,t,x._getLocalSchemaRequest())}static async createRemote(e,t){const r=await x._getRemoteSchemaRequest(t),n=new x({},null,r),s=(await n.requestSchema(`${e||"webscene"}_schema.json`)).schema;return new x(n.definitions,s,r)}static _getLocalSchemaRequest(){return function(e){const r=F(e),n=this.definitions[r];return n?Promise.resolve({schemaId:r,schema:n}):Promise.reject(new t("spec-certification:spec-invalid-local-schema","Schema reference is not a local reference"))}}static async _getRemoteSchemaRequest(e){if(!e)return Promise.reject(new t("spec-certification:spec-missing-base-url","The base url of the remote schema directory must be specified when using a remote schema"));const r=x._getLocalSchemaRequest(),n=(await import("../../request.js")).default;return function(t){return r.call(this,t).catch(()=>n(`${e}/${t}`,{responseType:"json"}).then(e=>(this.definitions[F(t)]=e.data,{schemaId:t,schema:e.data})))}}}const A=Object.keys(n.definitions).map(e=>e.replace(/_schema\.json$/,""));export{D as scan,A as schemaDefinitions};