carto.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import Map from 'ol/Map';
  2. import TileLayer from 'ol/layer/Tile';
  3. import XYZ from 'ol/source/XYZ';
  4. import { MapLayerRegistryItem, MapLayerOptions, GrafanaTheme2 } from '@grafana/data';
  5. // https://carto.com/help/building-maps/basemap-list/
  6. export enum LayerTheme {
  7. Auto = 'auto',
  8. Light = 'light',
  9. Dark = 'dark',
  10. }
  11. export interface CartoConfig {
  12. theme?: LayerTheme;
  13. showLabels?: boolean;
  14. }
  15. export const defaultCartoConfig: CartoConfig = {
  16. theme: LayerTheme.Auto,
  17. showLabels: true,
  18. };
  19. export const carto: MapLayerRegistryItem<CartoConfig> = {
  20. id: 'carto',
  21. name: 'CARTO reference map',
  22. isBaseMap: true,
  23. defaultOptions: defaultCartoConfig,
  24. /**
  25. * Function that configures transformation and returns a transformer
  26. * @param options
  27. */
  28. create: async (map: Map, options: MapLayerOptions<CartoConfig>, theme: GrafanaTheme2) => ({
  29. init: () => {
  30. const cfg = { ...defaultCartoConfig, ...options.config };
  31. let style = cfg.theme as string;
  32. if (!style || style === LayerTheme.Auto) {
  33. style = theme.isDark ? 'dark' : 'light';
  34. }
  35. if (cfg.showLabels) {
  36. style += '_all';
  37. } else {
  38. style += '_nolabels';
  39. }
  40. return new TileLayer({
  41. source: new XYZ({
  42. attributions: `<a href="https://carto.com/attribution/">© CARTO</a>`,
  43. url: `https://{1-4}.basemaps.cartocdn.com/${style}/{z}/{x}/{y}.png`,
  44. }),
  45. });
  46. },
  47. registerOptionsUI: (builder) => {
  48. builder
  49. .addRadio({
  50. path: 'config.theme',
  51. name: 'Theme',
  52. settings: {
  53. options: [
  54. { value: LayerTheme.Auto, label: 'Auto', description: 'Match grafana theme' },
  55. { value: LayerTheme.Light, label: 'Light' },
  56. { value: LayerTheme.Dark, label: 'Dark' },
  57. ],
  58. },
  59. defaultValue: defaultCartoConfig.theme!,
  60. })
  61. .addBooleanSwitch({
  62. path: 'config.showLabels',
  63. name: 'Show labels',
  64. description: '',
  65. defaultValue: defaultCartoConfig.showLabels,
  66. });
  67. },
  68. }),
  69. };
  70. export const cartoLayers = [carto];