Skip to content

Commit

Permalink
Rework the avatar template to use templates. Remove unnecessary root …
Browse files Browse the repository at this point in the history
…div element.

#136
  • Loading branch information
Borislav Kulov committed Feb 9, 2018
1 parent 75fb610 commit 794fa17
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/avatar/avatar.component.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<div class="igx-avatar igx-avatar--{{size}}" aria-label="avatar" role="img">
<img #image *ngIf="src" src="{{src}}" class="igx-avatar--image igx-avatar--{{size}}" [class.igx-avatar--rounded]="isRounded"
[style.backgroundColor]="bgColor" [attr.aria-roledescription]="roleDescription" />
<svg #initialsImage *ngIf="initials && !src" xmlns="http://www.w3.org/2000/svg" [class.igx-avatar--rounded]="isRounded" [style.backgroundColor]="bgColor"
[attr.aria-roledescription]="roleDescription">
<text text-anchor="middle" />
</svg>
<span *ngIf="!src && !initials" class="igx-avatar--{{size}} igx-avatar--icon" [class.igx-avatar--rounded]="isRounded" [style.backgroundColor]="bgColor"
<ng-template #imageTemplate>
<img #image src="{{src}}" class="igx-avatar--image igx-avatar--{{size}}" [class.igx-avatar--rounded]="isRounded" [style.backgroundColor]="bgColor"
[attr.aria-roledescription]="roleDescription" />
</ng-template>

<ng-template #initialsTemplate>
<svg #initialsImage xmlns="http://www.w3.org/2000/svg" [class.igx-avatar--rounded]="isRounded" [style.backgroundColor]="bgColor"
[attr.aria-roledescription]="roleDescription">
<text text-anchor="middle" />
</svg>
</ng-template>

<ng-template #iconTemplate>
<span class="igx-avatar--{{size}} igx-avatar--icon" [class.igx-avatar--rounded]="isRounded" [style.backgroundColor]="bgColor"
[style.color]="color" [attr.aria-roledescription]="roleDescription">
<igx-icon fontSet="material" [name]="icon"></igx-icon>
</span>
<ng-content></ng-content>
</div>
</ng-template>

<ng-container *ngTemplateOutlet="template"></ng-container>
<ng-content></ng-content>
25 changes: 25 additions & 0 deletions src/avatar/avatar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import {
AfterViewInit,
Component,
ElementRef,
HostBinding,
Input,
NgModule,
Renderer2,
TemplateRef,
ViewChild,
ViewEncapsulation
} from "@angular/core";
Expand Down Expand Up @@ -35,8 +37,19 @@ export class IgxAvatarComponent implements AfterViewInit, AfterContentChecked {
public sizeEnum = Size;
public roleDescription: string;

@ViewChild("imageTemplate", { read: TemplateRef }) protected imageTemplate: TemplateRef<any>;
@ViewChild("initialsTemplate", { read: TemplateRef }) protected initialsTemplate: TemplateRef<any>;
@ViewChild("iconTemplate", { read: TemplateRef }) protected iconTemplate: TemplateRef<any>;

protected fontName = "Titillium Web";

@HostBinding("attr.aria-label") private ariaLabel = "avatar";
@HostBinding("attr.role") private role = "img";
@HostBinding("attr.class")
get classes() {
return "igx-avatar igx-avatar--" + this.size;
}

private _size: string;
private _bgColor: string;
private _icon = "android";
Expand Down Expand Up @@ -73,6 +86,18 @@ export class IgxAvatarComponent implements AfterViewInit, AfterContentChecked {
return this.roundShape.toUpperCase() === "TRUE" ? true : false;
}

get template() {
if (this.src) {
return this.imageTemplate;
}

if (this.initials) {
return this.initialsTemplate;
}

return this.iconTemplate;
}

@Input()
public get icon(): string {
return this._icon;
Expand Down

0 comments on commit 794fa17

Please sign in to comment.