Skip to content

Commit f1dc02e

Browse files
committed
Hide paths while pressing the "h" key or press a small icon next to the map attribution
1 parent 3d1ff27 commit f1dc02e

File tree

6 files changed

+96
-10
lines changed

6 files changed

+96
-10
lines changed

src/App.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ function LargeScreenLayout({ query, route, map, error, mapOptions, encodedValues
219219
<LocationButton queryPoints={query.queryPoints} />
220220
</div>
221221
<div className={styles.map}>
222-
<MapComponent map={map} />
222+
<MapComponent map={map} paths={route.routingResult.paths}
223+
selectedPath={route.selectedPath}
224+
queryPoints={query.queryPoints}
225+
smallScreenRoutingResultVisible={false}/>
223226
</div>
224227

225228
<div className={styles.pathDetails}>
@@ -243,7 +246,10 @@ function SmallScreenLayout({ query, route, map, error, mapOptions, encodedValues
243246
/>
244247
</div>
245248
<div className={styles.smallScreenMap}>
246-
<MapComponent map={map} />
249+
<MapComponent map={map} paths={route.routingResult.paths}
250+
selectedPath={route.selectedPath}
251+
queryPoints={query.queryPoints}
252+
smallScreenRoutingResultVisible={true}/>
247253
</div>
248254
<div className={styles.smallScreenMapOptions}>
249255
<div className={styles.onMapRightSide}>

src/layers/UsePathsLayer.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ export default function usePathsLayer(map: Map, paths: Path[], selectedPath: Pat
3535
}, [map, paths, selectedPath])
3636
}
3737

38-
function removeCurrentPathLayers(map: Map) {
38+
export function removeCurrentPathLayers(map: Map) {
3939
map.getLayers()
4040
.getArray()
4141
.filter(l => l.get(pathsLayerKey) || l.get(selectedPathLayerKey) || l.get(accessNetworkLayerKey))
4242
.forEach(l => map.removeLayer(l))
4343
}
4444

45-
function addUnselectedPathsLayer(map: Map, paths: Path[]) {
45+
export function addUnselectedPathsLayer(map: Map, paths: Path[]) {
4646
const styleArray = [
4747
new Style({
4848
stroke: new Stroke({
@@ -118,7 +118,7 @@ function createBezierLineString(start: number[], end: number[]): LineString {
118118
return new LineString(bezierPoints)
119119
}
120120

121-
function addAccessNetworkLayer(map: Map, selectedPath: Path, queryPoints: QueryPoint[]) {
121+
export function addAccessNetworkLayer(map: Map, selectedPath: Path, queryPoints: QueryPoint[]) {
122122
const style = new Style({
123123
stroke: new Stroke({
124124
color: 'rgba(143,183,241,0.9)',
@@ -143,7 +143,7 @@ function addAccessNetworkLayer(map: Map, selectedPath: Path, queryPoints: QueryP
143143
map.addLayer(layer)
144144
}
145145

146-
function addSelectedPathsLayer(map: Map, selectedPath: Path) {
146+
export function addSelectedPathsLayer(map: Map, selectedPath: Path) {
147147
const styleArray = [
148148
new Style({
149149
stroke: new Stroke({

src/map/Map.module.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,27 @@
4141
.customAttribution button {
4242
display: none;
4343
}
44+
45+
.topBar {
46+
position: absolute;
47+
left: 0;
48+
bottom: -0.3rem;
49+
width: 100%;
50+
z-index: 1;
51+
padding: 1px 180px;
52+
}
53+
54+
.hidePathsButton {
55+
display: inline-block;
56+
background: none;
57+
border: none;
58+
font-size: 13px;
59+
padding: 8px 16px;
60+
margin: 0;
61+
}
62+
63+
@media (max-width: 44rem) {
64+
.smallScreenRoutingResultVisible .topBar {
65+
bottom: 8.25rem;
66+
}
67+
}

src/map/MapComponent.tsx

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,78 @@
11
import 'ol/ol.css'
22
import styles from '@/map/Map.module.css'
3-
import { useEffect, useRef } from 'react'
3+
import { useEffect, useRef, useState } from 'react'
44
import { Map } from 'ol'
55
import { Bbox } from '@/api/graphhopper'
66
import Dispatcher from '@/stores/Dispatcher'
77
import { ErrorAction } from '@/actions/Actions'
88
import { tr } from '@/translation/Translation'
99
import { Coordinate, getBBoxFromCoord } from '@/utils'
10+
import { addUnselectedPathsLayer, addSelectedPathsLayer, addAccessNetworkLayer, removeCurrentPathLayers } from '@/layers/UsePathsLayer'
11+
import { Path } from '@/api/graphhopper'
12+
import { QueryPoint } from '@/stores/QueryStore'
13+
import VisibilityOnIcon from '@/sidebar/visibility_on.svg'
14+
import VisibilityOffIcon from '@/sidebar/visibility_off.svg'
1015

1116
type MapComponentProps = {
12-
map: Map
17+
map: Map,
18+
paths: Path[],
19+
selectedPath: Path,
20+
queryPoints: QueryPoint[]
1321
}
1422

1523
/** A small react component that simply attaches our map instance to a div to show the map **/
16-
export default function ({ map }: MapComponentProps) {
24+
export default function ({
25+
map, paths, selectedPath, queryPoints, smallScreenRoutingResultVisible
26+
}: MapComponentProps & { smallScreenRoutingResultVisible?: boolean }) {
1727
const mapElement = useRef<HTMLDivElement | null>(null)
1828
useEffect(() => {
1929
map.setTarget(mapElement.current!)
2030
}, [map])
21-
return <div ref={mapElement} className={styles.mapContainer} />
31+
const [showPaths, setShowPaths] = useState(true)
32+
useEffect(() => {
33+
const handleKeyDown = (e: KeyboardEvent) => {
34+
if (e.key === 'h') setShowPaths(false)
35+
}
36+
const handleKeyUp = (e: KeyboardEvent) => {
37+
if (e.key === 'h') setShowPaths(true)
38+
}
39+
window.addEventListener('keydown', handleKeyDown)
40+
window.addEventListener('keyup', handleKeyUp)
41+
return () => {
42+
window.removeEventListener('keydown', handleKeyDown)
43+
window.removeEventListener('keyup', handleKeyUp)
44+
}
45+
}, [])
46+
useEffect(() => {
47+
removeCurrentPathLayers(map)
48+
if (showPaths) {
49+
addUnselectedPathsLayer(map, paths.filter(p => p != selectedPath))
50+
addSelectedPathsLayer(map, selectedPath)
51+
addAccessNetworkLayer(map, selectedPath, queryPoints)
52+
}
53+
return () => {
54+
removeCurrentPathLayers(map)
55+
}
56+
}, [map, paths, selectedPath, showPaths])
57+
return (
58+
<div
59+
ref={mapElement}
60+
className={
61+
styles.mapContainer +
62+
(smallScreenRoutingResultVisible ? ' ' + styles.smallScreenRoutingResultVisible : '')
63+
}
64+
>
65+
<div className={styles.topBar}>
66+
<button
67+
className={styles.hidePathsButton}
68+
onClick={() => setShowPaths(v => !v)}
69+
title={showPaths ? tr('hide_route') : tr('show_route')}
70+
>
71+
{showPaths ? <VisibilityOffIcon width={20} height={20} /> : <VisibilityOnIcon width={20} height={20} />}
72+
</button>
73+
</div>
74+
</div>
75+
)
2276
}
2377

2478
export function onCurrentLocationSelected(

src/sidebar/visibility_off.svg

Lines changed: 1 addition & 0 deletions
Loading

src/sidebar/visibility_on.svg

Lines changed: 1 addition & 0 deletions
Loading

0 commit comments

Comments
 (0)