Skip to content

Commit

Permalink
mapLifecycleListener submodule extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Stassi committed Nov 14, 2024
1 parent 3d25705 commit 9554e83
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
40 changes: 14 additions & 26 deletions public/leaflet-fullscreen/fullscreen-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../leaflet/leaflet-src.esm.js'

import { joinClassNames } from './join-class-names.js'
import { mapLifecycleListener } from './map-lifecycle-listener.js'
import { setControlTitle } from './set-control-title.js'
import { useBoolean } from './use-boolean.js'
import { useLink } from './use-link.js'
Expand Down Expand Up @@ -46,30 +47,17 @@ export function fullscreenMap({
DomUtil.create('a', joinClassNames(linkClassNames), container),
),
control = leafletControl({ position }),
map = leafletMap(id, mapOptions)

function mapLifecycleListener(documentFirstReady) {
return function handleFullscreenMapLifecycleEvent() {
DomEvent[documentFirstReady ? 'on' : 'off'](
document,
'fullscreenchange',
function handleFullscreenMapChange() {
;(getFullscreenState() ? DomUtil.removeClass : DomUtil.addClass)(
map.getContainer(),
mapFullscreenClassName,
)

map.invalidateSize()
toggleFullscreenState()
setControlTitle({
fullscreen: getFullscreenState(),
linkAssign,
title,
})
},
)
}
}
map = leafletMap(id, mapOptions),
handleMapLifecycleChange = (documentFirstReady) =>
mapLifecycleListener({
documentFirstReady,
getFullscreenState,
linkAssign,
map,
mapFullscreenClassName,
title,
toggleFullscreenState,
})

linkAssign({ href: '#' })

Expand All @@ -94,8 +82,8 @@ export function fullscreenMap({

control.addTo(map)

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

return map
}
34 changes: 34 additions & 0 deletions public/leaflet-fullscreen/map-lifecycle-listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { DomEvent, DomUtil } from '../leaflet/leaflet-src.esm.js'

import { setControlTitle } from './set-control-title.js'

export function mapLifecycleListener({
documentFirstReady,
getFullscreenState,
linkAssign,
map,
mapFullscreenClassName,
title,
toggleFullscreenState,
}) {
return function handleFullscreenMapLifecycleEvent() {
DomEvent[documentFirstReady ? 'on' : 'off'](
document,
'fullscreenchange',
function handleFullscreenMapChange() {
;(getFullscreenState() ? DomUtil.removeClass : DomUtil.addClass)(
map.getContainer(),
mapFullscreenClassName,
)

map.invalidateSize()
toggleFullscreenState()
setControlTitle({
fullscreen: getFullscreenState(),
linkAssign,
title,
})
},
)
}
}

0 comments on commit 9554e83

Please sign in to comment.