-
Notifications
You must be signed in to change notification settings - Fork 24
/
search-manager.ts
599 lines (516 loc) 路 22.2 KB
/
search-manager.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import { KeepTrackApiEvents } from '@app/interfaces';
import { SatelliteFov } from '@app/plugins/satellite-fov/satellite-fov';
import { SatInfoBox } from '@app/plugins/select-sat-manager/sat-info-box';
import { SelectSatManager } from '@app/plugins/select-sat-manager/select-sat-manager';
import type { CatalogManager } from '@app/singletons/catalog-manager';
import { GroupType, ObjectGroup } from '@app/singletons/object-group';
import { CruncerMessageTypes } from '@app/webworker/positionCruncher';
import { DetailedSatellite, SpaceObjectType, Star } from 'ootk';
import { keepTrackApi } from '../keepTrackApi';
import { getEl } from '../lib/get-el';
import { slideInDown, slideOutUp } from '../lib/slide';
import { TopMenu } from '../plugins/top-menu/top-menu';
import { LegendManager } from '../static/legend-manager';
import { UrlManager } from '../static/url-manager';
import { MissileObject } from './catalog-manager/MissileObject';
import type { UiManager } from './uiManager';
export interface SearchResult {
id: number; // Catalog Index
searchType: SearchResultType; // Type of search result
strIndex: number; // Index of the search string in the name
patlen: number; // Length of the search string
}
export enum SearchResultType {
BUS,
ON,
SCC,
INTLDES,
LV,
MISSILE,
STAR,
}
/**
* The `SearchManager` class is responsible for managing the search functionality in the UI.
* It provides methods for performing searches, hiding search results, and retrieving information
* about the current search state.
*/
export class SearchManager {
isSearchOpen = false;
isResultsOpen = false;
private lastResultGroup_ = <ObjectGroup>null;
private uiManager_: UiManager;
constructor(uiManager: UiManager) {
this.uiManager_ = uiManager;
const uiWrapper = getEl('ui-wrapper');
const searchResults = document.createElement('div');
searchResults.id = TopMenu.SEARCH_RESULT_ID;
uiWrapper.prepend(searchResults);
keepTrackApi.register({
event: KeepTrackApiEvents.uiManagerFinal,
cbName: 'Search Manager',
cb: this.addListeners_.bind(this),
});
this.addListeners_();
}
private addListeners_() {
getEl('search-results')?.addEventListener('click', (evt: Event) => {
let satId = SearchManager.getSatIdFromSearchResults_(evt);
if (isNaN(satId) || satId === -1) return;
const catalogManagerInstance = keepTrackApi.getCatalogManager();
const obj = catalogManagerInstance.getObject(satId);
if (obj?.type === SpaceObjectType.STAR) {
keepTrackApi.getMainCamera().lookAtStar(obj as Star);
} else {
keepTrackApi.getPlugin(SelectSatManager)?.selectSat(satId);
}
});
getEl('search-results')?.addEventListener('mouseover', (evt) => {
let satId = SearchManager.getSatIdFromSearchResults_(evt);
if (isNaN(satId) || satId === -1) return;
keepTrackApi.getHoverManager().setHoverId(satId);
this.uiManager_.searchHoverSatId = satId;
});
getEl('search-results')?.addEventListener('mouseout', () => {
keepTrackApi.getHoverManager().setHoverId(-1);
this.uiManager_.searchHoverSatId = -1;
});
getEl('search')?.addEventListener('input', () => {
const searchStr = (<HTMLInputElement>getEl('search')).value;
this.doSearch(searchStr);
});
getEl('search')?.addEventListener('blur', () => {
if (this.isSearchOpen && this.getCurrentSearch().length === 0) {
this.toggleSearch();
}
});
getEl('search-icon')?.addEventListener('click', () => {
this.toggleSearch();
});
}
private static getSatIdFromSearchResults_(evt: Event) {
let satId = -1;
if ((<HTMLElement>evt.target).classList.contains('search-result')) {
const satIdStr = (<HTMLElement>evt.target).dataset.objId;
satId = satIdStr ? parseInt(satIdStr) : -1;
} else if ((<HTMLElement>evt.target).parentElement?.classList.contains('search-result')) {
const satIdStr = (<HTMLElement>evt.target).parentElement?.dataset.objId;
satId = satIdStr ? parseInt(satIdStr) : -1;
} else if ((<HTMLElement>evt.target).parentElement?.parentElement?.classList.contains('search-result')) {
const satIdStr = (<HTMLElement>evt.target).parentElement?.parentElement?.dataset.objId;
satId = satIdStr ? parseInt(satIdStr) : -1;
}
return satId;
}
/**
* Returns a boolean indicating whether the search results box is currently open.
*/
getLastResultGroup(): ObjectGroup {
return this.lastResultGroup_;
}
/**
* Returns the current search string entered by the user.
*/
getCurrentSearch(): string {
if (this.isResultsOpen) {
const searchDom = <HTMLInputElement>getEl('search', true);
if (searchDom) {
return searchDom.value;
}
}
return '';
}
/**
* Hides the search results box and clears the selected satellite group.
* Also updates the color scheme if necessary.
*/
hideResults(): void {
try {
const catalogManagerInstance = keepTrackApi.getCatalogManager();
const dotsManagerInstance = keepTrackApi.getDotsManager();
const groupManagerInstance = keepTrackApi.getGroupsManager();
const colorSchemeManagerInstance = keepTrackApi.getColorSchemeManager();
slideOutUp(getEl('search-results'), 1000);
groupManagerInstance.clearSelect();
this.isResultsOpen = false;
settingsManager.lastSearch = '';
settingsManager.lastSearchResults = [];
dotsManagerInstance.updateSizeBuffer(catalogManagerInstance.objectCache.length);
if (colorSchemeManagerInstance.currentColorScheme === colorSchemeManagerInstance.group) {
colorSchemeManagerInstance.setColorScheme(colorSchemeManagerInstance.default, true);
} else if (colorSchemeManagerInstance.currentColorScheme === colorSchemeManagerInstance.groupCountries) {
colorSchemeManagerInstance.setColorScheme(colorSchemeManagerInstance.countries, true);
} else {
colorSchemeManagerInstance.setColorScheme(colorSchemeManagerInstance.currentColorScheme, true);
}
} catch (error) {
console.warn(error);
}
}
static doArraySearch(catalogManagerInstance: CatalogManager, array: number[]) {
return array.reduce((searchStr, i) => `${searchStr}${(<DetailedSatellite>catalogManagerInstance.objectCache[i])?.sccNum},`, '').slice(0, -1);
}
doSearch(searchString: string, isPreventDropDown?: boolean): void {
if (searchString == '') {
this.hideResults();
return;
}
const catalogManagerInstance = keepTrackApi.getCatalogManager();
const dotsManagerInstance = keepTrackApi.getDotsManager();
if (catalogManagerInstance.objectCache.length === 0) throw new Error('No sat data loaded! Check if TLEs are corrupted!');
if (searchString.length === 0) {
settingsManager.lastSearch = '';
settingsManager.lastSearchResults = [];
dotsManagerInstance.updateSizeBuffer(catalogManagerInstance.objectCache.length);
(<HTMLInputElement>getEl('search')).value = '';
this.hideResults();
return;
}
const searchDom = <HTMLInputElement>getEl('search');
if (searchDom) {
searchDom.value = searchString;
}
// Don't search for things until at least the minimum characters
// are typed otherwise there are just too many search results.
if (searchString.length <= settingsManager.minimumSearchCharacters && searchString !== 'RV_') {
return;
}
// Uppercase to make this search not case sensitive
searchString = searchString.toUpperCase();
// NOTE: We are no longer using spaces because it is difficult
// to predict when a space is part of satellite name.
// Split string into array using comma or space as delimiter
// let searchList = searchString.split(/(?![0-9]+)\s(?=[0-9]+)|,/u);
let results = <SearchResult[]>[];
// If so, then do a number only search
if (/^[0-9,]+$/u.test(searchString)) {
results = SearchManager.doNumOnlySearch_(searchString);
} else {
// If not, then do a regular search
results = SearchManager.doRegularSearch_(searchString);
}
// Remove any results greater than the maximum allowed
results = results.splice(0, settingsManager.searchLimit);
// Make a group to hilight results
const idList = results.map((sat) => sat.id);
settingsManager.lastSearchResults = idList;
dotsManagerInstance.updateSizeBuffer(catalogManagerInstance.objectCache.length);
const groupManagerInstance = keepTrackApi.getGroupsManager();
const uiManagerInstance = keepTrackApi.getUiManager();
const dispGroup = groupManagerInstance.createGroup(GroupType.ID_LIST, idList);
this.lastResultGroup_ = dispGroup;
groupManagerInstance.selectGroup(dispGroup);
if (!isPreventDropDown) this.fillResultBox(results, catalogManagerInstance);
if (idList.length === 0) {
if (settingsManager.lastSearch?.length > settingsManager.minimumSearchCharacters) {
uiManagerInstance.toast('No Results Found', 'serious', false);
}
this.hideResults();
return;
}
if (keepTrackApi.getPlugin(SatelliteFov)?.isSatOverflyModeOn) {
catalogManagerInstance.satCruncher.postMessage({
typ: CruncerMessageTypes.SATELLITE_SELECTED,
satelliteSelected: idList,
});
}
// Don't let the search overlap with the legend
LegendManager.change('clear');
UrlManager.updateURL();
}
private static doRegularSearch_(searchString: string) {
const results: SearchResult[] = [];
// Split string into array using comma
let searchList = searchString.split(/,/u);
// Update last search with the most recent search results
settingsManager.lastSearch = searchList;
// Initialize search results
const satData = SearchManager.getSearchableObjects_(true) as (DetailedSatellite & MissileObject)[];
searchList.forEach((searchStringIn) => {
satData.every((sat) => {
if (results.length >= settingsManager.searchLimit) return false;
const len = searchStringIn.length;
if (len === 0) return true; // Skip empty strings
if (sat.name.toUpperCase().indexOf(searchStringIn) !== -1 && !sat.name.includes('Vimpel')) {
results.push({
strIndex: sat.name.indexOf(searchStringIn),
searchType: SearchResultType.ON,
patlen: len,
id: sat.id,
});
return true; // Prevent's duplicate results
}
if (typeof sat.bus !== 'undefined' && sat.bus.toUpperCase().indexOf(searchStringIn) !== -1) {
results.push({
strIndex: sat.bus.toUpperCase().indexOf(searchStringIn),
searchType: SearchResultType.BUS,
patlen: len,
id: sat.id,
});
return true; // Prevent's duplicate results
}
if (!sat.desc) {
// Do nothing there is no description property
} else if (sat.desc.toUpperCase().indexOf(searchStringIn) !== -1) {
results.push({
strIndex: sat.desc.toUpperCase().indexOf(searchStringIn),
searchType: SearchResultType.MISSILE,
patlen: len,
id: sat.id,
});
return true; // Prevent's duplicate results
} else {
return true; // Last check for missiles
}
if (sat.sccNum && sat.sccNum.indexOf(searchStringIn) !== -1) {
// Ignore Notional Satellites unless all 6 characters are entered
if (sat.name.includes(' Notional)') && searchStringIn.length < 6) return true;
results.push({
strIndex: sat.sccNum.indexOf(searchStringIn),
searchType: SearchResultType.SCC,
patlen: len,
id: sat.id,
});
return true; // Prevent's duplicate results
}
if (sat.intlDes && sat.intlDes.indexOf(searchStringIn) !== -1) {
// Ignore Notional Satellites
if (sat.name.includes(' Notional)')) return true;
results.push({
strIndex: sat.intlDes.indexOf(searchStringIn),
searchType: SearchResultType.INTLDES,
patlen: len,
id: sat.id,
});
return true; // Prevent's duplicate results
}
if (sat.launchVehicle && sat.launchVehicle.toUpperCase().indexOf(searchStringIn) !== -1) {
results.push({
strIndex: sat.launchVehicle.toUpperCase().indexOf(searchStringIn),
searchType: SearchResultType.LV,
patlen: len,
id: sat.id,
});
return true; // Prevent's duplicate results
}
return true;
});
});
return results;
}
private static doNumOnlySearch_(searchString: string) {
let results: SearchResult[] = [];
// Split string into array using comma
let searchList = searchString.split(/,/u).filter((str) => str.length > 0);
// Sort the numbers so that the lowest numbers are searched first
searchList = searchList.sort((a, b) => parseInt(a) - parseInt(b));
// Update last search with the most recent search results
settingsManager.lastSearch = searchList;
// Initialize search results
const satData = (SearchManager.getSearchableObjects_(false) as DetailedSatellite[]).sort((a, b) => parseInt(a.sccNum6) - parseInt(b.sccNum6));
let i = 0;
let lastFoundI = 0;
searchList.forEach((searchStringIn) => {
// Don't search for things until at least the minimum characters
if (searchStringIn.length <= settingsManager.minimumSearchCharacters) return;
// Last one never got found
if (i >= satData.length) i = lastFoundI;
for (; i < satData.length; i++) {
if (results.length >= settingsManager.searchLimit) break;
const sat = satData[i];
// Ignore Notional Satellites unless all 6 characters are entered
if (sat.type === SpaceObjectType.NOTIONAL && searchStringIn.length < 6) continue;
// Check if matches 6Digit
if (sat.sccNum6 && sat.sccNum6.indexOf(searchStringIn) !== -1) {
results.push({
strIndex: sat.sccNum.indexOf(searchStringIn),
patlen: searchStringIn.length,
id: sat.id,
searchType: SearchResultType.SCC,
});
lastFoundI = i;
if (searchStringIn.length === 6) {
break;
}
}
}
});
// Remove any duplicates in results
results = results.filter((result, index, self) => index === self.findIndex((t) => t.id === result.id));
return results;
}
private static getSearchableObjects_(isIncludeMissiles = true): (DetailedSatellite | MissileObject)[] | DetailedSatellite[] {
const catalogManagerInstance = keepTrackApi.getCatalogManager();
const searchableObjects = (
catalogManagerInstance.objectCache.filter((obj) => {
if (obj.isSensor() || obj.isMarker() || obj.isGroundObject() || obj.isStar()) return false; // Skip static dots (Maybe these should be searchable?)
if (!isIncludeMissiles && obj.isMissile()) return false; // Skip missiles (if not searching for missiles
if (keepTrackApi.getPlugin(SatelliteFov)?.isSatOverflyModeOn && obj.type !== SpaceObjectType.PAYLOAD) return false; // Skip Debris and Rocket Bodies if In Satelltie FOV Mode
if (!(obj as MissileObject).active) return false; // Skip inactive missiles.
if ((obj as DetailedSatellite).country == 'ANALSAT' && !obj.active) return false; // Skip Fake Analyst satellites
if (!obj.name) return false; // Everything has a name. If it doesn't then assume it isn't what we are searching for.
return true;
}) as (DetailedSatellite & MissileObject)[]
).sort((a, b) => {
// Sort by sccNum
if ((a as DetailedSatellite).sccNum && (b as DetailedSatellite).sccNum) {
return parseInt((a as DetailedSatellite).sccNum) - parseInt((b as DetailedSatellite).sccNum);
} else {
return 0;
}
});
return isIncludeMissiles ? (searchableObjects as (DetailedSatellite | MissileObject)[]) : (searchableObjects as DetailedSatellite[]);
}
fillResultBox(results: SearchResult[], catalogManagerInstance: CatalogManager) {
const colorSchemeManagerInstance = keepTrackApi.getColorSchemeManager();
let satData = catalogManagerInstance.objectCache;
getEl('search-results').innerHTML = results.reduce((html, result) => {
const obj = <DetailedSatellite | MissileObject>satData[result.id];
html += '<div class="search-result" data-obj-id="' + obj.id + '">';
html += '<div class="truncate-search">';
// Left half of search results
if (obj.isMissile()) {
html += obj.name;
} else if (result.searchType === SearchResultType.ON) {
// If the name matched - highlight it
html += obj.name.substring(0, result.strIndex);
html += '<span class="search-hilight">';
html += obj.name.substring(result.strIndex, result.strIndex + result.patlen);
html += '</span>';
html += obj.name.substring(result.strIndex + result.patlen);
} else {
// If not, just write the name
html += obj.name;
}
html += '</div>';
html += '<div class="search-result-scc">';
// Right half of search results
switch (result.searchType) {
case SearchResultType.SCC:
{
const sat = obj as DetailedSatellite;
// If the object number matched
result.strIndex = result.strIndex || 0;
result.patlen = result.patlen || 5;
html += sat.sccNum.substring(0, result.strIndex);
html += '<span class="search-hilight">';
html += sat.sccNum.substring(result.strIndex, result.strIndex + result.patlen);
html += '</span>';
html += sat.sccNum.substring(result.strIndex + result.patlen);
}
break;
case SearchResultType.INTLDES:
{
const sat = obj as DetailedSatellite;
// If the international designator matched
result.strIndex = result.strIndex || 0;
result.patlen = result.patlen || 5;
html += sat.intlDes.substring(0, result.strIndex);
html += '<span class="search-hilight">';
html += sat.intlDes.substring(result.strIndex, result.strIndex + result.patlen);
html += '</span>';
html += sat.intlDes.substring(result.strIndex + result.patlen);
}
break;
case SearchResultType.BUS:
{
const sat = obj as DetailedSatellite;
// If the object number matched
result.strIndex = result.strIndex || 0;
result.patlen = result.patlen || 5;
html += sat.bus.substring(0, result.strIndex);
html += '<span class="search-hilight">';
html += sat.bus.substring(result.strIndex, result.strIndex + result.patlen);
html += '</span>';
html += sat.bus.substring(result.strIndex + result.patlen);
}
break;
case SearchResultType.LV:
{
const sat = obj as DetailedSatellite;
result.strIndex = result.strIndex || 0;
result.patlen = result.patlen || 5;
html += sat.launchVehicle.substring(0, result.strIndex);
html += '<span class="search-hilight">';
html += sat.launchVehicle.substring(result.strIndex, result.strIndex + result.patlen);
html += '</span>';
html += sat.launchVehicle.substring(result.strIndex + result.patlen);
}
break;
case SearchResultType.MISSILE:
{
const misl = obj as MissileObject;
html += misl.desc;
}
break;
case SearchResultType.STAR:
html += 'Star';
break;
default:
if (obj.isMissile()) {
const misl = obj as MissileObject;
html += misl.desc;
} else if (obj.isStar()) {
html += 'Star';
} else if (obj.isSatellite()) {
const sat = obj as DetailedSatellite;
html += sat.sccNum;
}
break;
}
html += '</div></div>';
return html;
}, '');
const satInfoboxDom = getEl('sat-infobox');
if (satInfoboxDom) SatInfoBox.resetMenuLocation(satInfoboxDom, false);
slideInDown(getEl('search-results'), 1000);
this.isResultsOpen = true;
if (
colorSchemeManagerInstance.currentColorScheme === colorSchemeManagerInstance.groupCountries ||
colorSchemeManagerInstance.currentColorScheme === colorSchemeManagerInstance.countries
) {
colorSchemeManagerInstance.setColorScheme(colorSchemeManagerInstance.groupCountries, true);
} else {
colorSchemeManagerInstance.setColorScheme(colorSchemeManagerInstance.group, true);
}
}
toggleSearch() {
if (!this.isSearchOpen) {
this.openSearch();
} else {
this.closeSearch();
}
}
closeSearch(isForce = false) {
if (!this.isSearchOpen && !isForce) return;
this.isSearchOpen = false;
getEl('search-holder')?.classList.remove('search-slide-down');
getEl('search-holder')?.classList.add('search-slide-up');
getEl('search-icon')?.classList.remove('search-icon-search-on');
setTimeout(function () {
getEl('fullscreen-icon')?.classList.remove('top-menu-icons-search-on');
getEl('tutorial-icon')?.classList.remove('top-menu-icons-search-on');
getEl('legend-icon')?.classList.remove('top-menu-icons-search-on');
getEl('sound-icon')?.classList.remove('top-menu-icons-search-on');
}, 500);
this.uiManager_.hideSideMenus();
this.hideResults();
}
openSearch(isForce = false) {
if (this.isSearchOpen && !isForce) return;
this.isSearchOpen = true;
getEl('search-holder')?.classList.remove('search-slide-up');
getEl('search-holder')?.classList.add('search-slide-down');
getEl('search-icon')?.classList.add('search-icon-search-on');
getEl('fullscreen-icon')?.classList.add('top-menu-icons-search-on');
getEl('tutorial-icon')?.classList.add('top-menu-icons-search-on');
getEl('legend-icon')?.classList.add('top-menu-icons-search-on');
getEl('sound-icon')?.classList.add('top-menu-icons-search-on');
const searchDom = <HTMLInputElement>getEl('search');
if (searchDom) {
const curSearch = searchDom.value;
if (curSearch.length > settingsManager.minimumSearchCharacters) {
this.doSearch(curSearch);
}
}
}
}