@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 5.58 kB
JavaScript
/*
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"../../Color.js";import t from"../../request.js";import i from"../../core/Error.js";import"../../core/has.js";import{clone as s}from"../../core/lang.js";import r from"../../core/Logger.js";import{LRUCache as o}from"../../core/LRUCache.js";import{isAbortError as n}from"../../core/promiseUtils.js";import{Version as l}from"../../core/Version.js";import{isPolygon as a,isPolyline as c}from"../../geometry/support/jsonUtils.js";import f from"../../layers/support/FieldsIndex.js";import{loadArcade as m,createDictionaryExpression as u}from"../../support/arcadeOnDemand.js";import h from"../../symbols/CIMSymbol.js";import{getFieldName as y,getTextCasing as p}from"../../symbols/cim/utils.js";const d="esri.renderers.support.DictionaryLoader",g={type:"CIMSimpleLineCallout",lineSymbol:{type:"CIMLineSymbol",symbolLayers:[{type:"CIMSolidStroke",width:.5,color:[0,0,0,255]}]}};class _{constructor(e,t,i){this.config=null,this.fieldMap=null,this.url=null,this._ongoingRequests=new Map,this._symbolCache=new o(100),this._dictionaryVersion=null,this._fieldIndex=null,this._dictionaryPromise=null,this.url=e,this.config=t,this.fieldMap=i}getSymbolFields(){return this._symbolFields}async getSymbolAsync(t,i){let s;this._dictionaryPromise||(this._dictionaryPromise=this.fetchResources(i));try{s=await this._dictionaryPromise}catch(p){if(n(p))return this._dictionaryPromise=null,null}const r=this._dictionaryVersion?.greaterEqual(4,0),o={};if(this.fieldMap)for(const e of this._symbolFields){const i=y(this.fieldMap[e],this._fieldIndex);if(i){const s=t.attributes[i];o[e]=r?s:null!=s?""+t.attributes[i]:""}else o[e]=""}let l=null;try{l=s?.(o,i)}catch(d){return null}if(!l||"string"!=typeof l||"invalid"===l)return null;const f=l.split(";"),m=[],u=[];for(let n=0;n<f.length;n++){const i=f[n];if(i)if(i.includes("po:")){const s=i.slice(3).split("|");if(3===s.length){const i=s[0],r=s[1];let o=s[2];const n="countrylabel";if(i===n&&"TextString"===r){if(null!=this.fieldMap){let e=this.fieldMap[n];null==e&&(e=n,this.fieldMap[e]=e),t.attributes[e]=o}continue}if("DashTemplate"===r)o=o.split(" ").map((e=>Number(e)));else if("Color"===r){const t=new e(o).toRgba();o=[t[0],t[1],t[2],255*t[3]]}else o=Number(o);u.push({primitiveName:i,propertyName:r,value:o,defaultValue:null})}}else if(i.includes("|")){for(const e of i.split("|"))if(this._itemNames.has(e)){m.push(e);break}}else if(this._itemNames.has(i))m.push(i);else if(0===n){m.length=0;let e="Invalid_P";null!=t.geometry&&(a(t.geometry)?e="Invalid_A":c(t.geometry)&&(e="Invalid_L")),m.push(e);break}}const h=null==t.geometry||!t.geometry.hasZ&&"point"===t.geometry.type;return this._cimPartsToCIMSymbol(t,m,u,h,i)}async fetchResources(e){if(this._dictionaryPromise)return this._dictionaryPromise;if(!this.url)return void r.getLogger(d).error("no valid URL!");const s=t(this.url+"/resources/styles/dictionary-info.json",{responseType:"json",query:{f:"json"},signal:null!=e?e.signal:null}),[{data:o}]=await Promise.all([s,m()]);if(!o)throw this._dictionaryPromise=null,new i("esri.renderers.DictionaryRenderer","Bad dictionary data!");const{authoringInfo:n,dictionary_version:a,expression:c,itemsNames:h}=o,y=c;let p=!1;a&&(this._dictionaryVersion=l.parse(a),p=this._dictionaryVersion.greaterEqual(4,0)),this._refSymbolUrlTemplate=this.url+"/"+o.cimRefTemplateUrl,this._itemNames=new Set(h),this._symbolFields=n.symbol;const g={};if(this.config){const e=this.config;for(const t in e)g[t]=e[t]}if(n.configuration)for(const t of n.configuration)g.hasOwnProperty(t.name)||(g[t.name]=t.value);const _=[];if(e?.fields&&this.fieldMap)for(const t in this.fieldMap){const i=this.fieldMap[t],s=e.fields.filter((e=>e.name.toLowerCase()===i?.toLowerCase()));s.length>0&&_.push({...s[0],type:p?s[0].type:"esriFieldTypeString"})}_.length>0&&(this._fieldIndex=new f(_));const b=u(y,null!=e?e.spatialReference:null,_,g).then((e=>{const t={scale:0};return(i,s)=>{if(null==e)return null;const r=e.repurposeFeature({geometry:null,attributes:i});return t.scale=null!=s?s.scale??void 0:void 0,e.evaluate({$feature:r,$view:t},e.services)}})).catch((e=>(r.getLogger(d).error("Creating dictionary expression failed:",e),null)));return this._dictionaryPromise=b,b}async _cimPartsToCIMSymbol(e,t,i,r,o){const n=new Array(t.length);for(let s=0;s<t.length;s++)n[s]=this._getSymbolPart(t[s],o);let l=await Promise.all(n);const a=this.fieldMap;if(a&&l.length>0){const{OverrideHelper:t}=await import("../../symbols/cim/OverrideHelper.js");l=s(l);for(const i of l)t.applyDictionaryTextOverrides(i,e,a,this._fieldIndex,p(i))}return new h({data:b(l,i,r)})}async _getSymbolPart(e,i){const s=this._symbolCache.get(e);if(s)return s;if(this._ongoingRequests.has(e))return this._ongoingRequests.get(e).then((e=>e.data));const r=this._refSymbolUrlTemplate.replaceAll(/\{itemName\}/gi,e),o=t(r,{responseType:"json",query:{f:"json"},...i});this._ongoingRequests.set(e,o),o.finally((()=>this._ongoingRequests.delete(e)));try{const t=await o;return this._symbolCache.put(e,t.data),t.data}catch(n){throw n}}}function b(e,t,i){if(!e||0===e.length)return null;const s={...e[0]};if(e.length>1){s.effects=null,s.symbolLayers=[];for(const t of e){const e=t;if(null!=e.effects)for(const t of e.symbolLayers)null==t.effects?t.effects=e.effects:t.effects.unshift(...e.effects);s.symbolLayers.unshift(...e.symbolLayers)}}return i&&(s.callout=g),{type:"CIMSymbolReference",symbol:s,primitiveOverrides:t}}export{_ as DictionaryLoader};