From 9e8d3367fe08a708b1019ff39ac752e0105be20a Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sat, 13 Feb 2021 12:11:08 +0100 Subject: [PATCH] Handle all page changes via page store (#70) * move serialize_form and clean_up_url_parameters to UrlSubmitForm * switch pages by setting page store * simplify parameter handling in reverse bar * add documentation for refresh_page() function * scroll to top of details page on reload --- src/App.svelte | 2 +- src/components/DetailsLink.svelte | 37 +++++++++++++--------- src/components/PageLink.svelte | 3 +- src/components/ReverseBar.svelte | 49 +++++++++++++++-------------- src/components/ReverseLink.svelte | 25 ++++++++------- src/components/SearchBar.svelte | 4 +-- src/components/UrlSubmitForm.svelte | 22 ++++++++----- src/lib/api_utils.js | 38 ---------------------- src/lib/stores.js | 38 +++++++++++++++++----- src/pages/DetailsPage.svelte | 16 +++++----- src/pages/ReversePage.svelte | 19 +++++------ src/pages/SearchPage.svelte | 17 +++++----- 12 files changed, 135 insertions(+), 135 deletions(-) diff --git a/src/App.svelte b/src/App.svelte index 54cad5d..ec1ca0b 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -19,7 +19,7 @@ - + refresh_page()} />
{#if view === 'search'} diff --git a/src/components/DetailsLink.svelte b/src/components/DetailsLink.svelte index 51a0c8c..fbeb4f1 100644 --- a/src/components/DetailsLink.svelte +++ b/src/components/DetailsLink.svelte @@ -4,7 +4,8 @@ export let extra_classes = ''; export let feature = null; - let url_params = ''; + let url_params = new URLSearchParams(); + let href = 'details.html'; function formatShortOSMType(sType) { if (sType === 'node') return 'N'; @@ -14,29 +15,35 @@ } function handleClick() { - window.history.pushState([], '', 'details.html' + url_params); - refresh_page(); + refresh_page('details', url_params); } $: { + let new_params = new URLSearchParams(); + if (feature !== null && feature.osm_type) { - let param = '?osmtype='; - if (feature.osm_type.length == 1) { - param += encodeURIComponent(feature.osm_type); + if (feature.osm_type.length === 1) { + new_params.set('osmtype', feature.osm_type); } else { - param += formatShortOSMType(feature.osm_type); + new_params.set('osmtype', formatShortOSMType(feature.osm_type)); } - param += '&osmid=' + encodeURIComponent(feature.osm_id); + + new_params.set('osmid', feature.osm_id); + if (feature.class) { - param += '&class=' + encodeURIComponent(feature.class); + new_params.set('class', feature.class); } else if (feature.category) { - param += '&class=' + encodeURIComponent(feature.category); + new_params.set('class', feature.category); } - url_params = param - } else { - url_params = ''; } - } + + url_params = new_params; + } + + $: { + let param_str = url_params.toString(); + href = 'details.html' + (param_str ? '?' : '') + param_str; + } - + diff --git a/src/components/PageLink.svelte b/src/components/PageLink.svelte index d70c3f0..9205c2f 100644 --- a/src/components/PageLink.svelte +++ b/src/components/PageLink.svelte @@ -4,8 +4,7 @@ import { refresh_page } from '../lib/stores.js'; export let page; function handleClick() { - window.history.pushState([], '', page + '.html'); - refresh_page(); + refresh_page(page); } diff --git a/src/components/ReverseBar.svelte b/src/components/ReverseBar.svelte index 53d7bff..62227c8 100644 --- a/src/components/ReverseBar.svelte +++ b/src/components/ReverseBar.svelte @@ -4,28 +4,29 @@ import PageLink from '../components/PageLink.svelte'; import { zoomLevels } from '../lib/helpers.js'; - import { map_store } from '../lib/stores.js'; + import { map_store, refresh_page } from '../lib/stores.js'; - export let api_request_params = {}; + export let lat = ''; + export let lon = ''; + export let zoom = ''; - map_store.subscribe(map => { - if (!map) { return; } + function gotoCoordinates(newlat, newlon, newzoom) { + let params = new URLSearchParams(); + params.set('lat', newlat); + params.set('lon', newlon); + params.set('zoom', newzoom || zoom); + refresh_page('reverse', params); + } - map.on('click', function (e) { - document.querySelector('input[name=lat]').value = e.latlng.lat.toFixed(5); - document.querySelector('input[name=lon]').value = e.latlng.wrap().lng.toFixed(5); - document.querySelector('form').submit(); - }); + map_store.subscribe(map => { + if (map) { + map.on('click', (e) => { + let coords = e.latlng.wrap(); + gotoCoordinates(coords.lat.toFixed(5), coords.lng.toFixed(5)); + }); + } }); - function handleSwitchCoords() { - let lat = document.querySelector('input[name=lat]').value; - let lon = document.querySelector('input[name=lon]').value; - document.querySelector('input[name=lat]').value = lon; - document.querySelector('input[name=lon]').value = lat; - document.querySelector('form').submit(); - } - // common mistake is to copy&paste latitude and longitude into the 'lat' search box function maybeSplitLatitude(e) { var coords_split = e.target.value.split(','); @@ -38,7 +39,7 @@
- +
@@ -47,10 +48,10 @@ type="text" class="form-control form-control-sm" placeholder="latitude" - value="{api_request_params.lat || ''}" + bind:value={lat} on:change={maybeSplitLatitude} /> gotoCoordinates(lon, lat)} class="btn btn-outline-secondary btn-sm" title="switch lat and lon"><> @@ -59,12 +60,12 @@ type="text" class="form-control form-control-sm" placeholder="longitude" - value="{api_request_params.lon || ''}" /> + bind:value={lon} /> - + {#each zoomLevels() as zoomTitle, i} - + {/each}