]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/stores.js
allow theming (#84)
[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 page = writable();
7
8 /**
9  * Update the global page state.
10  *
11  * When called without a parameter, then the current window.location is
12  * parsed and the page state is set accordingly. Otherwise the page state
13  * is set from the parameters. 'pagename' is the overall subpage (without
14  * .html extension). 'params' must be an URLSearchParams object and contain
15  * the requested query parameters. It may also be omitted completely for a
16  * link without query parameters.
17  */
18 const pagenames = ['search', 'reverse', 'details', 'deletable', 'polygons', 'about'];
19
20 export function refresh_page(pagename, params) {
21   if (typeof pagename === 'undefined') {
22     pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
23
24     if (!pagenames.includes(pagename)) pagename = 'search';
25
26     params = new URLSearchParams(window.location.search);
27   } else {
28     if (typeof params === 'undefined') {
29       params = new URLSearchParams();
30     }
31
32     let param_str = params.toString();
33     if (param_str) {
34       param_str = '?' + param_str;
35     }
36     window.history.pushState([], '', pagename + '.html' + param_str);
37   }
38
39   page.set({ tab: pagename, params: params });
40 }