]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/components/SearchSectionReverse.svelte
fix Svelte accessibily linter warnings, no more autofocus
[nominatim-ui.git] / src / components / SearchSectionReverse.svelte
1 <script>
2   import UrlSubmitForm from '../components/UrlSubmitForm.svelte';
3
4   import { zoomLevels } from '../lib/helpers.js';
5   import { map_store, refresh_page } from '../lib/stores.js';
6
7   export let lat = '';
8   export let lon = '';
9   export let zoom = '';
10
11   function gotoCoordinates(newlat, newlon, newzoom) {
12     if (newlat === null || newlon === null) return;
13
14     let params = new URLSearchParams();
15     params.set('lat', newlat);
16     params.set('lon', newlon);
17     params.set('zoom', newzoom || zoom);
18     refresh_page('reverse', params);
19   }
20
21   map_store.subscribe(map => {
22     if (map) {
23       map.on('click', (e) => {
24         let coords = e.latlng.wrap();
25         gotoCoordinates(coords.lat.toFixed(5), coords.lng.toFixed(5));
26       });
27     }
28   });
29
30   // common mistake is to copy&paste latitude and longitude into the 'lat' search box
31   function maybeSplitLatitude(e) {
32     var coords_split = e.target.value.split(/,|%2C/);
33     if (coords_split.length === 2) {
34       document.querySelector('input[name=lat]').value = L.Util.trim(coords_split[0]);
35       document.querySelector('input[name=lon]').value = L.Util.trim(coords_split[1]);
36     }
37   }
38
39 </script>
40
41 <UrlSubmitForm page="reverse">
42   <div class="col-auto">
43     <label for="reverse-lat">lat</label>
44   </div>
45   <div class="col-auto">
46     <input id="reverse-lat"
47            name="lat"
48            type="text"
49            class="form-control form-control-sm d-inline"
50            placeholder="latitude"
51            pattern="^-?\d+(\.\d+)?$"
52            bind:value={lat}
53            on:change={maybeSplitLatitude} />
54   </div>
55   <div class="col-auto">
56     <button id="switch-coords"
57        on:click|preventDefault|stopPropagation={() => gotoCoordinates(lon, lat)}
58        class="btn btn-outline-secondary btn-sm"
59        title="switch lat and lon">&lt;&gt;</button>
60   </div>
61   <div class="col-auto">
62     <label for="reverse-lon">lon</label>
63   </div>
64   <div class="col-auto">
65     <input id="reverse-lon"
66            name="lon"
67            type="text"
68            class="form-control form-control-sm"
69            placeholder="longitude"
70            pattern="^-?\d+(\.\d+)?$"
71            bind:value={lon} />
72   </div>
73   <div class="col-auto">
74     <label for="reverse-zoom">max zoom</label>
75   </div>
76   <div class="col-auto">
77     <select id="reverse-zoom" name="zoom" class="form-select form-select-sm" bind:value={zoom}>
78       <option value="">---</option>
79       {#each zoomLevels() as zoomTitle, i}
80         <option value="{i}">{i} - {zoomTitle}</option>
81       {/each}
82     </select>
83   </div>
84   <div class="col-auto">
85     <button type="submit" class="btn btn-primary btn-sm mx-1">Search</button>
86   </div>
87 </UrlSubmitForm>
88
89 <style>
90   label {
91     font-size: 0.9rem;
92     margin-top: 0.3rem;
93   }
94
95   #switch-coords {
96     font-size: 0.6rem;
97     font-weight: bold;
98     cursor: pointer;
99     padding: 2px;
100     margin: 5px;
101   }
102
103   @media (max-width: 850px) {
104     #reverse-lon, #reverse-lat, #reverse-zoom {
105       width: 8em;
106     }
107   }
108 </style>