@defra/interactive-map 0.0.61-alpha → 0.0.63-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 (56) hide show
  1. package/dist/esm/im-core.js +1 -1
  2. package/dist/esm/im-shell.js +1 -1
  3. package/dist/umd/im-core.js +1 -1
  4. package/dist/umd/index.js +1 -1
  5. package/docs/api.md +20 -0
  6. package/package.json +1 -1
  7. package/plugins/datasets/dist/esm/im-datasets-esri-adapter.js +1 -1
  8. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  9. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  10. package/plugins/datasets/src/adapters/esri/esriLayerAdapter.js +27 -10
  11. package/plugins/datasets/src/adapters/esri/esriLayerAdapter.test.js +38 -0
  12. package/plugins/datasets/src/reducers/datasetsToMenu.js +12 -9
  13. package/plugins/datasets/src/reducers/datasetsToMenu.test.js +42 -0
  14. package/plugins/datasets/src/reducers/pluginState.test.js +12 -0
  15. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  16. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  17. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  18. package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  19. package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  20. package/providers/openlayers/src/openlayersProvider.js +2 -1
  21. package/providers/openlayers/src/openlayersProvider.test.js +1 -1
  22. package/src/App/components/MapControls/MapControls.jsx +2 -1
  23. package/src/App/components/MapControls/MapControls.test.jsx +1 -1
  24. package/src/App/components/PopupMenu/PopupMenu.test.jsx +22 -0
  25. package/src/App/components/PopupMenu/usePopupMenu.js +9 -6
  26. package/src/App/components/Viewport/MapController.jsx +2 -0
  27. package/src/App/components/Viewport/MapController.test.jsx +3 -0
  28. package/src/App/hooks/useMapProviderOverrides.js +10 -5
  29. package/src/App/hooks/useMapProviderOverrides.test.js +11 -0
  30. package/src/App/hooks/useResetViewTarget.js +135 -0
  31. package/src/App/hooks/useResetViewTarget.test.js +211 -0
  32. package/src/App/hooks/useUserMoveTracking.js +7 -0
  33. package/src/App/hooks/useUserMoveTracking.test.js +21 -3
  34. package/src/App/renderer/mapButtons.js +2 -1
  35. package/src/App/renderer/mapButtons.test.js +12 -0
  36. package/src/App/store/ServiceProvider.jsx +8 -2
  37. package/src/App/store/ServiceProvider.test.jsx +13 -0
  38. package/src/App/store/mapActionsMap.js +14 -0
  39. package/src/App/store/mapActionsMap.test.js +11 -0
  40. package/src/App/store/mapReducer.js +4 -0
  41. package/src/App/store/mapReducer.test.js +2 -0
  42. package/src/InteractiveMap/InteractiveMap.js +8 -0
  43. package/src/InteractiveMap/InteractiveMap.test.js +5 -0
  44. package/src/config/appConfig.js +17 -0
  45. package/src/config/appConfig.test.js +28 -0
  46. package/src/config/defaults.js +1 -0
  47. package/src/config/events.js +2 -0
  48. package/src/services/mapFocus.js +3 -2
  49. package/src/services/mapFocus.test.js +10 -0
  50. package/src/types.js +5 -0
  51. package/src/utils/focusAfterPanelClose.js +3 -1
  52. package/src/utils/getFocusOptions.js +3 -0
  53. package/src/utils/getFocusOptions.test.js +11 -0
  54. package/src/utils/mapStateSync.js +1 -0
  55. package/src/utils/resetView.js +61 -0
  56. package/src/utils/resetView.test.js +62 -0
@@ -227,6 +227,18 @@ describe('mapButtons module', () => {
227
227
  viewportFocusSpy.mockRestore()
228
228
  })
229
229
 
