Skip to content

Commit

Permalink
Add node params works when hashrouting enabled (#2490)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesDoberer authored Jan 26, 2022
1 parent b2d29d4 commit b673709
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
28 changes: 7 additions & 21 deletions core/src/core-api/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())}`;
}
url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash);
} else {
this._modifySearchParam(params, url.searchParams);
RoutingHelpers.modifySearchParams(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(paramKey);
}
}
}

addNodeParams(params, keepBrowserHistory) {
if (!GenericHelpers.isObject(params)) {
console.log('Params argument must be an object');
Expand All @@ -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')) {
url.hash = RoutingHelpers.addParamsOnHashRouting(params, url.hash, paramPrefix);
} else {
RoutingHelpers.modifySearchParams(params, url.searchParams, paramPrefix);
}

this.handleBrowserHistory(keepBrowserHistory, url.href);
LuigiConfig.configChanged();
Expand Down
24 changes: 24 additions & 0 deletions core/src/utilities/helpers/routing-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,30 @@ class RoutingHelpersClass {
};
component.showAlert(alertSettings, false);
}

// Adds and remove properties from searchParams
modifySearchParams(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(paramKey);
}
}
}

addParamsOnHashRouting(params, hash, paramPrefix) {
let localhash = hash;
const [hashValue, givenQueryParamsString] = localhash.split('?');
const searchParams = new URLSearchParams(givenQueryParamsString);
this.modifySearchParams(params, searchParams, paramPrefix);
localhash = hashValue;
if (searchParams.toString() !== '') {
localhash += `?${decodeURIComponent(searchParams.toString())}`;
}
return localhash;
}
}

export const RoutingHelpers = new RoutingHelpersClass();
13 changes: 7 additions & 6 deletions core/test/core-api/routing.spec.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -136,7 +137,7 @@ describe('Luigi routing', function() {
});
});

describe('modifySearchParam', () => {
describe('modifySearchParams', () => {
beforeEach(() => {
sinon
.stub(LuigiConfig, 'getConfigValue')
Expand All @@ -146,14 +147,14 @@ describe('Luigi routing', function() {
afterEach(() => {
sinon.restore();
});
it('_modifySearchParam', () => {
it('modifySearchParams', () => {
const searchParams = new URLSearchParams('mario=rocks');
LuigiRouting._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');
LuigiRouting._modifySearchParam({ test: 'tets', luigi: 'rocks' }, searchParams, '~');
RoutingHelpers.modifySearchParams({ test: 'tets', luigi: 'rocks' }, searchParams, '~');
assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks');
});
});
Expand Down Expand Up @@ -188,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);
Expand Down
21 changes: 21 additions & 0 deletions core/test/utilities/helpers/routing-helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,25 @@ describe('Routing-helpers', () => {
assert.equal(actual, expected);
});
});
describe('modifySearchParams', () => {
beforeEach(() => {
sinon
.stub(LuigiConfig, 'getConfigValue')
.withArgs('routing.useHashRouting')
.returns(false);
});
afterEach(() => {
sinon.restore();
});
it('modifySearchParams', () => {
const searchParams = new URLSearchParams('mario=rocks');
RoutingHelpers.modifySearchParams({ test: 'tets', luigi: 'rocks', mario: undefined }, searchParams);
assert.equal(searchParams.toString(), 'test=tets&luigi=rocks');
});
it('modifySearchParams with paramPrefix', () => {
const searchParams = new URLSearchParams('~mario=rocks');
RoutingHelpers.modifySearchParams({ test: 'tets', luigi: 'rocks' }, searchParams, '~');
assert.equal(searchParams.toString(), '%7Emario=rocks&%7Etest=tets&%7Eluigi=rocks');
});
});
});

0 comments on commit b673709

Please sign in to comment.