X-Git-Url: https://git.openstreetmap.org./nominatim-ui.git/blobdiff_plain/163775c3501257af8f62601b0bb3565133c3bebb..21bcfb32a13839ff8c69a452d09e294da800a15a:/src/lib/stores.js?ds=inline diff --git a/src/lib/stores.js b/src/lib/stores.js index 0707eef..a7a524a 100644 --- a/src/lib/stores.js +++ b/src/lib/stores.js @@ -3,16 +3,38 @@ import { writable } from 'svelte/store'; export const map_store = writable(); export const results_store = writable(); export const last_api_request_url_store = writable(); -export const page = writable({ count: 0 }); +export const page = writable(); -export function refresh_page() { - let pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, ''); +/** + * Update the global page state. + * + * When called without a parameter, then the current window.location is + * parsed and the page state is set accordingly. Otherwise the page state + * is set from the parameters. 'pagename' is the overall subpage (without + * .html extension). 'params' must be an URLSearchParams object and contain + * the requested query parameters. It may also be omitted completely for a + * link without query parameters. + */ +export function refresh_page(pagename, params) { + if (typeof pagename === 'undefined') { + pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, ''); - if (['search', 'reverse', 'details', 'deletable', 'polygons'].indexOf(pagename) === -1) { - pagename = 'search'; + if (['search', 'reverse', 'details', 'deletable', 'polygons'].indexOf(pagename) === -1) { + pagename = 'search'; + } + + params = new URLSearchParams(window.location.search); + } else { + if (typeof params === 'undefined') { + params = new URLSearchParams(); + } + + let param_str = params.toString(); + if (param_str) { + param_str = '?' + param_str; + } + window.history.pushState([], '', pagename + '.html' + param_str); } - // Add a counter here to make sure the store change is triggered - // everytime we refresh, not just when the page changes. - page.update(function (v) { return { tab: pagename, count: v.count + 1 }; }); + page.set({ tab: pagename, params: params }); }