230
+ it.each([
231
+ ['pointer', true],
232
+ ['keyboard', false]
233
+ ])('focuses viewport after onClick with preventScroll for %s users', (interfaceType, preventScroll) => {
234
+ const rafSpy = jest.spyOn(global, 'requestAnimationFrame').mockImplementation(cb => { cb(); return 1 })
235
+ appState.interfaceType = interfaceType
236
+ const onClick = jest.fn()
237
+ render({ ...baseBtn, onClick }).props.onClick({})
238
+ expect(appState.layoutRefs.viewportRef.current.focus).toHaveBeenCalledWith({ preventScroll })
239
+ rafSpy.mockRestore()
240
+ })
241
+
230
242
  it('does not call requestAnimationFrame when keepFocus is true', () => {
231
243
  const rafSpy = jest.spyOn(global, 'requestAnimationFrame')
232
244
  const onClick = jest.fn()
@@ -17,8 +17,14 @@ export const ServiceProvider = ({ eventBus, pluginRegistry, children }) => {
17
17
  const mapStatusRef = useRef(null)
18
18
  const announce = useMemo(() => createAnnouncer(mapStatusRef), [])
19
19
  const hints = useMemo(() => createHints(announce), [announce])
20
- const { layoutRefs } = useApp()
21
- const mapFocus = useMemo(() => createMapFocus({ viewportRef: layoutRefs.viewportRef }), [])
20
+ const { layoutRefs, interfaceType } = useApp()
21
+ // Read through a ref so the memoised mapFocus always sees the current interface type
22
+ const interfaceTypeRef = useRef(interfaceType)
23
+ interfaceTypeRef.current = interfaceType
24
+ const mapFocus = useMemo(() => createMapFocus({
25
+ viewportRef: layoutRefs.viewportRef,
26
+ getInterfaceType: () => interfaceTypeRef.current
27
+ }), [])
22
28
  // One of each per map, so maps on the same page keep their own symbols, symbolDefaults and patterns
23
29
  const symbolRegistry = useMemo(() => createSymbolRegistry(), [])
24
30
  const patternRegistry = useMemo(() => createPatternRegistry(), [])
@@ -71,6 +71,19 @@ describe('ServiceProvider', () => {
71
71
  expect(result.current.mapFocus.isHoldingAnnouncements()).toBe(true)
72
72
  })
73
73
 
74
+ test('focusMap reads the current interface type each time it focuses', () => {
75
+ const { useApp } = jest.requireMock('../store/appContext.js')
76
+ useApp.mockReturnValue({ layoutRefs: { viewportRef: mockViewportRef }, interfaceType: 'pointer' })
77
+ const { result, rerender } = renderHook(() => React.useContext(ServiceContext), { wrapper })
78
+ result.current.focusMap()
79
+ expect(mockViewportRef.current.focus).toHaveBeenLastCalledWith({ preventScroll: true })
80
+
81
+ useApp.mockReturnValue({ layoutRefs: { viewportRef: mockViewportRef }, interfaceType: 'keyboard' })
82
+ rerender()
83
+ result.current.focusMap()
84
+ expect(mockViewportRef.current.focus).toHaveBeenLastCalledWith({ preventScroll: false })
85
+ })
86
+
74
87
  test('gives each map its own symbol registry, kept across renders, with that map\'s symbolDefaults', () => {
75
88
  const { useConfig } = jest.requireMock('../store/configContext.js')
76
89
  useConfig.mockReturnValue({ id: 'map-a', symbolDefaults: { backgroundColor: '#111111' } })
@@ -29,6 +29,18 @@ const setMapSize = (state, payload) => {
29
29
  }
30
30
  }
31
31
 
32
+ const setResetViewTarget = (state, payload) => ({
33
+ ...state,
34
+ resetViewTarget: payload,
35
+ isResetViewTargetKnown: true
36
+ })
37
+
38
+ const clearResetViewTarget = (state) => ({
39
+ ...state,
40
+ resetViewTarget: null,
41
+ isResetViewTargetKnown: false
42
+ })
43
+
32
44
  const updateCrossHair = (state, payload) => {
33
45
  return {
34
46
  ...state,
@@ -75,6 +87,8 @@ export const actionsMap = {
75
87
  SET_MAP_STYLE: setMapStyle,
76
88
  SET_MAP_STYLES: setMapStyles,
77
89
  SET_MAP_SIZE: setMapSize,
90
+ SET_RESET_VIEW_TARGET: setResetViewTarget,
91
+ CLEAR_RESET_VIEW_TARGET: clearResetViewTarget,
78
92
  UPDATE_CROSS_HAIR: updateCrossHair,
79
93
  UPSERT_LOCATION_MARKER: upsertMarker,
80
94
  REMOVE_LOCATION_MARKER: removeMarker
@@ -33,6 +33,17 @@ describe('actionsMap', () => {
33
33
  expect(result).toEqual({ ...state, ...payload })
34
34
  })
35
35
 
36
+ test('SET_RESET_VIEW_TARGET sets resetViewTarget', () => {
37
+ const payload = { center: [1, 2], zoom: 10 }
38
+ const result = actionsMap.SET_RESET_VIEW_TARGET(state, payload)
39
+ expect(result).toEqual({ ...state, resetViewTarget: payload, isResetViewTargetKnown: true })
40
+ })
41
+
42
+ test('CLEAR_RESET_VIEW_TARGET marks the reset view target as not known', () => {
43
+ const result = actionsMap.CLEAR_RESET_VIEW_TARGET({ ...state, resetViewTarget: { center: [1, 2], zoom: 10 }, isResetViewTargetKnown: true })
44
+ expect(result).toEqual({ ...state, resetViewTarget: null, isResetViewTargetKnown: false })
45
+ })
46
+
36
47
  test('MAP_MOVE merges payload into state', () => {
37
48
  const payload = { center: [10, 20] }
38
49
  const result = actionsMap.MAP_MOVE(state, payload)
@@ -33,6 +33,10 @@ export const initialState = (config) => {
33
33
  isAtMinZoom: null,
34
34
  // Whether the user started the latest move, rather than code (useUserMoveTracking)
35
35
  isUserMove: false,
36
+ // The center and zoom the map settled at when last showing its configured view, or null when
37
+ // there isn't one (useResetViewTarget). Not known yet on load and during a reset.
38
+ resetViewTarget: null,
39
+ isResetViewTargetKnown: false,
36
40
 
37
41
  // Full target marker state
38
42
  crossHair: {
@@ -48,6 +48,8 @@ describe('mapReducer', () => {
48
48
  isAtMaxZoom: null,
49
49
  isAtMinZoom: null,
50
50
  isUserMove: false,
51
+ resetViewTarget: null,
52
+ isResetViewTargetKnown: false,
51
53
  crossHair: {
52
54
  isVisible: false,
53
55
  isPinnedToMap: false,
@@ -554,6 +554,14 @@ export default class InteractiveMap {
554
554
  this.eventBus.emitWhenReady(events.MAP_SET_VIEW, opts)
555
555
  }
556
556
 
557
+ /**
558
+ * Move the map to its configured `bounds` or `extent`, or to `center` and `zoom` if no bounds are set,
559
+ * respecting the safe zone padding. Ignores any view read from the URL when the map loaded.
560
+ */
561
+ resetView () {
562
+ this.eventBus.emitWhenReady(events.MAP_RESET_VIEW)
563
+ }
564
+
557
565
  /**
558
566
  * Programmatically open the map.
559
567
  *
@@ -641,6 +641,11 @@ describe('InteractiveMap — Public API Methods', () => {
641
641
  expect(map.eventBus.emitWhenReady).toHaveBeenCalledWith('map:setview', { center, zoom: 12 })
642
642
  })
643
643
 
644
+ it('resetView emits the reset view event', () => {
645
+ map.resetView()
646
+ expect(map.eventBus.emitWhenReady).toHaveBeenCalledWith('map:resetview')
647
+ })
648
+
644
649
  it('showHint and dismissHint emit correct events', () => {
645
650
  map.showHint('Press <kbd>Enter</kbd> to select', { duration: 2000 })
646
651
  map.dismissHint()
@@ -1,5 +1,7 @@
1
1
  import { KeyboardHelp } from '../App/components/KeyboardHelp/KeyboardHelp.jsx'
2
2
  import { MapControls } from '../App/components/MapControls/MapControls.jsx'
3
+ import { EVENTS as events } from './events.js'
4
+ import { getConfiguredView, isAtResetViewTarget } from '../utils/resetView.js'
3
5
 
4
6
  const keyboardBasePanelSlots = {
5
7
  slot: 'middle',
@@ -116,6 +118,18 @@ export const defaultAppConfig = {
116
118
  mobile: buttonSlots,
117
119
  tablet: buttonSlots,
118
120
  desktop: buttonSlots
121
+ }, {
122
+ id: 'resetView',
123
+ label: 'Reset view',
124
+ iconId: 'reset',
125
+ keepFocus: true,
126
+ onClick: (_e, { services }) => services.eventBus.emit(events.MAP_RESET_VIEW),
127
+ excludeWhen: ({ appConfig }) => !appConfig.enableResetView || !getConfiguredView(appConfig),
128
+ // Disabled until the target is known, so it doesn't flash enabled on load or during a reset
129
+ enableWhen: ({ mapState, mapProvider }) => mapState.isResetViewTargetKnown && !isAtResetViewTarget(mapState, mapProvider),
130
+ mobile: buttonSlots,
131
+ tablet: buttonSlots,
132
+ desktop: buttonSlots
119
133
  }],
120
134
 
121
135
  panels: [{
@@ -157,6 +171,9 @@ export const defaultAppConfig = {
157
171
  }, {
158
172
  id: 'minus',
159
173
  svgContent: '<path d="M5 12h14"/>'
174
+ }, {
175
+ id: 'reset',
176
+ svgContent: '<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/>'
160
177
  }, {
161
178
  id: 'chevron',
162
179
  svgContent: '<path d="m6 9 6 6 6-6"/>'
@@ -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)
@@ -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
@@ -875,6 +875,11 @@
875
875
  * trigger a button click but not a drag. When enabled, enableZoomControls' buttons
876
876
  * are hidden to avoid duplication.
877
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
+ *
878
883
  * @property {boolean} [enableZoomControls=true]
879
884
  * Whether zoom control buttons are displayed. Not displayed when the interface
880
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
+ })