2 import { get_config_value } from './config_reader.js';
3 import { last_updated_store } from './stores.js';
7 export async function fetch_from_api(endpoint_name, params, callback) {
8 var api_url = generate_nominatim_api_url(endpoint_name, params);
10 document.getElementById('loading').style.display = 'block';
12 .then(response => response.json())
15 document.getElementById('loading').style.display = 'none';
19 fetch(generate_nominatim_api_url('status', {format: 'json'}))
20 .then(response => response.json())
23 api_request_url: api_url,
24 api_request_url_debug: api_url + '&debug=1',
25 date: data.data_updated
27 last_updated_store.set(last_updated)
31 function generate_nominatim_api_url(endpoint_name, params) {
32 return get_config_value('Nominatim_API_Endpoint') + endpoint_name + '.php?'
33 + Object.keys(clean_up_parameters(params)).map( (k) => {
34 return encodeURIComponent(k) + '=' + encodeURIComponent(params[k])
39 * Serialize all form data into a SearchParams string
40 * (c) 2020 Chris Ferdinandi, MIT License, https://gomakethings.com
41 * @param {Node} form The form to serialize
42 * @return {String} The serialized form data
44 export function serialize_form(form) {
46 Array.prototype.slice.call(form.elements).forEach(function (field) {
47 if (!field.name || field.disabled || ['file', 'reset', 'submit', 'button'].indexOf(field.type) > -1) return;
48 // if (field.type === 'select-multiple') {
49 // Array.prototype.slice.call(field.options).forEach(function (option) {
50 // if (!option.selected) return;
51 // arr.push(encodeURIComponent(field.name) + '=' + encodeURIComponent(option.value));
55 if (['checkbox', 'radio'].indexOf(field.type) >-1 && !field.checked) return;
56 if (typeof(field.value) === 'undefined') return;
57 arr.push(encodeURIComponent(field.name) + '=' + encodeURIComponent(field.value));
63 // remove any URL paramters with empty values
64 // '&empty=&filled=value' => 'filled=value'
65 export function clean_up_url_parameters(url) {
66 var url_params = new URLSearchParams(url);
67 var to_delete = []; // deleting inside loop would skip iterations
68 url_params.forEach(function (value, key) {
69 if (value === '') to_delete.push(key);
71 for (var i = 0; i < to_delete.length; i += 1) {
72 url_params.delete(to_delete[i]);
74 return url_params.toString();
77 function clean_up_parameters(params) {
78 // `&a=&b=&c=1` => '&c=1'
79 var param_names = Object.keys(params);
80 for (var i = 0; i < param_names.length; i += 1) {
81 var val = params[param_names[i]];
82 if (typeof (val) === 'undefined' || val === '' || val === null) {
83 delete params[param_names[i]];
89 export function update_html_title(title) {
90 document.title = [title, 'OpenStreetMap Nominatim']
91 .filter( (val) => val && val.length > 1 )