Browse Source

Format and refactor title-strategy.service

pull/19022/head
masumulu28 3 years ago
parent
commit
3a830d21f8
  1. 66
      npm/ng-packs/packages/core/src/lib/services/title-strategy.service.ts

66
npm/ng-packs/packages/core/src/lib/services/title-strategy.service.ts

@ -1,40 +1,44 @@
import { Injectable, effect, inject } from "@angular/core"; import { Injectable, effect, inject } from '@angular/core';
import { Title } from "@angular/platform-browser"; import { toSignal } from '@angular/core/rxjs-interop';
import { TitleStrategy, RouterStateSnapshot } from "@angular/router"; import { TitleStrategy, RouterStateSnapshot } from '@angular/router';
import { ConfigStateService } from "./config-state.service"; import { Title } from '@angular/platform-browser';
import { LocalizationService } from "./localization.service"; import { ConfigStateService } from './config-state.service';
import { toSignal } from "@angular/core/rxjs-interop"; import { LocalizationService } from './localization.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root',
}) })
export class AbpTitleStrategy extends TitleStrategy { export class AbpTitleStrategy extends TitleStrategy {
protected readonly title = inject(Title); protected readonly title = inject(Title);
protected readonly configState = inject(ConfigStateService); protected readonly configState = inject(ConfigStateService);
protected readonly localizationService = inject(LocalizationService); protected readonly localizationService = inject(LocalizationService);
routerState: RouterStateSnapshot; protected routerState: RouterStateSnapshot;
projectName = toSignal(this.configState.getDeep$("localization.defaultResourceName"), { initialValue: "MyProjectName" });
langugageChange = toSignal(this.localizationService.languageChange$);
override updateTitle(routerState: RouterStateSnapshot) { projectName = toSignal(this.configState.getDeep$('localization.defaultResourceName'), {
let title = this.buildTitle(routerState); initialValue: 'MyProjectName',
this.routerState = routerState; });
langugageChange = toSignal(this.localizationService.languageChange$);
if (title === undefined) { constructor() {
this.title.setTitle(`${this.projectName()}`); super();
return; effect(() => {
} if (this.langugageChange()) {
this.updateTitle(this.routerState);
}
});
}
const localizedTitle = this.localizationService.instant({ key: title, defaultValue: title }); override updateTitle(routerState: RouterStateSnapshot) {
this.title.setTitle(`${localizedTitle} | ${this.projectName()}`); let title = this.buildTitle(routerState);
}
constructor() { if (!title) {
super(); this.title.setTitle(this.projectName());
effect(() => { return;
if (this.langugageChange()) {
this.updateTitle(this.routerState);
}
});
} }
}
this.routerState = routerState;
const localizedTitle = this.localizationService.instant({ key: title, defaultValue: title });
this.title.setTitle(`${localizedTitle} | ${this.projectName()}`);
}
}

Loading…
Cancel
Save