From 52521128a144a4e42653e0bf8c6a6f0fddb8a6bc Mon Sep 17 00:00:00 2001 From: marc tobias Date: Sun, 11 Dec 2022 03:36:26 +0100 Subject: [PATCH] when searching for OSM Url, show detail page result page --- src/lib/helpers.js | 9 +++++++++ src/lib/stores.js | 11 +++++++++++ test/search.js | 19 +++++++++++++++++++ test/unit/helpers.js | 11 ++++++++++- 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/lib/helpers.js b/src/lib/helpers.js index dfe4ced..023da09 100644 --- a/src/lib/helpers.js +++ b/src/lib/helpers.js @@ -13,6 +13,15 @@ export function formatOSMType(sType, bExcludeExternal) { return ''; } +// https://www.openstreetmap.org/relation/123 => ['R', 123] +// w123 => ['W', 123] +export function identifyLinkInQuery(query) { + if (!query) return undefined; + const m = query.match(/\/(relation|way|node)\/(\d+)/) || query.match(/^([nwr])(\d+)$/i); + if (!m) return undefined; + return [m[1][0].toUpperCase(), Number(m[2])]; +} + export function osmLink(aPlace) { if (!aPlace.osm_type) return ''; var sOSMType = formatOSMType(aPlace.osm_type, false); diff --git a/src/lib/stores.js b/src/lib/stores.js index 35344ce..5e755c1 100644 --- a/src/lib/stores.js +++ b/src/lib/stores.js @@ -1,4 +1,5 @@ import { writable } from 'svelte/store'; +import { identifyLinkInQuery } from './helpers.js'; export const map_store = writable(); export const results_store = writable(); @@ -54,6 +55,16 @@ export function refresh_page(pagename, params) { } } + if (pagename === 'search' && params.has('q')) { + const arrTypeAndId = identifyLinkInQuery(params.get('q')); + if (arrTypeAndId instanceof Array) { + pagename = 'details'; + params = new URLSearchParams(); + params.set('osmtype', arrTypeAndId[0]); + params.set('osmid', arrTypeAndId[1]); + } + } + page.set({ tab: pagename, params: params }); last_api_request_url_store.set(null); error_store.set(null); diff --git a/test/search.js b/test/search.js index b398b0d..3470814 100644 --- a/test/search.js +++ b/test/search.js @@ -143,4 +143,23 @@ describe('Search Page', function () { assert.ok(page_header.includes('Paris')); }); }); + + describe('Search for OSM URL', function () { + before(async function () { + page = await browser.newPage(); + await page.goto('http://localhost:9999/search.html'); + await page.type('input[name=q]', 'https://www.openstreetmap.org/relation/3459013#map=11/41.2388/-8.3867'); + await page.click('button[type=submit]'); + await page.waitForSelector('table#address'); + }); + + after(async function () { + await page.close(); + }); + + it('should redirect to detail page search', async function () { + assert.equal(await page.title(), 'Details for R3459013 | Nominatim Demo'); + assert.ok((await page.$eval('.container h1', el => el.textContent)).includes('Porto')); + }); + }); }); diff --git a/test/unit/helpers.js b/test/unit/helpers.js index cea1512..ed57c03 100644 --- a/test/unit/helpers.js +++ b/test/unit/helpers.js @@ -1,8 +1,17 @@ import assert from 'assert'; -import { formatLabel, wikipediaLink } from '../../src/lib/helpers.js'; +import { identifyLinkInQuery, formatLabel, wikipediaLink } from '../../src/lib/helpers.js'; describe('Helpers', function () { + it('.identifyLinkInQuery', function () { + assert.equal(identifyLinkInQuery(''), undefined); + assert.equal(identifyLinkInQuery('http://example.com'), undefined); + + assert.deepStrictEqual(identifyLinkInQuery('https://www.openstreetmap.org/relation/1234#map=11/41.2388/-8.3867'), ['R', 1234]); + assert.deepStrictEqual(identifyLinkInQuery('n1234'), ['N', 1234]); + assert.deepStrictEqual(identifyLinkInQuery('W1234'), ['W', 1234]); + }); + it('.formatLabel', function () { // not enough data assert.equal(formatLabel({}), ''); -- 2.39.5