1 import { writable } from 'svelte/store';
2 import { untrack } from 'svelte';
3 import { identifyLinkInQuery } from './helpers.js';
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();
12 * Update the global page state.
14 * When called without a parameter, then the current window.location is
15 * parsed and the page state is set accordingly. Otherwise the page state
16 * is set from the parameters. 'pagename' is the overall subpage (without
17 * .html extension). 'params' must be an URLSearchParams object and contain
18 * the requested query parameters. It may also be omitted completely for a
19 * link without query parameters.
21 const default_pagename = Nominatim_Config.Reverse_Only ? 'reverse' : 'search';
32 export function refresh_page(pagename, params) {
33 if (typeof pagename === 'undefined') {
34 pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
36 if (!pagenames.includes(pagename)) pagename = default_pagename;
38 params = new URLSearchParams(window.location.search);
40 if (!pagenames.includes(pagename)) pagename = default_pagename;
42 if (typeof params === 'undefined') {
43 params = new URLSearchParams();
46 let param_str = params.toString();
48 param_str = '?' + param_str;
50 let new_url = pagename + '.html' + param_str;
52 if (window.location.protocol.match(/^http/)) {
53 window.history.pushState([], '', new_url);
55 window.location.href = new_url;
59 if (pagename === 'search' && params.has('q')) {
60 const arrTypeAndId = identifyLinkInQuery(params.get('q'));
61 if (arrTypeAndId instanceof Array) {
63 params = new URLSearchParams();
64 params.set('osmtype', arrTypeAndId[0]);
65 params.set('osmid', arrTypeAndId[1]);
70 page.set({ tab: pagename, params: params });
71 last_api_request_url_store.set(null);
72 error_store.set(null);