Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 14x 14x 6x 8x 8x 8x 1x 1x 7x 7x 7x 8x 8x 8x 3x 8x 13x 13x 14x | /**
* Copyright (c) Siemens 2016 - 2025
* SPDX-License-Identifier: MIT
*/
import { NgClass } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
computed,
effect,
inject,
input,
numberAttribute
} from '@angular/core';
import { EntityStatusType } from '@siemens/element-ng/common';
import { SiIconComponent, STATUS_ICON_CONFIG } from '@siemens/element-ng/icon';
import { TranslatableString } from '@siemens/element-translate-ng/translate';
import { SiAvatarBackgroundColorDirective } from './si-avatar-background-color.directive';
export type AvatarSize = 'tiny' | 'xsmall' | 'small' | 'regular' | 'large' | 'xlarge';
@Component({
selector: 'si-avatar',
imports: [NgClass, SiIconComponent],
templateUrl: './si-avatar.component.html',
styleUrl: './si-avatar.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
host: {
'[class]': 'size()'
},
hostDirectives: [
{
directive: SiAvatarBackgroundColorDirective,
inputs: ['color', 'autoColor']
}
]
})
export class SiAvatarComponent {
private readonly statusIcons = inject(STATUS_ICON_CONFIG);
/**
* Size of the component.
*
* @defaultValue 'regular'
*/
readonly size = input<AvatarSize>('regular');
/** Image src URL when using an image. */
readonly imageUrl = input<string>();
/** Icon name when using an icon. */
readonly icon = input<string>();
/**
* Initials to be displayed as default avatar if no `icon` or `imageUrl` are provided.
* If also no initials are provided, they will be automatically calculated from the `altText`.
* The value will be used to calculate the background color when `autoColor` is true.
*/
readonly initials = input<string>();
/**
* The desired color index from $element-data-* color tokens. This can be set to any kind of
* positive integer that is then mapped to a color index.
* A better way to set a pseudo-random color is to set * {@link autoColor} to `true`.
*
* @defaultValue undefined
*/
readonly color = input<number | undefined, unknown>(undefined, { transform: numberAttribute });
/** The `alt` text for image, `title` for other modes. */
readonly altText = input.required<string>();
/**
* The status (success, info, warning, caution, danger, critical, pending, progress) to be
* visualized.
*/
readonly status = input<EntityStatusType>();
/**
* aria-label for status
*/
readonly statusAriaLabel = input<TranslatableString>();
protected readonly statusIcon = computed(() => {
const status = this.status();
return status ? this.statusIcons[status] : undefined;
});
protected readonly displayInitials = computed(() => {
const initials = this.initials();
if (initials) {
return initials;
}
const name = this.altText()
.replaceAll(/\([^)]*\)/g, '')
.trim();
const byComma = name.split(/,\s*/);
let first: string;
let last: string;
if (byComma.length > 1) {
last = byComma[0];
first = byComma[1];
} else {
const parts = name.split(' ');
first = parts.shift() ?? '';
last = parts.pop() ?? '';
}
if (first) {
first = first[0].toLocaleUpperCase();
}
if (last) {
last = last[0].toLocaleUpperCase();
}
return first + last;
});
private readonly autoBackgroundColorDirective = inject(SiAvatarBackgroundColorDirective);
constructor() {
effect(() => {
this.autoBackgroundColorDirective.calculateColorFromInitials(this.displayInitials());
});
}
}
|