]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/stores.js
make sure webpages still work on file:// protocol (#94)
[nominatim-ui.git] / src / lib / stores.js
1 import { writable } from 'svelte/store';
2
3 export const map_store = writable();
4 export const results_store = writable();
5 export const last_api_request_url_store = writable();
6 export const error_store = writable();
7 export const page = writable();
8
9 /**
10  * Update the global page state.
11  *
12  * When called without a parameter, then the current window.location is
13  * parsed and the page state is set accordingly. Otherwise the page state
14  * is set from the parameters. 'pagename' is the overall subpage (without
15  * .html extension). 'params' must be an URLSearchParams object and contain
16  * the requested query parameters. It may also be omitted completely for a
17  * link without query parameters.
18  */
19 const pagenames = ['search', 'reverse', 'details', 'deletable', 'polygons', 'about'];
20
21 export function refresh_page(pagename, params) {
22   if (typeof pagename === 'undefined') {
23     pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
24
25     if (!pagenames.includes(pagename)) pagename = 'search';
26
27     params = new URLSearchParams(window.location.search);
28   } else {
29     if (typeof params === 'undefined') {
30       params = new URLSearchParams();
31     }
32
33     let param_str = params.toString();
34     if (param_str) {
35       param_str = '?' + param_str;
36     }
37     let new_url = pagename + '.html' + param_str;
38
39     if (window.location.protocol.match(/^http/)) {
40       window.history.pushState([], '', new_url);
41     } else {
42       window.location.href = new_url;
43     }
44   }
45
46   page.set({ tab: pagename, params: params });
47   last_api_request_url_store.set(null);
48   error_store.set(null);
49 }