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 { IqserUser } from '../../iqser-user.model';
import { IqserUserService } from '../../services/iqser-user.service';
import { NamePipeOptions } from '../../types/name-pipe-options';
import { IqserUser } from '../../iqser-user.model';
import { IIqserUser } from '../../types/user.response';
@Component({
@ -14,20 +14,18 @@ import { IIqserUser } from '../../types/user.response';
export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser, Class extends IqserUser & Interface = IqserUser & Interface>
implements OnInit, OnChanges
{
readonly #translateService = inject(TranslateService);
@Input() color = 'lightgray';
@Input() size: 'small' | 'large' = 'small';
@Input() withName = false;
@Input() showYou = false;
@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;
colorClass?: string;
namePipeOptions?: NamePipeOptions;
constructor(
private readonly _userService: IqserUserService<Interface, Class>,
private readonly _translateService: TranslateService,
) {}
constructor(private readonly _userService: IqserUserService<Interface, Class>) {}
_user?: Class;
@ -45,14 +43,14 @@ export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser,
}
get disabled(): boolean {
return !!this._user && !this._isSystemUser && !this._user.hasAnyRole;
return !!this._user && !this.#isSystemUser && !this._user.hasAnyRole;
}
get isCurrentUser(): boolean {
return this._userService.currentUser?.id === this._user?.id;
}
private get _colorClass() {
get #colorClass() {
if (this.isCurrentUser) {
return 'primary-white';
}
@ -68,19 +66,19 @@ export class InitialsAvatarComponent<Interface extends IIqserUser = IIqserUser,
return `${this.color}-dark`;
}
private get _isSystemUser() {
get #isSystemUser() {
return this._user?.id?.toLowerCase() === 'system';
}
@Input() showBorderCondition: <T extends Class = Class>(user: T) => boolean = user => user.isSpecial;
ngOnChanges(): void {
if (this._isSystemUser) {
if (this.#isSystemUser) {
this.colorClass = 'primary-white primary';
return;
}
this.colorClass = this._colorClass;
this.colorClass = this.#colorClass;
}
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 { IqserUser } from './iqser-user.model';
import { IqserUserService } from './services/iqser-user.service';
import { NamePipeOptions } from './types/name-pipe-options';
import { IqserUser } from './iqser-user.model';
function getInitials(name: string) {
if (name.toLowerCase() === 'system') {
@ -20,16 +20,14 @@ function getInitials(name: string) {
name: 'name',
})
export class NamePipe implements PipeTransform {
readonly #translateService = inject(TranslateService);
protected readonly _defaultOptions: Required<NamePipeOptions> = {
defaultValue: this._translateService.instant('unknown') as string,
defaultValue: this.#translateService.instant('unknown') as string,
showYou: false,
showInitials: false,
};
constructor(
private readonly _userService: IqserUserService,
private readonly _translateService: TranslateService,
) {}
constructor(private readonly _userService: IqserUserService) {}
transform(value: IqserUser | string, options: NamePipeOptions = this._defaultOptions): string {
if (!value || value === 'undefined') {
@ -43,7 +41,7 @@ export class NamePipe implements PipeTransform {
}
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;

View File

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