]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/stores.js
when searching for OSM Url, show detail page result page
[nominatim-ui.git] / src / lib / stores.js
1 import { writable } from 'svelte/store';
2 import { identifyLinkInQuery } from './helpers.js';
3
4 export const map_store = writable();
5 export const results_store = writable();
6 export const last_api_request_url_store = writable();
7 export const error_store = writable();
8 export const page = writable();
9
10 /**
11  * Update the global page state.
12  *
13  * When called without a parameter, then the current window.location is
14  * parsed and the page state is set accordingly. Otherwise the page state
15  * is set from the parameters. 'pagename' is the overall subpage (without
16  * .html extension). 'params' must be an URLSearchParams object and contain
17  * the requested query parameters. It may also be omitted completely for a
18  * link without query parameters.
19  */
20 const default_pagename = Nominatim_Config.Reverse_Only ? 'reverse' : 'search';
21 const pagenames = [
22   default_pagename,
23   'reverse',
24   'details',
25   'deletable',
26   'polygons',
27   'status',
28   'about'
29 ];
30
31 export function refresh_page(pagename, params) {
32   if (typeof pagename === 'undefined') {
33     pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
34
35     if (!pagenames.includes(pagename)) pagename = default_pagename;
36
37     params = new URLSearchParams(window.location.search);
38   } else {
39     if (!pagenames.includes(pagename)) pagename = default_pagename;
40
41     if (typeof params === 'undefined') {
42       params = new URLSearchParams();
43     }
44
45     let param_str = params.toString();
46     if (param_str) {
47       param_str = '?' + param_str;
48     }
49     let new_url = pagename + '.html' + param_str;
50
51     if (window.location.protocol.match(/^http/)) {
52       window.history.pushState([], '', new_url);
53     } else {
54       window.location.href = new_url;
55     }
56   }
57
58   if (pagename === 'search' && params.has('q')) {
59     const arrTypeAndId = identifyLinkInQuery(params.get('q'));
60     if (arrTypeAndId instanceof Array) {
61       pagename = 'details';
62       params = new URLSearchParams();
63       params.set('osmtype', arrTypeAndId[0]);
64       params.set('osmid', arrTypeAndId[1]);
65     }
66   }
67
68   page.set({ tab: pagename, params: params });
69   last_api_request_url_store.set(null);
70   error_store.set(null);
71 }