]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/components/SearchSection.svelte
Bump semver from 6.3.0 to 6.3.1
[nominatim-ui.git] / src / components / SearchSection.svelte
1 <script>
2   import UrlSubmitForm from '../components/UrlSubmitForm.svelte';
3
4   import { map_store } from '../lib/stores.js';
5   import { get } from 'svelte/store';
6
7   export let bStructuredSearch = false;
8   export let api_request_params = {};
9   let sViewBox;
10   // lat,lon are later set in update_reverse_link()
11   let lat; // eslint-disable-line no-unused-vars
12   let lon; // eslint-disable-line no-unused-vars
13
14   function map_viewbox_as_string(map) {
15     var bounds = map.getBounds();
16     var west = bounds.getWest();
17     var east = bounds.getEast();
18
19     if ((east - west) >= 360) { // covers more than whole planet
20       west = map.getCenter().lng - 179.999;
21       east = map.getCenter().lng + 179.999;
22     }
23     east = L.latLng(77, east).wrap().lng;
24     west = L.latLng(77, west).wrap().lng;
25
26     return [
27       west.toFixed(5), // left
28       bounds.getNorth().toFixed(5), // top
29       east.toFixed(5), // right
30       bounds.getSouth().toFixed(5) // bottom
31     ].join(',');
32   }
33
34   function set_viewbox(map) {
35     let use_viewbox = document.getElementById('use_viewbox');
36     if (use_viewbox && use_viewbox.checked) {
37       sViewBox = map_viewbox_as_string(map);
38     } else {
39       sViewBox = '';
40     }
41   }
42
43   function update_reverse_link(map) {
44     let center_lat_lng = map.wrapLatLng(map.getCenter());
45     lat = center_lat_lng.lat.toFixed(5);
46     lon = center_lat_lng.lng.toFixed(5);
47   }
48
49   map_store.subscribe(map => {
50     if (!map) { return; }
51
52     map.on('move', function () {
53       set_viewbox(map);
54       update_reverse_link(map);
55     });
56
57     map.on('load', function () {
58       set_viewbox(map);
59       update_reverse_link(map);
60     });
61   });
62
63   function reset_viewbox() {
64     let map = get(map_store);
65     if (map) { set_viewbox(map); }
66   }
67
68   function set_bounded(e) {
69     document.querySelector('input[name=bounded]').value = e.target.checked ? 1 : '';
70   }
71
72   function set_dedupe(e) {
73     document.querySelector('input[name=dedupe]').value = e.target.checked ? 1 : 0;
74   }
75
76   function set_api_param(e) {
77     document.querySelector('input[name=' + e.target.dataset.apiParam + ']').value = e.target.value;
78   }
79 </script>
80
81 <ul class="nav nav-tabs">
82   <li class="nav-item">
83     <a class="nav-link" class:active={!bStructuredSearch} data-bs-toggle="tab" href="#simple">
84       Simple
85     </a>
86   </li>
87   <li class="nav-item">
88     <a class="nav-link" class:active={bStructuredSearch} data-bs-toggle="tab" href="#structured">
89       Structured
90     </a>
91   </li>
92 </ul>
93
94 <div class="tab-content py-2">
95   <div class="tab-pane" class:active={!bStructuredSearch} id="simple" role="tabpanel">
96     <UrlSubmitForm page="search">
97       <div class="col-auto">
98         <input id="q"
99                name="q"
100                type="text"
101                class="form-control form-control-sm"
102                placeholder="Search"
103                value="{api_request_params.q || ''}" />
104       </div>
105       <div class="col-auto">
106         <button type="submit" class="btn btn-primary btn-sm mx-1">Search</button>
107         <input type="hidden"
108                name="viewbox" value="{sViewBox || ''}" />
109         <input type="hidden"
110                name="dedupe" value="{api_request_params.dedupe === 0 ? 0 : 1}" />
111         <input type="hidden"
112                name="bounded" value="{api_request_params.bounded ? 1 : ''}" />
113         <input type="hidden"
114                name="accept-language"value="{api_request_params['accept-language'] || ''}" />
115         <input type="hidden"
116                name="countrycodes" value="{api_request_params.countrycodes || ''}"
117                                    pattern="^[a-zA-Z]{'{2}'}(,[a-zA-Z]{'{2}'})*$" />
118         <input type="hidden"
119                name="limit" value="{api_request_params.limit || ''}" />
120         <input type="hidden"
121                name="polygon_threshold" value="{api_request_params.polygon_threshold || ''}" />
122       </div>
123     </UrlSubmitForm>
124   </div>
125   <div class="tab-pane" class:active={bStructuredSearch} id="structured" role="tabpanel">
126     <UrlSubmitForm page="search">
127       <div class="col-auto">
128         <input name="street" type="text" class="form-control form-control-sm me-1"
129                placeholder="House number/Street"
130                value="{api_request_params.street || ''}" />
131       </div>
132       <div class="col-auto">
133         <input name="city" type="text" class="form-control form-control-sm me-1"
134                placeholder="City"
135                value="{api_request_params.city || ''}" />
136       </div>
137       <div class="col-auto">
138         <input id="county" name="county" type="text" class="form-control form-control-sm me-1"
139                placeholder="County"
140                value="{api_request_params.county || ''}" />
141       </div>
142       <div class="col-auto">
143         <input name="state" type="text" class="form-control form-control-sm me-1"
144                placeholder="State"
145                value="{api_request_params.state || ''}" />
146       </div>
147       <div class="col-auto">
148         <input name="country" type="text" class="form-control form-control-sm me-1"
149                placeholder="Country"
150                value="{api_request_params.country || ''}" />
151       </div>
152       <div class="col-auto">
153         <input name="postalcode" type="text" class="form-control form-control-sm me-1"
154                placeholder="Postal Code"
155                value="{api_request_params.postalcode || ''}" />
156       </div>
157       <div class="col-auto">
158         <button type="submit" class="btn btn-primary btn-sm">Search</button>
159         <input type="hidden"
160                name="viewbox" value="{sViewBox || ''}" />
161         <input type="hidden"
162                name="dedupe" value="{api_request_params.dedupe === 0 ? 0 : 1}" />
163         <input type="hidden"
164                name="bounded" value="{api_request_params.bounded ? 1 : ''}" />
165         <input type="hidden"
166                name="accept-language" value="{api_request_params['accept-language'] || ''}" />
167         <input type="hidden"
168                name="countrycodes" value="{api_request_params.countrycodes || ''}"
169                                    pattern="^[a-zA-Z]{'{2}'}(,[a-zA-Z]{'{2}'})*$" />
170         <input type="hidden"
171                name="limit" value="{api_request_params.limit || ''}" />
172         <input type="hidden"
173                name="polygon_threshold" value="{api_request_params.polygon_threshold || ''}" />
174       </div>
175     </UrlSubmitForm>
176   </div>
177 </div> <!-- /tab-content -->
178
179 <!-- Additional options -->
180 <details id="searchAdvancedOptions">
181   <summary><small>Advanced options</small></summary>
182   <ul>
183     <li>
184       <div class="form-check form-check-inline">
185         <label class="form-check-label" for="use_viewbox">apply viewbox</label>
186         <input type="checkbox" class="form-check-input api-param-setting"
187                id="use_viewbox" checked={api_request_params.viewbox} on:change={reset_viewbox}>
188       </div>
189     </li>
190
191     <li>
192       <div class="form-check form-check-inline">
193         <label class="form-check-label" for="option_bounded">bounded to viewbox</label>
194         <input type="checkbox" class="form-check-input api-param-setting"
195                id="option_bounded" checked={!!api_request_params.bounded} on:change={set_bounded}>
196       </div>
197     </li>
198
199     <li>
200       <div class="form-check form-check-inline">
201         <label class="form-check-label" for="option_dedupe">deduplicate results</label>
202         <input type="checkbox"
203                class="form-check-input api-param-setting"
204                id="option_dedupe"
205                checked={api_request_params.dedupe === 0 ? 0 : 1}
206                on:change={set_dedupe}>
207       </div>
208     </li>
209
210     <li>
211       <label for="option_limit">Maximum number of results</label>
212       <input type="number"
213              class="form-control form-control-sm d-inline w-auto api-param-setting"
214              data-api-param="limit" id="option_limit" min="1" max="50"
215              value="{api_request_params.limit || ''}"
216              on:change={set_api_param}>
217     </li>
218
219     <li>
220       <label for="option_polygon_threshold">Polygon simplification</label>
221       <input type="number"
222              class="form-control form-control-sm d-inline w-auto api-param-setting"
223              data-api-param="polygon_threshold" id="option_polygon_threshold"
224              min="0.0" max="1.0" step="0.001"
225              value="{api_request_params.polygon_threshold || ''}"
226              on:change={set_api_param}>
227     </li>
228
229     <li>
230       <label for="accept_lang">Languages</label>
231       <input type="text" placeholder="e.g. en,zh-Hant"
232              class="form-control form-control-sm d-inline w-auto api-param-setting"
233              data-api-param="accept-language" id="accept_lang" size="15"
234              value="{api_request_params['accept-language'] || ''}"
235              on:change={set_api_param}>
236     </li>
237
238     <li>
239       <label for="option_ccode">Country Codes</label>
240       <input type="text" placeholder="e.g. de,gb"
241             class="form-control form-control-sm d-inline w-auto api-param-setting"
242              data-api-param="countrycodes" id="option_ccode" size="15"
243              value="{api_request_params.countrycodes || ''}"
244              pattern="^[a-zA-Z]{'{2}'}(,[a-zA-Z]{'{2}'})*$"
245              on:change={set_api_param}>
246     </li>
247   </ul>
248 </details>
249
250 <style>
251   .nav-tabs {
252     font-size: 0.8em;
253     margin-top: -1em;
254   }
255
256   .nav-link {
257     padding: 0.1rem 1rem;
258   }
259
260   #q {
261     width: 500px;
262     max-width: 100%;
263   }
264
265   #searchAdvancedOptions ul {
266     list-style-type: none;
267     padding: 0;
268     font-size: 0.85rem;
269   }
270
271   #searchAdvancedOptions li {
272     display: inline-block;
273     padding: 4px 10px;
274     border-radius: 5px;
275     border: 1px dotted #ccc;
276     margin-right: 1em;
277   }
278
279   #searchAdvancedOptions label {
280     margin-right: 0.5em;
281   }
282
283 </style>