File
Implements
Index
Properties
|
|
Methods
|
|
Inputs
|
|
randomColor
|
Type : boolean
|
Default value : false
|
|
size
|
Type : string
|
Default value : ''
|
|
Methods
Private
createInititals
|
createInititals()
|
|
|
Public
circleColor
|
Type : string
|
|
Private
colors
|
Type : []
|
Default value : [
// '#EB7181', // red
'#306933', // green
// '#000000', // black
// '#3670B2', // blue
// '#4E9E87',
// '#7E4C8D',
]
|
|
random
|
Default value : Math.random().toString(36).slice(2)
|
|
Private
randomcolors
|
Type : []
|
Default value : [
'#EB7181', // red
'#006400', // green
'#000000', // black
'#3670B2', // blue
'#4E9E87',
'#7E4C8D',
]
|
|
Public
showInitials
|
Default value : false
|
|
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'lib-avatar-photo',
templateUrl: './avatar-photo.component.html',
styleUrls: ['./avatar-photo.component.css']
})
export class AvatarPhotoComponent implements OnInit {
@Input() photoUrl!: string;
@Input() name!: string;
@Input() size = '';
@Input() randomColor = false;
@Input() initials?: string;
public showInitials = false;
public circleColor!: string;
random = Math.random().toString(36).slice(2);
// public initials!: string
private colors = [
// '#EB7181', // red
'#306933', // green
// '#000000', // black
// '#3670B2', // blue
// '#4E9E87',
// '#7E4C8D',
];
private randomcolors = [
'#EB7181', // red
'#006400', // green
'#000000', // black
'#3670B2', // blue
'#4E9E87',
'#7E4C8D',
];
ngOnInit() {
if (!this.photoUrl) {
this.showInitials = true;
if (!this.initials) {
this.createInititals();
}
const randomIndex = Math.floor(Math.random() * Math.floor(this.colors.length));
this.circleColor = this.colors[randomIndex];
if (this.randomColor) {
const randomIndex1 = Math.floor(Math.random() * Math.floor(this.randomcolors.length));
this.circleColor = this.randomcolors[randomIndex1];
}
}
}
private createInititals(): void {
let initials = '';
const array = `${this.name} `.toString().split(' ');
if (array[0] !== 'undefined' && typeof array[1] !== 'undefined') {
initials += array[0].charAt(0);
initials += array[1].charAt(0);
} else {
for (let i = 0; i < this.name.length; i += 1) {
if (this.name.charAt(i) === ' ') {
continue;
}
if (this.name.charAt(i) === this.name.charAt(i)) {
initials += this.name.charAt(i);
if (initials.length === 2) {
break;
}
}
}
}
this.initials = initials.toUpperCase();
}
}
<div [ngClass]="{'circle-s' : size=== 's',
'circle-m' : size=== 'm',
'circle-l' : size=== 'l',
'circle-xl' : size=== 'xl',
'circle-xxl' : size=== 'xxl',
'circle-xl-s' : size=== 'xl-s',
'circle-xxl-s': size=== 'xxl-s'}" [ngStyle]="{'background-color': circleColor }">
<img *ngIf="!showInitials" src="{{photoUrl}}" [alt]="photoUrl? '' :(name || initials || size || random)">
<div *ngIf="showInitials" class="initials" i18n-aria-label>
{{ initials }}
</div>
</div>
Legend
Html element with directive