UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) • 15.8 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import{substitute as t}from"../intl.js";import s from"../PopupTemplate.js";import{deprecateWidget as o,deprecatedPropertyValue as r}from"../core/deprecate.js";import i from"../core/Logger.js";import{destroyMaybe as l,abortMaybe as n}from"../core/maybe.js";import{ignoreAbortErrors as a,isAbortError as c}from"../core/promiseUtils.js";import{watch as u,initial as h,on as d}from"../core/reactiveUtils.js";import{waitAnimationFrame as p}from"../core/scheduling.js";import{property as g,subclass as m}from"../core/accessorSupport/decorators.js";import v from"../portal/Portal.js";import{popupFromView as _,closePopup as w}from"../views/popupAdapter.js";import M from"./Widget.js";import{css as S}from"./Search/css.js";import b from"./Search/SearchResultRenderer.js";import y from"./Search/SearchViewModel.js";import{SourceCollection as f}from"./Search/types.js";import{loadCalciteComponents as C}from"./support/componentsUtils.js";import{globalCss as T}from"./support/globalCss.js";import{messageBundle as A,vmEvent as R,tsx as E}from"./support/widget.js";let x=class extends M{constructor(e,t){super(e,t),this._currentLocationValue=`${this.id}-search-use-current-location`,this._autocompleteNode=null,this._focusAbortController=null,this._searchResultRenderer=new b,this._suggestController=null,this._searchController=null,this._lastSearchTerm="",this._locateFailed=!1,this._showNoResults=!1,this.disabled=!1,this.messages=null,this.messagesCommon=null,this.viewModel=new y,this._renderSearchResultsContent=()=>this._searchResultRenderer,!0!==e?.suppressDeprecationWarning&&o(i.getLogger(this),"Search","arcgis-search",{version:"4.33"}),this.addHandles([u(()=>this.viewModel,e=>this._searchResultRenderer.viewModel=e,h),u(()=>this.viewModel.results,()=>this._searchResultRenderer.showMoreResultsOpen=!1),u(()=>this.messages,e=>this.viewModel.messages=e),u(()=>this.viewModel?.defaultPopupTemplate,e=>{e&&(e.content=this._renderSearchResultsContent)},h),u(()=>this.viewModel?.view?.popupElement&&this.viewModel?.defaultPopupTemplate,()=>{if(this.viewModel?.defaultPopupTemplate&&this.viewModel?.view?.popupElement){const e=document.createElement("arcgis-search-result-renderer");e.viewModel=this.viewModel,e.messages=this.messages,this.viewModel.defaultPopupTemplate.content=e}},h),u(()=>this.viewModel?.resultGraphic,(e,t)=>this._closePopup(t)),d(()=>this.viewModel,"select-result",e=>{const{popupEnabled:t}=e,{view:s,resultGraphic:o,resultLocation:r}=this.viewModel,i=_(s);s&&i&&t&&r&&o?.getEffectivePopupTemplate(i.defaultPopupTemplateEnabled)&&s.openPopup({features:o?[o]:[],location:r})})])}normalizeCtorArgs(e={}){const{suppressDeprecationWarning:t,...s}=e;return s}loadDependencies(){return C({autocomplete:()=>import("@esri/calcite-components/dist/components/calcite-autocomplete"),"autocomplete-item-group":()=>import("@esri/calcite-components/dist/components/calcite-autocomplete-item-group"),"autocomplete-item":()=>import("@esri/calcite-components/dist/components/calcite-autocomplete-item"),button:()=>import("@esri/calcite-components/dist/components/calcite-button"),dropdown:()=>import("@esri/calcite-components/dist/components/calcite-dropdown"),"dropdown-group":()=>import("@esri/calcite-components/dist/components/calcite-dropdown-group"),"dropdown-item":()=>import("@esri/calcite-components/dist/components/calcite-dropdown-item"),notice:()=>import("@esri/calcite-components/dist/components/calcite-notice")})}destroy(){this._cancelQueries(),this._searchResultRenderer=l(this._searchResultRenderer),this._focusAbortController?.abort()}get _isDisabled(){const{state:e}=this.viewModel;return"disabled"===e||"loading"===e||this.disabled}get _showCurrentLocation(){return this.locationEnabled&&!this.searchTerm?.trim()}get _effectiveActiveMenu(){const{activeMenu:e}=this;return this._isDisabled?"none":"warning"===e?"suggestion":e}get activeMenu(){return"none"}set activeMenu(e){"warning"===e&&r(i.getLogger(this),"activeMenu","warning",{replacement:"Use the value 'suggestion' instead",version:"4.32",warnOnce:!0}),this._overrideIfSome("activeMenu",e)}get activeSource(){return this.viewModel?.activeSource}get activeSourceIndex(){return this.viewModel.activeSourceIndex}set activeSourceIndex(e){this.viewModel.activeSourceIndex=e}get allPlaceholder(){return this.viewModel.allPlaceholder}set allPlaceholder(e){this.viewModel.allPlaceholder=e}get allSources(){return this.viewModel.allSources}get autoNavigate(){return this.viewModel.autoNavigate}set autoNavigate(e){this.viewModel.autoNavigate=e}get autoSelect(){return this.viewModel.autoSelect}set autoSelect(e){this.viewModel.autoSelect=e}get defaultSources(){return this.viewModel.defaultSources}get goToOverride(){return this.viewModel.goToOverride}set goToOverride(e){this.viewModel.goToOverride=e}get icon(){return"search"}set icon(e){this._overrideIfSome("icon",e)}get includeDefaultSources(){return this.viewModel.includeDefaultSources}set includeDefaultSources(e){this.viewModel.includeDefaultSources=e}get label(){return this.messages?.widgetLabel??""}set label(e){this._overrideIfSome("label",e)}get locationEnabled(){return this.viewModel.locationEnabled}set locationEnabled(e){this.viewModel.locationEnabled=e}get maxResults(){return this.viewModel.maxResults}set maxResults(e){this.viewModel.maxResults=e}get maxSuggestions(){return this.viewModel.maxSuggestions}set maxSuggestions(e){this.viewModel.maxSuggestions=e}get minSuggestCharacters(){return this.viewModel.minSuggestCharacters}set minSuggestCharacters(e){this.viewModel.minSuggestCharacters=e}get popupEnabled(){return this.viewModel.popupEnabled}set popupEnabled(e){this.viewModel.popupEnabled=e}get popupTemplate(){return this.viewModel.popupTemplate}set popupTemplate(e){this.viewModel.popupTemplate=e}get portal(){return this.viewModel?.portal}set portal(e){this.viewModel&&(this.viewModel.portal=e)}get resultGraphic(){return this.viewModel.resultGraphic}set resultGraphic(e){this.viewModel.resultGraphic=e}get resultGraphicEnabled(){return this.viewModel.resultGraphicEnabled}set resultGraphicEnabled(e){this.viewModel.resultGraphicEnabled=e}get results(){return this.viewModel.results}get searchAllEnabled(){return this.viewModel.searchAllEnabled}set searchAllEnabled(e){this.viewModel.searchAllEnabled=e}get searchTerm(){return this.viewModel.searchTerm}set searchTerm(e){this.viewModel.searchTerm=e}get selectedResult(){return this.viewModel.selectedResult}get sources(){return this.viewModel.sources}set sources(e){this.viewModel.sources=e}get suggestions(){return this.viewModel.suggestions}get suggestionsEnabled(){return this.viewModel.suggestionsEnabled}set suggestionsEnabled(e){this.viewModel.suggestionsEnabled=e}get view(){return this.viewModel.view}set view(e){this.viewModel.view=e}clear(){this._locateFailed=!1,this.viewModel.clear()}focus(){this._handleFocus(),this._emitFocus()}blur(){this._autocompleteNode?.blur(),this._emitBlur()}async search(e){this._cancelQueries();const t=new AbortController,{signal:s}=t;this._searchController=t;try{const o=await this.viewModel.search(e,{signal:s});if(this._searchController!==t)return;return o?.numResults||(this._showNoResults=!0,this.activeMenu="suggestion"),this._searchController=null,o}catch(o){if(this._searchController!==t)return;return void(this._searchController=null)}}async suggest(e){this._cancelSuggest();const t=new AbortController,{signal:s}=t;this._suggestController=t;try{const o=await this.viewModel.suggest(e,null,{signal:s});if(this._suggestController!==t)return;return this._suggestController=null,o?.numResults&&(this.activeMenu="suggestion",this._scrollToTopSuggestion()),o}catch(o){return this._suggestController!==t||(this._suggestController=null),null}}render(){return E("div",{class:this.classes(S.base,T.widget)},E("div",{class:S.container},this._renderSourcesMenu(),this._renderForm()))}_closePopup(e){const t=_(this.view);e&&t?.selectedFeature===e&&w(this.view?.popup,this.view?.popupElement)}_handleInputKeydown(e){"Tab"===e.key&&this._cancelSuggest()}_emitFocus(){this.emit("search-focus")}_emitBlur(){this.emit("search-blur")}async _handleFocus(){this._focusAbortController?.abort(),this._focusAbortController=new AbortController;const e=this._focusAbortController.signal;await a(p(e)),this._autocompleteNode?.setFocus()}_renderSourcesMenu(){const{messages:e,_isDisabled:t,_effectiveActiveMenu:s}=this,{allSources:o,searchAllEnabled:r}=this.viewModel;return o.length>1?E("calcite-dropdown",{bind:this,class:S.dropdown,disabled:t,open:"source"===s,overlayPositioning:"fixed",topLayerDisabled:this.topLayerDisabled,onCalciteDropdownClose:this._handleSourceClose,onCalciteDropdownOpen:this._handleSourceOpen,onCalciteDropdownSelect:this._handleSourceSelect},E("calcite-button",{appearance:"outline-fill",iconStart:"caret-down",kind:"neutral",label:e.searchIn,slot:"trigger"}),E("calcite-dropdown-group",{groupTitle:e.searchIn},r?this._renderSource(y.ALL_INDEX):null,o.map((e,t)=>this._renderSource(t)).toArray())):null}_renderUseCurrentLocation(){const{_currentLocationValue:e}=this;return E("calcite-autocomplete-item",{heading:this.messages.useCurrentLocation,iconStart:"gps-on",key:e,value:e})}_handleAutocompleteCreate(e){this._autocompleteNode=e}_handleCalciteAutocompleteTextInput(e){const t=e.target.inputValue;t!==this.viewModel.searchTerm&&(this.viewModel.searchTerm=t,t||this.clear(),this.suggest())}_handleAutocompleteOpen(){this.activeMenu="suggestion"}_handleAutocompleteClose(){"suggestion"===this.activeMenu&&(this.activeMenu="none")}_handleCalciteAutocompleteChange(e){const t=e.target.value;if(t===this._currentLocationValue)return void this._useCurrentLocation();const s=JSON.parse(t),o=this.viewModel.suggestions?.find(({sourceIndex:e})=>e===s.sourceIndex)?.results?.find(({key:e})=>`${e}`==`${s.key}`);o&&this.search(o)}_renderAutocomplete(){const{_effectiveActiveMenu:e,messages:t,_isDisabled:s}=this,{maxInputLength:o,placeholder:r,searchTerm:i,state:l}=this.viewModel,n=this.label??t.searchButtonTitle??"";return E("calcite-autocomplete",{afterCreate:this._handleAutocompleteCreate,autocomplete:"off",bind:this,class:S.autocomplete,disabled:s,icon:this.icon||!1,inputValue:i,label:n,loading:"searching"===l,maxLength:o,name:this.id,onblur:this._emitBlur,onfocus:this._emitFocus,onkeydown:this._handleInputKeydown,open:"suggestion"===e,overlayPositioning:"fixed",placeholder:r,title:i?"":r,topLayerDisabled:this.topLayerDisabled,onCalciteAutocompleteChange:this._handleCalciteAutocompleteChange,onCalciteAutocompleteClose:this._handleAutocompleteClose,onCalciteAutocompleteOpen:this._handleAutocompleteOpen,onCalciteAutocompleteTextChange:this._handleCalciteAutocompleteTextInput,onCalciteAutocompleteTextInput:this._handleCalciteAutocompleteTextInput},this._renderSuggestions(),this._renderNotices())}_renderForm(){return E("form",{bind:this,class:S.form,disabled:this._isDisabled,key:S.form,onsubmit:this._formSubmit,role:"search"},this._renderAutocomplete(),E("button",{class:S.submitButton,type:"submit"},this.label??this.messages.searchButtonTitle??""))}_renderSuggestGroup(e){return e.results?.map(e=>this._renderSuggestion(e))}_renderSuggestions(){const{suggestions:e}=this.viewModel;return this._showNoResults?null:this._showCurrentLocation?this._renderUseCurrentLocation():e?.map(e=>this._renderSuggestResults(e))}_renderSuggestResults(e){const{allSources:t,activeSourceIndex:s}=this.viewModel,{sourceIndex:o}=e,r=e.results?.length,i=t.length>1&&s===y.ALL_INDEX,l=this._getSourceName(o);return r&&i?E("calcite-autocomplete-item-group",{heading:l,key:`suggestion-group-${l}-${o}`},this._renderSuggestGroup(e)):this._renderSuggestGroup(e)}_renderSuggestion({key:e,sourceIndex:t,text:s}){const o={key:e,sourceIndex:t};return E("calcite-autocomplete-item",{heading:s??this.messages.untitledResult,key:`suggestion_${e}`,value:JSON.stringify(o)})}_renderSource(e){const t=this._getSourceName(e);return E("calcite-dropdown-item",{"data-source-index":`${e}`,key:`source-${t}-${e}`,selected:e===this.viewModel.activeSourceIndex},t)}_renderNoResultsWarning(e){const{messages:s}=this,o=e?t(s.noResultsFoundForValue,{value:`"${e}"`}):s.noResultsFound;return E("calcite-notice",{icon:"exclamation-mark-triangle",key:"no-results-warning",kind:"warning",open:!0,slot:"content-bottom"},E("div",{slot:"title"},s.noResults),E("div",{slot:"message"},o))}_renderLocateError(){return E("calcite-notice",{icon:"exclamation-mark-circle",key:"locate-warning",kind:"danger",open:!0,slot:"content-bottom"},E("div",{slot:"message"},this.messages.locateError))}_renderNotices(){return this._locateFailed?this._renderLocateError():this._showNoResults?this._renderNoResultsWarning(this._lastSearchTerm??this.viewModel.searchTerm):null}async _useCurrentLocation(){this._cancelQueries();const e=new AbortController,{signal:t}=e;this._searchController=e;try{await this.viewModel.searchNearby({signal:t})}catch(s){c(s)||(this._locateFailed=!0,this.activeMenu="suggestion")}finally{this._searchController=null}}_handleSourceOpen(){this.activeMenu="source"}_handleSourceClose(){"source"===this.activeMenu&&(this.activeMenu="none")}_handleSourceSelect(e){const t=e.target.selectedItems[0].getAttribute("data-source-index");t&&(this.viewModel.activeSourceIndex=parseInt(t,10))}_cancelSuggest(){this.activeMenu="none",this._locateFailed=!1,this._showNoResults=!1,this._suggestController=n(this._suggestController)}_cancelQueries(){this._cancelSuggest(),this._searchController=n(this._searchController)}_scrollToTopSuggestion(){this._autocompleteNode?.scrollContentTo({top:0})}_formSubmit(e){e.preventDefault(),this.activeMenu="none",this._lastSearchTerm=this.searchTerm,this.searchTerm&&this.search()}_getSourceName(e){const{messages:t}=this,{allSources:s}=this.viewModel,o=s.at(e);return e===y.ALL_INDEX?t.all:o?.name||t.untitledSource}};e([g()],x.prototype,"_locateFailed",void 0),e([g()],x.prototype,"_showNoResults",void 0),e([g()],x.prototype,"_isDisabled",null),e([g()],x.prototype,"_showCurrentLocation",null),e([g()],x.prototype,"_effectiveActiveMenu",null),e([g()],x.prototype,"activeMenu",null),e([g({readOnly:!0})],x.prototype,"activeSource",null),e([g()],x.prototype,"activeSourceIndex",null),e([g()],x.prototype,"allPlaceholder",null),e([g({readOnly:!0})],x.prototype,"allSources",null),e([g()],x.prototype,"autoNavigate",null),e([g()],x.prototype,"autoSelect",null),e([g({readOnly:!0})],x.prototype,"defaultSources",null),e([g()],x.prototype,"disabled",void 0),e([g()],x.prototype,"goToOverride",null),e([g()],x.prototype,"icon",null),e([g()],x.prototype,"includeDefaultSources",null),e([g()],x.prototype,"label",null),e([g()],x.prototype,"locationEnabled",null),e([g()],x.prototype,"maxResults",null),e([g()],x.prototype,"maxSuggestions",null),e([g(),A("esri/widgets/Search/t9n/Search")],x.prototype,"messages",void 0),e([g(),A("esri/t9n/common")],x.prototype,"messagesCommon",void 0),e([g()],x.prototype,"minSuggestCharacters",null),e([g()],x.prototype,"popupEnabled",null),e([g({type:s})],x.prototype,"popupTemplate",null),e([g({type:v})],x.prototype,"portal",null),e([g()],x.prototype,"resultGraphic",null),e([g()],x.prototype,"resultGraphicEnabled",null),e([g({readOnly:!0})],x.prototype,"results",null),e([g()],x.prototype,"searchAllEnabled",null),e([g()],x.prototype,"searchTerm",null),e([g({readOnly:!0})],x.prototype,"selectedResult",null),e([g({type:f})],x.prototype,"sources",null),e([g({readOnly:!0})],x.prototype,"suggestions",null),e([g()],x.prototype,"suggestionsEnabled",null),e([g()],x.prototype,"view",null),e([R(["search-complete","search-clear","search-start","select-result","suggest-start","suggest-complete"]),g({type:y})],x.prototype,"viewModel",void 0),x=e([m("esri.widgets.Search")],x);const I=x;export{I as default};