File
Implements
Metadata
selector |
ngx-auth-firebaseui-avatar |
styleUrls |
./ngx-auth-firebaseui-avatar.component.scss |
templateUrl |
./ngx-auth-firebaseui-avatar.component.html |
Index
Properties
|
|
Methods
|
|
Inputs
|
|
Outputs
|
|
Constructor
constructor(afa: AngularFireAuth, dialog: MatDialog)
|
|
Parameters :
Name |
Type |
Optional |
afa |
AngularFireAuth
|
No
|
dialog |
MatDialog
|
No
|
|
canLogout
|
Default value : true
|
|
Outputs
onSignOut
|
Type : EventEmitter<void>
|
|
Methods
getDisplayNameInitials
|
getDisplayNameInitials(displayName: string | null)
|
|
Parameters :
Name |
Type |
Optional |
displayName |
string | null
|
No
|
Returns : string | null
|
openProfile
|
openProfile()
|
|
|
Public
afa
|
Type : AngularFireAuth
|
|
Public
dialog
|
Type : MatDialog
|
|
displayNameInitials
|
Type : string | null
|
|
user$
|
Type : Observable<User | null>
|
|
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {AngularFireAuth} from '@angular/fire/auth';
import {User} from 'firebase';
import {Observable} from 'rxjs';
import {MatDialog} from '@angular/material';
import {UserComponent} from '../../..';
export interface LinkMenuItem {
text: string;
icon?: string;
callback?: Function;
}
@Component({
selector: 'ngx-auth-firebaseui-avatar',
templateUrl: './ngx-auth-firebaseui-avatar.component.html',
styleUrls: ['./ngx-auth-firebaseui-avatar.component.scss']
})
export class NgxAuthFirebaseuiAvatarComponent implements OnInit {
@Input()
canLogout = true;
@Input()
links: LinkMenuItem[];
@Output()
onSignOut: EventEmitter<void> = new EventEmitter();
user: User;
user$: Observable<User | null>;
displayNameInitials: string | null;
constructor(public afa: AngularFireAuth,
public dialog: MatDialog) {
}
ngOnInit() {
this.user$ = this.afa.user;
this.user$.subscribe((user: User) => {
this.user = user;
this.displayNameInitials = user ? this.getDisplayNameInitials(user.displayName) : null;
});
}
getDisplayNameInitials(displayName: string | null): string | null {
if (!displayName) {
return null;
}
const initialsRegExp: RegExpMatchArray = displayName.match(/\b\w/g) || [];
const initials = ((initialsRegExp.shift() || '') + (initialsRegExp.pop() || '')).toUpperCase();
return initials;
}
openProfile() {
this.dialog.open(UserComponent);
}
async signOut() {
try {
await this.afa.auth.signOut();
// Sign-out successful.
this.onSignOut.emit();
} catch (e) {
// An error happened.
console.error('An error happened while signing out!', e);
}
}
}
<button *ngIf="user"
mat-mini-fab
[matMenuTriggerFor]="posXMenu"
aria-label="Open x-positioned menu"
[style.background-image]="'url(' + user?.photoURL + ')'"
style="background-size: cover"
[matTooltip]="user?.displayName">
<span *ngIf="!user?.photoURL">{{displayNameInitials || ''}}</span>
</button>
<mat-menu xPosition="before" #posXMenu="matMenu" class="before">
<div fxLayout="row" fxLayout.xs="column" style="padding-left: 10px; padding-right: 10px">
<button mat-fab
[style.background-image]="'url(' + user?.photoURL + ')'"
style="background-size: cover">
<span *ngIf="!user?.photoURL">{{displayNameInitials || ''}}</span>
</button>
<div fxLayout="column" style="padding-left: 10px; padding-right: 10px">
<strong mat-card-title>{{user?.displayName}}</strong>
<em mat-card-subtitle style="font-style: italic">{{user?.email}}</em>
</div>
</div>
<div fxLayout="column" fxFlex="100">
<div class="links-menu" *ngFor="let menuItem of links">
<button mat-menu-item (click)="menuItem?.callback()">
<mat-icon>{{menuItem?.icon}}</mat-icon>{{menuItem?.text}}</button>
</div>
<button mat-raised-button fxLayoutAlign="center" color="primary" (click)="openProfile()">Profile
</button>
<button *ngIf="canLogout" mat-raised-button fxLayoutAlign="center" color="warn" (click)="signOut()">Sign Out
</button>
</div>
</mat-menu>
.mat-raised-button {
margin: 0.2rem 1rem;
}
.links-menu {
text-align: center;
}
Legend
Html element with directive