@defra/interactive-map 0.0.60-alpha → 0.0.62-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 (84) hide show
  1. package/dist/css/index.css +1 -1
  2. package/dist/esm/im-core.js +1 -1
  3. package/dist/esm/im-shell.js +1 -1
  4. package/dist/umd/im-core.js +1 -1
  5. package/dist/umd/index.js +1 -1
  6. package/docs/api/button-definition.md +11 -0
  7. package/docs/api/context.md +2 -0
  8. package/docs/api.md +20 -0
  9. package/docs/plugins/draw.md +26 -0
  10. package/package.json +1 -1
  11. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  12. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  13. package/plugins/datasets/src/reducers/datasetsToMenu.js +12 -9
  14. package/plugins/datasets/src/reducers/datasetsToMenu.test.js +42 -0
  15. package/plugins/datasets/src/reducers/pluginState.test.js +12 -0
  16. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  17. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  18. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  19. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  20. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  21. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  22. package/plugins/draw/dist/umd/index.js +1 -1
  23. package/plugins/draw/src/DrawInit.jsx +3 -0
  24. package/plugins/draw/src/DrawInit.test.jsx +16 -0
  25. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +19 -3
  26. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
  27. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -2
  28. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +14 -0
  29. package/plugins/draw/src/defaults.js +4 -0
  30. package/plugins/draw/src/hooks/useSnapMinZoom.js +29 -0
  31. package/plugins/draw/src/hooks/useSnapMinZoom.test.js +82 -0
  32. package/plugins/draw/src/manifest.js +4 -1
  33. package/plugins/draw/src/manifest.test.js +15 -2
  34. package/plugins/draw/src/utils/snapMinZoom.js +8 -0
  35. package/plugins/draw/src/utils/snapMinZoom.test.js +22 -0
  36. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  37. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  38. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
  39. package/plugins/map-key/src/manifest.js +1 -1
  40. package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  41. package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  42. package/providers/openlayers/src/openlayersProvider.js +2 -1
  43. package/providers/openlayers/src/openlayersProvider.test.js +1 -1
  44. package/src/App/components/MapButton/MapButton.jsx +20 -2
  45. package/src/App/components/MapButton/MapButton.module.scss +15 -1
  46. package/src/App/components/MapButton/MapButton.test.jsx +21 -3
  47. package/src/App/components/MapControls/MapControls.jsx +2 -1
  48. package/src/App/components/MapControls/MapControls.test.jsx +1 -1
  49. package/src/App/components/PopupMenu/PopupMenu.test.jsx +22 -0
  50. package/src/App/components/PopupMenu/usePopupMenu.js +9 -6
  51. package/src/App/components/Tooltip/Tooltip.jsx +29 -11
  52. package/src/App/components/Tooltip/Tooltip.test.jsx +45 -3
  53. package/src/App/components/Viewport/MapController.jsx +4 -0
  54. package/src/App/components/Viewport/MapController.test.jsx +6 -0
  55. package/src/App/hooks/useMapProviderOverrides.js +10 -5
  56. package/src/App/hooks/useMapProviderOverrides.test.js +11 -0
  57. package/src/App/hooks/useResetViewTarget.js +135 -0
  58. package/src/App/hooks/useResetViewTarget.test.js +211 -0
  59. package/src/App/hooks/useUserMoveTracking.js +72 -0
  60. package/src/App/hooks/useUserMoveTracking.test.js +117 -0
  61. package/src/App/renderer/mapButtons.js +3 -1
  62. package/src/App/renderer/mapButtons.test.js +20 -0
  63. package/src/App/store/ServiceProvider.jsx +8 -2
  64. package/src/App/store/ServiceProvider.test.jsx +13 -0
  65. package/src/App/store/mapActionsMap.js +16 -1
  66. package/src/App/store/mapActionsMap.test.js +17 -0
  67. package/src/App/store/mapReducer.js +6 -0
  68. package/src/App/store/mapReducer.test.js +3 -0
  69. package/src/InteractiveMap/InteractiveMap.js +8 -0
  70. package/src/InteractiveMap/InteractiveMap.test.js +5 -0
  71. package/src/config/appConfig.js +17 -0
  72. package/src/config/appConfig.test.js +28 -0
  73. package/src/config/defaults.js +1 -0
  74. package/src/config/events.js +2 -0
  75. package/src/scss/settings/_colors.scss +4 -0
  76. package/src/services/mapFocus.js +3 -2
  77. package/src/services/mapFocus.test.js +10 -0
  78. package/src/types.js +9 -0
  79. package/src/utils/focusAfterPanelClose.js +3 -1
  80. package/src/utils/getFocusOptions.js +3 -0
  81. package/src/utils/getFocusOptions.test.js +11 -0
  82. package/src/utils/mapStateSync.js +1 -0
  83. package/src/utils/resetView.js +61 -0
  84. package/src/utils/resetView.test.js +62 -0
