src/module/component/avatar.component.ts
The main component for the avatar
<avatar [email]="email" [displayType]="'circle'"></avatar>
selector | avatar |
styleUrls | avatar.component.scss |
templateUrl | ./avatar.component.html |
Properties |
Methods |
Inputs |
constructor()
|
Defined in src/module/component/avatar.component.ts:65
|
background
|
Value to set a fixed color via HEX code
Default value: |
Defined in src/module/component/avatar.component.ts:41
|
defaultProtocol
|
Value to set a default protocol
Type:
Default value: |
Defined in src/module/component/avatar.component.ts:59
|
displayType
|
Value to set the display type
Default value: |
Defined in src/module/component/avatar.component.ts:47
|
email
|
The user email adresse for Gravatar.com
Type: |
Defined in src/module/component/avatar.component.ts:23
|
letter
|
Value to set a default letter
Default value: |
Defined in src/module/component/avatar.component.ts:53
|
name
|
The full name of the user for the avatar letter
Type: |
Defined in src/module/component/avatar.component.ts:29
|
size
|
The display size
Default value: |
Defined in src/module/component/avatar.component.ts:35
|
getAvatar |
getAvatar()
|
Defined in src/module/component/avatar.component.ts:105
|
Create a Gravatar API url
Returns :
void
|
getLetter |
getLetter()
|
Defined in src/module/component/avatar.component.ts:86
|
Set the avatar letter based on full name or email
Returns :
void
|
getRandomColor |
getRandomColor()
|
Defined in src/module/component/avatar.component.ts:74
|
Randomly generates a HEX color
Returns :
string
|
ngOnChanges |
ngOnChanges()
|
Defined in src/module/component/avatar.component.ts:152
|
Updates avatar image and letter on email updates
Returns :
void
|
ngOnInit |
ngOnInit()
|
Defined in src/module/component/avatar.component.ts:142
|
Set avatar size, background and display type
Returns :
void
|
setCssProps |
setCssProps()
|
Defined in src/module/component/avatar.component.ts:118
|
Returns :
void
|
displayImage |
displayImage:
|
Default value : true
|
Defined in src/module/component/avatar.component.ts:62
|
fontColor |
fontColor:
|
Default value : '#FFFFFF'
|
Defined in src/module/component/avatar.component.ts:64
|
fontSize |
fontSize:
|
Default value : 49
|
Defined in src/module/component/avatar.component.ts:63
|
gravatarUrl |
gravatarUrl:
|
Type : string
|
Defined in src/module/component/avatar.component.ts:61
|
props |
props:
|
Type : any
|
Default value : null
|
Defined in src/module/component/avatar.component.ts:65
|
import {Component, Input, OnChanges, OnInit} from '@angular/core';
import {Md5} from '../Md5';
/**
* The main component for the avatar
*
* @example
* <avatar [email]="email" [displayType]="'circle'"></avatar>
*
*/
@Component({
selector: 'avatar',
templateUrl: './avatar.component.html',
styleUrls: ['./avatar.component.scss']
})
export class AvatarComponent implements OnInit, OnChanges {
/**
* The user email adresse for Gravatar.com
*/
@Input('email') email: string;
/**
* The full name of the user for the avatar letter
* @type {string}
*/
@Input('name') name: string;
/**
* The display size
* @type {number}
*/
@Input('size') size = 100;
/**
* Value to set a fixed color via HEX code
* @type {string}
*/
@Input('background') background = this.getRandomColor();
/**
* Value to set the display type
* @type {string} - none|circle|rounded
*/
@Input('displayType') displayType = 'none';
/**
* Value to set a default letter
* @type {string}
*/
@Input('letter') letter = '?';
/**
* Value to set a default protocol
* @type {string|null} - http|https
*/
@Input('defaultProtocol') defaultProtocol: string = null;
gravatarUrl: string;
displayImage = true;
fontSize = 49;
fontColor = '#FFFFFF';
props: any = null;
constructor() {
}
/**
* Randomly generates a HEX color
* @return {string}
*/
getRandomColor(): string {
const letters = '0123456789ABCDEF'.split('');
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
/**
* Set the avatar letter based on full name or email
*/
getLetter(): void {
if (this.name && this.name.length) {
const nameInitials = this.name.match(/\b(\w)/g);
if (nameInitials) {
const nameLetters = nameInitials.slice(0, 3).join('');
this.letter = nameLetters.toUpperCase();
} else {
this.letter = this.name[0];
}
} else if (this.email && this.email.length) {
const emailInitials = this.email.split('@')[0].match(/\b(\w)/g);
const emailLetters = emailInitials.slice(0, 3).join('');
this.letter = emailLetters.toUpperCase();
}
}
/**
* Create a Gravatar API url
*/
getAvatar(): void {
// tslint:disable-next-line
if (this.email && /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(this.email)) {
const hash = Md5.init(this.email);
const protocol = this.defaultProtocol ? this.defaultProtocol + ':' : '';
this.gravatarUrl = `${protocol}//www.gravatar.com/avatar/${hash}?s=${this.size}&d=404`;
this.displayImage = true;
} else {
this.displayImage = false;
}
}
setCssProps() {
this.fontSize = (39 * this.size) / 100;
this.props = {
size: `${this.size}px`,
lineheight: `${this.size}px`,
background: this.background,
fontSize: `${this.fontSize}px`
};
switch (this.displayType) {
case 'rounded':
this.props['borderradius'] = '5%';
break;
case 'circle':
this.props['borderradius'] = '50%';
break;
default:
this.props['borderradius'] = '0';
}
}
/**
* Set avatar size, background and display type
*/
ngOnInit() {
this.setCssProps();
this.getLetter();
this.getAvatar();
}
/**
* Updates avatar image and letter on email updates
*/
ngOnChanges() {
this.getAvatar();
this.getLetter();
}
}
<div class="avatar"
*ngIf="props"
[style.background-color]="props.background"
[style.width]="props.size"
[style.line-height]='props.lineheight'
[style.height]='props.size'
[style.font-size]='props.fontSize'
[style.border-radius]='props.borderradius'>
<img *ngIf="displayImage"
[src]="gravatarUrl"
(error)="displayImage = false;"
alt="{{name}} | {{letter}}"/>
<span *ngIf="!displayImage" [style.color]='fontColor'>{{letter}}</span>
</div>