UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) • 15.3 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import{clone as t}from"../core/lang.js";import s from"../core/Logger.js";import{deepMerge as i}from"../core/object.js";import{watch as o}from"../core/reactiveUtils.js";import{property as l,cast as r,subclass as u}from"../core/accessorSupport/decorators.js";import{diff as n}from"../core/accessorSupport/diffUtils.js";import{ensureString as a,ensureType as p}from"../core/accessorSupport/ensureType.js";import{collectArcadeFieldNames as f,collectField as d}from"../layers/support/fieldUtils.js";import h from"../portal/Portal.js";import c from"./Renderer.js";import{VisualVariablesMixin as m}from"./mixins/VisualVariablesMixin.js";import{rendererBackgroundFillSymbolProperty as y,rendererSymbolProperty as b}from"./support/commonProperties.js";import g from"./support/RendererLegendOptions.js";import v from"./support/UniqueValue.js";import _ from"./support/UniqueValueClass.js";import V from"./support/UniqueValueGroup.js";import q from"./support/UniqueValueInfo.js";import{loadArcade as I}from"../support/loadArcade.js";import{f as S,t as w}from"../chunks/persistableUrlUtils.js";import U from"../symbols/WebStyleSymbol.js";import{fetchStyle as j}from"../symbols/support/styleUtils.js";import{ensureType as F}from"../symbols/support/typeUtils.js";import{addPropertyUnsupportedError as O}from"../webdoc/support/unsupportedErrors.js";import{enumeration as G}from"../core/accessorSupport/decorators/enumeration.js";import{writer as D}from"../core/accessorSupport/decorators/writer.js";import{reader as x}from"../core/accessorSupport/decorators/reader.js";var E;const M="uvInfos-watcher",R="uvGroups-watcher",N=",",L=p(q);function P(e){const{field1:t,field2:s,field3:i,fieldDelimiter:o,uniqueValueInfos:l,valueExpression:r}=e,u=!(!t||!s);return[{classes:(l??[]).map(e=>{const{symbol:l,label:n,value:a,description:p}=e,[f,d,h]=u?a?.toString()?.split(o||"")||[]:[a],c=[];return(t||r)&&c.push(f),s&&c.push(d),i&&c.push(h),{symbol:l,label:n,values:[c],description:p}})}]}function C(e){return null!=e&&""!==e&&("string"!=typeof e||""!==e.trim()&&"<null>"!==e.toLowerCase())||(e=null),e+""}let $=E=class extends(m(c)){constructor(e){super(e),this._valueInfoMap={},this._isDefaultSymbolDerived=!1,this._isInfosSource=null,this.type="unique-value",this.backgroundFillSymbol=null,this.orderByClassesEnabled=!1,this.valueExpressionTitle=null,this.legendOptions=null,this.defaultLabel=null,this.portal=null,this.styleOrigin=null,this.diff={uniqueValueInfos(e,t){if(!e&&!t)return;if(!e||!t)return{type:"complete",oldValue:e,newValue:t};let s=!1;const i={type:"collection",added:[],removed:[],changed:[],unchanged:[]};for(let o=0;o<t.length;o++){const l=e.find(e=>e.value===t[o].value);l?n(l,t[o])?(i.changed.push({type:"complete",oldValue:l,newValue:t[o]}),s=!0):i.unchanged.push({oldValue:l,newValue:t[o]}):(i.added.push(t[o]),s=!0)}for(let o=0;o<e.length;o++){t.find(t=>t.value===e[o].value)||(i.removed.push(e[o]),s=!0)}return s?i:void 0}},this._set("uniqueValueInfos",[]),this._set("uniqueValueGroups",[])}get _cache(){return{compiledFunc:null}}set field(e){this._set("field",e),this._updateFieldDelimiter(),this._updateUniqueValues()}castField(e){return null==e||"function"==typeof e?e:a(e)}writeField(e,t,i,o){"string"==typeof e?t[i]=e:o?.messages?O(o,"UniqueValueRenderer.field","set to a function cannot be written to JSON"):s.getLogger(this).error(".field: cannot write field to JSON since it's not a string value")}set field2(e){this._set("field2",e),this._updateFieldDelimiter(),this._updateUniqueValues()}set field3(e){this._set("field3",e),this._updateUniqueValues()}set valueExpression(e){this._set("valueExpression",e),this._updateUniqueValues()}set defaultSymbol(e){this._isDefaultSymbolDerived=!1,this._set("defaultSymbol",e)}set fieldDelimiter(e){this._set("fieldDelimiter",e),this._updateUniqueValues()}readPortal(e,t,s){return s.portal||h.getDefault()}readStyleOrigin(e,t,s){if(t.styleName)return Object.freeze({styleName:t.styleName});if(t.styleUrl){const e=S(t.styleUrl,s);return Object.freeze({styleUrl:e})}}writeStyleOrigin(e,t,s,i){e.styleName?t.styleName=e.styleName:e.styleUrl&&(t.styleUrl=w(e.styleUrl,i))}set uniqueValueGroups(e){this.styleOrigin?s.getLogger(this).error("#uniqueValueGroups=","Cannot modify unique value groups of a UniqueValueRenderer created from a web style"):(this._set("uniqueValueGroups",e),this._updateInfosFromGroups(),this._isInfosSource=!1,this._watchUniqueValueGroups())}set uniqueValueInfos(e){this.styleOrigin?s.getLogger(this).error("#uniqueValueInfos=","Cannot modify unique value infos of a UniqueValueRenderer created from a web style"):(this._set("uniqueValueInfos",e),this._updateValueInfoMap(),this._updateGroupsFromInfos(),this._isInfosSource=!0,this._watchUniqueValueInfos())}addUniqueValueInfo(e,t){if(this.styleOrigin)return void s.getLogger(this).error("#addUniqueValueInfo()","Cannot modify unique value infos of a UniqueValueRenderer created from a web style");let i;i="object"==typeof e?L(e):new q({value:e,symbol:F(t)}),this.uniqueValueInfos?.push(i),this._valueInfoMap[C(i.value)]=i,this._updateGroupsFromInfos(),this._isInfosSource=!0,this._watchUniqueValueInfos()}removeUniqueValueInfo(e){if(this.styleOrigin)return void s.getLogger(this).error("#removeUniqueValueInfo()","Cannot modify unique value infos of a UniqueValueRenderer created from a web style");const t=this.uniqueValueInfos;if(t)for(let s=0;s<t.length;s++){const i=t[s];if(String(i.value)===String(e)){delete this._valueInfoMap[C(e)],t.splice(s,1);break}}this._updateGroupsFromInfos(),this._isInfosSource=!0,this._watchUniqueValueInfos()}async getUniqueValueInfo(e,t){let s=t;return this.valueExpression&&null==t?.arcade&&(s={...s,arcade:await I()}),this._getUniqueValueInfo(e,s)}getSymbol(e,t){if(this.valueExpression&&null==t?.arcade)return void s.getLogger(this).error("#getSymbol()","Please use getSymbolAsync if valueExpression is used");const i=this._getUniqueValueInfo(e,t);return i?.symbol||this.defaultSymbol}async getSymbolAsync(e,t){let s=t;if(this.valueExpression&&null==s?.arcade){const e=await I(),{arcadeUtils:t}=e;t.hasGeometryOperations(this.valueExpression)&&await t.enableGeometryOperations(),s={...s,arcade:e}}const i=this._getUniqueValueInfo(e,s);return i?.symbol||this.defaultSymbol}get symbols(){const e=[];if(this._isInfosSource)for(const t of this.uniqueValueInfos??[])t.symbol&&e.push(t.symbol);else for(const t of this.uniqueValueGroups??[])for(const s of t.classes??[])s.symbol&&e.push(s.symbol);return this.defaultSymbol&&e.push(this.defaultSymbol),e}getAttributeHash(){return this.visualVariables?.reduce((e,t)=>e+t.getAttributeHash(),"")??""}getMeshHash(){const e=JSON.stringify(this.backgroundFillSymbol),t=JSON.stringify(this.defaultSymbol),s=this.uniqueValueInfos?.reduce((e,t)=>e+t.getMeshHash(),"");return`${e}.${t}.${s}.${`${this.field}.${this.field2}.${this.field3}.${this.fieldDelimiter}`}.${this.valueExpression}`}clone(){const e=new E({field:this.field,field2:this.field2,field3:this.field3,defaultLabel:this.defaultLabel,defaultSymbol:t(this.defaultSymbol),orderByClassesEnabled:this.orderByClassesEnabled,valueExpression:this.valueExpression,valueExpressionTitle:this.valueExpressionTitle,fieldDelimiter:this.fieldDelimiter,visualVariables:t(this.visualVariables),legendOptions:t(this.legendOptions),authoringInfo:t(this.authoringInfo),backgroundFillSymbol:t(this.backgroundFillSymbol)});this._isDefaultSymbolDerived&&(e._isDefaultSymbolDerived=!0),e._set("portal",this.portal);const s=t(this.uniqueValueInfos),i=t(this.uniqueValueGroups);return this.styleOrigin&&(e._set("styleOrigin",Object.freeze(t(this.styleOrigin))),Object.freeze(s),Object.freeze(i)),e._set("uniqueValueInfos",s),e._updateValueInfoMap(),e._set("uniqueValueGroups",i),e._isInfosSource=this._isInfosSource,e._watchUniqueValueInfosAndGroups(),e}get arcadeRequired(){return this.arcadeRequiredForVisualVariables||!!this.valueExpression}async collectRequiredFields(e,t){const s=[this.collectVVRequiredFields(e,t),this.collectSymbolFields(e,t)];await Promise.all(s)}async collectSymbolFields(e,t){const s=[...this.symbols.map(s=>s.collectRequiredFields(e,t)),f(e,t,null,this.valueExpression)];d(e,t,this.field),d(e,t,this.field2),d(e,t,this.field3),await Promise.all(s)}populateFromStyle(){return j(this.styleOrigin,{portal:this.portal}).then(e=>{const t=[];return this._valueInfoMap={},e?.data&&Array.isArray(e.data.items)&&e.data.items.forEach(s=>{const i=new U({styleUrl:e.styleUrl,styleName:e.styleName,portal:this.portal,name:s.name});this.defaultSymbol||s.name!==e.data.defaultItem||(this.defaultSymbol=i,this._isDefaultSymbolDerived=!0);const o=new q({value:s.name,symbol:i});t.push(o),this._valueInfoMap[C(s.name)]=o}),this._set("uniqueValueInfos",Object.freeze(t)),this._updateGroupsFromInfos(!0),this._isInfosSource=null,this._watchUniqueValueInfos(),!this.defaultSymbol&&this.uniqueValueInfos?.length&&(this.defaultSymbol=this.uniqueValueInfos[0].symbol,this._isDefaultSymbolDerived=!0),this})}_updateFieldDelimiter(){this.field&&this.field2&&!this.fieldDelimiter&&this._set("fieldDelimiter",N)}_updateUniqueValues(){null!=this._isInfosSource&&(this._isInfosSource?this._updateGroupsFromInfos():this._updateInfosFromGroups())}_updateValueInfoMap(){this._valueInfoMap={};const{uniqueValueInfos:e}=this;if(e)for(const t of e)this._valueInfoMap[C(t.value)]=t}_watchUniqueValueInfosAndGroups(){this._watchUniqueValueInfos(),this._watchUniqueValueGroups()}_watchUniqueValueInfos(){this.removeHandles(M);const{uniqueValueInfos:e}=this;if(e){const t=[];for(const s of e)t.push(o(()=>({symbol:s.symbol,value:s.value,label:s.label,description:s.description}),(e,t)=>{e!==t&&(this._updateGroupsFromInfos(),this._isInfosSource=!0)},{sync:!0}));this.addHandles(t,M)}}_watchUniqueValueGroups(){this.removeHandles(R);const{uniqueValueGroups:e}=this;if(e){const t=[];for(const s of e){t.push(o(()=>({classes:s.classes}),(e,t)=>{e!==t&&(this._updateInfosFromGroups(),this._isInfosSource=!1)},{sync:!0}));for(const e of s.classes??[])t.push(o(()=>({symbol:e.symbol,values:e.values,label:e.label,description:e.description}),(e,t)=>{e!==t&&(this._updateInfosFromGroups(),this._isInfosSource=!1)},{sync:!0}))}this.addHandles(t,R)}}_updateInfosFromGroups(){if(!this.uniqueValueGroups)return this._set("uniqueValueInfos",null),this._updateValueInfoMap(),void this._watchUniqueValueInfos();const e=[],{field:t,field2:s,field3:i,fieldDelimiter:o,uniqueValueGroups:l,valueExpression:r}=this;if(!t&&!r)return this._set("uniqueValueInfos",e),this._updateValueInfoMap(),void this._watchUniqueValueInfos();const u=!(!t||!s);for(const n of l)for(const t of n.classes??[]){const{symbol:l,label:r,values:n,description:a}=t;for(const t of n??[]){const{value:n,value2:p,value3:f}=t,d=[n];s&&d.push(p),i&&d.push(f);const h=u?d.join(o||""):d[0]??void 0;e.push(new q({symbol:l,label:r,value:h,description:a}))}}this._set("uniqueValueInfos",e),this._updateValueInfoMap(),this._watchUniqueValueInfos()}_updateGroupsFromInfos(e=!1){if(!this.uniqueValueInfos)return this._set("uniqueValueGroups",null),void this._watchUniqueValueGroups();const{field:t,field2:s,valueExpression:i,fieldDelimiter:o,uniqueValueInfos:l}=this;if(!t&&!i||!l.length)return this._set("uniqueValueGroups",[]),void this._watchUniqueValueGroups();const r=!(!t||!s),u=l.map(e=>{const{symbol:t,label:s,value:i,description:l}=e,[u,n,a]=r?i?.toString()?.split(o||"")||[]:[i];return new _({symbol:t,label:s,description:l,values:[new v({value:u,value2:n,value3:a})]})}),n=[new V({classes:u})];e&&Object.freeze(n),this._set("uniqueValueGroups",n),this._watchUniqueValueGroups()}_getUniqueValueInfo(e,t){return this.valueExpression?this._getUnqiueValueInfoForExpression(e,t):this._getUnqiueValueInfoForFields(e)}_getUnqiueValueInfoForExpression(e,t){const{viewingMode:s,scale:i,spatialReference:o,arcade:l,timeZone:r}=t??{};let u=this._cache.compiledFunc;const n=l.arcadeUtils;if(!u){const e=n.createSyntaxTree(this.valueExpression);u=n.createFunction(e),this._cache.compiledFunc=u}const a=n.executeFunction(u,n.createExecContext(e,n.getViewInfo({viewingMode:s,scale:i,spatialReference:o}),r));return this._valueInfoMap[C(a)]}_getUnqiueValueInfoForFields(e){const t=this.field,s=e.attributes;let i;if(this.field2){const e=this.field2,o=this.field3,l=[];t&&l.push(s[t]),e&&l.push(s[e]),o&&l.push(s[o]),i=l.join(this.fieldDelimiter||"")}else t&&(i=s[t]);return this._valueInfoMap[C(i)]}static fromPortalStyle(e,t){const i=new E(t?.properties);i._set("styleOrigin",Object.freeze({styleName:e})),i._set("portal",t?.portal||h.getDefault());const o=i.populateFromStyle();return o.catch(t=>{s.getLogger(this.prototype).error(`#fromPortalStyle('${e}'[, ...])`,"Failed to create unique value renderer from style name",t)}),o}static fromStyleUrl(e,t){const i=new E(t?.properties);i._set("styleOrigin",Object.freeze({styleUrl:e}));const o=i.populateFromStyle();return o.catch(t=>{s.getLogger(this.prototype).error(`#fromStyleUrl('${e}'[, ...])`,"Failed to create unique value renderer from style URL",t)}),o}};e([l({readOnly:!0})],$.prototype,"_cache",null),e([G({uniqueValue:"unique-value"})],$.prototype,"type",void 0),e([l(y)],$.prototype,"backgroundFillSymbol",void 0),e([l({value:null,json:{type:String,read:{source:"field1"},write:{target:"field1"}}})],$.prototype,"field",null),e([r("field")],$.prototype,"castField",null),e([D("field")],$.prototype,"writeField",null),e([l({type:String,value:null,json:{write:!0}})],$.prototype,"field2",null),e([l({type:String,value:null,json:{write:!0}})],$.prototype,"field3",null),e([l({type:Boolean,json:{name:"drawInClassOrder",default:!1,write:!0,origins:{"web-scene":{write:!1}}}})],$.prototype,"orderByClassesEnabled",void 0),e([l({type:String,value:null,json:{write:!0}})],$.prototype,"valueExpression",null),e([l({type:String,json:{write:!0}})],$.prototype,"valueExpressionTitle",void 0),e([l({type:g,json:{write:!0}})],$.prototype,"legendOptions",void 0),e([l({type:String,json:{write:!0}})],$.prototype,"defaultLabel",void 0),e([l(i({...b},{json:{write:{overridePolicy(){return{enabled:!this._isDefaultSymbolDerived}}},origins:{"web-scene":{write:{overridePolicy(){return{enabled:!this._isDefaultSymbolDerived}}}}}}}))],$.prototype,"defaultSymbol",null),e([l({type:String,value:null,json:{write:!0}})],$.prototype,"fieldDelimiter",null),e([l({type:h,readOnly:!0})],$.prototype,"portal",void 0),e([x("portal",["styleName"])],$.prototype,"readPortal",null),e([l({readOnly:!0,json:{write:{enabled:!1,overridePolicy:()=>({enabled:!0})}}})],$.prototype,"styleOrigin",void 0),e([x("styleOrigin",["styleName","styleUrl"])],$.prototype,"readStyleOrigin",null),e([D("styleOrigin",{styleName:{type:String},styleUrl:{type:String}})],$.prototype,"writeStyleOrigin",null),e([l({type:[V],json:{read:{source:["uniqueValueGroups","uniqueValueInfos"],reader:(e,t,s)=>(t.uniqueValueGroups||P(t)).map(e=>V.fromJSON(e,s))},write:{overridePolicy(){return this.styleOrigin?{enabled:!1}:{enabled:!0}}}}})],$.prototype,"uniqueValueGroups",null),e([l({type:[q],json:{read:!1,write:{isRequired:!0,overridePolicy(){return this.styleOrigin?{enabled:!1}:{enabled:!0,isRequired:!0}}}}})],$.prototype,"uniqueValueInfos",null),$=E=e([u("esri.renderers.UniqueValueRenderer")],$);export{$ as default};