radar-sdk-js 4.2.1-beta.1 → 4.2.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,140 +0,0 @@
1
- import maplibregl from 'maplibre-gl';
2
-
3
- import Http from '../http';
4
- import Logger from '../logger';
5
-
6
- import type RadarMap from './RadarMap';
7
-
8
- import type { RadarMarkerImage, RadarMarkerOptions } from '../types';
9
-
10
- const defaultMarkerOptions: maplibregl.MarkerOptions = {
11
- color: '#000257',
12
- };
13
-
14
- const fileExtensionToContentType: Record<string, string> = {
15
- svg: 'image/svg+xml',
16
- png: 'image/png',
17
- };
18
-
19
- const getFileExtension = (file: string): string => {
20
- return file.split('.').pop() || '';
21
- }
22
-
23
- const createImageElement = (options: RadarMarkerImage) => {
24
- const element = document.createElement('img');
25
- element.src = options.url;
26
- if (options.width) {
27
- element.width = options.width;
28
- }
29
- if (options.height) {
30
- element.height = options.height;
31
- }
32
- element.style.maxWidth = '64px';
33
- element.style.maxHeight = '64px';
34
- return element;
35
- }
36
- class RadarMarker extends maplibregl.Marker {
37
- _map!: RadarMap;
38
- _image?: RadarMarkerImage;
39
- _customMarkerId?: string;
40
-
41
- constructor(markerOptions: RadarMarkerOptions) {
42
- const maplibreOptions: maplibregl.MarkerOptions = Object.assign({}, defaultMarkerOptions);
43
-
44
- if (markerOptions.color) {
45
- maplibreOptions.color = markerOptions.color;
46
- }
47
- if (markerOptions.element) {
48
- maplibreOptions.element = markerOptions.element;
49
- }
50
- if (markerOptions.scale) {
51
- maplibreOptions.scale = markerOptions.scale;
52
- }
53
- if (markerOptions.image) {
54
- maplibreOptions.element = createImageElement(markerOptions.image);
55
- }
56
-
57
- super(maplibreOptions);
58
-
59
- if (markerOptions.image) {
60
- this._image = markerOptions.image;
61
- }
62
-
63
- if (markerOptions.customMarkerId) {
64
- const ext = getFileExtension(markerOptions.customMarkerId);
65
- if (ext !== 'svg' && ext !== 'png') {
66
- Logger.error(`Invalid custom marker extension ${ext} - falling back to default marker`);
67
- } else {
68
- this._customMarkerId = markerOptions.customMarkerId;
69
-
70
- const originalElement = this._element.cloneNode(true);
71
- this._element.childNodes.forEach((child) => {
72
- child.remove();
73
- });
74
- const contentType = fileExtensionToContentType[ext];
75
-
76
- Http.request({
77
- method: 'GET',
78
- versioned: false,
79
- path: `maps/PlACEHOLDER/markers/${markerOptions.customMarkerId}`,
80
- headers: {
81
- 'Content-Type': contentType,
82
- },
83
- }).then((res) => {
84
- if (res.data instanceof Blob) {
85
- // png returns a Blob
86
- const customMarkerUrl = URL.createObjectURL(res.data);
87
- this._element.replaceChildren(createImageElement({ url: customMarkerUrl }));
88
- } else {
89
- // svg returns an xml string
90
- this._element.innerHTML = res.data;
91
- }
92
- }).catch((err) => {
93
- Logger.error(`Could not get custom marker: ${err.message} - falling back to default marker`);
94
- this._element.replaceChildren(...originalElement.childNodes);
95
- });
96
- }
97
- }
98
-
99
- // set popup text or HTML
100
- if (markerOptions.text) {
101
- const popup = new maplibregl.Popup({ offset: 35 }).setText(markerOptions.text);
102
- this.setPopup(popup);
103
- } else if (markerOptions.html) {
104
- const popup = new maplibregl.Popup({ offset: 35 }).setHTML(markerOptions.html);
105
- this.setPopup(popup);
106
- }
107
- }
108
-
109
- addTo(map: RadarMap) {
110
- map._markers.push(this);
111
- return super.addTo(map);
112
- }
113
-
114
- remove() {
115
- if (this._map) {
116
- this._map._markers = this._map._markers.filter((marker) => marker !== this);
117
- }
118
- return super.remove();
119
- }
120
-
121
- getOptions(): RadarMarkerOptions {
122
- const markerOptions: RadarMarkerOptions = {
123
- // TODO: element: marker.getElement(),
124
- image: this._image,
125
- color: this._color,
126
- scale: this._scale,
127
- offset: this.getOffset(),
128
- anchor: this._anchor,
129
- draggable: this.isDraggable(),
130
- clickTolerance: this._clickTolerance,
131
- rotation: this.getRotation(),
132
- rotationAlignment: this.getRotationAlignment(),
133
- pitchAlignment: this.getPitchAlignment()
134
- }
135
-
136
- return markerOptions;
137
- }
138
- }
139
-
140
- export default RadarMarker;