Skip to content

Commit

Permalink
✨ [#2173] Show map background in form design view
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmolen committed Nov 25, 2024
1 parent 7491954 commit 25aa02f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/openforms/js/components/form/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {CRS_RD, TILE_LAYER_RD} from '@open-formulieren/leaflet-tools';
import * as L from 'leaflet';
import {Formio} from 'react-formio';

import jsonScriptToVar from 'utils/json-script';

import {localiseSchema} from './i18n';

const TextFieldComponent = Formio.Components.components.textfield;
Expand Down Expand Up @@ -70,8 +72,24 @@ export default class Map extends TextFieldComponent {

const map = L.map(`map-${this.id}`, MAP_DEFAULTS);

const {url: tileUrl, ...options} = TILE_LAYER_RD;
const tiles = L.tileLayer(tileUrl, options);
const tiles = L.tileLayer(this.getMapBackgroundUrl(this.originalComponent), TILE_LAYER_RD);
map.addLayer(tiles);
}

// Try to get the map background url for the component.
// If it cannot be found, fall back to the TILE_LAYER_RD url.
getMapBackgroundUrl(originalComponent) {
const backgroundIdentifier = originalComponent?.backgroundIdentifier;
const LEAFLET_MAP_BACKGROUNDS = jsonScriptToVar('config-LEAFLET_MAP_BACKGROUNDS', {
default: [],
});
if (LEAFLET_MAP_BACKGROUNDS === [] || !backgroundIdentifier) {
return TILE_LAYER_RD.url;
}

const leafletBackground = LEAFLET_MAP_BACKGROUNDS.find(
background => background.identifier === backgroundIdentifier
);
return leafletBackground?.url || TILE_LAYER_RD.url;
}
}

0 comments on commit 25aa02f

Please sign in to comment.