Skip to content

Commit 5fd7630

Browse files
committed
refreshableControlAnchorTitle replaces updateControlAnchorTitle & eliminates downstream boilerplate by closing over properties at fullscreenMap scope
1 parent 369903d commit 5fd7630

File tree

6 files changed

+75
-78
lines changed

6 files changed

+75
-78
lines changed

src/leaf/map/fullscreen/control/added-listener.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { DomEvent, type Map } from 'leaflet'
22

3-
import { type ControlAnchor } from './anchor/anchor'
4-
import {
5-
type UpdateControlAnchorTitleAnchorOptions,
6-
updateControlAnchorTitle,
7-
} from './anchor/update-title'
3+
import { type ControlAnchorOnClick } from './anchor/anchor'
4+
import { type RefreshableControlAnchorTitle } from './anchor/refreshable-title'
85

96
import { type ControlOnAdd, type ToggleableState } from '@stassi/leaf'
107

118
export type ControlAddedListenerOptions = {
129
map: {
1310
control: {
14-
anchor: ControlAnchor & UpdateControlAnchorTitleAnchorOptions
11+
anchor: {
12+
onClick: ControlAnchorOnClick
13+
refreshTitle: RefreshableControlAnchorTitle
14+
}
1515
container: { element: HTMLElement }
1616
}
1717
fullscreen: { enabled: ToggleableState }
@@ -22,29 +22,16 @@ export function controlAddedListener({
2222
map: {
2323
control: {
2424
anchor: {
25-
assign: anchorAssign,
2625
onClick: anchorOnClick,
27-
titleStates: anchorTitleStates,
26+
refreshTitle: refreshControlAnchorTitle,
2827
},
2928
container: { element: containerElement },
3029
},
3130
fullscreen: { enabled: fullscreenMapEnabled },
3231
},
3332
}: ControlAddedListenerOptions): ControlOnAdd {
3433
return function handleControlAdded(map: Map): HTMLElement {
35-
updateControlAnchorTitle({
36-
map: {
37-
control: {
38-
anchor: {
39-
assign: anchorAssign,
40-
titleStates: anchorTitleStates,
41-
},
42-
},
43-
fullscreen: {
44-
enabled: fullscreenMapEnabled,
45-
},
46-
},
47-
})
34+
refreshControlAnchorTitle()
4835

4936
anchorOnClick(async function handleAnchorClick(
5037
event: Event,

src/leaf/map/fullscreen/control/anchor/anchor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ export type ControlAnchorAssign = (
1111
) => HTMLElement
1212

1313
type EventHandlerAsync = EventHandler<true>
14+
export type ControlAnchorOnClick = (handler: EventHandlerAsync) => void
1415
export type ControlAnchor = {
1516
assign: ControlAnchorAssign
16-
onClick: (handler: EventHandlerAsync) => void
17+
onClick: ControlAnchorOnClick
1718
}
1819

1920
export function controlAnchor({
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { type ControlAnchorAssign } from './anchor'
2+
3+
import { type ToggleableState } from '@stassi/leaf'
4+
5+
type StringifyBoolean<T extends boolean> = `${T}`
6+
export type ControlAnchorTitleStates = Record<StringifyBoolean<boolean>, string>
7+
export type RefreshableControlAnchorTitleOptions = {
8+
map: {
9+
control: {
10+
anchor: {
11+
assign: ControlAnchorAssign
12+
titleStates: ControlAnchorTitleStates
13+
}
14+
}
15+
fullscreen: {
16+
enabled: ToggleableState
17+
}
18+
}
19+
}
20+
21+
type Callback<T> = () => T
22+
export type RefreshableControlAnchorTitle = Callback<void>
23+
24+
export function refreshableControlAnchorTitle({
25+
map: {
26+
control: {
27+
anchor: { assign: anchorAssign, titleStates: anchorTitleStates },
28+
},
29+
fullscreen: { enabled: fullscreenMapEnabled },
30+
},
31+
}: RefreshableControlAnchorTitleOptions): RefreshableControlAnchorTitle {
32+
return function refreshControlAnchorTitle(): void {
33+
anchorAssign({
34+
title:
35+
anchorTitleStates[
36+
<keyof ControlAnchorTitleStates>fullscreenMapEnabled().toString()
37+
],
38+
})
39+
}
40+
}

src/leaf/map/fullscreen/control/anchor/update-title.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/leaf/map/fullscreen/fullscreen.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import {
1414
type ControlAnchorAttributes,
1515
controlAnchor,
1616
} from './control/anchor/anchor'
17-
import { type ControlAnchorTitleStates } from './control/anchor/update-title'
17+
import {
18+
type ControlAnchorTitleStates,
19+
type RefreshableControlAnchorTitle,
20+
refreshableControlAnchorTitle,
21+
} from './control/anchor/refreshable-title'
1822
import {
1923
type FullscreenMapLifecycleEvent,
2024
fullscreenMapLifecycleListener,
@@ -100,6 +104,20 @@ export function fullscreenMap({
100104
}),
101105
}),
102106
map: Map = leafletMap(id, mapOptions),
107+
refreshControlAnchorTitle: RefreshableControlAnchorTitle =
108+
refreshableControlAnchorTitle({
109+
map: {
110+
control: {
111+
anchor: {
112+
assign: anchorAssign,
113+
titleStates: anchorTitleStates,
114+
},
115+
},
116+
fullscreen: {
117+
enabled: fullscreenMapEnabled,
118+
},
119+
},
120+
}),
103121
handleMapLifecycleChange: (
104122
mapLifecycleEvent: FullscreenMapLifecycleEvent,
105123
) => () => void = (
@@ -109,8 +127,7 @@ export function fullscreenMap({
109127
map: {
110128
control: {
111129
anchor: {
112-
assign: anchorAssign,
113-
titleStates: anchorTitleStates,
130+
refreshTitle: refreshControlAnchorTitle,
114131
},
115132
},
116133
fullscreen: {
@@ -129,9 +146,8 @@ export function fullscreenMap({
129146
map: {
130147
control: {
131148
anchor: {
132-
assign: anchorAssign,
133149
onClick: anchorOnClick,
134-
titleStates: anchorTitleStates,
150+
refreshTitle: refreshControlAnchorTitle,
135151
},
136152
container: { element: containerElement },
137153
},

src/leaf/map/fullscreen/lifecycle-listener.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { DomUtil } from 'leaflet'
22

3-
import { type ControlAnchorAssign } from './control/anchor/anchor'
4-
import {
5-
type ControlAnchorTitleStates,
6-
updateControlAnchorTitle,
7-
} from './control/anchor/update-title'
3+
import { type RefreshableControlAnchorTitle } from './control/anchor/refreshable-title'
84

95
import {
106
type Map,
@@ -19,8 +15,7 @@ export type FullscreenMapLifecycleListenerOptions = {
1915
map: {
2016
control: {
2117
anchor: {
22-
assign: ControlAnchorAssign
23-
titleStates: ControlAnchorTitleStates
18+
refreshTitle: RefreshableControlAnchorTitle
2419
}
2520
}
2621
fullscreen: {
@@ -38,7 +33,7 @@ export type FullscreenMapLifecycleListener = () => void
3833
export function fullscreenMapLifecycleListener({
3934
map: {
4035
control: {
41-
anchor: { assign: anchorAssign, titleStates: anchorTitleStates },
36+
anchor: { refreshTitle: refreshControlAnchorTitle },
4237
},
4338
fullscreen: {
4439
className: fullscreenMapClassName,
@@ -61,14 +56,7 @@ export function fullscreenMapLifecycleListener({
6156

6257
map.invalidateSize()
6358
toggleFullscreenMap()
64-
updateControlAnchorTitle({
65-
map: {
66-
control: {
67-
anchor: { assign: anchorAssign, titleStates: anchorTitleStates },
68-
},
69-
fullscreen: { enabled: fullscreenMapEnabled },
70-
},
71-
})
59+
refreshControlAnchorTitle()
7260
},
7361
})
7462
}

0 commit comments

Comments
 (0)