diff --git a/frontend/app/framework/angular/title.component.spec.ts b/frontend/app/framework/angular/title.component.spec.ts index 9e7b24db3..18f5deaf3 100644 --- a/frontend/app/framework/angular/title.component.spec.ts +++ b/frontend/app/framework/angular/title.component.spec.ts @@ -31,6 +31,9 @@ describe('TitleComponent', () => { router.setup(x => x.serializeUrl(tree)) .returns(() => 'my-url'); + + titleService.setup(x => x.push(It.isAnyString(), It.isAny(), It.isAny())) + .returns(() => 0); }); it('should set title in service', () => { @@ -51,7 +54,7 @@ describe('TitleComponent', () => { expect().nothing(); - titleService.verify(x => x.push('title2', 'title1', 'my-url'), Times.once()); + titleService.verify(x => x.push('title2', 0, 'my-url'), Times.once()); }); it('should remove title on destroy if set before', () => { diff --git a/frontend/app/framework/angular/title.component.ts b/frontend/app/framework/angular/title.component.ts index 5cedcd0fd..36bfbb0d7 100644 --- a/frontend/app/framework/angular/title.component.ts +++ b/frontend/app/framework/angular/title.component.ts @@ -10,6 +10,7 @@ import { ChangeDetectionStrategy, Component, Input, OnChanges, OnDestroy } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { TitleService } from '@app/framework/internal'; +import { Types } from '@app/shared'; @Component({ selector: 'sqx-title[message]', @@ -17,7 +18,7 @@ import { TitleService } from '@app/framework/internal'; changeDetection: ChangeDetectionStrategy.OnPush, }) export class TitleComponent implements OnDestroy, OnChanges { - private previous: any; + private previousIndex: undefined | number; @Input() public url: any[] = ['./']; @@ -36,13 +37,11 @@ export class TitleComponent implements OnDestroy, OnChanges { const routeTree = this.router.createUrlTree(this.url, { relativeTo: this.route }); const routeUrl = this.router.serializeUrl(routeTree); - this.titleService.push(this.message, this.previous, routeUrl); - - this.previous = this.message; + this.previousIndex = this.titleService.push(this.message, this.previousIndex, routeUrl); } public ngOnDestroy() { - if (this.previous) { + if (Types.isNumber(this.previousIndex)) { this.titleService.pop(); } } diff --git a/frontend/app/framework/services/title.service.spec.ts b/frontend/app/framework/services/title.service.spec.ts index dc5233a3e..c4bd2176b 100644 --- a/frontend/app/framework/services/title.service.spec.ts +++ b/frontend/app/framework/services/title.service.spec.ts @@ -44,16 +44,26 @@ describe('TitleService', () => { expect(document.title).toBe('my-title1 | my-title2'); }); - it('should replace previous element if found', () => { + it('should replace previous index if found', () => { const titleService = new TitleService(new TitlesConfig(), localizer.object); titleService.push('i18n:my-title1'); titleService.push('i18n:my-title2'); - titleService.push('i18n:my-title3', 'i18n:my-title2'); + titleService.push('i18n:my-title3', 1); expect(document.title).toBe('my-title1 | my-title3'); }); + it('should replace previous index if found and not last', () => { + const titleService = new TitleService(new TitlesConfig(), localizer.object); + + titleService.push('i18n:my-title1'); + titleService.push('i18n:my-title2'); + titleService.push('i18n:my-title3', 0); + + expect(document.title).toBe('my-title3 | my-title2'); + }); + it('should concatenate remainging parts if title elements are popped', () => { const titleService = new TitleService(new TitlesConfig(), localizer.object); diff --git a/frontend/app/framework/services/title.service.ts b/frontend/app/framework/services/title.service.ts index c31b4a67b..f731ef43a 100644 --- a/frontend/app/framework/services/title.service.ts +++ b/frontend/app/framework/services/title.service.ts @@ -7,6 +7,7 @@ import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; +import { Types } from './../utils/types'; import { LocalizerService } from './localizer.service'; export class TitlesConfig { @@ -43,7 +44,9 @@ export class TitleService { }); } - public push(value: string, previous?: string, route?: any) { + public push(value: string, index?: number, route?: any) { + let result: number | undefined; + if (value) { const clone = [...this.path$.value]; @@ -52,14 +55,20 @@ export class TitleService { const title = { localized, value, route }; - if (previous && clone[lastIndex].value === previous) { - clone[lastIndex] = title; + if (Types.isNumber(index) && index >= 0 && index <= lastIndex) { + clone[index] = title; + + result = index; } else { clone.push(title); + + result = lastIndex + 1; } this.path$.next(clone); } + + return result; } public pop() {