]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/components/ResultsList.svelte
3b6c7133945a564e1098608e75325d611ef23976
[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
5   import DetailsLink from './DetailsLink.svelte';
6   import Welcome from './Welcome.svelte';
7   import MapIcon from './MapIcon.svelte';
8
9   export let reverse_search = false;
10   export let current_result = null;
11
12   let aSearchResults;
13   let iHighlightNum;
14   let sMoreURL;
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 URLSearchParams(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 URLSearchParams(window.location.search);
38     parsed_url.set('exclude_place_ids', aExcludePlaceIds.join(','));
39     sMoreURL = '?' + parsed_url.toString();
40   });
41
42   function handleClick(e) {
43     let result_el = e.target;
44     if (!result_el.className.match('result')) {
45       result_el = result_el.parentElement;
46     }
47     let pos = Number(result_el.dataset.position);
48
49     current_result = aSearchResults[pos];
50     iHighlightNum = pos;
51   }
52
53 </script>
54
55 {#if aSearchResults && aSearchResults.length > 0}
56   <div id="searchresults">
57
58     {#each aSearchResults as aResult, iResNum}
59       <div class="result"
60            class:highlight={iResNum === iHighlightNum}
61            data-position="{iResNum}"
62            on:click|stopPropagation={handleClick}>
63         <div style="float:right">
64           <MapIcon aPlace={aResult} />
65         </div>
66         <span class="name">{aResult.display_name}</span>
67         <span class="type">{formatLabel(aResult)}</span>
68         <p class="coords">{aResult.lat},{aResult.lon}</p>
69
70         <DetailsLink extra_classes="btn btn-outline-secondary btn-sm" feature={aResult}>
71           details
72         </DetailsLink>
73       </div>
74     {/each}
75
76     {#if sMoreURL && !reverse_search}
77       <div class="more">
78         <a class="btn btn-primary" href="{sMoreURL}">
79           Search for more results
80         </a>
81       </div>
82     {/if}
83   </div>
84 {:else if aSearchResults}
85   {#if reverse_search}
86     <div id="intro" class="sidebar">Search for coordinates or click anywhere on the map.</div>
87   {:else}
88     <div class="noresults">No search results found</div>
89   {/if}
90 {:else}
91   <Welcome/>
92 {/if}
93
94 <style>
95   .result {
96     font-size: 0.8em;
97     margin: 5px;
98     margin-top:0px;
99     padding: 4px 8px;
100     border-radius: 2px;
101     background:#F0F7FF;
102     border: 2px solid #D7E7FF;
103     cursor:pointer;
104     min-height: 5em;
105   }
106
107   .result.highlight {
108     background-color: #D9E7F7;
109     border-color: #9DB9E4;
110   }
111   .result.highlight :global(a) {
112     margin: 10px auto;
113     display: block;
114     max-width: 10em;
115     padding: 1px;
116     background-color: white;
117   }
118   .result .type{
119     color: gray;
120     font-size: 0.8em;
121   }
122   .result :global(a) {
123     display: none;
124   }
125
126   .result .coords {
127     display: none;
128   }
129
130   .noresults{
131     text-align: center;
132     padding: 1em;
133   }
134
135   .more{
136     text-align:center;
137     margin-top: 1em;
138   }
139
140   .result.highlight :global(a):hover {
141     color: #111;
142   }
143 </style>