UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) • 7.71 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import"../../../intl.js";import e from"../../../core/Error.js";import has from"../../../core/has.js";import{clone as t}from"../../../core/lang.js";import r from"../../../core/Logger.js";import{mappedFind as i}from"../../../core/maybe.js";import{getMetersPerUnitForSR as n}from"../../../core/unitUtils.js";import l from"../../../geometry/Polygon.js";import{createExtentFromGeometry as o,scaleExtent as s}from"../../../geometry/support/geometryUtils.js";import u from"../../../rest/support/FullTextSearch.js";import{substitute as a}from"../../../intl/substitute.js";import{formatDate as c}from"../../../intl/date.js";const d=/https?:\/\/services.*\.arcgis\.com/i,f=/(?:\{([^}]+)\})/g,m=()=>r.getLogger("esri.widgets.Search.support.layerSearchUtils");function p(t,r){const{exactMatch:i=!1,location:l,maxResults:o,spatialReference:s,source:u,sourceIndex:a,suggestResult:c,view:d}=t,{layer:f,filter:p,zoomScale:g}=u,b=d?.scale,L=y(u,d),R=r?.signal;return x(f).then((()=>{const e=f.popupTemplate;return e?e.getRequiredFields(f.fieldsIndex):null})).then((t=>{const{objectIdField:r,returnZ:y}=f,x=S(u);if(!I(f,x))throw m().error("invalid-field: displayField is invalid."),new e("getResults():invalid-field","displayField is invalid.");const C=t?.length?t:[x],E=u.outFields||C,D=h(E);E.includes(r)||D||E.push(r),f.floorInfo?.floorField&&E.push(f.floorInfo.floorField);if(!(D||j(f,E)))throw m().error("invalid-field: outField is invalid."),new e("getResults():invalid-field","outField is invalid.");const A=f.createQuery(),{orderByFields:N}=u;if(N&&(A.orderByFields=N),s){A.outSpatialReference=s;const e=1/n(s);e&&(A.maxAllowableOffset=e)}const k="mesh"===f.geometryType||"multipatch"===f.geometryType,O=f.capabilities?.data?.supportsZ&&!k;if(A.returnZ=O&&!1!==y,A.returnGeometry=!0,A.multipatchOption=k?"xyFootprint":null,E&&(A.outFields=E),l)A.geometry=l;else if(c.key)A.objectIds=[c.key];else{const t=u.searchFields||[x];if(!j(f,t))throw m().error("invalid-field: search field is invalid."),new e("getResults():invalid-field","search field is invalid.");T(f)&&(A.num=o),L&&(A.geometry=L);const r=c.text?.trim();if(!r)return[];const n=c.text,{prefix:l="",suffix:s=""}=u,a=q(`${l}${n}${s}`);$(f)&&v(f,t)&&!i&&n&&(A.fullText=w({text:n,searchFields:t}));const d=M({searchTerm:a,layer:f,searchFields:t,filter:p,exactMatch:i,query:A,type:"search"});if(A.where=d,!F(A))return[]}return f.queryFeatures(A,{signal:R}).then((e=>Z(e,d,u,a,x,b,g)))}))}function g(t,r){const{source:i,spatialReference:n,view:l,suggestTerm:o,maxSuggestions:s,sourceIndex:u,exactMatch:a}=t,{layer:c,filter:d}=i,p=r?.signal,g=y(i,l);return x(c).then((()=>{if(!T(c))return[];const t=S(i),r=i.searchFields||[t],l=[];i.suggestionTemplate?i.suggestionTemplate.replaceAll(f,((e,t)=>(l.push(t),e))):l.push(t);const y=h(l);l.includes(c.objectIdField)||y||l.push(c.objectIdField);const x=I(c,t),b=y||j(c,l),L=j(c,r);if(!x)throw m().error("invalid-field: displayField is invalid."),new e("getSuggestions():invalid-field","displayField is invalid.");if(!b)throw m().error("invalid-field: outField is invalid."),new e("getSuggestions():invalid-field","outField is invalid.");if(!L)throw m().error("invalid-field: search field is invalid."),new e("getSuggestions():invalid-field","search field is invalid.");const R=c.createQuery(),{orderByFields:C}=i;C&&(R.orderByFields=C),R.outSpatialReference=n,R.returnGeometry=!1,R.num=s,R.outFields=l,g&&(R.geometry=g);if(!o.trim())return[];const{prefix:E="",suffix:D=""}=i,A=q(`${E}${o}${D}`);$(c)&&v(c,r)&&!a&&o&&(R.fullText=w({text:o,searchFields:r}));const N=M({searchTerm:A,layer:c,searchFields:r,filter:d,exactMatch:a,query:R,type:"suggest"});return R.where=N,F(R)?c.queryFeatures(R,{signal:p}).then((e=>B(e,i,u,t))):[]}))}function y(e,t){const{filter:r,withinViewEnabled:i}=e,n=t?.extent,l=r?.geometry;return l??(i&&n?n:void 0)}function h(e){return e&&e.includes("*")}async function x(e){e&&await e.load()}function F(e){return!(!e.fullText&&!e.where)}function v(e,t){const r=e?.indexes;if(!r||!t?.length)return!1;return r.filter((e=>"FullText"===e.indexType)).some((e=>{const r=e.fields?.split(",").map((e=>e.trim().toLowerCase()))||[];return t.every((e=>r.includes(e.toLowerCase())))}))}function w({text:e,searchFields:t}){return e.trim().split(" ").filter((e=>!!e.trim())).map((e=>new u({onFields:t,searchTerm:e,searchType:"prefix"})))}function $(e){return e?.capabilities?.query?.supportsFullTextSearch??!1}function T(e){return e?.capabilities?.query?.supportsPagination??!1}function b(e){return e?.fieldsIndex?.fields?.find((e=>"string"===e.type))?.name??""}function S(e){return e.displayField||e.layer.displayField||b(e.layer)}function j(e,t){return!(!e||!t?.length)&&t.every((t=>I(e,t)))}function I(e,t){return!!e.getField(t)}function L(e){for(let t=0;t<e.length;t++)if(e.charCodeAt(t)>255)return!0;return!1}function R({codedValues:e},t,r){return i(e??[],(e=>{const i=e.name,n=r?i:i.toLowerCase();return(r?t:t.toLowerCase())===n?e.code.toString():null}))??null}function q(e){return e.replaceAll("'","''")}function C(e,t){const r=t?.where;return r?`(${e}) AND (${r})`:e}function E({currentTerm:e,field:t,filter:r,exactMatch:i,url:n,type:l}){const o=t?.type,s=t?.name;if("string"===o||"date"===o||"global-id"===o){const t=d.test(n??""),o=t&&L(e)?"N":"";if(i&&"search"===l)return C(`${s} = ${o}'${e}'`,r);if(t){return C(`${s} LIKE ${o}${`'${e}%'`}`,r)}return C(`${`LOWER(${s})`} LIKE ${o}${`'${e.toLowerCase()}%'`}`,r)}if("oid"===o||"small-integer"===o||"integer"===o||"single"===o||"double"===o){const t=Number(e);return isNaN(t)?null:C(`${s} = ${t}`,r)}return C(`${s} = ${e}`,r)}function D(e,t){return e?` OR (${t})`:`(${t})`}function M({searchTerm:e,layer:t,searchFields:r,filter:i,exactMatch:n,query:l,type:o}){const{definitionExpression:s,url:u}=t;let a="";return!l.fullText&&e&&r&&r.forEach((r=>{const l=t.getField(r),s=t.getFieldDomain?.(r,{excludeImpliedDomains:has("esri-widget-legacy-field-domain-calculation")}),c=(s&&"coded-value"===s.type?R(s,e,n):null)||e||null;if(null!==c){const e=E({currentTerm:c,field:l,filter:i,exactMatch:n,url:u,type:o});e&&(a+=D(a,e))}})),s&&a?`(${s}) AND (${a})`:s||a}function A(e,t){let r=null;const{codedValues:i}=e;return i&&i.length&&i.some((e=>e.code===t&&(r=e.name,!0))),r}function N(e,t){return e[t]??e[Object.keys(e).find((e=>e.toLowerCase()===t.toLowerCase()))]}function k(e,t,r){const i=e.sourceLayer,{attributes:n}=e,l=i.getFieldDomain?.(r,{feature:e,excludeImpliedDomains:has("esri-widget-legacy-field-domain-calculation")});if(t)return a(t,n);if(r&&n){const e=i.getField(r),t=N(n,r);return null==t?"":l&&"coded-value"===l.type?A(l,t)??"":"date"===e?.type?c(new Date(t)):"number"==typeof t?t.toString():"string"!=typeof t?"":t.trim()}return""}function O(e,t,r,i){const n=e.sourceLayer,{attributes:l}=e,{objectIdField:o}=n,s=o?l[o]:null;return{text:k(e,t.suggestionTemplate,i),key:s,sourceIndex:r}}function B(e,t,r,i){return e.features.map((e=>O(e,t,r,i)))}function U(e){return null!=e&&null!=e.minScale&&null!=e.maxScale}function V(e,r,i,n,u,a,c){const d=e.clone(),f=e.sourceLayer,m=f?.objectIdField,p=m?e.attributes[m]:null,g=k(e,i.searchTemplate,u);null!=a&&U(f)&&(f.minScale&&f.minScale<a?a=f.minScale:f.maxScale&&f.maxScale>a&&(a=f.maxScale));const y=d.geometry?o(d.geometry,r??void 0,a??void 0):void 0,h="number"==typeof c&&y?s(t(y),r??void 0,c):y,x=e.clone();return null!=h&&(x.geometry=l.fromExtent(h)),{extent:h,target:x,feature:d,key:p,name:g,sourceIndex:n}}function Z(e,t,r,i,n,l,o){return e.features.map((e=>V(e,t,r,i,n,l,o)))}export{w as createFullTextSearchInfos,p as getResults,g as getSuggestions};