1 import { writable } from 'svelte/store';
2 import { identifyLinkInQuery } from './helpers.js';
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();
11 * Update the global page state.
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.
20 const default_pagename = Nominatim_Config.Reverse_Only ? 'reverse' : 'search';
31 export function refresh_page(pagename, params) {
32 if (typeof pagename === 'undefined') {
33 pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
35 if (!pagenames.includes(pagename)) pagename = default_pagename;
37 params = new URLSearchParams(window.location.search);
39 if (!pagenames.includes(pagename)) pagename = default_pagename;
41 if (typeof params === 'undefined') {
42 params = new URLSearchParams();
45 let param_str = params.toString();
47 param_str = '?' + param_str;
49 let new_url = pagename + '.html' + param_str;
51 if (window.location.protocol.match(/^http/)) {
52 window.history.pushState([], '', new_url);
54 window.location.href = new_url;
58 if (pagename === 'search' && params.has('q')) {
59 const arrTypeAndId = identifyLinkInQuery(params.get('q'));
60 if (arrTypeAndId instanceof Array) {
62 params = new URLSearchParams();
63 params.set('osmtype', arrTypeAndId[0]);
64 params.set('osmid', arrTypeAndId[1]);
68 page.set({ tab: pagename, params: params });
69 last_api_request_url_store.set(null);
70 error_store.set(null);