fix(aio): selectedNodes should work for URLs ending with a slash

The mapping was not accounting for the fact that URLs with or without
a trailing slash are actually the same node.
This commit is contained in:
Peter Bacon Darwin 2017-03-26 12:35:40 +01:00 committed by Pete Bacon Darwin
parent fd72fad8fd
commit 99951911d5
2 changed files with 24 additions and 4 deletions

View File

@ -82,7 +82,7 @@ describe('NavigationService', () => {
const nodeTree: NavigationNode[] = [ const nodeTree: NavigationNode[] = [
{ title: 'a', children: [ { title: 'a', children: [
{ url: 'b', title: 'b', children: [ { url: 'b', title: 'b', children: [
{ url: 'c', title: 'c' }, { url: 'c/', title: 'c' },
{ url: 'd', title: 'd' } { url: 'd', title: 'd' }
] }, ] },
{ url: 'e', title: 'e' } { url: 'e', title: 'e' }
@ -124,6 +124,21 @@ describe('NavigationService', () => {
location.urlSubject.next('g'); location.urlSubject.next('g');
expect(currentNodes).toEqual([]); expect(currentNodes).toEqual([]);
}); });
it('should ignore trailing slashes on URLs in the navmap', () => {
location.urlSubject.next('c');
expect(currentNodes).toEqual([
nodeTree[0].children[0].children[0],
nodeTree[0].children[0],
nodeTree[0]
]);
location.urlSubject.next('c/');
expect(currentNodes).toEqual([
nodeTree[0].children[0].children[0],
nodeTree[0].children[0],
nodeTree[0]
]);
});
}); });
describe('versionInfo', () => { describe('versionInfo', () => {

View File

@ -108,7 +108,11 @@ export class NavigationService {
const selectedNodes = combineLatest( const selectedNodes = combineLatest(
navigationViews.map(this.computeUrlToNodesMap), navigationViews.map(this.computeUrlToNodesMap),
this.location.currentUrl, this.location.currentUrl,
(navMap, url) => navMap[url] || []) (navMap, url) => {
// strip trailing slashes from the currentUrl - they are not relevant to matching against the navMap
url = url.replace(/\/$/, '');
return navMap[url] || [];
})
.publishReplay(1); .publishReplay(1);
selectedNodes.connect(); selectedNodes.connect();
return selectedNodes; return selectedNodes;
@ -127,9 +131,10 @@ export class NavigationService {
function walkNodes(node: NavigationNode, ancestors: NavigationNode[] = []) { function walkNodes(node: NavigationNode, ancestors: NavigationNode[] = []) {
const nodes = [node, ...ancestors]; const nodes = [node, ...ancestors];
if (node.url) {
// only map to this node if it has a url associated with it // only map to this node if it has a url associated with it
navMap[node.url] = nodes; if (node.url) {
// Strip off trailing slashes from nodes in the navMap - they are not relevant to matching
navMap[node.url.replace(/\/$/, '')] = nodes;
} }
if (node.children) { if (node.children) {
node.children.forEach(child => walkNodes(child, nodes)); node.children.forEach(child => walkNodes(child, nodes));