X-Git-Url: https://git.openstreetmap.org./nominatim-ui.git/blobdiff_plain/e0d087f4a2b5fbc758de432f8932de296279a82f..a82c578374260ba3450fbd475da5739b9d92a4ce:/src/assets/js/controller.js diff --git a/src/assets/js/controller.js b/src/assets/js/controller.js index 3c122ee..2babae4 100644 --- a/src/assets/js/controller.js +++ b/src/assets/js/controller.js @@ -18,13 +18,41 @@ jQuery(document).ready(function () { } } + function is_relative_url(url) { + if (!url) return false; + if (url.indexOf('?') === 0) return true; + if (url.indexOf('/') === 0) return true; + if (url.indexOf('#') === 0) return false; + if (url.match(/^http/)) return false; + if (!url.match(/\.html/)) return true; + + return false; + } + + // remove any URL paramters with empty values + // '&empty=&filled=value' => 'filled=value' + function clean_up_url_parameters(url) { + var url_params = new URLSearchParams(url); + var to_delete = []; // deleting inside loop would skip iterations + url_params.forEach(function (value, key) { + if (value === '') to_delete.push(key); + }); + for (var i = 0; i < to_delete.length; i += 1) { + url_params.delete(to_delete[i]); + } + return url_params.toString(); + } + parse_url_and_load_page(); // load page after form submit $(document).on('submit', 'form', function (e) { e.preventDefault(); - window.history.pushState(myhistory, '', '?' + $(this).serialize()); + var target_url = $(this).serialize(); + target_url = clean_up_url_parameters(target_url); + + window.history.pushState(myhistory, '', '?' + target_url); parse_url_and_load_page(); }); @@ -32,10 +60,10 @@ jQuery(document).ready(function () { // load page after click on relative URL $(document).on('click', 'a', function (e) { var target_url = $(this).attr('href'); - if (target_url && target_url.match(/^http/)) return; - if (target_url && !target_url.match(/\.html/)) return; + if (!is_relative_url(target_url)) return; e.preventDefault(); + e.stopPropagation(); window.history.pushState(myhistory, '', target_url);