1
|
import L, { PointExpression } from 'leaflet'
|
2
|
import { CatalogItemDto } from '../../swagger/data-contracts'
|
3
|
import { mdiMapMarker } from '@mdi/js'
|
4
|
|
5
|
// For more comprehensive code alias CatalogItemDto[] as path variant
|
6
|
export type PathVariant = MapPoint[]
|
7
|
|
8
|
export enum MapPointType {
|
9
|
LocalCatalog, // Fetched from local catalog
|
10
|
ExternalCatalog, // Fetched from external catalog
|
11
|
GeoJson, // From GeoJSON file
|
12
|
FromCoordinates, // From coordinates
|
13
|
}
|
14
|
|
15
|
// Represents a point on the map - wrapper for CatalogItemDto to make it easier to work with
|
16
|
export interface MapPoint {
|
17
|
id: string // unique id for react
|
18
|
idx: number,
|
19
|
addToPath: boolean, // whether to add the point to the path
|
20
|
catalogItem: CatalogItemDto,
|
21
|
type: MapPointType
|
22
|
hidden?: boolean // if true the point will not be displayed on the map
|
23
|
}
|
24
|
|
25
|
export const isMapPointDisplayable = (mapPoint: MapPoint): boolean =>
|
26
|
!!mapPoint.catalogItem.latitude && !!mapPoint.catalogItem.longitude && !mapPoint.hidden
|
27
|
|
28
|
/**
|
29
|
* Based on its type - either imported from local catalog, remote catalogs etc. each type has its own color to differentiate them
|
30
|
* @param item item to get color for
|
31
|
* @returns CSS color string
|
32
|
*/
|
33
|
export const getMapPointSemanticColor = (item: MapPoint) => {
|
34
|
switch (item.type) {
|
35
|
case MapPointType.LocalCatalog:
|
36
|
return 'inherit'
|
37
|
case MapPointType.FromCoordinates:
|
38
|
return '#21972D'
|
39
|
case MapPointType.ExternalCatalog:
|
40
|
return '#A72020'
|
41
|
case MapPointType.GeoJson:
|
42
|
return '#967520'
|
43
|
}
|
44
|
}
|
45
|
|
46
|
const createMapMarkerSvg = (color: string) => {
|
47
|
return `data:image/svg+xml;utf8, ${encodeURIComponent(`
|
48
|
<svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="${color}">
|
49
|
<path d="M0 0h24v24H0z" fill="none" /><path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z" />
|
50
|
</svg>`)}`
|
51
|
}
|
52
|
|
53
|
const mapMarkerSvgs = {
|
54
|
[MapPointType.LocalCatalog]: createMapMarkerSvg('#285CAB'),
|
55
|
[MapPointType.ExternalCatalog]: createMapMarkerSvg('#A72020'),
|
56
|
[MapPointType.GeoJson]: createMapMarkerSvg('#967520'),
|
57
|
[MapPointType.FromCoordinates]: createMapMarkerSvg('#21972D'),
|
58
|
}
|
59
|
|
60
|
const iconAnchor = [32, 32] as PointExpression
|
61
|
const iconSize = [42, 42] as PointExpression
|
62
|
|
63
|
const mapMarkers = {
|
64
|
[MapPointType.LocalCatalog]: L.icon({
|
65
|
iconAnchor, iconSize,
|
66
|
iconUrl: mapMarkerSvgs[MapPointType.LocalCatalog],
|
67
|
}),
|
68
|
[MapPointType.ExternalCatalog]: L.icon({
|
69
|
iconAnchor, iconSize,
|
70
|
iconUrl: mapMarkerSvgs[MapPointType.ExternalCatalog],
|
71
|
}),
|
72
|
|
73
|
[MapPointType.GeoJson]: L.icon({
|
74
|
iconAnchor, iconSize,
|
75
|
iconUrl: mapMarkerSvgs[MapPointType.GeoJson],
|
76
|
}),
|
77
|
[MapPointType.FromCoordinates]: L.icon({
|
78
|
iconAnchor, iconSize,
|
79
|
iconUrl: mapMarkerSvgs[MapPointType.FromCoordinates],
|
80
|
}),
|
81
|
}
|
82
|
|
83
|
export const getMapPointIcon = (item: MapPoint): L.Icon => mapMarkers[item.type]
|