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