]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/stores.js
a7a524a3b4f738cf3f5ef4abec407dba552ccf42
[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 export function refresh_page(pagename, params) {
19   if (typeof pagename === 'undefined') {
20     pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
21
22     if (['search', 'reverse', 'details', 'deletable', 'polygons'].indexOf(pagename) === -1) {
23       pagename = 'search';
24     }
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 }