terriajs
Version:
Geospatial data visualization platform.
149 lines (129 loc) • 4.49 kB
text/typescript
// import CatalogItemNameSearchProviderViewModel from "../ViewModels/CatalogItemNameSearchProviderViewModel";
import {
observable,
reaction,
IReactionDisposer,
computed,
action
} from "mobx";
import Terria from "../Models/Terria";
import SearchProviderResults from "../Models/SearchProviders/SearchProviderResults";
import SearchProvider from "../Models/SearchProviders/SearchProvider";
import filterOutUndefined from "../Core/filterOutUndefined";
import CatalogSearchProvider from "../Models/SearchProviders/CatalogSearchProvider";
interface SearchStateOptions {
terria: Terria;
catalogSearchProvider?: CatalogSearchProvider;
locationSearchProviders?: SearchProvider[];
}
export default class SearchState {
catalogSearchProvider: SearchProvider | undefined;
locationSearchProviders: SearchProvider[];
catalogSearchText: string = "";
isWaitingToStartCatalogSearch: boolean = false;
locationSearchText: string = "";
isWaitingToStartLocationSearch: boolean = false;
unifiedSearchText: string = "";
isWaitingToStartUnifiedSearch: boolean = false;
showLocationSearchResults: boolean = false;
showMobileLocationSearch: boolean = false;
showMobileCatalogSearch: boolean = false;
locationSearchResults: SearchProviderResults[] = [];
catalogSearchResults: SearchProviderResults | undefined;
unifiedSearchResults: SearchProviderResults[] = [];
private _catalogSearchDisposer: IReactionDisposer;
private _locationSearchDisposer: IReactionDisposer;
private _unifiedSearchDisposer: IReactionDisposer;
constructor(options: SearchStateOptions) {
this.catalogSearchProvider =
options.catalogSearchProvider ||
new CatalogSearchProvider({ terria: options.terria });
this.locationSearchProviders = options.locationSearchProviders || [];
this._catalogSearchDisposer = reaction(
() => this.catalogSearchText,
() => {
this.isWaitingToStartCatalogSearch = true;
if (this.catalogSearchProvider) {
this.catalogSearchResults = this.catalogSearchProvider.search("");
}
}
);
this._locationSearchDisposer = reaction(
() => this.locationSearchText,
() => {
this.isWaitingToStartLocationSearch = true;
this.locationSearchResults = this.locationSearchProviders.map(
(provider) => {
return provider.search("");
}
);
}
);
this._unifiedSearchDisposer = reaction(
() => this.unifiedSearchText,
() => {
this.isWaitingToStartUnifiedSearch = true;
this.unifiedSearchResults = this.unifiedSearchProviders.map(
(provider) => {
return provider.search("");
}
);
}
);
}
dispose() {
this._catalogSearchDisposer();
this._locationSearchDisposer();
this._unifiedSearchDisposer();
}
get unifiedSearchProviders(): SearchProvider[] {
return filterOutUndefined([
this.catalogSearchProvider,
...this.locationSearchProviders
]);
}
searchCatalog() {
if (this.isWaitingToStartCatalogSearch) {
this.isWaitingToStartCatalogSearch = false;
if (this.catalogSearchResults) {
this.catalogSearchResults.isCanceled = true;
}
if (this.catalogSearchProvider) {
this.catalogSearchResults = this.catalogSearchProvider.search(
this.catalogSearchText
);
}
}
}
setCatalogSearchText(newText: string) {
this.catalogSearchText = newText;
}
searchLocations() {
if (this.isWaitingToStartLocationSearch) {
this.isWaitingToStartLocationSearch = false;
this.locationSearchResults.forEach((results) => {
results.isCanceled = true;
});
this.locationSearchResults = this.locationSearchProviders.map(
(searchProvider) => searchProvider.search(this.locationSearchText)
);
}
}
searchUnified() {
if (this.isWaitingToStartUnifiedSearch) {
this.isWaitingToStartUnifiedSearch = false;
this.unifiedSearchResults.forEach((results) => {
results.isCanceled = true;
});
this.unifiedSearchResults = this.unifiedSearchProviders.map(
(searchProvider) => searchProvider.search(this.unifiedSearchText)
);
}
}
}