Skip to content

Commit

Permalink
fullscreenMap implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stassi committed Nov 15, 2024
1 parent 3a1e548 commit 388af68
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/leaf/map/fullscreen/fullscreen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
DomUtil,
control as untypedLeafletControl,
map as leafletMap,
type Control,
type ControlOptions,
type ControlPosition,
type Map,
type MapOptions,
} from 'leaflet'

import { controlAddedListener } from './control/control-added-listener'
import { joinClassNames } from './dom-node-class/join-class-names'
import { mapLifecycleListener } from './map/map-lifecycle-listener'
import { useBoolean } from './state/use-boolean'
import { type UseLink, useLink } from './state/use-link'

const leafletControl = <(options: ControlOptions) => Control>(
(<unknown>untypedLeafletControl)
)

export type FullscreenMapOptions = MapOptions & {
id: string
} & Partial<{
fullscreenOptions: {
classNames: Record<'container' | 'link', string[]> & {
mapFullscreen: string
}
control: {
position: ControlPosition
title: Record<'false' | 'true', string>
}
}
}>

export function fullscreenMap({
fullscreenOptions: {
classNames: {
container: containerClassNames,
link: linkClassNames,
mapFullscreen: mapFullscreenClassName,
},
control: { position, title },
} = {
classNames: {
container: [
'leaflet-bar',
'leaflet-control',
'leaflet-control-fullscreen',
],
link: ['leaflet-bar-part', 'leaflet-control-fullscreen-button'],
mapFullscreen: 'leaflet-fullscreen-on',
},
control: {
position: 'topleft',
title: {
false: 'View Fullscreen',
true: 'Exit Fullscreen',
},
},
},
id,
...mapOptions
}: FullscreenMapOptions): Map {
const { get: getFullscreenState, toggle: toggleFullscreenState } =
useBoolean(false),
container: HTMLDivElement = DomUtil.create(
'div',
joinClassNames(containerClassNames),
),
{ assign: linkAssign, onClick: onLinkClick }: UseLink = useLink({
element: DomUtil.create('a', joinClassNames(linkClassNames), container),
initialProps: { href: '#' },
}),
control: Control = leafletControl({ position }),
map: Map = leafletMap(id, mapOptions),
handleMapLifecycleChange: (documentFirstReady: boolean) => () => void = (
documentFirstReady: boolean,
): (() => void) =>
mapLifecycleListener({
documentFirstReady,
getFullscreenState,
linkAssign,
map,
mapFullscreenClassName,
title,
toggleFullscreenState,
})

control.onAdd = controlAddedListener({
container,
getFullscreenState,
linkAssign,
onLinkClick,
title,
})
control.addTo(map)

map.whenReady(handleMapLifecycleChange(true))
map.on('unload', handleMapLifecycleChange(false))

return map
}

0 comments on commit 388af68

Please sign in to comment.