This commit is contained in:
Dan Percic 2023-11-29 17:05:59 +02:00
parent 2bb459961a
commit 59fbd1f78f
3 changed files with 17 additions and 22 deletions

View File

@ -1,8 +1,8 @@
import { ChangeDetectionStrategy, Component, Input, OnChanges, OnInit } from '@angular/core'; import { ChangeDetectionStrategy, Component, inject, Input, OnChanges, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { IqserUser } from '../../iqser-user.model';
import { IqserUserService } from '../../services/iqser-user.service'; import { IqserUserService } from '../../services/iqser-user.service';
import { NamePipeOptions } from '../../types/name-pipe-options'; import { NamePipeOptions } from '../../types/name-pipe-options';
import { IqserUser } from '../../iqser-user.model';
import { IIqserUser } from '../../types/user.response'; import { IIqserUser } from '../../types/user.response';
@Component({ @Component({
@ -14,20 +14,18 @@ import { IIqserUser } from '../../types/user.response';
export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser, Class extends IqserUser & Interface = IqserUser & Interface> export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser, Class extends IqserUser & Interface = IqserUser & Interface>
implements OnInit, OnChanges implements OnInit, OnChanges
{ {
readonly #translateService = inject(TranslateService);
@Input() color = 'lightgray'; @Input() color = 'lightgray';
@Input() size: 'small' | 'large' = 'small'; @Input() size: 'small' | 'large' = 'small';
@Input() withName = false; @Input() withName = false;
@Input() showYou = false; @Input() showYou = false;
@Input() tooltipPosition: 'below' | 'above' = 'above'; @Input() tooltipPosition: 'below' | 'above' = 'above';
@Input() defaultValue: string = this._translateService.instant('initials-avatar.unassigned'); @Input() defaultValue: string = this.#translateService.instant('initials-avatar.unassigned');
@Input() showTooltip = true; @Input() showTooltip = true;
colorClass?: string; colorClass?: string;
namePipeOptions?: NamePipeOptions; namePipeOptions?: NamePipeOptions;
constructor( constructor(private readonly _userService: IqserUserService<Interface, Class>) {}
private readonly _userService: IqserUserService<Interface, Class>,
private readonly _translateService: TranslateService,
) {}
_user?: Class; _user?: Class;
@ -45,14 +43,14 @@ export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser,
} }
get disabled(): boolean { get disabled(): boolean {
return !!this._user && !this._isSystemUser && !this._user.hasAnyRole; return !!this._user && !this.#isSystemUser && !this._user.hasAnyRole;
} }
get isCurrentUser(): boolean { get isCurrentUser(): boolean {
return this._userService.currentUser?.id === this._user?.id; return this._userService.currentUser?.id === this._user?.id;
} }
private get _colorClass() { get #colorClass() {
if (this.isCurrentUser) { if (this.isCurrentUser) {
return 'primary-white'; return 'primary-white';
} }
@ -68,19 +66,19 @@ export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser,
return `${this.color}-dark`; return `${this.color}-dark`;
} }
private get _isSystemUser() { get #isSystemUser() {
return this._user?.id?.toLowerCase() === 'system'; return this._user?.id?.toLowerCase() === 'system';
} }
@Input() showBorderCondition: <T extends Class = Class>(user: T) => boolean = user => user.isSpecial; @Input() showBorderCondition: <T extends Class = Class>(user: T) => boolean = user => user.isSpecial;
ngOnChanges(): void { ngOnChanges(): void {
if (this._isSystemUser) { if (this.#isSystemUser) {
this.colorClass = 'primary-white primary'; this.colorClass = 'primary-white primary';
return; return;
} }
this.colorClass = this._colorClass; this.colorClass = this.#colorClass;
} }
ngOnInit(): void { ngOnInit(): void {

View File

@ -1,8 +1,8 @@
import { Pipe, PipeTransform } from '@angular/core'; import { inject, Pipe, PipeTransform } from '@angular/core';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { IqserUser } from './iqser-user.model';
import { IqserUserService } from './services/iqser-user.service'; import { IqserUserService } from './services/iqser-user.service';
import { NamePipeOptions } from './types/name-pipe-options'; import { NamePipeOptions } from './types/name-pipe-options';
import { IqserUser } from './iqser-user.model';
function getInitials(name: string) { function getInitials(name: string) {
if (name.toLowerCase() === 'system') { if (name.toLowerCase() === 'system') {
@ -20,16 +20,14 @@ function getInitials(name: string) {
name: 'name', name: 'name',
}) })
export class NamePipe implements PipeTransform { export class NamePipe implements PipeTransform {
readonly #translateService = inject(TranslateService);
protected readonly _defaultOptions: Required<NamePipeOptions> = { protected readonly _defaultOptions: Required<NamePipeOptions> = {
defaultValue: this._translateService.instant('unknown') as string, defaultValue: this.#translateService.instant('unknown') as string,
showYou: false, showYou: false,
showInitials: false, showInitials: false,
}; };
constructor( constructor(private readonly _userService: IqserUserService) {}
private readonly _userService: IqserUserService,
private readonly _translateService: TranslateService,
) {}
transform(value: IqserUser | string, options: NamePipeOptions = this._defaultOptions): string { transform(value: IqserUser | string, options: NamePipeOptions = this._defaultOptions): string {
if (!value || value === 'undefined') { if (!value || value === 'undefined') {
@ -43,7 +41,7 @@ export class NamePipe implements PipeTransform {
} }
if (options.showYou && this._isCurrentUser(value)) { if (options.showYou && this._isCurrentUser(value)) {
name = `${name} (${this._translateService.instant('initials-avatar.you')})`; name = `${name} (${this.#translateService.instant('initials-avatar.you')})`;
} }
return options.showInitials ? getInitials(name) : name; return options.showInitials ? getInitials(name) : name;

View File

@ -11,7 +11,6 @@
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"noPropertyAccessFromIndexSignature": true, "noPropertyAccessFromIndexSignature": true,
"sourceMap": true, "sourceMap": true,
"downlevelIteration": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"moduleResolution": "node", "moduleResolution": "node",
"importHelpers": true, "importHelpers": true,
@ -24,7 +23,7 @@
"@biesbjerg/ngx-translate-extract-marker": ["src/lib/translations/ngx-translate-extract-marker"] "@biesbjerg/ngx-translate-extract-marker": ["src/lib/translations/ngx-translate-extract-marker"]
} }
}, },
"include": ["./**/*"], "include": ["./src/**/*"],
"angularCompilerOptions": { "angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false, "enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true, "strictInjectionParameters": true,