Revize de12c6be
Přidáno uživatelem Václav Honzík před asi 2 roky(ů)
frontend/src/features/TrackingTool/buildPathVariants.ts | ||
---|---|---|
3 | 3 |
import { CatalogItemDto, PathDto } from '../../swagger/data-contracts' |
4 | 4 |
|
5 | 5 |
// For more comprehensive code alias CatalogItemDto[] as path variant |
6 |
export type PathVariant = CatalogItemDto[]
|
|
6 |
export type PathVariant = MapPoint[]
|
|
7 | 7 |
|
8 |
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 |
*/ |
|
8 | 28 |
const cartesianProduct = (sets: CatalogItemDto[][]): CatalogItemDto[][] => |
9 | 29 |
sets.reduce<CatalogItemDto[][]>( |
10 | 30 |
(results, ids) => |
... | ... | |
14 | 34 |
[[]] |
15 | 35 |
) |
16 | 36 |
|
37 |
/** |
|
38 |
* Builds a list of all possible path variants from pathDto |
|
39 |
* @param pathDto |
|
40 |
* @returns |
|
41 |
*/ |
|
17 | 42 |
export const buildPathVariants = (pathDto: PathDto): PathVariant[] => { |
18 | 43 |
if (!pathDto.foundCatalogItems) { |
19 | 44 |
return [] |
20 | 45 |
} |
21 | 46 |
|
22 |
const catalogItems = pathDto.foundCatalogItems |
|
23 |
if (catalogItems.length === 1) { |
|
24 |
return catalogItems |
|
25 |
} |
|
26 |
|
|
27 |
return cartesianProduct(catalogItems) |
|
47 |
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 |
) |
|
28 | 61 |
} |
29 | 62 |
|
30 | 63 |
export default buildPathVariants |
Také k dispozici: Unified diff
draggable
re #9741