2 import { get_config_value } from './config_reader.js';
3 import { last_api_request_url_store, error_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');
17 if (endpoint_name !== 'status') last_api_request_url_store.set(null);
21 .then(response => response.json())
24 error_store.set(data.error.message);
27 api_request_progress('finish');
30 error_store.set(`Error fetching data from ${api_url} (${error})`);
31 api_request_progress('finish');
34 if (endpoint_name !== 'status') last_api_request_url_store.set(api_url);
37 var fetch_content_cache = {};
38 export async function fetch_content_into_element(url, dom_element) {
39 if (fetch_content_cache[url]) {
40 dom_element.innerHTML = fetch_content_cache[url];
44 .then(response => response.text())
46 html = html.replace('Nominatim_API_Endpoint', get_config_value('Nominatim_API_Endpoint'));
47 dom_element.innerHTML = html;
48 fetch_content_cache[url] = html;
52 function generate_nominatim_api_url(endpoint_name, params) {
53 return get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
54 + Object.keys(clean_up_parameters(params)).map((k) => {
55 return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
60 function clean_up_parameters(params) {
61 // `&a=&b=&c=1` => '&c=1'
62 var param_names = Object.keys(params);
63 for (var i = 0; i < param_names.length; i += 1) {
64 var val = params[param_names[i]];
65 if (typeof (val) === 'undefined' || val === '' || val === null) {
66 delete params[param_names[i]];
72 export function update_html_title(title) {
73 document.title = [title, get_config_value('Page_Title')]
74 .filter((val) => val && val.length > 1)