@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 12.4 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import has from"../../../core/has.js";import e from"../../../core/Logger.js";import{replace as t}from"../../../core/string.js";import{convertDateFormatToIntlOptions as r,formatDate as n}from"../../../intl/date.js";import{formatNumber as i,convertNumberFormatToIntlOptions as o}from"../../../intl/number.js";import{isRasterPixelValueField as a,featureHasFields as l,isTimeOnlyField as u}from"../../../layers/support/fieldUtils.js";import{getEffectiveLayerCapabilities as s}from"../../../layers/support/layerUtils.js";import{formatAnyDate as f,isAnyDateField as c}from"../../../smartMapping/support/utils.js";import{loadArcade as d}from"../../../support/loadArcade.js";import{getTimeZoneFormattingOptions as p}from"../../../time/timeZoneUtils.js";const y="esri.widgets.Feature.support.featureUtils",m=()=>e.getLogger(y),b=/href=(""|'')/gi,g=/(\{([^{\r\n]+)\})/g,h=/'/g,I=/^\s*expression\//i,w=/(\n)/gi,T=/[\u00A0-\u9999<>&]/gim,F=/href\s*=\s*(?:"([^"]+)"|'([^']+)')/gi,N=/^(?:mailto:|tel:)/,j="relationships/",Z=r("short-date-short-time");function L(e){if(null!=e)return(e.sourceLayer||e.layer)??void 0}async function q({type:e,value:t,event:r}){try{return"function"==typeof t?t(r):await t}catch(n){return void m().error("error",`An error occurred when calling the "${e}" function`,{error:n,graphic:r.graphic,value:t})}}function x(e=""){if(e)return!N.test(e.trim().toLowerCase())}function E(e){return!!e&&I.test(e)}function v(e,t){if(!t||!E(t)||!e)return;const r=t.replace(I,"").toLowerCase();return e.find((({name:e})=>e.toLowerCase()===r))}function A(e,t){const r=v(t,e?.fieldName);return r?r.title||null:e?e.label||e.fieldName:null}function C(e,t){const r=t.get(e.toLowerCase());return`{${r?.fieldName||e}}`}function U(e){return e.replaceAll(b,"")}function M(e,t){const r=k(t,e);return r?r.name:e}function R(e,t){return e&&e.map((e=>M(e,t)))}function k(e,t){return e&&"function"==typeof e.getField&&t?e.getField(t)??null:null}function $(e){return`${e}`.trim()}function D({attributes:e,globalAttributes:t,layer:r,text:n,expressionAttributes:i,fieldInfoMap:o}){return n?z({formattedAttributes:t,template:P(n,{...t,...i,...e},r),fieldInfoMap:o}):""}function z({formattedAttributes:e,template:r,fieldInfoMap:n}){return $(U(t(t(r,(e=>C(e,n))),e)))}function O(e,t,r=!1){const n=t[e];if("string"==typeof n){const i="%27",o=(r?encodeURIComponent(n):n).replaceAll(h,i);t[e]=o}}function G(e,t=!1){const r={...e};return Object.keys(r).forEach((e=>O(e,r,t))),r}function S(e,r,n){const i=(r=$(r))&&"{"!==r[0];return t(e,G(n,i||!1))}function Q(e,t){return e.replaceAll(g,((e,r,n)=>{const i=k(t,n);return i?`{${i.name}}`:r}))}function P(e,t,r){const n=Q(e,r);return n?n.replaceAll(F,((e,r,n)=>S(e,r||n,t))):n}function _(e,t){if("string"==typeof e&&t&&null==t.dateFormat&&(null!=t.places||null!=t.digitSeparator)){const t=Number(e);if(!isNaN(t))return t}return e}function H(e){return null!=e&&"object"==typeof e&&"fieldsIndex"in e&&"geometryType"in e&&"getField"in e&&"load"in e&&"loaded"in e&&"objectIdField"in e&&"spatialReference"in e&&"type"in e&&("feature"===e.type||"scene"===e.type||"subtype-group"===e.type||"subtype-sublayer"===e.type||"sublayer"===e.type)&&"when"in e}function B(e){return null!=e&&"object"==typeof e&&"createQuery"in e&&"queryFeatureCount"in e&&"queryObjectIds"in e&&"queryRelatedFeatures"in e&&"queryRelatedFeaturesCount"in e&&"relationships"in e}function J(e){return H(e)&&B(e)}function K(e){return!(!(e&&"object"==typeof e&&"createQuery"in e&&"getField"in e&&"queryFeatureCount"in e&&"queryFeatures"in e&&"queryObjectIds"in e&&"capabilities"in e&&"fields"in e&&"fieldsIndex"in e&&"type"in e)||"feature"!==e.type&&"subtype-group"!==e.type&&"subtype-sublayer"!==e.type&&"sublayer"!==e.type||!("when"in e))&&("subtype-sublayer"===e.type&&"parent"in e&&e.parent&&"object"==typeof e.parent?"globalIdField"in e.parent:"globalIdField"in e)}function V(e){return!!e&&"object"==typeof e&&"sourceLayer"in e&&J(e.sourceLayer)}function W(e,t){const{fieldInfos:r,fieldName:n,preventPlacesFormatting:l,layer:u,timeZone:s}=t,c=ee(r,n),d=k(u,n);if(c&&!a(n)){const t=d?.type,r=c.format?.dateFormat;if("date"===t||"date-only"===t||"time-only"===t||"timestamp-offset"===t||r)return f(e,{format:r,fieldType:t,timeZoneOptions:{layerTimeZone:u&&"preferredTimeZone"in u?u.preferredTimeZone:null,viewTimeZone:s,datesInUnknownTimezone:!(!u||!("datesInUnknownTimezone"in u))&&!!u.datesInUnknownTimezone}})}const p=c?.format;return"string"==typeof e&&a(n)&&p?X(e,p):"string"==typeof(e=_(e,p))||null==e||null==p?le(e):i(e,l?{...o(p),minimumFractionDigits:0,maximumFractionDigits:20}:o(p))}function X(e,t){return e=e.trim(),/\d{2}-\d{2}/.test(e)?e:e.includes(",")?Y(e,",",", ",t):e.includes(";")?Y(e,";","; ",t):e.includes(" ")?Y(e," "," ",t):i(Number(e),o(t))}function Y(e,t,r,n){return e.trim().split(t).map((e=>i(Number(e),o(n)))).join(r)}function ee(e,t){if(e?.length&&t)return e.find((e=>e.fieldName?.toLowerCase()===t.toLowerCase()))}function te({fieldName:e,graphic:t,layer:r}){if(pe(e))return null;if(!r||"function"!=typeof r.getFeatureType)return null;const{typeIdField:n}=r;if(!n||e!==n)return null;const i=r.getFeatureType(t);return i?i.name:null}function re({fieldName:e,value:t,graphic:r,layer:n}){if(pe(e))return null;if(!n||"function"!=typeof n.getFieldDomain)return null;const i=r&&n.getFieldDomain(e,{feature:r,excludeImpliedDomains:has("esri-widget-legacy-field-domain-calculation")});return i&&"coded-value"===i.type?i.getName(t):null}function ne(e,t,r,i){const{creatorField:o,creationDateField:a,editorField:l,editDateField:u}=e;if(!t)return;const s=p(i&&"preferredTimeZone"in i?i.preferredTimeZone:null,!(!i||!("datesInUnknownTimezone"in i))&&!!i.datesInUnknownTimezone,r,Z,"date"),f={...Z,...s},c=t[u];if("number"==typeof c){const e=t[l];return{type:"edit",date:n(c,f),user:e}}const d=t[a];if("number"==typeof d){const e=t[o];return{type:"create",date:n(d,f),user:e}}return null}function ie(e,t){const r=new Map;if(!e)return r;for(const n of e){if(!n.fieldName)continue;const e=M(n.fieldName,t);n.fieldName=e,r.set(e.toLowerCase(),n)}return r}function oe(e){const t=[];if(!e)return t;const{fieldInfos:r,content:n}=e;return r&&t.push(...r),n&&Array.isArray(n)?(n.forEach((e=>{if("fields"===e.type){const r=e?.fieldInfos;r&&t.push(...r)}})),t):t}function ae(e){return e.replaceAll(T,(e=>`&#${e.charCodeAt(0)};`))}function le(e){return"string"==typeof e?e.replaceAll(w,'<br class="esri-text-new-line" />'):e}function ue(e){const{value:t,fieldName:r,fieldInfos:n,fieldInfoMap:i,layer:o,graphic:a,timeZone:l}=e;if(null==t)return"";const s=re({fieldName:r,value:t,graphic:a,layer:o});if(s)return s;const d=te({fieldName:r,graphic:a,layer:o});if(d)return d;if(i.get(r.toLowerCase()))return W(t,{fieldInfos:n||Array.from(i.values()),fieldName:r,layer:o,timeZone:l});const p=o?.fieldsIndex?.get(r);return p&&(c(p)||u(p))?f(t,{fieldType:p.type,timeZoneOptions:{layerTimeZone:o&&"preferredTimeZone"in o?o.preferredTimeZone:null,viewTimeZone:l,datesInUnknownTimezone:!(!o||!("datesInUnknownTimezone"in o))&&!!o.datesInUnknownTimezone}}):le(t)}function se({fieldInfos:e,attributes:t,layer:r,graphic:n,fieldInfoMap:i,relatedInfos:o,timeZone:a}){const l={};return o?.forEach((t=>be({attributes:l,relatedInfo:t,fieldInfoMap:i,fieldInfos:e,layer:r,timeZone:a}))),t&&Object.keys(t).forEach((o=>{const u=t[o];l[o]=ue({fieldName:o,fieldInfos:e,fieldInfoMap:i,layer:r,value:u,graphic:n,timeZone:a})})),l}async function fe(e,t){const{layer:r,graphic:n,outFields:i,objectIds:o,returnGeometry:a,spatialReference:l}=e,u=o[0];if("number"!=typeof u&&"string"!=typeof u){const e="Could not query required fields for the specified feature. The feature's ID is invalid.",t={layer:r,graphic:n,objectId:u,requiredFields:i};return m().warn(e,t),null}if(!s(r)?.operations?.supportsQuery){const e="The specified layer cannot be queried. The following fields will not be available.",t={layer:r,graphic:n,requiredFields:i,returnGeometry:a};return m().warn(e,t),null}const f=r.createQuery();f.objectIds=o,f.outFields=i?.length?i:[r.objectIdField],f.returnGeometry=!!a,f.returnZ=!!a,f.returnM=!!a,f.outSpatialReference=l;return(await r.queryFeatures(f,t)).features[0]}async function ce(e){if(!e.expressionInfos?.length)return!1;const t=await d(),{arcadeUtils:{hasGeometryFunctions:r}}=t;return r(e)}async function de({graphic:e,popupTemplate:t,layer:r,spatialReference:n},i){if(!r||!t)return;if("function"==typeof r.load&&await r.load(i),!e.attributes)return;const o=r.objectIdField,a=e.attributes[o];if(null==a)return;const u=[a],s=await t.getRequiredFields(r.fieldsIndex),f=l(e,s),c=f?[]:s.includes(o)?s:[...s,o],d=t.returnGeometry||await ce(t);if(f&&!d)return;const p=await fe({layer:r,graphic:e,outFields:c,objectIds:u,returnGeometry:d,spatialReference:n},i);p&&(p.geometry&&(e.geometry=p.geometry),p.attributes&&(e.attributes={...e.attributes,...p.attributes}))}function pe(e=""){return!!e&&e.includes(j)}function ye(e){return e?`${j}${e.layerId}/${e.fieldName}`:""}function me({attributes:e,graphic:t,relatedInfo:r,fieldInfos:n,fieldInfoMap:i,layer:o,timeZone:a}){e&&t&&r&&Object.keys(t.attributes).forEach((l=>{const u=ye({layerId:r.relation.id.toString(),fieldName:l}),s=t.attributes[l];e[u]=ue({fieldName:u,fieldInfos:n,fieldInfoMap:i,layer:o,value:s,graphic:t,timeZone:a})}))}function be({attributes:e,relatedInfo:t,fieldInfoMap:r,fieldInfos:n,layer:i,timeZone:o}){e&&t&&(t.relatedFeatures?.forEach((a=>me({attributes:e,graphic:a,relatedInfo:t,fieldInfoMap:r,fieldInfos:n,layer:i,timeZone:o}))),t.relatedStatsFeatures?.forEach((a=>me({attributes:e,graphic:a,relatedInfo:t,fieldInfoMap:r,fieldInfos:n,layer:i,timeZone:o}))))}const ge=e=>{if(!e)return!1;const t=e.toUpperCase();return t.includes("CURRENT_TIMESTAMP")||t.includes("CURRENT_DATE")||t.includes("CURRENT_TIME")},he=({layer:e,method:t,query:r,definitionExpression:n})=>{if(!e.capabilities?.query?.supportsCacheHint||"attachments"===t)return;const i=null!=r.where?r.where:null,o=null!=r.geometry?r.geometry:null;ge(n)||ge(i)||"extent"===o?.type||"tile"===r.resultType||(r.cacheHint=!0)},Ie=({query:e,layer:t,method:r})=>{he({layer:t,method:r,query:e,definitionExpression:`${t.definitionExpression} ${t.serviceDefinitionExpression??""}`})},we=({queryPayload:e,layer:t,method:r})=>{he({layer:t,method:r,query:e,definitionExpression:`${t.definitionExpression} ${t.serviceDefinitionExpression??""}`})};function Te(e,t,r){return e&&t&&r?"sublayer"===t.type?je({layers:t.layer?.allSublayers,map:e,relatedLayer:t,relationship:r})||je({layers:t.layer?.subtables,map:e,relatedLayer:t,relationship:r}):je({layers:e.allLayers,map:e,relatedLayer:t,relationship:r})||je({layers:e.allTables,map:e,relatedLayer:t,relationship:r}):null}function Fe(e,t){return e&&"utilityNetworks"in e&&t?e.utilityNetworks?.find((e=>e.isUtilityLayer(t))):null}function Ne(e,t){return e?.allTables.find((e=>"feature"===e.type&&e.layerId===t.id&&e.url===t.layer?.url))}function je({map:e,relationship:t,relationship:{relatedTableId:r},relatedLayer:n,layers:i}){if(!i)return null;for(const o of i){if("map-image"===o.type){const r=je({layers:o.sublayers,map:e,relatedLayer:n,relationship:t})||je({layers:o.subtables,map:e,relatedLayer:n,relationship:t});if(r)return r;continue}if(!J(o))continue;if("sublayer"===n.type){if(o!==n&&o.id===r)return o.isTable?Ne(e,o):o;continue}const i="scene"===n.type&&n.associatedLayer?n.associatedLayer.url:n.url;if(!i)return null;if("sublayer"!==o.type){if(o!==n&&o.url===i&&o.layerId===r)return o}else if(o!==n&&o.layer?.url===i&&o.id===r)return o.isTable?Ne(e,o):o}return null}export{le as applyTextFormattingHTML,ie as createFieldInfoMap,Te as findRelatedLayer,Fe as findUtilityNetwork,Q as fixTokens,se as formatAttributes,ne as formatEditInfo,W as formatValueToFieldInfo,oe as getAllFieldInfos,ee as getFieldInfo,A as getFieldInfoLabel,M as getFixedFieldName,R as getFixedFieldNames,L as getSourceLayer,q as graphicCallback,ae as htmlEntities,K as isAssociatedFeatureSupportedLayer,E as isExpressionField,H as isFeatureSupportedLayer,V as isGraphicForRelatableFeatureSupportedLayer,J as isRelatableFeatureSupportedLayer,B as isRelatableLayer,pe as isRelatedField,Ie as preLayerQueryCallback,we as preRequestCallback,fe as querySourceLayer,de as queryUpdatedFeature,x as shouldOpenInNewTab,z as substituteAttributes,D as substituteFieldsInLinksAndAttributes};