|
1 | 1 | <script lang="ts">
|
2 |
| - import type { FeatureCollection } from "geojson"; |
| 2 | + import type { Feature, FeatureCollection } from "geojson"; |
3 | 3 | import { onDestroy, onMount } from "svelte";
|
4 | 4 | import { GeoJSON, LineLayer } from "svelte-maplibre";
|
5 | 5 | import { notNull, Popup } from "./common";
|
|
14 | 14 | }
|
15 | 15 | | {
|
16 | 16 | state: "chose-road";
|
17 |
| - road: number; |
| 17 | + roadGj: Feature; |
18 | 18 | gj: FeatureCollection;
|
19 |
| - shortcutIndex: number | null; |
| 19 | + shortcutIndex: number; |
20 | 20 | };
|
21 | 21 | let state: State = { state: "neutral" };
|
22 | 22 |
|
23 |
| - function choseRoad(road: number) { |
24 |
| - let gj = JSON.parse($app!.getShortcutsCrossingRoad(road)); |
| 23 | + function choseRoad(roadGj: Feature) { |
| 24 | + let gj = JSON.parse($app!.getShortcutsCrossingRoad(roadGj.properties!.id)); |
25 | 25 | if (gj.features.length == 0) {
|
26 | 26 | window.alert("No shortcuts here");
|
27 | 27 | return;
|
28 | 28 | }
|
29 | 29 |
|
30 | 30 | state = {
|
31 | 31 | state: "chose-road",
|
32 |
| - road, |
| 32 | + roadGj, |
33 | 33 | gj,
|
34 |
| - shortcutIndex: null, |
| 34 | + shortcutIndex: 0, |
35 | 35 | };
|
36 | 36 | }
|
37 | 37 |
|
|
44 | 44 |
|
45 | 45 | function onKeyDown(e: KeyboardEvent) {
|
46 | 46 | if (state.state == "chose-road") {
|
47 |
| - if (e.key == "ArrowLeft" && state.shortcutIndex) { |
| 47 | + if (e.key == "ArrowLeft" && state.shortcutIndex > 0) { |
48 | 48 | e.stopPropagation();
|
49 | 49 | state.shortcutIndex--;
|
50 | 50 | }
|
51 | 51 | if (e.key == "ArrowRight") {
|
52 | 52 | e.stopPropagation();
|
53 |
| - if (state.shortcutIndex == null) { |
54 |
| - state.shortcutIndex = 0; |
55 |
| - } else if (state.shortcutIndex != state.gj.features.length - 1) { |
| 53 | + if (state.shortcutIndex != state.gj.features.length - 1) { |
56 | 54 | state.shortcutIndex++;
|
57 | 55 | }
|
58 | 56 | }
|
|
69 | 67 |
|
70 | 68 | function prev() {
|
71 | 69 | if (state.state == "chose-road") {
|
72 |
| - state.shortcutIndex!--; |
| 70 | + state.shortcutIndex--; |
73 | 71 | }
|
74 | 72 | }
|
75 | 73 |
|
76 | 74 | function next() {
|
77 | 75 | if (state.state == "chose-road") {
|
78 |
| - state.shortcutIndex = |
79 |
| - state.shortcutIndex == null ? 0 : state.shortcutIndex + 1; |
| 76 | + state.shortcutIndex++; |
80 | 77 | }
|
81 | 78 | }
|
82 | 79 | </script>
|
|
91 | 88 | <p>Click a road to see shortcuts</p>
|
92 | 89 | {:else if state.state == "chose-road"}
|
93 | 90 | <div>
|
94 |
| - <button |
95 |
| - disabled={state.shortcutIndex == null || state.shortcutIndex == 0} |
96 |
| - on:click={prev} |
| 91 | + <button on:click={() => (state = { state: "neutral" })} |
| 92 | + >Pick a different road</button |
97 | 93 | >
|
| 94 | + </div> |
| 95 | + <div> |
| 96 | + <button disabled={state.shortcutIndex == 0} on:click={prev}> |
98 | 97 | Prev
|
99 | 98 | </button>
|
100 |
| - {state.shortcutIndex} / {state.gj.features.length} |
| 99 | + {state.shortcutIndex + 1} / {state.gj.features.length} |
101 | 100 | <button
|
102 | 101 | disabled={state.shortcutIndex == state.gj.features.length - 1}
|
103 | 102 | on:click={next}
|
|
112 | 111 | {#if state.state == "neutral"}
|
113 | 112 | <RenderNeighbourhood
|
114 | 113 | gjInput={JSON.parse(notNull($app).renderNeighbourhood())}
|
115 |
| - onClickLine={(f) => choseRoad(notNull(f.properties).id)} |
| 114 | + onClickLine={choseRoad} |
116 | 115 | >
|
117 | 116 | <div slot="line-popup">
|
118 |
| - <Popup openOn="hover" let:props>{props.shortcuts}</Popup> |
| 117 | + <Popup openOn="hover" let:props> |
| 118 | + <p> |
| 119 | + {props.shortcuts} shortcuts through {props.name ?? "unnamed road"} |
| 120 | + </p> |
| 121 | + </Popup> |
119 | 122 | </div>
|
120 | 123 | </RenderNeighbourhood>
|
121 | 124 | {:else if state.state == "chose-road"}
|
122 |
| - {#if state.shortcutIndex == null} |
123 |
| - <GeoJSON data={state.gj}> |
124 |
| - <LineLayer |
125 |
| - paint={{ |
126 |
| - "line-width": 5, |
127 |
| - "line-color": "red", |
128 |
| - }} |
129 |
| - /> |
130 |
| - </GeoJSON> |
131 |
| - {:else} |
132 |
| - <GeoJSON data={state.gj.features[state.shortcutIndex]}> |
133 |
| - <LineLayer |
134 |
| - paint={{ |
135 |
| - "line-width": 5, |
136 |
| - "line-color": "red", |
137 |
| - }} |
138 |
| - /> |
139 |
| - </GeoJSON> |
140 |
| - {/if} |
| 125 | + <GeoJSON data={state.gj.features[state.shortcutIndex]}> |
| 126 | + <LineLayer |
| 127 | + paint={{ |
| 128 | + "line-width": 5, |
| 129 | + "line-color": "red", |
| 130 | + }} |
| 131 | + /> |
| 132 | + </GeoJSON> |
| 133 | + <GeoJSON data={state.roadGj}> |
| 134 | + <LineLayer |
| 135 | + paint={{ |
| 136 | + "line-width": 5, |
| 137 | + "line-color": "blue", |
| 138 | + }} |
| 139 | + /> |
| 140 | + </GeoJSON> |
141 | 141 | {/if}
|
142 | 142 | <ModalFilterLayer />
|
143 | 143 | </div>
|
|
0 commit comments