From 0feb092f5e5dc9b2763e07af85cd8481b8b7e111 Mon Sep 17 00:00:00 2001 From: Andreas Stassivik Date: Wed, 13 Nov 2024 19:01:01 -0800 Subject: [PATCH] `controlAddedListener` submodule extraction --- .../control-added-listener.js | 30 +++++++++++++++++++ public/leaflet-fullscreen/fullscreen-map.js | 29 +++++------------- 2 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 public/leaflet-fullscreen/control-added-listener.js diff --git a/public/leaflet-fullscreen/control-added-listener.js b/public/leaflet-fullscreen/control-added-listener.js new file mode 100644 index 0000000..09bd9b5 --- /dev/null +++ b/public/leaflet-fullscreen/control-added-listener.js @@ -0,0 +1,30 @@ +import { DomEvent } from '../leaflet/leaflet-src.esm.js' + +import { setControlTitle } from './set-control-title.js' + +export function controlAddedListener({ + container, + getFullscreenState, + linkAssign, + onLinkClick, + title, +}) { + return function handleControlAdded(map) { + setControlTitle({ + fullscreen: getFullscreenState(), + linkAssign, + title, + }) + + onLinkClick(async function handleLinkClick(e) { + DomEvent.stopPropagation(e) + DomEvent.preventDefault(e) + + await (getFullscreenState() + ? document?.exitFullscreen() + : map.getContainer()?.requestFullscreen()) + }) + + return container + } +} diff --git a/public/leaflet-fullscreen/fullscreen-map.js b/public/leaflet-fullscreen/fullscreen-map.js index dc08026..2f97d84 100644 --- a/public/leaflet-fullscreen/fullscreen-map.js +++ b/public/leaflet-fullscreen/fullscreen-map.js @@ -1,13 +1,12 @@ import { control as leafletControl, - DomEvent, DomUtil, map as leafletMap, } from '../leaflet/leaflet-src.esm.js' +import { controlAddedListener } from './control-added-listener.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' @@ -61,25 +60,13 @@ export function fullscreenMap({ linkAssign({ href: '#' }) - control.onAdd = function handleControlAdded(addedMap) { - setControlTitle({ - fullscreen: getFullscreenState(), - linkAssign, - title, - }) - - onLinkClick(async function handleLinkClick(e) { - DomEvent.stopPropagation(e) - DomEvent.preventDefault(e) - - await (getFullscreenState() - ? document?.exitFullscreen() - : addedMap.getContainer()?.requestFullscreen()) - }) - - return container - } - + control.onAdd = controlAddedListener({ + container, + getFullscreenState, + linkAssign, + onLinkClick, + title, + }) control.addTo(map) map.whenReady(handleMapLifecycleChange(true))