-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathSearchState.ts
More file actions
171 lines (147 loc) · 4.89 KB
/
SearchState.ts
File metadata and controls
171 lines (147 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
action,
computed,
IReactionDisposer,
makeObservable,
observable,
reaction
} from "mobx";
import filterOutUndefined from "../Core/filterOutUndefined";
import CatalogSearchProviderMixin from "../ModelMixins/SearchProviders/CatalogSearchProviderMixin";
import LocationSearchProviderMixin from "../ModelMixins/SearchProviders/LocationSearchProviderMixin";
import SearchProviderMixin from "../ModelMixins/SearchProviders/SearchProviderMixin";
import SearchProviderResults from "../Models/SearchProviders/SearchProviderResults";
import Terria from "../Models/Terria";
interface SearchStateOptions {
terria: Terria;
}
export default class SearchState {
@observable catalogSearchText: string = "";
@observable isWaitingToStartCatalogSearch: boolean = false;
@observable locationSearchText: string = "";
@observable isWaitingToStartLocationSearch: boolean = false;
@observable unifiedSearchText: string = "";
@observable isWaitingToStartUnifiedSearch: boolean = false;
@observable showLocationSearchResults: boolean = false;
@observable showMobileLocationSearch: boolean = false;
@observable showMobileCatalogSearch: boolean = false;
@observable locationSearchResults: SearchProviderResults[] = [];
@observable catalogSearchResults: SearchProviderResults | undefined;
@observable unifiedSearchResults: SearchProviderResults[] = [];
private _catalogSearchDisposer: IReactionDisposer;
private _locationSearchDisposer: IReactionDisposer;
private _unifiedSearchDisposer: IReactionDisposer;
private _workbenchItemsSubscription: IReactionDisposer;
private readonly terria: Terria;
constructor(options: SearchStateOptions) {
makeObservable(this);
this.terria = options.terria;
const self = this;
this._catalogSearchDisposer = reaction(
() => self.catalogSearchText,
() => {
self.isWaitingToStartCatalogSearch = true;
if (self.catalogSearchProvider) {
self.catalogSearchResults = self.catalogSearchProvider.search("");
}
}
);
this._locationSearchDisposer = reaction(
() => self.locationSearchText,
() => {
self.isWaitingToStartLocationSearch = true;
self.locationSearchResults = self.locationSearchProviders.map(
(provider) => {
return provider.search("");
}
);
}
);
this._unifiedSearchDisposer = reaction(
() => this.unifiedSearchText,
() => {
this.isWaitingToStartUnifiedSearch = true;
this.unifiedSearchResults = this.unifiedSearchProviders.map(
(provider) => {
return provider.search("");
}
);
}
);
this._workbenchItemsSubscription = reaction(
() => this.terria.workbench.items,
() => {
this.showLocationSearchResults = false;
}
);
}
dispose(): void {
this._catalogSearchDisposer();
this._locationSearchDisposer();
this._unifiedSearchDisposer();
this._workbenchItemsSubscription();
}
@computed
get supportsAutocomplete(): boolean {
return this.locationSearchProviders.every((provider) =>
provider.supportsAutocomplete()
);
}
@computed
private get locationSearchProviders(): LocationSearchProviderMixin.Instance[] {
return this.terria.searchBarModel.locationSearchProvidersArray;
}
@computed
get catalogSearchProvider(): CatalogSearchProviderMixin.Instance | undefined {
return this.terria.catalog.searchProvider;
}
@computed
get unifiedSearchProviders(): SearchProviderMixin.Instance[] {
return filterOutUndefined([
this.catalogSearchProvider,
...this.locationSearchProviders
]);
}
@action
searchCatalog(): void {
if (this.isWaitingToStartCatalogSearch) {
this.isWaitingToStartCatalogSearch = false;
if (this.catalogSearchResults) {
this.catalogSearchResults.isCanceled = true;
}
if (this.catalogSearchProvider) {
this.catalogSearchResults = this.catalogSearchProvider.search(
this.catalogSearchText
);
}
}
}
@action
setCatalogSearchText(newText: string): void {
this.catalogSearchText = newText;
}
@action
searchLocations(): void {
if (this.isWaitingToStartLocationSearch) {
this.isWaitingToStartLocationSearch = false;
this.locationSearchResults.forEach((results) => {
results.isCanceled = true;
});
this.locationSearchResults = this.locationSearchProviders.map(
(searchProvider) => searchProvider.search(this.locationSearchText)
);
}
}
@action
searchUnified(): void {
if (this.isWaitingToStartUnifiedSearch) {
this.isWaitingToStartUnifiedSearch = false;
this.unifiedSearchResults.forEach((results) => {
results.isCanceled = true;
});
this.unifiedSearchResults = this.unifiedSearchProviders.map(
(searchProvider) => searchProvider.search(this.unifiedSearchText)
);
}
}
}