angular5-toaster
Version:
An Angular Toaster Notification library based on AngularJS-Toaster
66 lines (55 loc) • 2.45 kB
text/typescript
import {Component, Input, Output, ViewChild, ViewContainerRef, EventEmitter,
ComponentFactoryResolver, ChangeDetectorRef, OnInit, AfterViewInit
} from '@angular/core';
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
import {Toast} from './toast';
import {BodyOutputType} from './bodyOutputType';
export class ToastComponent implements OnInit, AfterViewInit {
toast: Toast;
iconClass: string;
componentBody: ViewContainerRef;
safeCloseHtml: SafeHtml;
public bodyOutputType = BodyOutputType;
public clickEvent = new EventEmitter();
constructor(
private sanitizer: DomSanitizer,
private componentFactoryResolver: ComponentFactoryResolver,
private changeDetectorRef: ChangeDetectorRef
) {}
ngOnInit() {
if (this.toast.closeHtml) {
this.safeCloseHtml = this.sanitizer.bypassSecurityTrustHtml(this.toast.closeHtml);
}
}
ngAfterViewInit() {
if (this.toast.bodyOutputType === this.bodyOutputType.Component) {
const component = this.componentFactoryResolver.resolveComponentFactory(this.toast.body);
const componentInstance: any = this.componentBody.createComponent(component, undefined, this.componentBody.injector);
componentInstance.instance.toast = this.toast;
this.changeDetectorRef.detectChanges();
}
}
click(event: MouseEvent, toast: Toast) {
event.stopPropagation();
this.clickEvent.emit({
value : { toast: toast, isCloseButton: true}
});
}
}