@defra/interactive-map 0.0.53-alpha → 0.0.55-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/assets/templates/add-polygons.njk +1 -1
  2. package/assets/templates/add-symbols.njk +4 -4
  3. package/assets/templates/draw-tools.njk +7 -7
  4. package/assets/templates/marker-panel.njk +5 -5
  5. package/assets/templates/place-marker.njk +8 -8
  6. package/assets/templates/search-control.njk +1 -1
  7. package/assets/templates/select-feature.njk +7 -7
  8. package/assets/templates/style-switcher.njk +4 -4
  9. package/assets/templates/toggle-marker-label.njk +3 -3
  10. package/dist/css/index.css +1 -1
  11. package/dist/esm/im-core.js +1 -1
  12. package/dist/esm/im-shell.js +1 -1
  13. package/dist/esm/index.js +1 -1
  14. package/dist/umd/im-core.js +1 -1
  15. package/dist/umd/im-core.js.LICENSE.txt +0 -30
  16. package/dist/umd/index.js +1 -1
  17. package/dist/umd/index.js.LICENSE.txt +0 -20
  18. package/docs/api/context.md +12 -0
  19. package/docs/api.md +14 -16
  20. package/docs/building-a-plugin.md +2 -2
  21. package/docs/examples/add-marker-with-panel.mdx +46 -44
  22. package/docs/examples/add-polygons.mdx +100 -98
  23. package/docs/examples/add-symbols.mdx +75 -73
  24. package/docs/examples/basic-map.mdx +16 -14
  25. package/docs/examples/button-map.mdx +16 -14
  26. package/docs/examples/draw-tools.mdx +119 -117
  27. package/docs/examples/place-marker.mdx +51 -49
  28. package/docs/examples/search-control.mdx +22 -20
  29. package/docs/examples/select-feature.mdx +107 -105
  30. package/docs/examples/style-switcher.mdx +47 -45
  31. package/docs/examples/toggle-marker-label.mdx +30 -28
  32. package/docs/getting-started.md +26 -24
  33. package/docs/plugins/datasets.md +2 -0
  34. package/docs/plugins/draw.md +10 -10
  35. package/docs/plugins/interact.md +9 -9
  36. package/docs/plugins/map-key.md +41 -0
  37. package/docs/plugins/plugin-descriptor.md +1 -1
  38. package/docs/plugins/search.md +3 -3
  39. package/docusaurus.config.cjs +23 -0
  40. package/package.json +3 -7
  41. package/plugins/beta/draw-es/dist/esm/im-draw-es-plugin.js +1 -1
  42. package/plugins/beta/draw-es/dist/esm/index.js +1 -1
  43. package/plugins/beta/draw-ml/dist/esm/im-draw-ml-plugin.js +1 -1
  44. package/plugins/beta/draw-ml/dist/esm/index.js +1 -1
  45. package/plugins/beta/draw-ml/dist/umd/im-draw-ml-plugin.js +1 -1
  46. package/plugins/beta/draw-ml/dist/umd/index.js +1 -1
  47. package/plugins/beta/draw-ol/dist/esm/DrawMode.js +1 -1
  48. package/plugins/beta/draw-ol/dist/esm/EditMode.js +1 -1
  49. package/plugins/beta/draw-ol/dist/esm/geometryHelpers.js +1 -1
  50. package/plugins/beta/draw-ol/dist/esm/im-draw-ol-plugin.js +1 -1
  51. package/plugins/beta/draw-ol/dist/esm/index.js +1 -1
  52. package/plugins/beta/frame/dist/esm/im-frame-plugin.js +1 -1
  53. package/plugins/beta/frame/dist/esm/index.js +1 -1
  54. package/plugins/beta/frame/dist/umd/im-frame-plugin.js +1 -1
  55. package/plugins/beta/frame/dist/umd/index.js +1 -1
  56. package/plugins/beta/map-styles/dist/esm/im-map-styles-plugin.js +1 -1
  57. package/plugins/beta/map-styles/dist/esm/index.js +1 -1
  58. package/plugins/beta/map-styles/dist/umd/im-map-styles-plugin.js +1 -1
  59. package/plugins/beta/map-styles/dist/umd/index.js +1 -1
  60. package/plugins/beta/scale-bar/dist/esm/im-scale-bar-plugin.js +1 -1
  61. package/plugins/beta/scale-bar/dist/esm/index.js +1 -1
  62. package/plugins/beta/scale-bar/dist/umd/im-scale-bar-plugin.js +1 -1
  63. package/plugins/beta/scale-bar/dist/umd/index.js +1 -1
  64. package/plugins/beta/use-location/dist/esm/im-use-location-plugin.js +1 -1
  65. package/plugins/beta/use-location/dist/esm/index.js +1 -1
  66. package/plugins/beta/use-location/dist/umd/im-use-location-plugin.js +1 -1
  67. package/plugins/beta/use-location/dist/umd/index.js +1 -1
  68. package/plugins/datasets/dist/esm/im-datasets-esri-adapter.js +1 -1
  69. package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
  70. package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
  71. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  72. package/plugins/datasets/dist/esm/index.js +1 -1
  73. package/plugins/datasets/dist/esm/layerAdapter.js +1 -1
  74. package/plugins/datasets/dist/esm/mapboxStyleLayerAdapter.js +1 -1
  75. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  76. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  77. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  78. package/plugins/datasets/dist/umd/index.js +1 -1
  79. package/plugins/datasets/src/adapters/esri/esriLayerAdapter.js +6 -6
  80. package/plugins/datasets/src/adapters/esri/registry/esriDataset.test.js +1 -1
  81. package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +1 -1
  82. package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +3 -3
  83. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +1 -1
  84. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +1 -3
  85. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +0 -12
  86. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +2 -2
  87. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +1 -2
  88. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +3 -3
  89. package/plugins/datasets/src/fetch/createDynamicSource.js +2 -2
  90. package/plugins/datasets/src/initialise/DatasetsInit.jsx +2 -2
  91. package/plugins/datasets/src/initialise/defaults.js +12 -24
  92. package/plugins/datasets/src/initialise/defaults.test.js +25 -57
  93. package/plugins/datasets/src/initialise/initialiseDatasets.js +1 -4
  94. package/plugins/datasets/src/reducers/mappedDatasetsReducer.test.js +1 -1
  95. package/plugins/datasets/src/registry/dataset.js +21 -16
  96. package/plugins/datasets/src/registry/dataset.test.js +101 -11
  97. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  98. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  99. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  100. package/plugins/draw/dist/esm/index.js +1 -1
  101. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  102. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  103. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  104. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  105. package/plugins/draw/dist/umd/index.js +1 -1
  106. package/plugins/draw/src/adapters/maplibre/styles.js +3 -2
  107. package/plugins/draw/src/adapters/maplibre/styles.test.js +3 -3
  108. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  109. package/plugins/interact/dist/esm/index.js +1 -1
  110. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  111. package/plugins/interact/dist/umd/index.js +1 -1
  112. package/plugins/map-key/dist/css/index.css +1 -1
  113. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  114. package/plugins/map-key/dist/esm/index.js +1 -1
  115. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
  116. package/plugins/map-key/dist/umd/index.js +1 -1
  117. package/plugins/map-key/src/components/Key/Key.module.scss +144 -53
  118. package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +34 -3
  119. package/plugins/map-key/src/components/Key/KeyGroupItem.test.jsx +44 -2
  120. package/plugins/map-key/src/components/Key/KeySvg.jsx +5 -5
  121. package/plugins/map-key/src/components/Key/KeySvg.test.jsx +4 -4
  122. package/plugins/map-key/src/components/Key/KeySvgRamp.jsx +35 -6
  123. package/plugins/map-key/src/components/Key/KeySvgRamp.test.jsx +56 -16
  124. package/plugins/map-key/src/components/Key/useRampOrientation.js +61 -0
  125. package/plugins/map-key/src/components/Key/useRampOrientation.test.jsx +155 -0
  126. package/plugins/map-key/src/index.js +34 -0
  127. package/plugins/map-key/src/utils/groupStyles.js +5 -2
  128. package/plugins/menu/dist/esm/im-menu-plugin.js +1 -1
  129. package/plugins/menu/dist/esm/index.js +1 -1
  130. package/plugins/menu/dist/umd/im-menu-plugin.js +1 -1
  131. package/plugins/menu/dist/umd/index.js +1 -1
  132. package/plugins/search/dist/esm/im-search-plugin.js +1 -1
  133. package/plugins/search/dist/esm/index.js +1 -1
  134. package/plugins/search/dist/umd/im-search-plugin.js +1 -1
  135. package/plugins/search/dist/umd/index.js +1 -1
  136. package/plugins/search/src/Search.jsx +0 -1
  137. package/plugins/search/src/components/Form/Form.jsx +3 -3
  138. package/plugins/search/src/components/Suggestions/Suggestions.jsx +2 -0
  139. package/plugins/search/src/components/Suggestions/Suggestions.test.jsx +6 -0
  140. package/plugins/search/src/defaults.js +3 -0
  141. package/plugins/search/src/events/formHandlers.js +3 -9
  142. package/plugins/search/src/events/formHandlers.test.js +6 -9
  143. package/plugins/search/src/events/inputHandlers.js +8 -2
  144. package/plugins/search/src/events/inputHandlers.test.js +20 -0
  145. package/plugins/search/src/events/suggestionHandlers.js +3 -5
  146. package/plugins/search/src/events/suggestionHandlers.test.js +5 -8
  147. package/plugins/search/src/utils/showResult.js +9 -0
  148. package/plugins/search/src/utils/showResult.test.js +16 -0
  149. package/providers/beta/esri/dist/esm/im-esri-provider.js +1 -1
  150. package/providers/beta/esri/dist/esm/index.js +1 -1
  151. package/providers/beta/open-names/dist/esm/im-reverse-geocode.js +1 -1
  152. package/providers/beta/open-names/dist/esm/index.js +1 -1
  153. package/providers/beta/open-names/dist/umd/im-reverse-geocode.js +1 -1
  154. package/providers/beta/open-names/dist/umd/index.js +1 -1
  155. package/providers/beta/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  156. package/providers/beta/openlayers/dist/esm/index.js +1 -1
  157. package/providers/beta/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  158. package/providers/beta/openlayers/dist/umd/index.js +1 -1
  159. package/providers/beta/openlayers/src/index.js +27 -4
  160. package/providers/beta/openlayers/src/index.test.js +47 -0
  161. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  162. package/providers/maplibre/dist/esm/index.js +1 -1
  163. package/providers/maplibre/dist/umd/im-maplibre-framework.js +1 -1
  164. package/providers/maplibre/dist/umd/im-maplibre-framework.js.LICENSE.txt +3 -3
  165. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  166. package/providers/maplibre/dist/umd/im-maplibre-worker.js +2 -0
  167. package/providers/maplibre/dist/umd/im-maplibre-worker.js.LICENSE.txt +4 -0
  168. package/providers/maplibre/dist/umd/index.js +1 -1
  169. package/providers/maplibre/src/index.js +41 -12
  170. package/providers/maplibre/src/index.test.js +43 -12
  171. package/providers/maplibre/src/utils/detectModuleWorker.js +25 -0
  172. package/providers/maplibre/src/utils/detectModuleWorker.test.js +40 -0
  173. package/providers/maplibre/src/utils/spatial.js +22 -12
  174. package/providers/maplibre/src/utils/spatial.test.js +6 -6
  175. package/providers/maplibre/src/utils/workerUrl.js +15 -0
  176. package/providers/maplibre/src/utils/workerUrl.test.js +14 -0
  177. package/rollup.esm.mjs +38 -1
  178. package/src/App/components/Hints/Hints.module.scss +1 -1
  179. package/src/App/components/Viewport/Viewport.jsx +6 -4
  180. package/src/App/components/Viewport/Viewport.test.jsx +18 -1
  181. package/src/App/hooks/useMapAnnouncements.js +7 -2
  182. package/src/App/hooks/useMapAnnouncements.test.js +13 -2
  183. package/src/App/hooks/useModalPanelBehaviour.js +3 -1
  184. package/src/App/initialiseApp.js +1 -1
  185. package/src/App/store/ServiceProvider.jsx +7 -1
  186. package/src/App/store/ServiceProvider.test.jsx +15 -0
  187. package/src/InteractiveMap/InteractiveMap.js +5 -4
  188. package/src/InteractiveMap/InteractiveMap.test.js +9 -2
  189. package/src/InteractiveMap/domStateManager.js +21 -1
  190. package/src/InteractiveMap/domStateManager.test.js +48 -1
  191. package/src/InteractiveMap/polyfills.js +4 -3
  192. package/src/InteractiveMap/polyfills.test.js +12 -0
  193. package/src/services/mapFocus.js +64 -0
  194. package/src/services/mapFocus.test.js +76 -0
  195. package/src/types.js +5 -7
  196. package/src/utils/detectInterfaceType.js +16 -5
  197. package/src/utils/detectInterfaceType.test.js +32 -0
  198. package/webpack.dev.mjs +2 -0
  199. package/webpack.umd.mjs +28 -7
