-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from Stassi/feature/leaflet-fullscreen-upgrade…
…-mjs feature/leaflet-fullscreen-upgrade-mjs
- Loading branch information
Showing
12 changed files
with
285 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
public/leaflet-fullscreen/control/control-added-listener.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { DomEvent } from '../../leaflet/leaflet-src.esm.js' | ||
|
||
import { setControlTitle } from './set-control-title.js' | ||
|
||
export function controlAddedListener({ | ||
container, | ||
getFullscreenState, | ||
onLinkClick, | ||
...titleOptions | ||
}) { | ||
return function handleControlAdded(map) { | ||
setControlTitle({ | ||
fullscreen: getFullscreenState(), | ||
...titleOptions, | ||
}) | ||
|
||
onLinkClick(async function handleLinkClick(e) { | ||
DomEvent.stopPropagation(e) | ||
DomEvent.preventDefault(e) | ||
|
||
await (getFullscreenState() | ||
? document?.exitFullscreen() | ||
: map.getContainer()?.requestFullscreen()) | ||
}) | ||
|
||
return container | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function setControlTitle({ fullscreen, linkAssign, title }) { | ||
linkAssign({ title: title[fullscreen] }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function joinClassNames(classNames) { | ||
return classNames.join(' ') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { | ||
control as leafletControl, | ||
DomUtil, | ||
map as leafletMap, | ||
} from '../leaflet/leaflet-src.esm.js' | ||
|
||
import { controlAddedListener } from './control/control-added-listener.js' | ||
import { joinClassNames } from './dom-node-class/join-class-names.js' | ||
import { mapLifecycleListener } from './map/map-lifecycle-listener.js' | ||
import { useBoolean } from './state/use-boolean.js' | ||
import { useLink } from './state/use-link.js' | ||
|
||
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 | ||
}) { | ||
const { get: getFullscreenState, toggle: toggleFullscreenState } = | ||
useBoolean(false), | ||
container = DomUtil.create('div', joinClassNames(containerClassNames)), | ||
{ assign: linkAssign, onClick: onLinkClick } = useLink({ | ||
element: DomUtil.create('a', joinClassNames(linkClassNames), container), | ||
initialProps: { href: '#' }, | ||
}), | ||
control = leafletControl({ position }), | ||
map = leafletMap(id, mapOptions), | ||
handleMapLifecycleChange = (documentFirstReady) => | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { DomEvent, DomUtil } from '../../leaflet/leaflet-src.esm.js' | ||
import { setControlTitle } from '../control/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, | ||
}) | ||
}, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function useBoolean(initialValue) { | ||
let state = initialValue | ||
return { | ||
get() { | ||
return state | ||
}, | ||
toggle() { | ||
state = !state | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DomEvent } from '../../leaflet/leaflet-src.esm.js' | ||
|
||
export function useLink({ element, initialProps }) { | ||
function assign(props) { | ||
Object.assign(element, props) | ||
} | ||
|
||
assign(initialProps) | ||
|
||
return { | ||
assign, | ||
onClick(handler) { | ||
DomEvent.on(element, 'click', handler) | ||
}, | ||
} | ||
} |
Oops, something went wrong.