Browse Source

Format and refactor title-strategy.service

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

Loading…
Cancel
Save