1 jQuery(document).ready(function () {
4 function parse_url_and_load_page() {
5 // 'search', 'reverse', 'details'
6 var pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
8 if (pagename === '') pagename = 'search';
10 $('body').attr('id', pagename + '-page');
12 if (pagename === 'search' || pagename === 'reverse') {
14 } else if (pagename === 'details') {
16 } else if (pagename === 'deletable') {
17 deletable_page_load();
18 } else if (pagename === 'polygons') {
23 function is_relative_url(url) {
24 if (!url) return false;
25 if (url.indexOf('?') === 0) return true;
26 if (url.indexOf('/') === 0) return true;
27 if (url.indexOf('#') === 0) return false;
28 if (url.match(/^http/)) return false;
29 if (!url.match(/\.html/)) return true;
34 // remove any URL paramters with empty values
35 // '&empty=&filled=value' => 'filled=value'
36 function clean_up_url_parameters(url) {
37 var url_params = new URLSearchParams(url);
38 var to_delete = []; // deleting inside loop would skip iterations
39 url_params.forEach(function (value, key) {
40 if (value === '') to_delete.push(key);
42 for (var i = 0; i < to_delete.length; i += 1) {
43 url_params.delete(to_delete[i]);
45 return url_params.toString();
48 parse_url_and_load_page();
50 // load page after form submit
51 $(document).on('submit', 'form', function (e) {
54 var target_url = $(this).serialize();
55 target_url = clean_up_url_parameters(target_url);
57 window.history.pushState(myhistory, '', '?' + target_url);
59 parse_url_and_load_page();
62 // load page after click on relative URL
63 $(document).on('click', 'a', function (e) {
64 var target_url = $(this).attr('href');
65 if (!is_relative_url(target_url)) return;
66 if ($(this).parents('#last-updated').length !== 0) return;
71 window.history.pushState(myhistory, '', target_url);
73 parse_url_and_load_page();
76 // deal with back-button and other user action
77 window.onpopstate = function () {
78 parse_url_and_load_page();