]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/components/ResultsList.svelte
Svelte5: runes, events and @render context (#289)
[nominatim-ui.git] / src / components / ResultsList.svelte
1 <script>
2   import { results_store } from '../lib/stores.js';
3   import { formatLabel } from '../lib/helpers.js';
4   import { SvelteURLSearchParams } from 'svelte/reactivity';
5
6   import DetailsLink from './DetailsLink.svelte';
7   import Welcome from './Welcome.svelte';
8   import MapIcon from './MapIcon.svelte';
9
10   let { reverse_search = false, current_result = $bindable() } = $props();
11
12   let aSearchResults = $state();
13   let iHighlightNum = $state();
14   let sMoreURL = $state();
15
16   results_store.subscribe(data => {
17     if (!data) { return; }
18     aSearchResults = data;
19     iHighlightNum = 0;
20     current_result = aSearchResults[0];
21
22
23     let search_params = new SvelteURLSearchParams(window.location.search);
24
25     let aResults = data;
26     // lonvia wrote: https://github.com/osm-search/nominatim-ui/issues/24
27     // I would suggest to remove the guessing and always show the link. Nominatim only returns
28     // one or two results when it believes the result to be a good enough match.
29     // if (aResults.length >= 10) {
30     var aExcludePlaceIds = [];
31     if (search_params.has('exclude_place_ids')) {
32       aExcludePlaceIds = search_params.get('exclude_place_ids').split(',');
33     }
34     for (var i = 0; i < aResults.length; i += 1) {
35       aExcludePlaceIds.push(aResults[i].place_id);
36     }
37     var parsed_url = new SvelteURLSearchParams(window.location.search);
38     parsed_url.set('exclude_place_ids', aExcludePlaceIds.join(','));
39     sMoreURL = '?' + parsed_url.toString();
40   });
41
42   function handleClick(e) {
43     e.stopPropagation();
44     let result_el = e.target;
45     if (!result_el.className.match('result')) {
46       result_el = result_el.parentElement;
47     }
48     let pos = Number(result_el.dataset.position);
49
50     current_result = aSearchResults[pos];
51     iHighlightNum = pos;
52   }
53
54 </script>
55
56 {#if aSearchResults && aSearchResults.length > 0}
57   <div id="searchresults" role="list">
58
59     {#each aSearchResults as aResult, iResNum}
60       <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
61       <div class="result"
62            class:highlight={iResNum === iHighlightNum}
63            role="listitem"
64            data-position="{iResNum}"
65            onclick={handleClick}
66            onkeypress={handleClick}>
67         <div style="float:right">
68           <MapIcon aPlace={aResult} />
69         </div>
70         <span class="name">{aResult.display_name}</span>
71         <span class="type">{formatLabel(aResult)}</span>
72         <p class="coords">{aResult.lat},{aResult.lon}</p>
73
74         <DetailsLink extra_classes="btn btn-outline-secondary btn-sm" feature={aResult} />
75       </div>
76     {/each}
77
78     {#if sMoreURL && !reverse_search}
79       <div class="more">
80         <a class="btn btn-primary" href="{sMoreURL}">
81           Search for more results
82         </a>
83       </div>
84     {/if}
85   </div>
86 {:else if aSearchResults}
87   {#if reverse_search}
88     <div id="intro" class="sidebar">Search for coordinates or click anywhere on the map.</div>
89   {:else}
90     <div class="noresults">No search results found</div>
91   {/if}
92 {:else}
93   <Welcome/>
94 {/if}
95
96 <style>
97   .result {
98     font-size: 0.8em;
99     margin: 5px;
100     margin-top: 0;
101     padding: 4px 8px;
102     border-radius: 2px;
103     background: var(--bs-secondary-bg);
104     border: 1px solid var(--bs-secondary-color);
105     cursor: pointer;
106     min-height: 5em;
107   }
108
109   .result.highlight {
110     background-color: var(--bs-primary-bg-subtle);
111     border-color: var(--bs-primary-color-subtle);
112   }
113   .result.highlight :global(a) {
114     margin: 10px auto;
115     display: block;
116     max-width: 10em;
117     padding: 1px;
118     color: var(--bs-secondary-color);
119     background-color: var(--bs-secondary-bg);
120   }
121   .result .type {
122     color: var(--bs-secondary-color);
123     font-size: 0.8em;
124   }
125   .result :global(a) {
126     display: none;
127   }
128
129   .result .coords {
130     display: none;
131   }
132
133   .noresults{
134     text-align: center;
135     padding: 1em;
136   }
137
138   .more{
139     text-align:center;
140     margin-top: 1em;
141   }
142
143   .result.highlight :global(a):hover {
144     background-color: var(--bs-primary-bg-subtle);
145   }
146 </style>