radar-sdk-js
Advanced tools
Comparing version 4.1.18 to 4.1.19-beta.0
@@ -7,3 +7,3 @@ export type HttpMethod = 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE'; | ||
declare class Http { | ||
static request({ method, path, data, host, headers, }: { | ||
static request({ method, path, data, host, versioned, headers, }: { | ||
method: HttpMethod; | ||
@@ -13,2 +13,3 @@ path: string; | ||
host?: string; | ||
versioned?: boolean; | ||
headers?: Record<string, string>; | ||
@@ -15,0 +16,0 @@ }): Promise<HttpResponse>; |
/// <reference types="mapbox__point-geometry" /> | ||
import { Marker } from 'maplibre-gl'; | ||
import type { RadarMapOptions, RadarMarkerOptions } from '../types'; | ||
declare class MapUI { | ||
private static customMarkerRawSvg; | ||
private static markers; | ||
static getMapLibre(): { | ||
@@ -17,3 +20,3 @@ supported: import("@mapbox/mapbox-gl-supported").IsSupported; | ||
Popup: typeof import("maplibre-gl").Popup; | ||
Marker: typeof import("maplibre-gl").Marker; | ||
Marker: typeof Marker; | ||
Style: typeof import("maplibre-gl").Style; | ||
@@ -20,0 +23,0 @@ LngLat: typeof import("maplibre-gl").LngLat; |
@@ -1,2 +0,2 @@ | ||
declare const _default: "4.1.18"; | ||
declare const _default: "4.1.19-beta.0"; | ||
export default _default; |
{ | ||
"name": "radar-sdk-js", | ||
"version": "4.1.18", | ||
"version": "4.1.19-beta.0", | ||
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://radar.com", |
@@ -59,3 +59,3 @@ <p align="center"> | ||
```html | ||
<script src="https://js.radar.com/v4.1.18/radar.min.js"></script> | ||
<script src="https://js.radar.com/v4.1.19-beta.0/radar.min.js"></script> | ||
``` | ||
@@ -77,4 +77,4 @@ | ||
<head> | ||
<link href="https://js.radar.com/v4.1.18/radar.css" rel="stylesheet"> | ||
<script src="https://js.radar.com/v4.1.18/radar.min.js"></script> | ||
<link href="https://js.radar.com/v4.1.19-beta.0/radar.css" rel="stylesheet"> | ||
<script src="https://js.radar.com/v4.1.19-beta.0/radar.min.js"></script> | ||
</head> | ||
@@ -103,4 +103,4 @@ | ||
<head> | ||
<link href="https://js.radar.com/v4.1.18/radar.css" rel="stylesheet"> | ||
<script src="https://js.radar.com/v4.1.18/radar.min.js"></script> | ||
<link href="https://js.radar.com/v4.1.19-beta.0/radar.css" rel="stylesheet"> | ||
<script src="https://js.radar.com/v4.1.19-beta.0/radar.min.js"></script> | ||
</head> | ||
@@ -136,4 +136,4 @@ | ||
<head> | ||
<link href="https://js.radar.com/v4.1.18/radar.css" rel="stylesheet"> | ||
<script src="https://js.radar.com/v4.1.18/radar.min.js"></script> | ||
<link href="https://js.radar.com/v4.1.19-beta.0/radar.css" rel="stylesheet"> | ||
<script src="https://js.radar.com/v4.1.19-beta.0/radar.min.js"></script> | ||
</head> | ||
@@ -140,0 +140,0 @@ |
@@ -34,3 +34,4 @@ import SDK_VERSION from './version'; | ||
host, | ||
headers, | ||
versioned = true, | ||
headers = {}, | ||
}: { | ||
@@ -41,2 +42,3 @@ method: HttpMethod; | ||
host?: string; | ||
versioned?: boolean; | ||
headers?: Record<string, string>; | ||
@@ -59,2 +61,6 @@ }) { | ||
if (!versioned) { | ||
url = `${urlHost}/${path}`; | ||
} | ||
// remove undefined values from request data | ||
@@ -84,28 +90,31 @@ let body: any = {}; | ||
// set standard headers | ||
xhr.setRequestHeader('Authorization', publishableKey); | ||
xhr.setRequestHeader('Content-Type', 'application/json'); | ||
xhr.setRequestHeader('X-Radar-Device-Type', 'Web'); | ||
xhr.setRequestHeader('X-Radar-SDK-Version', SDK_VERSION); | ||
const defaultHeaders = { | ||
'Authorization': publishableKey, | ||
'Content-Type': 'application/json', | ||
'X-Radar-Device-Type': 'Web', | ||
'X-Radar-SDK-Version': SDK_VERSION, | ||
}; | ||
// set passed custom headers if present | ||
if (headers) { | ||
Object.keys(headers).forEach(key => { | ||
const val = headers[key]; | ||
xhr.setRequestHeader(key, val); | ||
}); | ||
} | ||
// set config custom headers if present | ||
// set custom config headers if present | ||
let configHeaders: { [key: string]: string } = {}; | ||
if (typeof options.getRequestHeaders === 'function') { | ||
const headers: { [key: string]: string } = options.getRequestHeaders(); | ||
Object.keys(headers || {}).forEach((key) => { | ||
xhr.setRequestHeader(key, headers[key]); | ||
}); | ||
configHeaders = options.getRequestHeaders(); | ||
} | ||
// combines default headers with custom headers and config headers | ||
const allHeaders = Object.assign(defaultHeaders, headers, configHeaders); | ||
// set headers | ||
Object.entries(allHeaders).forEach(([key, val]) => { | ||
xhr.setRequestHeader(key, val); | ||
}); | ||
xhr.onload = () => { | ||
let response: any; | ||
try { | ||
response = JSON.parse(xhr.response); | ||
if (allHeaders['Content-Type'] === 'application/json') { | ||
response = JSON.parse(xhr.response); | ||
} else { | ||
response = { code: xhr.status, data: xhr.response }; | ||
} | ||
} catch (e) { | ||
@@ -112,0 +121,0 @@ return reject(new RadarServerError(response)); |
@@ -1,5 +0,7 @@ | ||
import maplibregl from 'maplibre-gl'; | ||
import maplibregl, { Marker, MarkerOptions } from 'maplibre-gl'; | ||
import Config from '../config'; | ||
import Http from '../http'; | ||
import Logger from '../logger'; | ||
import RadarLogoControl from './RadarLogoControl'; | ||
@@ -31,2 +33,4 @@ import type { RadarOptions, RadarMapOptions, RadarMarkerOptions } from '../types'; | ||
const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/; | ||
const createStyleURL = (options: RadarOptions, style: string = DEFAULT_STYLE) => ( | ||
@@ -36,8 +40,30 @@ `${options.host}/maps/styles/${style}?publishableKey=${options.publishableKey}` | ||
// use formatted style URL if using one of Radar's out-of-the-box styles | ||
const getMarkerOptions = (marker: Marker): MarkerOptions => { | ||
const markerOptions: MarkerOptions = { | ||
// element: marker.getElement(), | ||
color: marker._color, | ||
scale: marker._scale, | ||
offset: marker.getOffset(), | ||
anchor: marker._anchor, | ||
draggable: marker.isDraggable(), | ||
clickTolerance: marker._clickTolerance, | ||
rotation: marker.getRotation(), | ||
rotationAlignment: marker.getRotationAlignment(), | ||
pitchAlignment: marker.getPitchAlignment() | ||
} | ||
return markerOptions; | ||
} | ||
/** Check if style is a Radar style or a custom style */ | ||
const isRadarStyle = (style: string) => { | ||
return RADAR_STYLES.includes(style) || uuidRegex.test(style) | ||
}; | ||
/** Use formatted style URL if using one of Radar's out-of-the-box styles or is a Radar custom style */ | ||
const getStyle = (options: RadarOptions, mapOptions: RadarMapOptions) => { | ||
const style = mapOptions.style; | ||
if (!style || (typeof style === 'string' && RADAR_STYLES.includes(style))) { | ||
return createStyleURL(options, mapOptions.style as string); | ||
if (!style || (typeof style === 'string' && isRadarStyle(style))) { | ||
return createStyleURL(options, style); | ||
} | ||
@@ -49,2 +75,5 @@ | ||
class MapUI { | ||
private static customMarkerRawSvg: string | undefined; | ||
private static markers: Marker[] = []; | ||
public static getMapLibre() { | ||
@@ -75,3 +104,3 @@ return maplibregl; | ||
maplibreOptions.transformRequest = (url, resourceType) => { | ||
if (resourceType === 'Style' && RADAR_STYLES.includes(url)) { | ||
if (resourceType === 'Style' && isRadarStyle(url)) { | ||
const radarStyleURL = createStyleURL(options, url); | ||
@@ -93,17 +122,5 @@ return { url: radarStyleURL }; | ||
// add radar logo | ||
const img = document.createElement('img'); | ||
img.src = RADAR_LOGO_URL; | ||
const radarLogo = new RadarLogoControl(); | ||
map.addControl(radarLogo, 'bottom-left'); | ||
const link = document.createElement('a'); | ||
link.id = 'radar-map-logo'; | ||
link.href = 'https://radar.com?ref=powered_by_radar'; | ||
link.target = '_blank'; | ||
link.style.position = 'absolute'; | ||
link.style.bottom = '0'; | ||
link.style.left = '5px'; | ||
link.style.width = '80px'; | ||
link.style.height = '38px'; | ||
link.appendChild(img) | ||
map.getContainer().appendChild(link); | ||
// add attribution | ||
@@ -129,4 +146,42 @@ const attribution = new maplibregl.AttributionControl({ compact: false }); | ||
}; | ||
const onStyleLoad = async () => { | ||
MapUI.customMarkerRawSvg = undefined; | ||
const style = map.getStyle(); | ||
const customMarkers = (style.metadata as any)?.['radar:customMarkers']; | ||
if (Array.isArray(customMarkers) && customMarkers.length > 0) { | ||
const customMarker = customMarkers[0]; // only support one custom marker for now | ||
try { | ||
const markerRawSvg = await Http.request({ | ||
method: 'GET', | ||
versioned: false, | ||
path: `maps/markers/${customMarker.id}`, | ||
headers: { | ||
'Content-Type': 'image/svg+xml', | ||
}, | ||
}); | ||
MapUI.customMarkerRawSvg = markerRawSvg.data; | ||
} catch (err) { | ||
Logger.warn(`Error getting custom marker: ${customMarker.id} - using default marker.`); | ||
} | ||
} | ||
// set markers if necessary | ||
for (let i = 0; i < MapUI.markers.length; i++) { | ||
if (MapUI.customMarkerRawSvg) { | ||
// set custom marker | ||
MapUI.markers[i]._element.innerHTML = MapUI.customMarkerRawSvg; | ||
} else { | ||
const markerOptions = getMarkerOptions(MapUI.markers[i]); | ||
const newMarker = new Marker(markerOptions); // get default element | ||
// set default element | ||
MapUI.markers[i]._element.innerHTML = newMarker._element.innerHTML; | ||
} | ||
} | ||
} | ||
map.on('resize', onResize); | ||
map.on('load', onResize); | ||
map.on('styledata', onStyleLoad); | ||
@@ -139,2 +194,8 @@ return map; | ||
// has a custom marker | ||
if (MapUI.customMarkerRawSvg) { | ||
maplibreOptions.element = document.createElement('div'); | ||
maplibreOptions.element.innerHTML = MapUI.customMarkerRawSvg; | ||
} | ||
if (markerOptions.color) { | ||
@@ -161,2 +222,3 @@ maplibreOptions.color = markerOptions.color; | ||
MapUI.markers.push(marker); | ||
return marker; | ||
@@ -163,0 +225,0 @@ } |
@@ -1,1 +0,1 @@ | ||
export default '4.1.18'; | ||
export default '4.1.19-beta.0'; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
330833
58
6221
1