12345 |
- /*
- All material copyright ESRI, All Rights Reserved, unless otherwise specified.
- See https://js.arcgis.com/4.24/esri/copyright.txt for details.
- */
- import{_ as e}from"../../chunks/tslib.es6.js";import{getAssetUrl as t}from"../../assets.js";import r from"../../config.js";import s from"../../Graphic.js";import o from"../../PopupTemplate.js";import"../../symbols.js";import i from"../../core/Accessor.js";import l from"../../core/Collection.js";import{deprecatedProperty as a}from"../../core/deprecate.js";import n from"../../core/Error.js";import u from"../../core/Evented.js";import c from"../../core/Handles.js";import h from"../../core/Logger.js";import{unwrap as p,isSome as d,isNone as g}from"../../core/maybe.js";import{eachAlways as m,after as y}from"../../core/promiseUtils.js";import{watch as f,whenOnce as S,initial as _}from"../../core/reactiveUtils.js";import{property as v}from"../../core/accessorSupport/decorators/property.js";import"../../core/arrayUtils.js";import"../../core/has.js";import"../../core/accessorSupport/ensureType.js";import{subclass as b}from"../../core/accessorSupport/decorators/subclass.js";import w from"../../geometry/Point.js";import x from"../../geometry/SpatialReference.js";import{onLocaleChange as I}from"../../intl/locale.js";import{fetchMessageBundle as L}from"../../intl/messages.js";import T from"../../portal/Portal.js";import{highlightsSupported as j}from"../../views/support/layerViewUtils.js";import R from"./LayerSearchSource.js";import P from"./LocatorSearchSource.js";import E from"./SearchSource.js";import{getPointFromGeometry as F,getPointWithElevation as A}from"./support/geometryUtils.js";import{isArcGISWorldGeocoder as C,meteredArcGISLocatorUrl as G,isProxiedArcGISWorldGeocoder as O,isMeteredArcGISWorldGeocoder as N}from"./support/locatorUtils.js";import{supported as D,getCurrentPosition as M,positionToPoint as V}from"../support/geolocationUtils.js";import{GoToMixin as k}from"../support/GoTo.js";import B from"../../symbols/PictureMarkerSymbol.js";import J from"../../symbols/SimpleLineSymbol.js";import U from"../../symbols/SimpleFillSymbol.js";import H from"../../symbols/TextSymbol.js";function Q(e,t){return e.hasOwnProperty(t)&&null!=e[t]&&""!==e[t]}const Z=()=>L("esri/widgets/Search/t9n/Search"),q="esri.widgets.Search.SearchViewModel",z=h.getLogger(q),K="highlight",W=l.ofType({key:e=>e.layer?"layer":"locator",base:E,typeMap:{layer:R,locator:P}}),X=x.WGS84,Y="esri/images/search/search-symbol-32.png",$=/<(?:.|\s)*?>/g,ee=-1;let te=class extends(k(u.EventedMixin(i))){constructor(e){super(e),this._handles=new c,this._gotoController=null,this._searching=null,this._createdFeatureLayers=[],this.autoNavigate=!0,this.autoSelect=!0,this.defaultPopupTemplate=null,this.defaultSources=new W,this.defaultSymbols={point:new B({url:t(Y),size:24,width:24,height:24}),polyline:new J({color:[130,130,130,1],width:2}),polygon:new U({color:[235,235,235,.4],outline:{color:[130,130,130,1],width:2}})},this.includeDefaultSources=!0,this.maxInputLength=128,this.maxResults=6,this.maxSuggestions=6,this.messages=null,this.minSuggestCharacters=3,this.popupEnabled=!0,this.popupTemplate=null,this.portal=T.getDefault(),this.resultCount=null,this.resultGraphicEnabled=!0,this.resultGraphic=null,this.results=null,this.selectedSuggestion=null,this.searchAllEnabled=!0,this.selectedResult=null,this.sources=new W,this.suggestionDelay=350,this.suggestionCount=null,this.suggestions=null,this.suggestionsEnabled=!0,this.view=null}initialize(){const e=async()=>{const e=await Z();this.messages=e,this.defaultPopupTemplate=new o({title:e.searchResult,content:"{Match_addr}"})};e(),this._handles.add([f((()=>[this.includeDefaultSources,this.view,this.portal]),(()=>this._update()),_),I(e)])}destroy(){this._destroyFeatureLayers(),this._abortGoTo(),this.clearGraphics(),this._handles.destroy(),this._handles=null}get activeSource(){return this.allSources.getItemAt(this.activeSourceIndex)||null}get activeSourceIndex(){return 1===this.allSources.length||!this.searchAllEnabled?0:ee}set activeSourceIndex(e){void 0!==e?this._override("activeSourceIndex",e):this._clearOverride("activeSourceIndex")}get allPlaceholder(){return this.messages?.allPlaceholder}set allPlaceholder(e){e?this._override("allPlaceholder",e):this._clearOverride("allPlaceholder")}get allSources(){const{sources:e,defaultSources:t,includeDefaultSources:r}=this,s="function"==typeof r?r.call(null,{sources:e,defaultSources:t}):r?t.concat(e):e,o=this._get("allSources")||new W;return o.removeAll(),o.addMany(s.filter(Boolean)),o}get defaultSymbol(){return a(z,"defaultSymbol",{replacement:"defaultSymbols",version:"4.22"}),this._get("defaultSymbol")}set defaultSymbol(e){a(z,"defaultSymbol",{replacement:"defaultSymbols",version:"4.22"}),this._set("defaultSymbol",e)}get locationEnabled(){return this._get("locationEnabled")||D()}set locationEnabled(e){if(void 0===e)return void this._clearOverride("locationEnabled");const t=D();if(e&&!t){const e=new n("locationEnabled:geolocation-unsupported","Geolocation API is unsupported.",{geolocation:navigator.geolocation});z.error(e)}this._override("locationEnabled",e&&t)}get placeholder(){const{allSources:e,activeSourceIndex:t,allPlaceholder:r}=this;if(t===ee)return r;const s=e.getItemAt(t);return s?s.placeholder:""}set searchTerm(e){this._set("searchTerm",e||""),this.clearGraphics(),this.selectedSuggestion&&this.selectedSuggestion.text!==e&&this._set("selectedSuggestion",null),""===e&&this._clear()}get searchTerm(){return this._get("searchTerm")||""}get state(){return this._searching?"searching":this.updating?"loading":0===this.allSources.length?"disabled":"ready"}get updating(){return null!=this._updatingPromise}clear(){this.searchTerm=""}clearGraphics(){this._removeHighlight(),this._closePopup(),this.view&&this.view.graphics.remove(this.resultGraphic),this._set("resultGraphic",null)}search(e,t){this.emit("search-start"),this.clearGraphics();const r=this._createSuggestionForSearch(e),s=this.when().then((()=>this._getResultsFromSources(r,t).then((e=>{if(p(t?.signal)?.aborted)return null;const s={activeSourceIndex:this.activeSourceIndex,searchTerm:r.text,numResults:0,numErrors:0,errors:[],results:[]};this._formatResponse(s,e,r);const o=this._getFirstResult(s.results),i=(r.location&&o?o.name:r.text).replace($,"");return this._set("searchTerm",i),(r.key&&"number"==typeof r.sourceIndex||r.location)&&this._set("selectedSuggestion",r),this._set("results",s.results),this._set("resultCount",s.results.reduce(((e,t)=>e+t.results.length),0)),this.emit("search-complete",s),this._selectFirstResult(o).then((()=>s))})))).then((e=>(this._clearSearching(),e)),(e=>{throw this._clearSearching(),e}));return this._searching=s,s}searchNearby(e){if(!this.locationEnabled){const e=new n("searchNearby:geolocation-unsupported","Geolocation API is unsupported.",{geolocation:navigator.geolocation});return z.error(e),Promise.reject(e)}const t=M().then((t=>V({position:t,view:this.view},e))).then((t=>this.search(t,e)));return this._searching=t,t.catch((()=>{})).then((()=>this._clearSearching())),t}select(e){if(this.clearGraphics(),!e){const t=new n("select:missing-parameter","Cannot select without a searchResult.",{searchResult:e});return z.error(t),Promise.reject(t)}const{view:t}=this,r=Q(e,"sourceIndex")?e.sourceIndex:this._getSourceIndexOfResult(e),o=this.allSources.getItemAt(r);if(!o){const e=new n("select:missing-source","Cannot select without a source.",{source:o});return z.error(e),Promise.reject(e)}const i=o instanceof R?this._getLayerSourcePopupTemplate(o):o.popupTemplate,l=o.resultSymbol||this._getDefaultSymbol(e),a=Q(o,"resultGraphicEnabled")?o.resultGraphicEnabled:this.resultGraphicEnabled,u=Q(o,"autoNavigate")?o.autoNavigate:this.autoNavigate,c=Q(o,"popupEnabled")?o.popupEnabled:this.popupEnabled,h=c?i||this.popupTemplate||this.defaultPopupTemplate:null,{feature:p}=e;if(!p){const e=new n("select:missing-feature","Cannot select without a feature.",{feature:p});return z.error(e),Promise.reject(e)}const{attributes:g,geometry:y,layer:f,sourceLayer:S}=p,_=F(y),v={layerViewQuery:this._getLayerView(p),elevationQuery:t&&d(_)?A(_,t).catch((()=>_)):Promise.resolve(_)};return m(v).then((i=>{const n=i.layerViewQuery.value,m=i.elevationQuery.value;l instanceof H&&(l.text=e.name);const _=t&&u?e.target||e.extent:null;return(d(_)?this._goToSearchResult(_):Promise.resolve()).then((()=>{const i=n?p:new s({geometry:y,symbol:l,attributes:g,layer:f,sourceLayer:S,popupTemplate:h}),u=c&&this.view?.popup,d=u&&i.getEffectivePopupTemplate(u.defaultPopupTemplateEnabled);return d&&t.popup.open({features:[i],location:m}),n&&j(n)&&!d&&this._highlightFeature({graphic:i,layerView:n}),!n&&a&&t&&t.graphics.push(i),this._setResultFloor(e),this._set("selectedResult",e),this._set("resultGraphic",i),this.emit("select-result",{result:e,source:o,sourceIndex:r}),e}))}))}suggest(e,t,r){const s=e||this.searchTerm;return this.emit("suggest-start",{searchTerm:s}),this._suggestTimer(t,r).then((()=>this._suggestImmediate(s,r).then((e=>(this._set("suggestions",e.results),this._set("suggestionCount",e.results.reduce(((e,t)=>e+t.results.length),0)),this.emit("suggest-complete",e),e)))))}async when(){await S((()=>!this.updating))}async _update(){const{portal:e,view:t}=this;if(this.includeDefaultSources){const r=this._updatingPromise=m([e?.load(),t?.when()]);if(this.destroyed)return;if(await r,r!==this._updatingPromise)return}await S((()=>this.messages)),this.destroyed||this._updateDefaultSources(),this._updatingPromise=null}_clearSearching(){this._searching=null}_convertHelperServices(){const e=this.portal?.helperServices?.geocode;if(!e)return[];return e.map((e=>{if(!1===e.placefinding)return;const t=r.apiKey&&C(e.url)?{url:G}:null,s=P.fromJSON({...e,...t}),o=s.url;if(C(o)||O(o)||N(o)){const e=s.outFields||["Addr_type","Match_addr","StAddr","City"],t=s.placeholder||this.messages.placeholder,r="number"==typeof s.defaultZoomScale?s.defaultZoomScale:2500;s.set({singleLineFieldName:"SingleLine",outFields:e,placeholder:t,defaultZoomScale:r})}return s.singleLineFieldName?s:void 0})).filter(Boolean)}_destroyFeatureLayers(){this._createdFeatureLayers.forEach((e=>e?.destroy())),this._createdFeatureLayers=[]}_getLayerSources(e,t){const r=this.view?.map;return e.map((e=>{const s=r.findLayerById(e.id);if(!s)return;const o=this._getLayerJSON(e),i=R.fromJSON(o);return i.placeholder=t,this._getLayer(s,o).then((e=>{i.layer=e})),i})).filter(Boolean).toArray()}_getTableSources(e,t){const r=this.view?.map;return e.map((e=>{const s=r.findTableById(e.id);if(!s)return;const o=this._getLayerJSON(e),i=R.fromJSON(o);return i.placeholder=t,this._getLayer(s,o).then((e=>{i.layer=e})),i})).filter(Boolean).toArray()}_convertApplicationProperties(){const e=this.view?.map,t=e?.applicationProperties?.viewing?.search;if(!t)return[];const{enabled:r,hintText:s,layers:o,tables:i}=t;if(!r)return[];return[...this._getLayerSources(o,s),...this._getTableSources(i,s)]}async _getSubLayer(e,t){if(await e.load(),!e.allSublayers)throw new Error;const r=e.allSublayers.find((e=>e.id===t.subLayer));if(!r)throw new Error;const s=await r.createFeatureLayer();return this._createdFeatureLayers.push(s),s}async _getBuildingSubLayer(e,t){await e.load();const r=e.allSublayers.find((e=>e.id===t.subLayer));if("building-component"!==r?.type)throw new Error;if(await r.load(),null==r.associatedLayer)throw new Error;return await r.associatedLayer.load(),r}_getLayer(e,t){return"feature"===e.type||"scene"===e.type||"csv"===e.type||"geojson"===e.type||"ogc-feature"===e.type?Promise.resolve(e):"map-image"===e.type?this._getSubLayer(e,t).catch((()=>{const t=new n("search:create-featurelayer","Could not create a FeatureLayer from the MapImageLayer",{layer:e});return z.error(t),null})):"building-scene"===e.type?this._getBuildingSubLayer(e,t):Promise.resolve(null)}_getLayerJSON(e){return"function"==typeof e.toJSON?e.toJSON():e}_updateDefaultSources(){const{defaultSources:e,includeDefaultSources:t}=this;this._destroyFeatureLayers(),e.removeAll(),t&&e.addMany([...this._convertApplicationProperties(),...this._convertHelperServices()])}_abortGoTo(){this._gotoController&&this._gotoController.abort(),this._gotoController=null}_clear(){this._abortGoTo(),this._set("resultCount",null),this._set("results",null),this._set("suggestions",null),this._set("suggestionCount",null),this._set("selectedResult",null),this._set("selectedSuggestion",null),this.emit("search-clear")}_closePopup(){const e=this.view?.popup,{resultGraphic:t}=this;if(!e||!t)return;const{selectedFeature:r}=e;r&&r===t&&e.close()}_suggestTimer(e,t){const r=null!=e?e:this.suggestionDelay;return y(r,null,t&&t.signal)}_createLocationForSearch(e){return e instanceof s?F(e.geometry):e instanceof w?e:Array.isArray(e)&&2===e.length?new w({longitude:e[0],latitude:e[1]}):null}_createSuggestionForSearch(e){if(e&&Q(e,"key")&&Q(e,"text")&&Q(e,"sourceIndex"))return e;const t=this._createLocationForSearch(e),r="string"==typeof e?e:this.searchTerm,{selectedSuggestion:s,selectedResult:o}=this,i=!e&&s&&o,l=i&&s.key===o.key&&s.sourceIndex===o.sourceIndex,a=i&&s.location;return l||a?s:{location:p(t),text:t?"":r,sourceIndex:null,key:null}}_getFirstResult(e){let t=null;return e&&e.some((e=>{const{results:r}=e,s=r[0],o=!!s;return o&&(t=s),o})),t}_selectFirstResult(e){return this.autoSelect&&e?this.select(e):Promise.resolve(null)}_suggestImmediate(e,t){return this.when().then((()=>this._getSuggestionsFromSources(e,t))).then((r=>{if(p(t?.signal)?.aborted)return null;const s={activeSourceIndex:this.activeSourceIndex,searchTerm:e,numResults:0,numErrors:0,errors:[],results:[]};return this._formatResponse(s,r),s}))}_formatSourceResponse(e,t,r){const s=t&&t.value||[],o=t&&t.error,i=this.allSources.getItemAt(r);if(o){const t={sourceIndex:r,source:i,error:o};e.errors.push(t),z.error(o),e.numErrors++}else{const t={sourceIndex:r,source:i,results:s};e.results.push(t),e.numResults+=s.length}}_formatResponse(e,t,r){if(t)if(e.activeSourceIndex===ee){const s=r&&Q(r,"sourceIndex")&&-1!==r.sourceIndex?r.sourceIndex:void 0;t.forEach(((t,r)=>{const o=void 0!==s?s:r;this._formatSourceResponse(e,t,o)}))}else this._formatSourceResponse(e,t[0],e.activeSourceIndex)}_getResultsFromSources(e,t){const{allSources:r}=this,s=!e.location&&Q(e,"sourceIndex")?e.sourceIndex:this.activeSourceIndex,o=[];if(!r.length){const e=new n("search:no-sources-defined","At least one source is required.",{allSources:r});return z.error(e),Promise.reject(e)}return s===ee?r.forEach(((r,s)=>{o.push(this._getResultsFromSource(e,s,t))})):o.push(this._getResultsFromSource(e,s,t)),m(o)}_getSuggestionsFromSources(e,t){const{allSources:r,activeSourceIndex:s}=this,o=[];if(!r.length){const e=new n("suggest:no-sources-defined","At least one source is required.",{allSources:r});return z.error(e),Promise.reject(e)}return s===ee?r.forEach(((r,s)=>{o.push(this._getSuggestionsFromSource(e,s,t))})):o.push(this._getSuggestionsFromSource(e,s,t)),m(o)}_getResultsFromSource(e,t,r){const s=this.allSources.getItemAt(t),{location:o=null}=e,i=this.view?.spatialReference||X,l=Q(s,"maxResults")?s.maxResults:this.maxResults,a=!!(s instanceof R&&Q(s,"exactMatch"))&&s.exactMatch,{view:n}=this;return s.getResults({view:n,sourceIndex:t,location:o,suggestResult:e,spatialReference:i,exactMatch:a,maxResults:l},r)}_getSuggestionsFromSource(e,t,r){const s=this.allSources.getItemAt(t),o=Q(s,"suggestionsEnabled")?s.suggestionsEnabled:this.suggestionsEnabled,i=e.length,l=Q(s,"minSuggestCharacters")?s.minSuggestCharacters:this.minSuggestCharacters;if(o&&e.trim()&&i>=l){const o=this.view?.spatialReference||X,i=Q(s,"maxSuggestions")?s.maxSuggestions:this.maxSuggestions,{view:l}=this,a=!!(s instanceof R&&Q(s,"exactMatch"))&&s.exactMatch;return s.getSuggestions({view:l,sourceIndex:t,suggestTerm:e,spatialReference:o,maxSuggestions:i,exactMatch:a},r)}return Promise.resolve(null)}_getLayerSourcePopupTemplate(e){const{layer:t}=e;if(t)return Q(e,"popupTemplate")?e.popupTemplate:t.popupTemplate}_getSourceIndexOfResult(e){const t=this.results;let r=null;return t.some((t=>t.results.some((s=>s===e&&(r=t.sourceIndex,!0))))),r}async _goToSearchResult(e){const t=!!e;this._abortGoTo();const r=new AbortController;this._gotoController=r;const s={target:{target:e},options:{animate:t,signal:r.signal}};await this.callGoTo(s),this._gotoController=null}_getDefaultSymbol(e){const{defaultSymbols:t}=this,r=e.feature?.geometry;if(g(r))return null;const s=this._get("defaultSymbol");if(s&&("point"===r.type||"multipoint"===r.type))return s;switch(r.type){case"point":case"multipoint":return t.point;case"polyline":return t.polyline;case"extent":case"polygon":return t.polygon;default:return null}}_removeHighlight(){this._handles.remove(K)}async _getLayerView(e){const{view:t}=this;if(!e||!t||"building-component"===e.layer.type||"subtype-sublayer"===e.layer.type)return null;const{layer:r}=e;return await t.when(),t.whenLayerView(r)}_highlightFeature(e){const{graphic:t,layerView:r}=e,{attributes:s,layer:o}=t,{objectIdField:i}=o,l=s&&s[i],a=r.highlight(l||t);this._handles.add(a,K)}_setResultFloor(e){const{view:t}=this,r=e.feature?.attributes,s=e.feature?.sourceLayer;if(s&&"floorInfo"in s&&s?.floorInfo?.floorField&&r){const e=r[s.floorInfo.floorField];t.emit("select-result-floor",e)}}};te.ALL_INDEX=ee,e([v()],te.prototype,"_searching",void 0),e([v()],te.prototype,"_updatingPromise",void 0),e([v({readOnly:!0,value:null})],te.prototype,"activeSource",null),e([v()],te.prototype,"activeSourceIndex",null),e([v()],te.prototype,"allPlaceholder",null),e([v({readOnly:!0})],te.prototype,"allSources",null),e([v()],te.prototype,"autoNavigate",void 0),e([v()],te.prototype,"autoSelect",void 0),e([v()],te.prototype,"defaultPopupTemplate",void 0),e([v({readOnly:!0})],te.prototype,"defaultSources",void 0),e([v()],te.prototype,"defaultSymbol",null),e([v()],te.prototype,"defaultSymbols",void 0),e([v()],te.prototype,"includeDefaultSources",void 0),e([v()],te.prototype,"locationEnabled",null),e([v()],te.prototype,"maxInputLength",void 0),e([v()],te.prototype,"maxResults",void 0),e([v()],te.prototype,"maxSuggestions",void 0),e([v()],te.prototype,"messages",void 0),e([v()],te.prototype,"minSuggestCharacters",void 0),e([v({readOnly:!0})],te.prototype,"placeholder",null),e([v()],te.prototype,"popupEnabled",void 0),e([v({type:o})],te.prototype,"popupTemplate",void 0),e([v({type:T})],te.prototype,"portal",void 0),e([v()],te.prototype,"resultCount",void 0),e([v()],te.prototype,"resultGraphicEnabled",void 0),e([v({readOnly:!0})],te.prototype,"resultGraphic",void 0),e([v({readOnly:!0})],te.prototype,"results",void 0),e([v({readOnly:!0})],te.prototype,"selectedSuggestion",void 0),e([v()],te.prototype,"searchAllEnabled",void 0),e([v({readOnly:!0})],te.prototype,"selectedResult",void 0),e([v()],te.prototype,"searchTerm",null),e([v({type:W})],te.prototype,"sources",void 0),e([v({readOnly:!0})],te.prototype,"state",null),e([v()],te.prototype,"suggestionDelay",void 0),e([v()],te.prototype,"suggestionCount",void 0),e([v({readOnly:!0})],te.prototype,"suggestions",void 0),e([v()],te.prototype,"suggestionsEnabled",void 0),e([v({readOnly:!0})],te.prototype,"updating",null),e([v()],te.prototype,"view",void 0),e([v()],te.prototype,"clear",null),te=e([b(q)],te);const re=te;export{re as default};
|