]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/stores.js
new config option -Reverse_Only- (#166)
[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 default_pagename = Nominatim_Config.Reverse_Only ? 'reverse' : 'search';
20 const pagenames = [
21   default_pagename,
22   'reverse',
23   'details',
24   'deletable',
25   'polygons',
26   'status',
27   'about'
28 ];
29
30 export function refresh_page(pagename, params) {
31   if (typeof pagename === 'undefined') {
32     pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
33
34     if (!pagenames.includes(pagename)) pagename = default_pagename;
35
36     params = new URLSearchParams(window.location.search);
37   } else {
38     if (!pagenames.includes(pagename)) pagename = default_pagename;
39
40     if (typeof params === 'undefined') {
41       params = new URLSearchParams();
42     }
43
44     let param_str = params.toString();
45     if (param_str) {
46       param_str = '?' + param_str;
47     }
48     let new_url = pagename + '.html' + param_str;
49
50     if (window.location.protocol.match(/^http/)) {
51       window.history.pushState([], '', new_url);
52     } else {
53       window.location.href = new_url;
54     }
55   }
56
57   page.set({ tab: pagename, params: params });
58   last_api_request_url_store.set(null);
59   error_store.set(null);
60 }