]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/main.js
Make page choice more svelty (#51)
[nominatim-ui.git] / src / main.js
1 import App from './App.svelte';
2 import { refresh_page } from './lib/stores.js';
3
4 let myhistory = [];
5
6 const app = new App({
7   target: document.body
8 });
9
10
11 function is_relative_url(url) {
12   if (!url) return false;
13   if (url.indexOf('?') === 0) return true;
14   if (url.indexOf('/') === 0) return true;
15   if (url.indexOf('#') === 0) return false;
16   if (url.match(/^http/)) return false;
17   if (!url.match(/\.html/)) return true;
18
19   return false;
20 }
21
22
23 // load page after click on relative URL
24 document.addEventListener('click', function (e) {
25
26   // loop parent nodes from the target to the delegation node
27   for (var target = e.target; target && target != this; target = target.parentNode) {
28     if (target.matches('a')) {
29
30       var target_url = target.href;
31
32       if (!is_relative_url(target_url)) return;
33
34       e.preventDefault();
35       e.stopPropagation();
36
37       window.history.pushState(myhistory, '', target_url);
38
39       refresh_page();
40       break;
41     }
42   }
43 });
44
45 // deal with back-button and other user action
46 window.onpopstate = function () {
47   refresh_page();
48 };