1 import App from './App.svelte';
2 import SearchPage from './pages/SearchPage.svelte';
3 import DetailsPage from './pages/DetailsPage.svelte';
4 import PolygonsPage from './pages/PolygonsPage.svelte';
5 import DeletablePage from './pages/DeletablePage.svelte';
7 import { serialize_form, clean_up_url_parameters } from './lib/api_utils.js'
18 // inspects window.location
19 function identify_current_page() {
20 var pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
22 if (pagename === '') { return 'search' }
24 if (['search', 'reverse', 'details', 'deletable', 'polygons'].indexOf(pagename) != '-1') {
31 function parse_url_and_load_page() {
32 let pagename = identify_current_page();
34 document.getElementById('main').replaceChildren();
36 if (pagename === 'search' || pagename === 'reverse') {
38 target: document.getElementById('main'),
40 reverse_search: (pagename === 'reverse')
43 } else if (pagename === 'details') {
45 target: document.getElementById('main')
47 } else if (pagename === 'deletable') {
49 target: document.getElementById('main')
51 } else if (pagename === 'polygons') {
53 target: document.getElementById('main')
60 function is_relative_url(url) {
61 if (!url) return false;
62 if (url.indexOf('?') === 0) return true;
63 if (url.indexOf('/') === 0) return true;
64 if (url.indexOf('#') === 0) return false;
65 if (url.match(/^http/)) return false;
66 if (!url.match(/\.html/)) return true;
72 parse_url_and_load_page();
74 // load page after form submit
75 document.addEventListener('submit', function (e) {
77 // loop parent nodes from the target to the delegation node
78 for (var target = e.target; target && target != this; target = target.parentNode) {
79 if (target.matches('form')) {
82 var target_url = serialize_form(target);
83 target_url = clean_up_url_parameters(target_url);
85 window.history.pushState(myhistory, '', '?' + target_url);
87 parse_url_and_load_page();
94 // load page after click on relative URL
95 document.addEventListener('click', function (e) {
97 // loop parent nodes from the target to the delegation node
98 for (var target = e.target; target && target != this; target = target.parentNode) {
99 if (target.matches('a')) {
101 var target_url = target.href;
103 if (!is_relative_url(target_url)) return;
108 window.history.pushState(myhistory, '', target_url);
110 parse_url_and_load_page();
116 // deal with back-button and other user action
117 window.onpopstate = function () {
118 parse_url_and_load_page();