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 var fetch_content_cache = {};
29 export async function fetch_content_into_element(url, dom_element) {
30 if (fetch_content_cache[url]) {
31 dom_element.innerHTML = fetch_content_cache[url];
35 .then(response => response.text())
37 html = html.replace('Nominatim_API_Endpoint', get_config_value('Nominatim_API_Endpoint'));
38 dom_element.innerHTML = html;
39 fetch_content_cache[url] = html;
43 function generate_nominatim_api_url(endpoint_name, params) {
44 return get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
45 + Object.keys(clean_up_parameters(params)).map((k) => {
46 return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]);
51 function clean_up_parameters(params) {
52 // `&a=&b=&c=1` => '&c=1'
53 var param_names = Object.keys(params);
54 for (var i = 0; i < param_names.length; i += 1) {
55 var val = params[param_names[i]];
56 if (typeof (val) === 'undefined' || val === '' || val === null) {
57 delete params[param_names[i]];
63 export function update_html_title(title) {
64 document.title = [title, get_config_value('Page_Title')]
65 .filter((val) => val && val.length > 1)