@@ -20,6 +20,7 @@ describe('defaultAppConfig', () => {
20
20
  const zoomInBtn = buttons.find(b => b.id === 'zoomIn')
21
21
  const zoomOutBtn = buttons.find(b => b.id === 'zoomOut')
22
22
  const mapControlsBtn = buttons.find(b => b.id === 'mapControls')
23
+ const resetViewBtn = buttons.find(b => b.id === 'resetView')
23
24
 
24
25
  // --- UI RENDER TESTS ---
25
26
  it('renders KeyboardHelp panel', () => {
@@ -209,6 +210,33 @@ describe('defaultAppConfig', () => {
209
210
  })
210
211
  })
211
212
 
213
+ // --- RESET VIEW ---
214
+ it('excludes resetView unless enabled and a view is configured', () => {
215
+ expect(resetViewBtn.excludeWhen({ appConfig: { enableResetView: false, center: [1, 2] } })).toBe(true)
216
+ expect(resetViewBtn.excludeWhen({ appConfig: { enableResetView: true } })).toBe(true)
217
+ expect(resetViewBtn.excludeWhen({ appConfig: { enableResetView: true, center: [1, 2] } })).toBe(false)
218
+ expect(resetViewBtn.excludeWhen({ appConfig: { enableResetView: true, extent: [0, 0, 1, 1] } })).toBe(false)
219
+ })
220
+
221
+ it('resetView onClick emits the reset view event', () => {
222
+ const eventBus = { emit: jest.fn() }
223
+ resetViewBtn.onClick({}, { services: { eventBus } })
224
+ expect(eventBus.emit).toHaveBeenCalledWith('map:resetview')
225
+ })
226
+
227
+ it('disables resetView while the map is at the reset view', () => {
228
+ const mapProvider = { mapToScreen: ([x, y]) => ({ x, y }) }
229
+ const resetViewTarget = { center: [1, 2], zoom: 10 }
230
+ expect(resetViewBtn.enableWhen({ mapProvider, mapState: { center: [1, 2], zoom: 10, resetViewTarget, isResetViewTargetKnown: true } })).toBe(false)
231
+ expect(resetViewBtn.enableWhen({ mapProvider, mapState: { center: [50, 2], zoom: 10, resetViewTarget, isResetViewTargetKnown: true } })).toBe(true)
232
+ })
233
+
234
+ it('disables resetView until the reset view target is known, and enables it when there is none', () => {
235
+ const mapProvider = { mapToScreen: ([x, y]) => ({ x, y }) }
236
+ expect(resetViewBtn.enableWhen({ mapProvider, mapState: { center: [1, 2], zoom: 10, resetViewTarget: null, isResetViewTargetKnown: false } })).toBe(false)
237
+ expect(resetViewBtn.enableWhen({ mapProvider, mapState: { center: [1, 2], zoom: 10, resetViewTarget: null, isResetViewTargetKnown: true } })).toBe(true)
238
+ })
239
+
212
240
  // --- MOVE CONTROLS ---
213
241
  it('covers all branches of mapControls control excludeWhen', () => {
214
242
  const control = defaultAppConfig.controls.find(c => c.id === 'mapControls')
@@ -17,6 +17,7 @@ const defaults = {
17
17
  deviceNotSupportedText: 'Your device is not supported. A map is available with a more up-to-date browser or device.',
18
18
  enableFullscreen: false,
19
19
  enableMapControls: true,
20
+ enableResetView: false,
20
21
  enableZoomControls: true,
21
22
  genericErrorText: 'There was a problem loading the map. Please try again later.',
22
23
  hasExitButton: false,
@@ -159,6 +159,8 @@ export const EVENTS = {
159
159
  MAP_FIT_TO_BOUNDS: 'map:fittobounds',
160
160
  /** @internal Set the map center and zoom. Payload: { center: [number, number], zoom?: number } */
161
161
  MAP_SET_VIEW: 'map:setview',
162
+ /** @internal Move the map to its configured bounds, or center and zoom. Payload: none */
163
+ MAP_RESET_VIEW: 'map:resetview',
162
164
 
163
165
  // ============================================
164
166
  // Map responses (advanced / subscribe)
@@ -17,6 +17,8 @@
17
17
  --pressed-button-foreground-color: #ffffff;
18
18
  --pressed-button-border-color: #0b0c0c;
19
19
  --pressed-button-background-color: #0b0c0c;
20
+ --disabled-pressed-button-foreground-color: #ffffff;
21
+ --disabled-pressed-button-background-color: #9d9e9e;
20
22
 
21
23
  // Scrollable content
22
24
  --scrollable-outline: 3px solid #ffdd00;
@@ -68,6 +70,8 @@
68
70
  --pressed-button-foreground-color: #0b0c0c;
69
71
  --pressed-button-border-color: #ffffff;
70
72
  --pressed-button-background-color: #ffffff;
73
+ --disabled-pressed-button-foreground-color: #383f43;
74
+ --disabled-pressed-button-background-color: #888c8e;
71
75
 
72
76
  // Content
73
77
  --content-border-color: #505a5f;
@@ -1,4 +1,5 @@
1
1
  // services/mapFocus.js
2
+ import { getFocusOptions } from '../utils/getFocusOptions.js'
2
3
 
3
4
  /**
4
5
  * Moves focus to the map viewport, optionally with a message that stands in
@@ -13,7 +14,7 @@
13
14
  */
14
15
  const ARIA_LABEL = 'aria-label'
15
16
 
16
- export function createMapFocus ({ viewportRef }) {
17
+ export function createMapFocus ({ viewportRef, getInterfaceType = () => undefined }) {
17
18
  let originalLabel = null
18
19
  let hasMessage = false
19
20
  let isHoldingAnnouncements = false
@@ -31,7 +32,7 @@ export function createMapFocus ({ viewportRef }) {
31
32
  hasMessage = true
32
33
  isHoldingAnnouncements = true
33
34
  }
34
- viewport.focus()
35
+ viewport.focus(getFocusOptions(getInterfaceType()))
35
36
  }
36
37
 
37
38
  const clear = () => {
@@ -16,6 +16,16 @@ describe('createMapFocus', () => {
16
16
  viewport.remove()
17
17
  })
18
18
 
19
+ it.each([
20
+ ['pointer', true],
21
+ ['keyboard', false]
22
+ ])('focuses the viewport with preventScroll for %s users', (interfaceType, preventScroll) => {
23
+ const focusSpy = jest.spyOn(viewport, 'focus')
24
+ mapFocus = createMapFocus({ viewportRef: { current: viewport }, getInterfaceType: () => interfaceType })
25
+ mapFocus.focus()
26
+ expect(focusSpy).toHaveBeenCalledWith({ preventScroll })
27
+ })
28
+
19
29
  it('focuses the viewport without changing its name or holding announcements', () => {
20
30
  mapFocus.focus()
21
31
  expect(document.activeElement).toBe(viewport)
package/src/types.js CHANGED
@@ -194,6 +194,10 @@
194
194
  * opens or expands a custom control rendered elsewhere by the plugin. Text or a function
195
195
  * returning the id.
196
196
  *
197
+ * @property {string | ((context: PluginContext) => string | undefined)} [description]
198
+ * Extra information read by screen readers after the label, set with aria-describedby. Text or a
199
+ * function returning the text; no description when it returns nothing.
200
+ *
197
201
  * @property {ButtonBreakpointConfig} [desktop]
198
202
  * Desktop breakpoint configuration.
199
203
  *
@@ -871,6 +875,11 @@
871
875
  * trigger a button click but not a drag. When enabled, enableZoomControls' buttons
872
876
  * are hidden to avoid duplication.
873
877
  *
878
+ * @property {boolean} [enableResetView=false]
879
+ * Whether a reset view button is displayed. It moves the map to the configured bounds
880
+ * or extent, or to center and zoom if no bounds are set. Disabled until the map is
881
+ * moved away from that view, and not displayed when none of these are configured.
882
+ *
874
883
  * @property {boolean} [enableZoomControls=true]
875
884
  * Whether zoom control buttons are displayed. Not displayed when the interface
876
885
  * type is 'touch', or when enableMapControls is enabled.
@@ -1,9 +1,11 @@
1
+ import { getFocusOptions } from './getFocusOptions.js'
2
+
1
3
  // Moves focus to the element that opened a closed panel, or to the map viewport if that element
2
4
  // has since left the page (e.g. a Features-list item that dropped out of the list). Runs in the
3
5
  // next frame so a modal panel has released its focus trap and inert state first.
4
6
  export const focusAfterPanelClose = ({ triggeringElement, viewportRef, interfaceType }) => {
5
7
  requestAnimationFrame(() => {
6
8
  const focusTarget = triggeringElement && document.contains(triggeringElement) ? triggeringElement : viewportRef.current
7
- focusTarget?.focus?.({ preventScroll: interfaceType !== 'keyboard' })
9
+ focusTarget?.focus?.(getFocusOptions(interfaceType))
8
10
  })
9
11
  }
@@ -0,0 +1,3 @@
1
+ // Keyboard users get the browser's own scroll to the newly focused element, as they're following
2
+ // focus. Mouse and touch users have just clicked something on the map, so the page stays where it is.
3
+ export const getFocusOptions = (interfaceType) => ({ preventScroll: interfaceType !== 'keyboard' })
@@ -0,0 +1,11 @@
1
+ import { getFocusOptions } from './getFocusOptions.js'
2
+
3
+ describe('getFocusOptions', () => {
4
+ it('lets the browser scroll to the focused element for keyboard users', () => {
5
+ expect(getFocusOptions('keyboard')).toEqual({ preventScroll: false })
6
+ })
7
+
8
+ it.each(['pointer', 'touch', undefined])('prevents the page scrolling for %s', (interfaceType) => {
9
+ expect(getFocusOptions(interfaceType)).toEqual({ preventScroll: true })
10
+ })
11
+ })
@@ -92,6 +92,7 @@ const getInitialMapState = ({ id, center, zoom, bounds, urlPosition = 'sync' },
92
92
  }
93
93
 
94
94
  export {
95
+ getMapStateFromURL,
95
96
  setMapStateInURL,
96
97
  getInitialMapState
97
98
  }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * The view the map was configured with, ignoring any view read from the URL.
3
+ * Follows the same order as getInitialMapState after the URL: bounds or extent, then center and zoom.
4
+ *
5
+ * @param {{ bounds?: any, extent?: any, center?: [number, number], zoom?: number }} appConfig
6
+ * @returns {{ bounds: any } | { center: [number, number], zoom?: number } | null}
7
+ */
8
+ export const getConfiguredView = ({ bounds, extent, center, zoom }) => {
9
+ const configuredBounds = bounds || extent
10
+ if (configuredBounds) {
11
+ return { bounds: configuredBounds }
12
+ }
13
+ if (center) {
14
+ return { center, zoom }
15
+ }
16
+ return null
17
+ }
18
+
19
+ /**
20
+ * Moves the map to its configured view. Calls the overridden mapProvider methods, so the safe zone is respected.
21
+ *
22
+ * @param {object} mapProvider
23
+ * @param {object} appConfig
24
+ */
25
+ export const resetView = (mapProvider, appConfig) => {
26
+ const view = getConfiguredView(appConfig)
27
+ if (!view) {
28
+ return
29
+ }
30
+ if (view.bounds) {
31
+ mapProvider.fitToBounds(view.bounds)
32
+ } else {
33
+ mapProvider.setView(view)
34
+ }
35
+ }
36
+
37
+ // How far the map can be from the reset view target and still count as at it. Absorbs the
38
+ // rounding of the reported center and zoom, not a move anyone would notice.
39
+ export const RESET_VIEW_PIXEL_TOLERANCE = 1
40
+ export const RESET_VIEW_ZOOM_TOLERANCE = 0.01
41
+
42
+ /**
43
+ * Whether the map is at the reset view target: where it settled after its last reset, or after
44
+ * loading at its configured view. Compares the center in screen pixels, so the tolerance is the
45
+ * same in every CRS.
46
+ *
47
+ * @param {{ center?: [number, number], zoom?: number, resetViewTarget?: { center: [number, number], zoom: number } | null }} mapState
48
+ * @param {{ mapToScreen: (coords: [number, number]) => { x: number, y: number } }} mapProvider
49
+ * @returns {boolean}
50
+ */
51
+ export const isAtResetViewTarget = ({ center, zoom, resetViewTarget }, mapProvider) => {
52
+ if (!resetViewTarget || !center || !mapProvider) {
53
+ return false
54
+ }
55
+ if (Math.abs(zoom - resetViewTarget.zoom) > RESET_VIEW_ZOOM_TOLERANCE) {
56
+ return false
57
+ }
58
+ const current = mapProvider.mapToScreen(center)
59
+ const target = mapProvider.mapToScreen(resetViewTarget.center)
60
+ return Math.hypot(current.x - target.x, current.y - target.y) <= RESET_VIEW_PIXEL_TOLERANCE
61
+ }
@@ -0,0 +1,62 @@
1
+ import { getConfiguredView, resetView, isAtResetViewTarget } from './resetView.js'
2
+
3
+ describe('getConfiguredView', () => {
4
+ it('prefers bounds, then extent, then center and zoom', () => {
5
+ expect(getConfiguredView({ bounds: [0, 0, 1, 1], extent: [2, 2, 3, 3], center: [5, 5], zoom: 4 })).toEqual({ bounds: [0, 0, 1, 1] })
6
+ expect(getConfiguredView({ extent: [2, 2, 3, 3], center: [5, 5], zoom: 4 })).toEqual({ bounds: [2, 2, 3, 3] })
7
+ expect(getConfiguredView({ center: [5, 5], zoom: 4 })).toEqual({ center: [5, 5], zoom: 4 })
8
+ })
9
+
10
+ it('returns null when nothing is configured', () => {
11
+ expect(getConfiguredView({ zoom: 4 })).toBeNull()
12
+ })
13
+ })
14
+
15
+ describe('resetView', () => {
16
+ const createMapProvider = () => ({ fitToBounds: jest.fn(), setView: jest.fn() })
17
+
18
+ it('fits to the configured bounds', () => {
19
+ const mapProvider = createMapProvider()
20
+ resetView(mapProvider, { bounds: [0, 0, 1, 1], center: [5, 5], zoom: 4 })
21
+ expect(mapProvider.fitToBounds).toHaveBeenCalledWith([0, 0, 1, 1])
22
+ expect(mapProvider.setView).not.toHaveBeenCalled()
23
+ })
24
+
25
+ it('sets the configured center and zoom when there are no bounds', () => {
26
+ const mapProvider = createMapProvider()
27
+ resetView(mapProvider, { center: [5, 5], zoom: 4 })
28
+ expect(mapProvider.setView).toHaveBeenCalledWith({ center: [5, 5], zoom: 4 })
29
+ expect(mapProvider.fitToBounds).not.toHaveBeenCalled()
30
+ })
31
+
32
+ it('does nothing when no view is configured', () => {
33
+ const mapProvider = createMapProvider()
34
+ resetView(mapProvider, {})
35
+ expect(mapProvider.fitToBounds).not.toHaveBeenCalled()
36
+ expect(mapProvider.setView).not.toHaveBeenCalled()
37
+ })
38
+ })
39
+
40
+ describe('isAtResetViewTarget', () => {
41
+ // 10 screen pixels per map unit
42
+ const mapProvider = { mapToScreen: ([x, y]) => ({ x: x * 10, y: y * 10 }) }
43
+ const resetViewTarget = { center: [1, 2], zoom: 10 }
44
+
45
+ it('is false without a target, a center or a map provider', () => {
46
+ expect(isAtResetViewTarget({ center: [1, 2], zoom: 10, resetViewTarget: null }, mapProvider)).toBe(false)
47
+ expect(isAtResetViewTarget({ zoom: 10, resetViewTarget }, mapProvider)).toBe(false)
48
+ expect(isAtResetViewTarget({ center: [1, 2], zoom: 10, resetViewTarget }, null)).toBe(false)
49
+ })
50
+
51
+ it('is true within a pixel and a small zoom difference', () => {
52
+ expect(isAtResetViewTarget({ center: [1.05, 2.05], zoom: 10.005, resetViewTarget }, mapProvider)).toBe(true)
53
+ })
54
+
55
+ it('is false once the center has moved more than a pixel', () => {
56
+ expect(isAtResetViewTarget({ center: [1.2, 2], zoom: 10, resetViewTarget }, mapProvider)).toBe(false)
57
+ })
58
+
59
+ it('is false once the zoom has changed', () => {
60
+ expect(isAtResetViewTarget({ center: [1, 2], zoom: 10.1, resetViewTarget }, mapProvider)).toBe(false)
61
+ })
62
+ })