2 import { get_config_value } from './config_reader.js';
3 import { last_api_request_url_store } from './stores.js';
6 function api_request_progress(status) {
7 var loading_el = document.getElementById('loading');
8 if (!loading_el) return; // might not be on page yet
10 loading_el.style.display = (status === 'start') ? 'block' : null;
13 export async function fetch_from_api(endpoint_name, params, callback) {
14 var api_url = generate_nominatim_api_url(endpoint_name, params);
16 api_request_progress('start');
19 .then(response => response.json())
22 api_request_progress('finish');
25 if (endpoint_name !== 'status') last_api_request_url_store.set(api_url);
28 function generate_nominatim_api_url(endpoint_name, params) {
29 return get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
30 + Object.keys(clean_up_parameters(params)).map((k) => {
31 return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
36 function clean_up_parameters(params) {
37 // `&a=&b=&c=1` => '&c=1'
38 var param_names = Object.keys(params);
39 for (var i = 0; i < param_names.length; i += 1) {
40 var val = params[param_names[i]];
41 if (typeof (val) === 'undefined' || val === '' || val === null) {
42 delete params[param_names[i]];
48 export function update_html_title(title) {
49 document.title = [title, 'OpenStreetMap Nominatim']
50 .filter((val) => val && val.length > 1)