1 |
91fd3fa6
|
Vaclav Honzik
|
// Business logic for tracking tool
|
2 |
|
|
|
3 |
|
|
import { CatalogItemDto, PathDto } from '../../swagger/data-contracts'
|
4 |
|
|
|
5 |
|
|
// For more comprehensive code alias CatalogItemDto[] as path variant
|
6 |
de12c6be
|
Vaclav Honzik
|
export type PathVariant = MapPoint[]
|
7 |
91fd3fa6
|
Vaclav Honzik
|
|
8 |
de12c6be
|
Vaclav Honzik
|
export class MapPoint {
|
9 |
|
|
constructor(
|
10 |
|
|
public idx: number,
|
11 |
|
|
public active: boolean,
|
12 |
|
|
public catalogItem: CatalogItemDto
|
13 |
|
|
) { }
|
14 |
|
|
|
15 |
|
|
/**
|
16 |
|
|
* @returns true if the map point is displayable - i.e. it has a valid lat/lng
|
17 |
|
|
*/
|
18 |
|
|
get displayable() {
|
19 |
|
|
return !!this.catalogItem.latitude && !!this.catalogItem.longitude
|
20 |
|
|
}
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
/**
|
24 |
|
|
* Cartesian product of two arrays
|
25 |
|
|
* @param sets
|
26 |
|
|
* @returns
|
27 |
|
|
*/
|
28 |
91fd3fa6
|
Vaclav Honzik
|
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] =>
|
29 |
|
|
sets.reduce<CatalogItemDto[][]>(
|
30 |
|
|
(results, ids) =>
|
31 |
|
|
results
|
32 |
|
|
.map((result) => ids.map((id) => [...result, id]))
|
33 |
|
|
.reduce((nested, result) => [...nested, ...result]),
|
34 |
|
|
[[]]
|
35 |
|
|
)
|
36 |
|
|
|
37 |
de12c6be
|
Vaclav Honzik
|
/**
|
38 |
|
|
* Builds a list of all possible path variants from pathDto
|
39 |
|
|
* @param pathDto
|
40 |
|
|
* @returns
|
41 |
|
|
*/
|
42 |
8c57f958
|
Vaclav Honzik
|
export const buildPathVariants = (pathDto: PathDto): PathVariant[] => {
|
43 |
91fd3fa6
|
Vaclav Honzik
|
if (!pathDto.foundCatalogItems) {
|
44 |
|
|
return []
|
45 |
|
|
}
|
46 |
|
|
|
47 |
de12c6be
|
Vaclav Honzik
|
return (
|
48 |
|
|
pathDto.foundCatalogItems.length === 1
|
49 |
|
|
? pathDto.foundCatalogItems
|
50 |
|
|
: cartesianProduct(pathDto.foundCatalogItems)
|
51 |
|
|
).map((variant, _) =>
|
52 |
|
|
variant.map(
|
53 |
|
|
(catalogItem, idx) =>
|
54 |
|
|
new MapPoint(
|
55 |
|
|
idx,
|
56 |
|
|
!!catalogItem.latitude && !!catalogItem.longitude,
|
57 |
|
|
catalogItem
|
58 |
|
|
)
|
59 |
|
|
)
|
60 |
|
|
)
|
61 |
91fd3fa6
|
Vaclav Honzik
|
}
|
62 |
|
|
|
63 |
|
|
export default buildPathVariants
|