diff --git a/resources/js/maps/station-map.js b/resources/js/maps/station-map.js index f737393..8c76fe0 100644 --- a/resources/js/maps/station-map.js +++ b/resources/js/maps/station-map.js @@ -10,6 +10,14 @@ L.Icon.Default.mergeOptions({ shadowUrl: markerShadow, }); +function escHtml(str) { + return String(str ?? '') + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); +} + const CLASSIFICATION_COLOURS = { current: '#22c55e', recent: '#64748b', @@ -20,10 +28,9 @@ const CLASSIFICATION_COLOURS = { const UK_CENTRE = [54.0, -2.0]; const UK_ZOOM = 6; -export function stationMap(results, meta) { +export function stationMap(results) { return { results, - meta, _map: null, _markers: [], @@ -47,7 +54,18 @@ export function stationMap(results, meta) { this._markers = []; }, + destroy() { + if (this._map) { + this._map.remove(); + this._map = null; + this._markers = []; + } + }, + _plotMarkers() { + if (!this._map) { + return; + } this._clearMarkers(); if (!this.results || this.results.length === 0) { @@ -65,10 +83,10 @@ export function stationMap(results, meta) { const popup = `
- ${station.name}${supermarketTag}
+ ${escHtml(station.name)}${supermarketTag}
${Number(station.price).toFixed(1)}p
- ${miles} miles away
- ${station.address}, ${station.postcode} + ${escHtml(miles)} miles away
+ ${escHtml(station.address)}, ${escHtml(station.postcode)}
`; @@ -86,7 +104,11 @@ export function stationMap(results, meta) { bounds.push([station.lat, station.lng]); }); - this._map.fitBounds(bounds, { padding: [40, 40], maxZoom: 14 }); + if (bounds.length === 1) { + this._map.setView(bounds[0], 14); + } else { + this._map.fitBounds(bounds, { padding: [40, 40], maxZoom: 14 }); + } }, }; }