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' : 'none';
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 * Serialize all form data into a SearchParams string
37 * (c) 2020 Chris Ferdinandi, MIT License, https://gomakethings.com
38 * @param {Node} form The form to serialize
39 * @return {String} The serialized form data
41 export function serialize_form(form) {
43 Array.prototype.slice.call(form.elements).forEach(function (field) {
44 if (!field.name || field.disabled || ['submit', 'button'].indexOf(field.type) > -1) return;
45 // if (field.type === 'select-multiple') {
46 // Array.prototype.slice.call(field.options).forEach(function (option) {
47 // if (!option.selected) return;
48 // arr.push(encodeURIComponent(field.name) + '=' + encodeURIComponent(option.value));
52 if (['checkbox', 'radio'].indexOf(field.type) > -1 && !field.checked) return;
53 if (typeof field.value === 'undefined') return;
54 arr.push(encodeURIComponent(field.name) + '=' + encodeURIComponent(field.value));
60 // remove any URL paramters with empty values
61 // '&empty=&filled=value' => 'filled=value'
62 export function clean_up_url_parameters(url) {
63 var url_params = new URLSearchParams(url);
64 var to_delete = []; // deleting inside loop would skip iterations
65 url_params.forEach(function (value, key) {
66 if (value === '') to_delete.push(key);
68 for (var i = 0; i < to_delete.length; i += 1) {
69 url_params.delete(to_delete[i]);
71 return url_params.toString();
74 function clean_up_parameters(params) {
75 // `&a=&b=&c=1` => '&c=1'
76 var param_names = Object.keys(params);
77 for (var i = 0; i < param_names.length; i += 1) {
78 var val = params[param_names[i]];
79 if (typeof (val) === 'undefined' || val === '' || val === null) {
80 delete params[param_names[i]];
86 export function update_html_title(title) {
87 document.title = [title, 'OpenStreetMap Nominatim']
88 .filter((val) => val && val.length > 1)