@@ -1,5 +1,10 @@
1
1
  import { render, screen } from '@testing-library/react'
2
2
  import { KeyGroupItem } from './KeyGroupItem'
3
+ import { useRampOrientation } from './useRampOrientation.js'
4
+
5
+ jest.mock('./useRampOrientation.js', () => ({
6
+ useRampOrientation: jest.fn(() => 'horizontal')
7
+ }))
3
8
 
4
9
  jest.mock('./KeyItem.jsx', () => ({
5
10
  KeyItem: ({ keyDefinition }) => <div data-testid='key-item'>{keyDefinition.id}</div>
@@ -52,8 +57,45 @@ describe('KeyGroupItem', () => {
52
57
  })
53
58
 
54
59
  it('appends the groupStyle modifier class to the dl when groupStyle is provided', () => {
55
- const { container } = render(<KeyGroupItem {...baseProps} groupStyle='horizontal-ramp' />)
56
- expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list-horizontal-ramp')
60
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='other' />)
61
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--other')
62
+ })
63
+
64
+ it('adds the measured orientation to a ramp', () => {
65
+ const keyDefinitions = [{ id: 'a', label: 'Low', style: {} }, { id: 'b', label: 'High', style: {} }]
66
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
67
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-horizontal')
68
+ expect(useRampOrientation).toHaveBeenLastCalledWith(expect.anything(), ['Low', 'High'], true, false)
69
+ })
70
+
71
+ it('renders a vertical ramp when the labels do not fit', () => {
72
+ useRampOrientation.mockReturnValueOnce('vertical')
73
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' />)
74
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-vertical')
75
+ })
76
+
77
+ it('shows a ramp with any symbol entries as a standard group', () => {
78
+ const keyDefinitions = [{ id: 'a', label: 'Low', style: { fill: '#000' } }, { id: 'b', label: 'High', style: { symbol: 'pin' } }]
79
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
80
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list')
81
+ expect(useRampOrientation).toHaveBeenLastCalledWith(expect.anything(), ['Low', 'High'], false, false)
82
+ })
83
+
84
+ it('does not measure groups that are not ramps', () => {
85
+ render(<KeyGroupItem {...baseProps} />)
86
+ expect(useRampOrientation).toHaveBeenLastCalledWith(expect.anything(), [], false, false)
87
+ })
88
+
89
+ it('adds the has-stroke modifier to a ramp when any item has a stroke', () => {
90
+ const keyDefinitions = [{ id: 'a', style: { fill: '#000' } }, { id: 'b', style: { stroke: { default: '#123' } } }]
91
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
92
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-horizontal im-c-map-key-list--has-stroke')
93
+ })
94
+
95
+ it('omits the has-stroke modifier when no ramp item has a stroke', () => {
96
+ const keyDefinitions = [{ id: 'a', style: { fill: '#000' } }, { id: 'b', style: { fillPattern: 'dot' } }]
97
+ const { container } = render(<KeyGroupItem {...baseProps} groupStyle='ramp' keyDefinitions={keyDefinitions} />)
98
+ expect(container.querySelector('dl').className).toBe('im-c-map-key-list im-c-map-key-list--ramp im-c-map-key-list--ramp-horizontal')
57
99
  })
58
100
 
59
101
  it('uses the base dl class when groupStyle is not provided', () => {
@@ -3,7 +3,7 @@ import { KeySvgSymbol } from './KeySvgSymbol.jsx'
3
3
  import { KeySvgLine } from './KeySvgLine.jsx'
4
4
  import { KeySvgRect } from './KeySvgRect.jsx'
5
5
  import { KeySvgRamp } from './KeySvgRamp.jsx'
6
- import { HORIZONTAL_RAMP } from '../../utils/groupStyles.js'
6
+ import { RAMP } from '../../utils/groupStyles.js'
7
7
 
8
8
  // Pure derivation of keyDefinition — computed directly during render (see KeyItem.jsx for why:
9
9
  // staging this through useState/useEffect meant every fresh mount painted a blank symbol first).
@@ -13,8 +13,8 @@ const getSymbolShape = (keyDefinition, groupStyle, mapStyle, symbolRegistry) =>
13
13
  return { symbolShape: null, keySymbol: null }
14
14
  }
15
15
  const { hasSymbol, hasPattern, style } = keyDefinition
16
- if (groupStyle === HORIZONTAL_RAMP) {
17
- return { symbolShape: HORIZONTAL_RAMP, keySymbol: null }
16
+ if (groupStyle === RAMP) {
17
+ return { symbolShape: RAMP, keySymbol: null }
18
18
  }
19
19
  if (hasSymbol) {
20
20
  const keySymbol = symbolRegistry.getKeySymbol(style, mapStyle)
@@ -42,8 +42,8 @@ export const KeySvg = ({ keyDefinition, groupStyle, mapStyle, symbolRegistry, pa
42
42
  return <KeySvgPattern mapStyle={mapStyle} keyDefinition={keyDefinition} patternRegistry={patternRegistry} />
43
43
  } else if (symbolShape === 'line') {
44
44
  return <KeySvgLine mapStyle={mapStyle} keyDefinition={keyDefinition} />
45
- } else if (symbolShape === HORIZONTAL_RAMP) {
46
- return <KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />
45
+ } else if (symbolShape === RAMP) {
46
+ return <KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} patternRegistry={patternRegistry} />
47
47
  } else {
48
48
  return <KeySvgRect mapStyle={mapStyle} keyDefinition={keyDefinition} />
49
49
  }
@@ -23,7 +23,7 @@ jest.mock('./KeySvgRect.jsx', () => ({
23
23
  }))
24
24
 
25
25
  jest.mock('./KeySvgRamp.jsx', () => ({
26
- KeySvgRamp: () => <svg data-testid='key-svg-horizontal-ramp' />
26
+ KeySvgRamp: () => <svg data-testid='key-svg-ramp' />
27
27
  }))
28
28
 
29
29
  const baseKeyDefinition = {
@@ -98,8 +98,8 @@ describe('KeySvg', () => {
98
98
  expect(container.firstChild).toBeNull()
99
99
  })
100
100
 
101
- it('renders KeySvgRamp when groupStyle is horizontal-ramp', () => {
102
- const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition }} groupStyle='horizontal-ramp' />)
103
- expect(getByTestId('key-svg-horizontal-ramp')).toBeTruthy()
101
+ it('renders KeySvgRamp when groupStyle is ramp', () => {
102
+ const { getByTestId } = render(<KeySvg {...baseProps} keyDefinition={{ ...baseKeyDefinition }} groupStyle='ramp' />)
103
+ expect(getByTestId('key-svg-ramp')).toBeTruthy()
104
104
  })
105
105
  })
@@ -1,13 +1,42 @@
1
+ import { useId } from 'react'
1
2
  import { getValueForStyle } from '../../../../../src/utils/getValueForStyle.js'
2
3
 
3
- export const KeySvgRamp = ({ mapStyle, keyDefinition }) => {
4
- const { style } = keyDefinition
5
- const fill = getValueForStyle(style.fill, mapStyle.id)
6
- const stroke = getValueForStyle(style.fill, mapStyle.id)
4
+ // Matches the 2px border and 16×16 tile inset of the standard pattern key symbol.
5
+ const BORDER_WIDTH = 2
6
+ const PATTERN_TILE_SIZE = 16
7
+
8
+ // No viewBox: the band stretches to its column's width, and a viewBox would scale the
9
+ // pattern tile and stroke with it. Shapes and userSpaceOnUse tiles resolve in real pixels.
10
+ export const KeySvgRamp = ({ mapStyle, keyDefinition, patternRegistry }) => {
11
+ const { style, hasPattern } = keyDefinition
12
+ const patternId = 'im-c-map-key-ramp-' + useId().replace(/[^\w-]/g, '')
13
+ const paths = hasPattern ? patternRegistry.getKeyPatternPaths(style, mapStyle.id) : null
14
+ const stroke = getValueForStyle(style.stroke, mapStyle.id)
15
+ const fill = paths ? `url(#${patternId})` : getValueForStyle(style.fill, mapStyle.id)
7
16
 
8
17
  return (
9
- <svg viewBox='0 0 5 5' preserveAspectRatio='none' fill={fill} stroke={stroke} strokeWidth='1'>
10
- <path d='M0 0h5v5H0z' />
18
+ <svg xmlns='http://www.w3.org/2000/svg' className='im-c-map-key-symbol' aria-hidden='true' focusable='false'>
19
+ {paths && (
20
+ <defs>
21
+ <pattern
22
+ id={patternId}
23
+ patternUnits='userSpaceOnUse'
24
+ x={BORDER_WIDTH}
25
+ y={BORDER_WIDTH}
26
+ width={PATTERN_TILE_SIZE}
27
+ height={PATTERN_TILE_SIZE}
28
+ dangerouslySetInnerHTML={{ __html: paths.content }}
29
+ />
30
+ </defs>
31
+ )}
32
+ {/* The stroke is drawn at double width on a flush rect; the svg clips the outer half */}
33
+ <rect
34
+ width='100%'
35
+ height='100%'
36
+ fill={fill || 'none'}
37
+ stroke={stroke}
38
+ strokeWidth={stroke ? BORDER_WIDTH * 2 : undefined}
39
+ />
11
40
  </svg>
12
41
  )
13
42
  }
@@ -7,37 +7,77 @@ jest.mock('../../../../../src/utils/getValueForStyle.js', () => ({
7
7
  }))
8
8
 
9
9
  const mapStyle = { id: 'outdoor' }
10
- const keyDefinition = { style: { fill: { outdoor: '#3d9', dark: '#1a5' } } }
10
+ const fillKeyDefinition = { style: { fill: { outdoor: '#3d9', dark: '#1a5' }, stroke: '#123' } }
11
+ const patternKeyDefinition = {
12
+ hasPattern: true,
13
+ style: { fillPattern: 'dot', fillPatternForegroundColor: '#2E7D32' }
14
+ }
15
+ const patternRegistry = {
16
+ getKeyPatternPaths: jest.fn(() => ({ border: '<path/>', content: '<path d="M0 0h1v1H0z" fill="#2E7D32"/>' }))
17
+ }
11
18
 
12
19
  beforeEach(() => {
20
+ jest.clearAllMocks()
13
21
  getValueForStyle.mockImplementation((value) =>
14
22
  typeof value === 'object' ? value.outdoor : value
15
23
  )
16
24
  })
17
25
 
26
+ const renderRamp = (keyDefinition) =>
27
+ render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} patternRegistry={patternRegistry} />)
28
+
18
29
  describe('KeySvgRamp', () => {
19
- it('renders an svg element', () => {
20
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
21
- expect(container.querySelector('svg')).toBeTruthy()
30
+ it('renders an svg without a viewBox, so the band stretches without distortion', () => {
31
+ const { container } = renderRamp(fillKeyDefinition)
32
+ const svg = container.querySelector('svg')
33
+ expect(svg.getAttribute('viewBox')).toBeNull()
34
+ expect(svg.getAttribute('class')).toBe('im-c-map-key-symbol')
35
+ })
36
+
37
+ it('fills the rect with the resolved fill and strokes it with the resolved stroke', () => {
38
+ const { container } = renderRamp(fillKeyDefinition)
39
+ const rect = container.querySelector('rect')
40
+ expect(rect.getAttribute('fill')).toBe('#3d9')
41
+ expect(rect.getAttribute('stroke')).toBe('#123')
42
+ expect(rect.getAttribute('stroke-width')).toBe('4')
43
+ })
44
+
45
+ it('omits the stroke when none is resolved', () => {
46
+ const { container } = renderRamp({ style: { fill: '#3d9' } })
47
+ const rect = container.querySelector('rect')
48
+ expect(rect.getAttribute('stroke')).toBeNull()
49
+ expect(rect.getAttribute('stroke-width')).toBeNull()
22
50
  })
23
51
 
24
- it('passes the resolved fill to the svg', () => {
25
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
26
- expect(container.querySelector('svg').getAttribute('fill')).toBe('#3d9')
52
+ it('fills the rect with a userSpaceOnUse pattern tile when the item has a pattern', () => {
53
+ const { container } = renderRamp(patternKeyDefinition)
54
+ const pattern = container.querySelector('pattern')
55
+ const rect = container.querySelector('rect')
56
+ expect(patternRegistry.getKeyPatternPaths).toHaveBeenCalledWith(patternKeyDefinition.style, 'outdoor')
57
+ expect(pattern.getAttribute('patternUnits')).toBe('userSpaceOnUse')
58
+ expect(pattern.getAttribute('width')).toBe('16')
59
+ expect(pattern.querySelector('path')).toBeTruthy()
60
+ expect(rect.getAttribute('fill')).toBe(`url(#${pattern.id})`)
27
61
  })
28
62
 
29
- it('passes the resolved stroke to the svg', () => {
30
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
31
- expect(container.querySelector('svg').getAttribute('stroke')).toBe('#3d9')
63
+ it('draws no stroke on a pattern without a stroke', () => {
64
+ const { container } = renderRamp(patternKeyDefinition)
65
+ expect(container.querySelector('rect').getAttribute('stroke')).toBeNull()
32
66
  })
33
67
 
34
- it('uses preserveAspectRatio none', () => {
35
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
36
- expect(container.querySelector('svg').getAttribute('preserveAspectRatio')).toBe('none')
68
+ it('gives each band its own pattern id', () => {
69
+ const { container } = render(
70
+ <>
71
+ <KeySvgRamp mapStyle={mapStyle} keyDefinition={patternKeyDefinition} patternRegistry={patternRegistry} />
72
+ <KeySvgRamp mapStyle={mapStyle} keyDefinition={patternKeyDefinition} patternRegistry={patternRegistry} />
73
+ </>
74
+ )
75
+ const [a, b] = container.querySelectorAll('pattern')
76
+ expect(a.id).not.toBe(b.id)
37
77
  })
38
78
 
39
- it('renders a path child', () => {
40
- const { container } = render(<KeySvgRamp mapStyle={mapStyle} keyDefinition={keyDefinition} />)
41
- expect(container.querySelector('path')).toBeTruthy()
79
+ it('uses no fill when neither a fill nor a pattern is set', () => {
80
+ const { container } = renderRamp({ style: { stroke: '#123' } })
81
+ expect(container.querySelector('rect').getAttribute('fill')).toBe('none')
42
82
  })
43
83
  })
@@ -0,0 +1,61 @@
1
+ import { useLayoutEffect, useState } from 'react'
2
+
3
+ export const HORIZONTAL = 'horizontal'
4
+ export const VERTICAL = 'vertical'
5
+
6
+ let measureContext
7
+ const getMeasureContext = () => {
8
+ if (measureContext === undefined) {
9
+ measureContext = document.createElement('canvas').getContext('2d') ?? null
10
+ }
11
+ return measureContext
12
+ }
13
+
14
+ // Whether every label fits on one line in an equal share of the list's width. Both ramp
15
+ // orientations share the list's side padding and gap, so the result doesn't depend on which
16
+ // orientation is currently rendered. Returns null when there's nothing to measure yet.
17
+ const labelsFitHorizontally = (list, labels) => {
18
+ const label = list.querySelector('.im-c-map-key-list__item-label')
19
+ const context = getMeasureContext()
20
+ if (!label || !context || !list.clientWidth) {
21
+ return null
22
+ }
23
+ const { fontStyle, fontWeight, fontSize, fontFamily } = getComputedStyle(label)
24
+ context.font = `${fontStyle} ${fontWeight} ${fontSize} ${fontFamily}`
25
+ const listStyle = getComputedStyle(list)
26
+ const gap = Number.parseFloat(listStyle.columnGap) || 0
27
+ const padding = (Number.parseFloat(listStyle.paddingLeft) || 0) + (Number.parseFloat(listStyle.paddingRight) || 0)
28
+ const columnWidth = (list.clientWidth - padding - gap * (labels.length - 1)) / labels.length
29
+ return labels.every(text => context.measureText(text).width <= columnWidth)
30
+ }
31
+
32
+ // A ramp is horizontal when every label fits on one line under its band, otherwise vertical.
33
+ // Measured before paint, and again when the list resizes or web fonts finish loading.
34
+ export const useRampOrientation = (listRef, labels, isRamp, hasStroke) => {
35
+ const [fits, setFits] = useState(true)
36
+ const labelsKey = labels.join('\n')
37
+
38
+ useLayoutEffect(() => {
39
+ const list = listRef.current
40
+ if (!isRamp || !list) {
41
+ return undefined
42
+ }
43
+ let active = true
44
+ const update = () => {
45
+ const result = active ? labelsFitHorizontally(list, labels) : null
46
+ if (result !== null) {
47
+ setFits(result)
48
+ }
49
+ }
50
+ update()
51
+ const observer = typeof ResizeObserver === 'function' ? new ResizeObserver(update) : null
52
+ observer?.observe(list)
53
+ document.fonts?.ready.then(update, () => {})
54
+ return () => {
55
+ active = false
56
+ observer?.disconnect()
57
+ }
58
+ }, [isRamp, hasStroke, labelsKey])
59
+
60
+ return fits ? HORIZONTAL : VERTICAL
61
+ }
@@ -0,0 +1,155 @@
1
+ import { useRef } from 'react'
2
+ import { render, act } from '@testing-library/react'
3
+ import { useRampOrientation } from './useRampOrientation.js'
4
+
5
+ // Each character measures 10px wide
6
+ const measureText = jest.fn(text => ({ width: text.length * 10 }))
7
+ jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue({ font: '', measureText })
8
+
9
+ let resizeCallback
10
+ let observe
11
+ let disconnect
12
+
13
+ const TestRamp = ({ labels, isRamp = true, width, padding = 0 }) => {
14
+ const listRef = useRef(null)
15
+ const setList = (list) => {
16
+ if (list) {
17
+ Object.defineProperty(list, 'clientWidth', { configurable: true, get: () => width })
18
+ }
19
+ listRef.current = list
20
+ }
21
+ const orientation = useRampOrientation(listRef, labels, isRamp, false)
22
+ return (
23
+ <dl ref={setList} data-orientation={orientation} style={{ paddingLeft: `${padding}px`, paddingRight: `${padding}px` }}>
24
+ {labels.map(label => <dd key={label} className='im-c-map-key-list__item-label'>{label}</dd>)}
25
+ </dl>
26
+ )
27
+ }
28
+
29
+ const getOrientation = (container) => container.querySelector('dl').dataset.orientation
30
+
31
+ beforeEach(() => {
32
+ jest.clearAllMocks()
33
+ observe = jest.fn()
34
+ disconnect = jest.fn()
35
+ globalThis.ResizeObserver = jest.fn(callback => {
36
+ resizeCallback = callback
37
+ return { observe, disconnect }
38
+ })
39
+ })
40
+
41
+ afterEach(() => {
42
+ delete globalThis.ResizeObserver
43
+ })
44
+
45
+ describe('useRampOrientation', () => {
46
+ it('is horizontal when every label fits in an equal column', () => {
47
+ // 3 columns of 100px; longest label is 90px
48
+ const { container } = render(<TestRamp labels={['Low', 'Medium', 'Very high']} width={300} />)
49
+ expect(getOrientation(container)).toBe('horizontal')
50
+ })
51
+
52
+ it('is vertical when any label is wider than its column', () => {
53
+ // 3 columns of 50px; 'Medium' is 60px
54
+ const { container } = render(<TestRamp labels={['Low', 'Medium', 'High']} width={150} />)
55
+ expect(getOrientation(container)).toBe('vertical')
56
+ })
57
+
58
+ it('subtracts the list padding from the available width', () => {
59
+ // 2 columns of 30px after 20px padding either side; 'High' is 40px
60
+ const { container } = render(<TestRamp labels={['Low', 'High']} width={100} padding={20} />)
61
+ expect(getOrientation(container)).toBe('vertical')
62
+ })
63
+
64
+ it('re-measures when the list resizes', () => {
65
+ const labels = ['Low', 'Medium', 'High']
66
+ const { container, rerender } = render(<TestRamp labels={labels} width={150} />)
67
+ expect(getOrientation(container)).toBe('vertical')
68
+ rerender(<TestRamp labels={labels} width={300} />)
69
+ act(() => resizeCallback())
70
+ expect(getOrientation(container)).toBe('horizontal')
71
+ })
72
+
73
+ it('keeps its current orientation while the list has no width', () => {
74
+ const { container } = render(<TestRamp labels={['Low', 'Medium', 'High']} width={0} />)
75
+ expect(getOrientation(container)).toBe('horizontal')
76
+ })
77
+
78
+ it('does not measure or observe when the group is not a ramp', () => {
79
+ const { container } = render(<TestRamp labels={['Low', 'Medium', 'High']} width={150} isRamp={false} />)
80
+ expect(getOrientation(container)).toBe('horizontal')
81
+ expect(measureText).not.toHaveBeenCalled()
82
+ expect(globalThis.ResizeObserver).not.toHaveBeenCalled()
83
+ })
84
+
85
+ it('still measures when ResizeObserver is unavailable', () => {
86
+ delete globalThis.ResizeObserver
87
+ const { container } = render(<TestRamp labels={['Low', 'Medium', 'High']} width={150} />)
88
+ expect(getOrientation(container)).toBe('vertical')
89
+ })
90
+
91
+ describe('when web fonts load', () => {
92
+ let resolveFonts
93
+ let rejectFonts
94
+
95
+ beforeEach(() => {
96
+ Object.defineProperty(document, 'fonts', {
97
+ configurable: true,
98
+ value: { ready: new Promise((resolve, reject) => { resolveFonts = resolve; rejectFonts = reject }) }
99
+ })
100
+ })
101
+
102
+ afterEach(() => {
103
+ delete document.fonts
104
+ })
105
+
106
+ it('re-measures', async () => {
107
+ const labels = ['Low', 'Medium', 'High']
108
+ const { container, rerender } = render(<TestRamp labels={labels} width={150} />)
109
+ expect(getOrientation(container)).toBe('vertical')
110
+ rerender(<TestRamp labels={labels} width={300} />)
111
+ await act(async () => resolveFonts())
112
+ expect(getOrientation(container)).toBe('horizontal')
113
+ })
114
+
115
+ it('ignores a font loading failure', async () => {
116
+ const { container } = render(<TestRamp labels={['Low', 'Medium', 'High']} width={150} />)
117
+ measureText.mockClear()
118
+ await act(async () => rejectFonts(new Error('font failed')))
119
+ expect(measureText).not.toHaveBeenCalled()
120
+ expect(getOrientation(container)).toBe('vertical')
121
+ })
122
+
123
+ it('does not measure after unmount', async () => {
124
+ const { unmount } = render(<TestRamp labels={['Low', 'Medium', 'High']} width={150} />)
125
+ unmount()
126
+ measureText.mockClear()
127
+ await act(async () => resolveFonts())
128
+ expect(measureText).not.toHaveBeenCalled()
129
+ })
130
+ })
131
+
132
+ it('stays horizontal when a canvas context is unavailable', () => {
133
+ // A fresh copy of the module, so its cached measuring context is unset. The pure entry point
134
+ // doesn't register cleanup hooks, which can't be added from inside a test.
135
+ jest.isolateModules(() => {
136
+ const { renderHook } = require('@testing-library/react/pure')
137
+ const { useRampOrientation: isolatedUseRampOrientation } = require('./useRampOrientation.js')
138
+ HTMLCanvasElement.prototype.getContext.mockReturnValueOnce(null)
139
+ const list = document.createElement('dl')
140
+ list.innerHTML = '<dd class="im-c-map-key-list__item-label">Medium</dd>'
141
+ Object.defineProperty(list, 'clientWidth', { get: () => 10 })
142
+ const { result, unmount } = renderHook(() => isolatedUseRampOrientation({ current: list }, ['Medium'], true, false))
143
+ expect(result.current).toBe('horizontal')
144
+ expect(measureText).not.toHaveBeenCalled()
145
+ unmount()
146
+ })
147
+ })
148
+
149
+ it('stops observing on unmount', () => {
150
+ const { unmount } = render(<TestRamp labels={['Low']} width={150} />)
151
+ expect(observe).toHaveBeenCalled()
152
+ unmount()
153
+ expect(disconnect).toHaveBeenCalled()
154
+ })
155
+ })
@@ -1,5 +1,39 @@
1
1
  import './mapKey.scss'
2
2
 
3
+ /**
4
+ * Display options for a group of key entries.
5
+ *
6
+ * @typedef {Object} MapKeyGroupConfig
7
+ *
8
+ * @property {string} [groupLabel]
9
+ * Heading shown above the group. Replaces the heading the group's entries provide.
10
+ *
11
+ * @property {'ramp'} [groupStyle]
12
+ * How the group's entries are laid out. `'ramp'` shows them as touching bands, each filled with the
13
+ * entry's fill or fill pattern and outlined in its stroke colour: in a row with each label centred
14
+ * below its band when every label fits on one line, otherwise stacked with each label beside its
15
+ * band. When any entry has a stroke, the bands are separated by a small gap. A group with any
16
+ * symbol entries is shown without a groupStyle.
17
+ */
18
+
19
+ /**
20
+ * Options for the map key plugin.
21
+ *
22
+ * @typedef {Object} MapKeyPluginOptions
23
+ *
24
+ * @property {string} [noKeyItemText='No features displayed']
25
+ * Text shown in the key panel when it has no visible entries to display.
26
+ *
27
+ * @property {Object<string, MapKeyGroupConfig>} [groups]
28
+ * Display options for key groups, keyed by group id.
29
+ */
30
+
31
+ /**
32
+ * Creates the map key plugin.
33
+ *
34
+ * @param {MapKeyPluginOptions} [options]
35
+ * @returns {import('../../../src/types.js').PluginDescriptor}
36
+ */
3
37
  export default function createPlugin (options = {}) {
4
38
  return {
5
39
  noKeyItemText: 'No features displayed',
@@ -1,7 +1,10 @@
1
- export const HORIZONTAL_RAMP = 'horizontal-ramp'
1
+ /**
2
+ * `groupStyle` value that lays a group's entries out as a ramp of touching bands.
3
+ */
4
+ export const RAMP = 'ramp'
2
5
 
3
6
  const groupStyles = {
4
- HORIZONTAL_RAMP
7
+ RAMP
5
8
  }
6
9
 
7
10
  export default groupStyles
@@ -1 +1 @@
1
- import{createContext as e,useContext as t,useState as a,useRef as r,useEffect as n}from"preact/compat";import{jsx as i,jsxs as o,Fragment as u}from"preact/jsx-runtime";import l from"@babel/runtime/helpers/defineProperty";var c={},m=e=>{var t,a=(null===(t=c)||void 0===t||null===(t=t.current)||void 0===t?void 0:t.menuState)||{};for(var[r,n]of Object.entries(e)){var i=a[r];if(!n.includes(i))return!1}return!0},s=e=>{if(null==e)return!0;if("boolean"==typeof e)return e;if("object"==typeof e){for(var[t,a]of Object.entries(e))if("menu"===t&&!m(a))return!1;return!0}return!0},d=e("map-menu"),p=e=>"".concat(t(d),"-").concat(e),h=e=>{var{menuGroupItem:t,name:a,checked:r,onChange:n}=e,u=p("radio-".concat(t.id)),{visibleWhen:l}=t;return!l||s(l)?i("div",{className:'im-c-menu__item govuk-radios govuk-radios--small"',"data-module":"govuk-radios",children:o("div",{className:"govuk-radios__item",children:[i("input",{id:u,className:"govuk-radios__input",type:"radio",name:a,value:t.id,checked:r,onChange:n}),i("label",{className:"im-c-menu__item-label govuk-label govuk-radios__label",htmlFor:u,children:t.label})]})}):null},v=e=>{var{menuGroup:t,children:a}=e,r=t.groupLabel||t.label;if(!r)return i(u,{children:a});return i("div",{className:"govuk-form-group im-c-menu-group",children:o("fieldset",{className:"im-c-menu-group__fieldset",children:[i("legend",{className:"im-c-menu-group__legend",children:o("h3",{children:[" ",r," "]})}),a]})},t.id)},b=e=>{var{menuState:t,dispatch:a,menuGroup:r}=e,{id:n,items:o,visibleWhen:u}=r;if(!(!u||s(u)))return null;var l=t[n],c=e=>{a({type:"UPDATE_MENU_STATE",payload:{[n]:e.target.value}})};return i(v,{menuGroup:r,children:i("div",{className:"govuk-radios govuk-radios--small","data-module":"govuk-radios",children:o.map(e=>i(h,{menuGroupItem:e,name:n,checked:e.id===l,onChange:c},e.id))})})},g=e=>{var{dispatch:t,menuGroupItem:a,onChange:r,checked:n}=e,u="im-c-menu__item govuk-checkboxes govuk-checkboxes--small".concat(n?"":" im-c-menu__item--checked"),l=p("checkbox-".concat(a.id));return i("div",{className:u,"data-module":"govuk-checkboxes",children:o("div",{className:"govuk-checkboxes__item",children:[i("input",{className:"govuk-checkboxes__input",id:l,name:l,type:"checkbox",checked:n,onChange:e=>{var n,i=Boolean(null===(n=e.target)||void 0===n?void 0:n.checked);r&&r(i),t({type:"UPDATE_MENU_STATE",payload:{[a.id]:i}})}}),i("label",{className:"im-c-menu__item-label govuk-label govuk-checkboxes__label",htmlFor:l,children:a.label})]})},l)},f=e=>{var{dispatch:t,menuGroup:a,menuState:r}=e;return o(v,{menuGroup:a,children:[" ",a.items.map(e=>i(g,{dispatch:t,menuGroupItem:e,checked:r[e.id],onChange:e.handleOnChange},e.id))]})},k=e=>{var{menuGroup:t,dispatch:a,menuState:r}=e;return"checkbox"===t.type?i(f,{menuGroup:t,dispatch:a,menuState:r}):i(b,{menuGroup:t,dispatch:a,menuState:r})},_=e=>{},y=e=>{var{menu:t,eventBus:a,dispatch:r,pluginStateRef:n}=e,i=(e=>{var t=new URL(window.location.href),{searchParams:a}=t,r={};return e.forEach(e=>{var t=a.get(e.id);if("radio"===e.type){var n;r[e.id]=t||e.value||(null===(n=e.items)||void 0===n?void 0:n[0].value)}else{var{items:i}=e;i.forEach(e=>{var{id:a,checked:n}=e,i=t?t.includes(a):n;r[a]=i})}}),r})(t);return r({type:"SET_MENU",payload:{menu:t,menuState:i}}),a.requestOnce("datasets:registry",_),a.emitWhenRequested("menu:state",n)};function S(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),a.push.apply(a,r)}return a}function O(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?S(Object(a),!0).forEach(function(t){l(e,t,a[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):S(Object(a)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))})}return e}var w={InitComponent:function(e){var{pluginConfig:t,pluginState:i,mapState:o,services:u}=e,{dispatch:l}=i,{eventBus:m}=u,[d,p]=a(!1),h=r(i);h.current=i,n(()=>{if(!o.isMapReady)return()=>{};(e=>{c=e})(h);var{menu:e}=t,a=y({menu:e,eventBus:m,dispatch:l,pluginStateRef:h});return p(!0),a},[o.isMapReady]),n(()=>{m.emit("menu:changed",i.menuState),d&&(e=>{var t=new URL(window.location.href),{searchParams:a}=t,{menu:r,menuState:n}=e;r.forEach(e=>{var{id:t,visibleWhen:r,type:i}=e;s(r)?"radio"===i?((e,t,a)=>{var{id:r}=e,n=t[r];a.set(r,n)})(e,n,a):((e,t,a)=>{var{id:r,items:n}=e,i=[];n.forEach(e=>{var{id:a}=e;t[a]&&i.push(a)}),i.length?a.set(r,i):a.delete(r)})(e,n,a):a.delete(t)}),t.search=decodeURIComponent(t.search),window.history.replaceState({},"",t.toString())})(i)},[i.menuState])},reducer:{initialState:{menu:[],menuState:{}},actions:{SET_MENU:(e,t)=>{var{menu:a,menuState:r}=t;return O(O({},e),{},{menu:a,menuState:r})},UPDATE_MENU_STATE:(e,t)=>O(O({},e),{},{menuState:O(O({},e.menuState),t)})}},panels:[{id:"menu",label:"Layers",mobile:{slot:"drawer",modal:!0,dismissible:!0},tablet:{slot:"left-top",dismissible:!0,exclusive:!0,width:"260px"},desktop:{slot:"left-top",modal:!1,dismissible:!0,exclusive:!0,width:"280px"},render:e=>{var{pluginState:t,appConfig:a}=e,{menu:r=[],dispatch:n,menuState:u}=t,l=(null==a?void 0:a.id)||"map",c=r.some(e=>e.groupLabel),m="im-c-menu".concat(c?" im-c-menu--has-groups":"");return i(d,{value:"".concat(l,"-menu"),children:o("div",{className:m,children:[" ",r.map(e=>i(k,{menuGroup:e,dispatch:n,menuState:u},e.id))]})})}}],buttons:[{id:"menuButton",label:"Layers",panelId:"menu",iconId:"layersMenu",mobile:{slot:"top-left",showLabel:!0},tablet:{slot:"top-left",showLabel:!0},desktop:{slot:"top-left",showLabel:!0}}],icons:[{id:"layersMenu",svgContent:'<path d="M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z"></path><path d="M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12"></path><path d="M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17"></path>'}]};export{w as manifest};
1
+ import{createContext as e,useContext as t,useState as n,useRef as a,useEffect as i}from"preact/compat";import{jsx as o,jsxs as s,Fragment as u}from"preact/jsx-runtime";let c={};const l=e=>{const t=c?.current?.menuState||{};for(const[n,a]of Object.entries(e)){const e=t[n];if(!a.includes(e))return!1}return!0},r=e=>{if(null==e)return!0;if("boolean"==typeof e)return e;if("object"==typeof e){for(const[t,n]of Object.entries(e))if("menu"===t&&!l(n))return!1;return!0}return!0},m=e("map-menu"),d=e=>`${t(m)}-${e}`,p=({menuGroupItem:e,name:t,checked:n,onChange:a})=>{const i=d(`radio-${e.id}`),{visibleWhen:u}=e;return!u||r(u)?o("div",{className:'im-c-menu__item govuk-radios govuk-radios--small"',"data-module":"govuk-radios",children:s("div",{className:"govuk-radios__item",children:[o("input",{id:i,className:"govuk-radios__input",type:"radio",name:t,value:e.id,checked:n,onChange:a}),o("label",{className:"im-c-menu__item-label govuk-label govuk-radios__label",htmlFor:i,children:e.label})]})}):null},h=({menuGroup:e,children:t})=>{const n=e.groupLabel||e.label;if(!n)return o(u,{children:t});return o("div",{className:"govuk-form-group im-c-menu-group",children:s("fieldset",{className:"im-c-menu-group__fieldset",children:[o("legend",{className:"im-c-menu-group__legend",children:s("h3",{children:[" ",n," "]})}),t]})},e.id)},g=({menuState:e,dispatch:t,menuGroup:n})=>{const{id:a,items:i,visibleWhen:s}=n;if(!(!s||r(s)))return null;const u=e[a],c=e=>{t({type:"UPDATE_MENU_STATE",payload:{[a]:e.target.value}})};return o(h,{menuGroup:n,children:o("div",{className:"govuk-radios govuk-radios--small","data-module":"govuk-radios",children:i.map(e=>o(p,{menuGroupItem:e,name:a,checked:e.id===u,onChange:c},e.id))})})},b=({dispatch:e,menuGroupItem:t,onChange:n,checked:a})=>{const i="im-c-menu__item govuk-checkboxes govuk-checkboxes--small"+(a?"":" im-c-menu__item--checked"),u=d(`checkbox-${t.id}`);return o("div",{className:i,"data-module":"govuk-checkboxes",children:s("div",{className:"govuk-checkboxes__item",children:[o("input",{className:"govuk-checkboxes__input",id:u,name:u,type:"checkbox",checked:a,onChange:a=>{const i=Boolean(a.target?.checked);n&&n(i),e({type:"UPDATE_MENU_STATE",payload:{[t.id]:i}})}}),o("label",{className:"im-c-menu__item-label govuk-label govuk-checkboxes__label",htmlFor:u,children:t.label})]})},u)},v=({dispatch:e,menuGroup:t,menuState:n})=>s(h,{menuGroup:t,children:[" ",t.items.map(t=>o(b,{dispatch:e,menuGroupItem:t,checked:n[t.id],onChange:t.handleOnChange},t.id))]}),k=({menuGroup:e,dispatch:t,menuState:n})=>"checkbox"===e.type?o(v,{menuGroup:e,dispatch:t,menuState:n}):o(g,{menuGroup:e,dispatch:t,menuState:n}),f=e=>{},_=({menu:e,eventBus:t,dispatch:n,pluginStateRef:a})=>{const i=(e=>{const t=new URL(window.location.href),{searchParams:n}=t,a={};return e.forEach(e=>{const t=n.get(e.id);if("radio"===e.type)a[e.id]=t||e.value||e.items?.[0].value;else{const{items:n}=e;n.forEach(e=>{const{id:n,checked:i}=e,o=t?t.includes(n):i;a[n]=o})}}),a})(e);return n({type:"SET_MENU",payload:{menu:e,menuState:i}}),t.requestOnce("datasets:registry",f),t.emitWhenRequested("menu:state",a)};const S={InitComponent:function({pluginConfig:e,pluginState:t,mapState:o,services:s}){const{dispatch:u}=t,{eventBus:l}=s,[m,d]=n(!1),p=a(t);p.current=t,i(()=>{if(!o.isMapReady)return()=>{};(e=>{c=e})(p);const{menu:t}=e,n=_({menu:t,eventBus:l,dispatch:u,pluginStateRef:p});return d(!0),n},[o.isMapReady]),i(()=>{l.emit("menu:changed",t.menuState),m&&(e=>{const t=new URL(window.location.href),{searchParams:n}=t,{menu:a,menuState:i}=e;a.forEach(e=>{const{id:t,visibleWhen:a,type:o}=e;r(a)?"radio"===o?((e,t,n)=>{const{id:a}=e,i=t[a];n.set(a,i)})(e,i,n):((e,t,n)=>{const{id:a,items:i}=e,o=[];i.forEach(e=>{const{id:n}=e;t[n]&&o.push(n)}),o.length?n.set(a,o):n.delete(a)})(e,i,n):n.delete(t)}),t.search=decodeURIComponent(t.search),window.history.replaceState({},"",t.toString())})(t)},[t.menuState])},reducer:{initialState:{menu:[],menuState:{}},actions:{SET_MENU:(e,t)=>{const{menu:n,menuState:a}=t;return{...e,menu:n,menuState:a}},UPDATE_MENU_STATE:(e,t)=>({...e,menuState:{...e.menuState,...t}})}},panels:[{id:"menu",label:"Layers",mobile:{slot:"drawer",modal:!0,dismissible:!0},tablet:{slot:"left-top",dismissible:!0,exclusive:!0,width:"260px"},desktop:{slot:"left-top",modal:!1,dismissible:!0,exclusive:!0,width:"280px"},render:({pluginState:e,appConfig:t})=>{const{menu:n=[],dispatch:a,menuState:i}=e,u=t?.id||"map",c=n.some(e=>e.groupLabel);return o(m,{value:`${u}-menu`,children:s("div",{className:"im-c-menu"+(c?" im-c-menu--has-groups":""),children:[" ",n.map(e=>o(k,{menuGroup:e,dispatch:a,menuState:i},e.id))]})})}}],buttons:[{id:"menuButton",label:"Layers",panelId:"menu",iconId:"layersMenu",mobile:{slot:"top-left",showLabel:!0},tablet:{slot:"top-left",showLabel:!0},desktop:{slot:"top-left",showLabel:!0}}],icons:[{id:"layersMenu",svgContent:'<path d="M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z"></path><path d="M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12"></path><path d="M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17"></path>'}]};export{S as manifest};
@@ -1 +1 @@
1
- import e from"@babel/runtime/helpers/asyncToGenerator";import r from"@babel/runtime/helpers/defineProperty";function t(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,n)}return t}function n(e){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?t(Object(o),!0).forEach(function(t){r(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(o,r))})}return e}function o(){var r;return n(n({noKeyItemText:"No features displayed"},arguments.length>0&&void 0!==arguments[0]?arguments[0]:{}),{},{id:"menu",load:(r=e(function*(){return(yield import(/* webpackChunkName: "im-menu-plugin" */ "./im-menu-plugin.js")).manifest}),function(){return r.apply(this,arguments)})})}export{o as default};
1
+ import e from"@babel/runtime/helpers/asyncToGenerator";import r from"@babel/runtime/helpers/defineProperty";import t from"@babel/runtime/regenerator";function n(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,n)}return t}function o(e){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?n(Object(o),!0).forEach(function(t){r(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):n(Object(o)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(o,r))})}return e}function i(){var r;return o(o({noKeyItemText:"No features displayed"},arguments.length>0&&void 0!==arguments[0]?arguments[0]:{}),{},{id:"menu",load:(r=e(t.mark(function e(){var r;return t.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=1,import(/* webpackChunkName: "im-menu-plugin" */ "./im-menu-plugin.js");case 1:return r=e.sent.manifest,e.abrupt("return",r);case 2:case"end":return e.stop()}},e)})),function(){return r.apply(this,arguments)})})}export{i as default};