From 5bd67bd36c0813946cd5fd82da7f77fb0b50b4b6 Mon Sep 17 00:00:00 2001 From: "Doberer, Johannes" Date: Fri, 14 Jan 2022 09:30:45 +0100 Subject: [PATCH 1/6] add node params works if hash routing is enabled --- core/src/core-api/routing.js | 28 +++++-------------- core/src/utilities/helpers/routing-helpers.js | 22 +++++++++++++++ core/test/core-api/routing.spec.js | 9 +++--- .../utilities/helpers/routing-helpers.spec.js | 21 ++++++++++++++ 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/core/src/core-api/routing.js b/core/src/core-api/routing.js index 94844a21cf..f0c6d1fcb9 100644 --- a/core/src/core-api/routing.js +++ b/core/src/core-api/routing.js @@ -51,33 +51,15 @@ class LuigiRouting { } const url = new URL(location); if (LuigiConfig.getConfigValue('routing.useHashRouting')) { - const [hashValue, givenQueryParamsString] = url.hash.split('?'); - const searchParams = new URLSearchParams(givenQueryParamsString); - this._modifySearchParam(params, searchParams); - url.hash = hashValue; - if (searchParams.toString() !== '') { - url.hash += `?${decodeURIComponent(searchParams.toString())}`; - } + RoutingHelpers.addParamsOnHashRouting(url, params); } else { - this._modifySearchParam(params, url.searchParams); + RoutingHelpers.modifySearchParam(params, url.searchParams); } this.handleBrowserHistory(keepBrowserHistory, url.href); LuigiConfig.configChanged(); } - // Adds and remove properties from searchParams - _modifySearchParam(params, searchParams, paramPrefix) { - for (const [key, value] of Object.entries(params)) { - const paramKey = paramPrefix ? `${paramPrefix}${key}` : key; - - searchParams.set(paramKey, value); - if (value === undefined) { - searchParams.delete(key); - } - } - } - addNodeParams(params, keepBrowserHistory) { if (!GenericHelpers.isObject(params)) { console.log('Params argument must be an object'); @@ -86,7 +68,11 @@ class LuigiRouting { const paramPrefix = RoutingHelpers.getContentViewParamPrefix(); const url = new URL(location); - this._modifySearchParam(params, url.searchParams, paramPrefix); + if (LuigiConfig.getConfigValue('routing.useHashRouting')) { + RoutingHelpers.addParamsOnHashRouting(url, params, paramPrefix); + }else{ + RoutingHelpers.modifySearchParam(params, url.searchParams, paramPrefix); + } this.handleBrowserHistory(keepBrowserHistory, url.href); LuigiConfig.configChanged(); diff --git a/core/src/utilities/helpers/routing-helpers.js b/core/src/utilities/helpers/routing-helpers.js index f1278b9633..fe856ca8e6 100644 --- a/core/src/utilities/helpers/routing-helpers.js +++ b/core/src/utilities/helpers/routing-helpers.js @@ -597,6 +597,28 @@ class RoutingHelpersClass { }; component.showAlert(alertSettings, false); } + + // Adds and remove properties from searchParams + modifySearchParam(params, searchParams, paramPrefix) { + for (const [key, value] of Object.entries(params)) { + const paramKey = paramPrefix ? `${paramPrefix}${key}` : key; + + searchParams.set(paramKey, value); + if (value === undefined) { + searchParams.delete(key); + } + } + } + + addParamsOnHashRouting(url, params, paramPrefix){ + const [hashValue, givenQueryParamsString] = url.hash.split('?'); + const searchParams = new URLSearchParams(givenQueryParamsString); + this.modifySearchParam(params, searchParams, paramPrefix); + url.hash = hashValue; + if (searchParams.toString() !== '') { + url.hash += `?${decodeURIComponent(searchParams.toString())}`; + } + } } export const RoutingHelpers = new RoutingHelpersClass(); diff --git a/core/test/core-api/routing.spec.js b/core/test/core-api/routing.spec.js index 1e54b6ebdb..c4f8a329db 100644 --- a/core/test/core-api/routing.spec.js +++ b/core/test/core-api/routing.spec.js @@ -1,5 +1,6 @@ import { afterEach } from 'mocha'; import { LuigiRouting, LuigiConfig } from '../../src/core-api'; +import { RoutingHelpers } from '../../src/utilities/helpers'; const chai = require('chai'); const assert = chai.assert; @@ -140,14 +141,14 @@ describe('Luigi routing', function() { afterEach(() => { sinon.restore(); }); - it('_modifySearchParam', () => { + it('modifySearchParam', () => { const searchParams = new URLSearchParams('mario=rocks'); - LuigiRouting._modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams); + RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams); assert.equal(searchParams.toString(), 'test=tets&luigi=rocks'); }); - it('_modifySearchParam with paramPrefix', () => { + it('modifySearchParam with paramPrefix', () => { const searchParams = new URLSearchParams('~mario=rocks'); - LuigiRouting._modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~'); + RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~'); assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks'); }); }); diff --git a/core/test/utilities/helpers/routing-helpers.spec.js b/core/test/utilities/helpers/routing-helpers.spec.js index dcf9878330..fe9733352f 100644 --- a/core/test/utilities/helpers/routing-helpers.spec.js +++ b/core/test/utilities/helpers/routing-helpers.spec.js @@ -850,4 +850,25 @@ describe('Routing-helpers', () => { assert.equal(actual, expected); }); }); + describe('modifySearchParam', () => { + beforeEach(() => { + sinon + .stub(LuigiConfig, 'getConfigValue') + .withArgs('routing.useHashRouting') + .returns(false); + }); + afterEach(() => { + sinon.restore(); + }); + it('modifySearchParam', () => { + const searchParams = new URLSearchParams('mario=rocks'); + RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams); + assert.equal(searchParams.toString(), 'test=tets&luigi=rocks'); + }); + it('modifySearchParam with paramPrefix', () => { + const searchParams = new URLSearchParams('~mario=rocks'); + RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~'); + assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks'); + }); + }); }); From 5274587749f44b95f9d23fed694ad673d79fb4fd Mon Sep 17 00:00:00 2001 From: "Doberer, Johannes" Date: Thu, 20 Jan 2022 21:42:46 +0100 Subject: [PATCH 2/6] refactor --- core/src/core-api/routing.js | 4 ++-- core/src/utilities/helpers/routing-helpers.js | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/core/src/core-api/routing.js b/core/src/core-api/routing.js index f0c6d1fcb9..b063fb1b0b 100644 --- a/core/src/core-api/routing.js +++ b/core/src/core-api/routing.js @@ -51,7 +51,7 @@ class LuigiRouting { } const url = new URL(location); if (LuigiConfig.getConfigValue('routing.useHashRouting')) { - RoutingHelpers.addParamsOnHashRouting(url, params); + url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash); } else { RoutingHelpers.modifySearchParam(params, url.searchParams); } @@ -69,7 +69,7 @@ class LuigiRouting { const paramPrefix = RoutingHelpers.getContentViewParamPrefix(); const url = new URL(location); if (LuigiConfig.getConfigValue('routing.useHashRouting')) { - RoutingHelpers.addParamsOnHashRouting(url, params, paramPrefix); + url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash, paramPrefix); }else{ RoutingHelpers.modifySearchParam(params, url.searchParams, paramPrefix); } diff --git a/core/src/utilities/helpers/routing-helpers.js b/core/src/utilities/helpers/routing-helpers.js index fe856ca8e6..067a3c1e8f 100644 --- a/core/src/utilities/helpers/routing-helpers.js +++ b/core/src/utilities/helpers/routing-helpers.js @@ -605,19 +605,21 @@ class RoutingHelpersClass { searchParams.set(paramKey, value); if (value === undefined) { - searchParams.delete(key); + searchParams.delete(paramKey);//TODO change to key } } } - addParamsOnHashRouting(url, params, paramPrefix){ - const [hashValue, givenQueryParamsString] = url.hash.split('?'); + addParamsOnHashRouting(params, hash, paramPrefix){ + let localhash = hash; + const [hashValue, givenQueryParamsString] = localhash.split('?'); const searchParams = new URLSearchParams(givenQueryParamsString); this.modifySearchParam(params, searchParams, paramPrefix); - url.hash = hashValue; + localhash = hashValue; if (searchParams.toString() !== '') { - url.hash += `?${decodeURIComponent(searchParams.toString())}`; + localhash += `?${decodeURIComponent(searchParams.toString())}`; } + return localhash; } } From 107165bc99e5c0fcc04b17cc41b522da415ee84a Mon Sep 17 00:00:00 2001 From: Johannes Doberer Date: Mon, 24 Jan 2022 20:46:26 +0100 Subject: [PATCH 3/6] Update core/src/core-api/routing.js Co-authored-by: Stanley Hsu --- core/src/core-api/routing.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/core-api/routing.js b/core/src/core-api/routing.js index b063fb1b0b..92c17f33b2 100644 --- a/core/src/core-api/routing.js +++ b/core/src/core-api/routing.js @@ -70,7 +70,7 @@ class LuigiRouting { const url = new URL(location); if (LuigiConfig.getConfigValue('routing.useHashRouting')) { url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash, paramPrefix); - }else{ + } else { RoutingHelpers.modifySearchParam(params, url.searchParams, paramPrefix); } From 2be5df3e550cbd7d3e2225498ef3fde73f5ef405 Mon Sep 17 00:00:00 2001 From: Johannes Doberer Date: Tue, 25 Jan 2022 14:19:03 +0100 Subject: [PATCH 4/6] Update core/src/utilities/helpers/routing-helpers.js Co-authored-by: Stanley Hsu --- core/src/utilities/helpers/routing-helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/utilities/helpers/routing-helpers.js b/core/src/utilities/helpers/routing-helpers.js index 067a3c1e8f..8e8d9e8bca 100644 --- a/core/src/utilities/helpers/routing-helpers.js +++ b/core/src/utilities/helpers/routing-helpers.js @@ -599,7 +599,7 @@ class RoutingHelpersClass { } // Adds and remove properties from searchParams - modifySearchParam(params, searchParams, paramPrefix) { + modifySearchParams(params, searchParams, paramPrefix) { for (const [key, value] of Object.entries(params)) { const paramKey = paramPrefix ? `${paramPrefix}${key}` : key; From 81bbd71f3893fd69a46f8ecaeede18144291e45e Mon Sep 17 00:00:00 2001 From: Johannes Doberer Date: Tue, 25 Jan 2022 14:21:15 +0100 Subject: [PATCH 5/6] Update routing-helpers.js --- core/src/utilities/helpers/routing-helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/utilities/helpers/routing-helpers.js b/core/src/utilities/helpers/routing-helpers.js index 8e8d9e8bca..e3c8759aca 100644 --- a/core/src/utilities/helpers/routing-helpers.js +++ b/core/src/utilities/helpers/routing-helpers.js @@ -605,7 +605,7 @@ class RoutingHelpersClass { searchParams.set(paramKey, value); if (value === undefined) { - searchParams.delete(paramKey);//TODO change to key + searchParams.delete(paramKey); } } } From f4a2f74f6b04c2ee4270887a3cb77916c94c1ba3 Mon Sep 17 00:00:00 2001 From: "Doberer, Johannes" Date: Tue, 25 Jan 2022 14:29:40 +0100 Subject: [PATCH 6/6] renaming --- core/src/core-api/routing.js | 4 ++-- core/src/utilities/helpers/routing-helpers.js | 6 +++--- core/test/core-api/routing.spec.js | 12 ++++++------ core/test/utilities/helpers/routing-helpers.spec.js | 10 +++++----- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/src/core-api/routing.js b/core/src/core-api/routing.js index 92c17f33b2..9d0c399c0f 100644 --- a/core/src/core-api/routing.js +++ b/core/src/core-api/routing.js @@ -53,7 +53,7 @@ class LuigiRouting { if (LuigiConfig.getConfigValue('routing.useHashRouting')) { url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash); } else { - RoutingHelpers.modifySearchParam(params, url.searchParams); + RoutingHelpers.modifySearchParams(params, url.searchParams); } this.handleBrowserHistory(keepBrowserHistory, url.href); @@ -71,7 +71,7 @@ class LuigiRouting { if (LuigiConfig.getConfigValue('routing.useHashRouting')) { url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash, paramPrefix); } else { - RoutingHelpers.modifySearchParam(params, url.searchParams, paramPrefix); + RoutingHelpers.modifySearchParams(params, url.searchParams, paramPrefix); } this.handleBrowserHistory(keepBrowserHistory, url.href); diff --git a/core/src/utilities/helpers/routing-helpers.js b/core/src/utilities/helpers/routing-helpers.js index e3c8759aca..cacb0e481e 100644 --- a/core/src/utilities/helpers/routing-helpers.js +++ b/core/src/utilities/helpers/routing-helpers.js @@ -610,14 +610,14 @@ class RoutingHelpersClass { } } - addParamsOnHashRouting(params, hash, paramPrefix){ + addParamsOnHashRouting(params, hash, paramPrefix) { let localhash = hash; const [hashValue, givenQueryParamsString] = localhash.split('?'); const searchParams = new URLSearchParams(givenQueryParamsString); - this.modifySearchParam(params, searchParams, paramPrefix); + this.modifySearchParams(params, searchParams, paramPrefix); localhash = hashValue; if (searchParams.toString() !== '') { - localhash += `?${decodeURIComponent(searchParams.toString())}`; + localhash += `?${decodeURIComponent(searchParams.toString())}`; } return localhash; } diff --git a/core/test/core-api/routing.spec.js b/core/test/core-api/routing.spec.js index 47ec8d6fd8..b151306b53 100644 --- a/core/test/core-api/routing.spec.js +++ b/core/test/core-api/routing.spec.js @@ -137,7 +137,7 @@ describe('Luigi routing', function() { }); }); - describe('modifySearchParam', () => { + describe('modifySearchParams', () => { beforeEach(() => { sinon .stub(LuigiConfig, 'getConfigValue') @@ -147,14 +147,14 @@ describe('Luigi routing', function() { afterEach(() => { sinon.restore(); }); - it('modifySearchParam', () => { + it('modifySearchParams', () => { const searchParams = new URLSearchParams('mario=rocks'); - RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams); + RoutingHelpers.modifySearchParams({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams); assert.equal(searchParams.toString(), 'test=tets&luigi=rocks'); }); - it('modifySearchParam with paramPrefix', () => { + it('modifySearchParams with paramPrefix', () => { const searchParams = new URLSearchParams('~mario=rocks'); - RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~'); + RoutingHelpers.modifySearchParams({ test: 'tets', luigi: 'rocks' }, searchParams, '~'); assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks'); }); }); @@ -189,7 +189,7 @@ describe('Luigi routing', function() { LuigiRouting.addNodeParams('bar', true); sinon.assert.calledWith(console.log, 'Params argument must be an object'); }); - it('remove node param if value of params object is undefined', ()=>{ + it('remove node param if value of params object is undefined', () => { window.state = {}; global.location = 'http://some.url.de'; LuigiRouting.addNodeParams({ test: undefined }, false); diff --git a/core/test/utilities/helpers/routing-helpers.spec.js b/core/test/utilities/helpers/routing-helpers.spec.js index fe9733352f..3cb4b1e5d3 100644 --- a/core/test/utilities/helpers/routing-helpers.spec.js +++ b/core/test/utilities/helpers/routing-helpers.spec.js @@ -850,7 +850,7 @@ describe('Routing-helpers', () => { assert.equal(actual, expected); }); }); - describe('modifySearchParam', () => { + describe('modifySearchParams', () => { beforeEach(() => { sinon .stub(LuigiConfig, 'getConfigValue') @@ -860,14 +860,14 @@ describe('Routing-helpers', () => { afterEach(() => { sinon.restore(); }); - it('modifySearchParam', () => { + it('modifySearchParams', () => { const searchParams = new URLSearchParams('mario=rocks'); - RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams); + RoutingHelpers.modifySearchParams({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams); assert.equal(searchParams.toString(), 'test=tets&luigi=rocks'); }); - it('modifySearchParam with paramPrefix', () => { + it('modifySearchParams with paramPrefix', () => { const searchParams = new URLSearchParams('~mario=rocks'); - RoutingHelpers.modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~'); + RoutingHelpers.modifySearchParams({ test: 'tets', luigi: 'rocks' }, searchParams, '~'); assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks'); }); });