registry.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import Map from 'ol/Map';
  2. import { MapLayerRegistryItem, Registry, MapLayerOptions, GrafanaTheme2 } from '@grafana/data';
  3. import { config } from 'app/core/config';
  4. import { basemapLayers } from './basemaps';
  5. import { carto } from './basemaps/carto';
  6. import { dataLayers } from './data';
  7. export const DEFAULT_BASEMAP_CONFIG: MapLayerOptions = {
  8. type: 'default',
  9. name: '', // will get filled in with a non-empty name
  10. config: {},
  11. };
  12. // Default base layer depending on the server setting
  13. export const defaultBaseLayer: MapLayerRegistryItem = {
  14. id: DEFAULT_BASEMAP_CONFIG.type,
  15. name: 'Default base layer',
  16. isBaseMap: true,
  17. create: (map: Map, options: MapLayerOptions, theme: GrafanaTheme2) => {
  18. const serverLayerType = config?.geomapDefaultBaseLayerConfig?.type;
  19. if (serverLayerType) {
  20. const layer = geomapLayerRegistry.getIfExists(serverLayerType);
  21. if (!layer) {
  22. throw new Error('Invalid basemap configuration on server');
  23. }
  24. return layer.create(map, config.geomapDefaultBaseLayerConfig!, theme);
  25. }
  26. // For now use carto as our default basemap
  27. return carto.create(map, options, theme);
  28. },
  29. };
  30. /**
  31. * Registry for layer handlers
  32. */
  33. export const geomapLayerRegistry = new Registry<MapLayerRegistryItem<any>>(() => [
  34. defaultBaseLayer,
  35. ...basemapLayers, // simple basemaps
  36. ...dataLayers, // Layers with update functions
  37. ]);