Israel Lavi | 1994c98 | 2018-05-21 17:42:00 +0300 | [diff] [blame] | 1 | import { Component, Input, Output, EventEmitter, OnInit, ViewContainerRef, ViewChild } from "@angular/core"; |
| 2 | import { NotificationSettings } from "../utilities/notification.config"; |
| 3 | import { CreateDynamicComponentService } from "../../utils/create-dynamic-component.service"; |
| 4 | import template from "./notification.component.html"; |
| 5 | |
| 6 | @Component({ |
| 7 | selector: 'sdc-notification', |
| 8 | template: template |
| 9 | }) |
| 10 | |
| 11 | export class NotificationComponent implements OnInit { |
| 12 | |
| 13 | @Input() notificationSetting:NotificationSettings; |
| 14 | @Output() destroyComponent = new EventEmitter<any>(); |
| 15 | @ViewChild("dynamicContentContainer", {read: ViewContainerRef}) contentContainer:ViewContainerRef; |
| 16 | private fade: boolean = false; |
| 17 | |
| 18 | constructor(private createDynamicComponentService: CreateDynamicComponentService) { |
| 19 | } |
| 20 | |
| 21 | public ngOnInit() { |
| 22 | if(this.notificationSetting.hasCustomContent){ |
| 23 | this.createDynamicComponentService.insertComponentDynamically(this.notificationSetting.innerComponentType, this.notificationSetting.innerComponentOptions, this.contentContainer); |
| 24 | } |
| 25 | |
| 26 | if(!this.notificationSetting.sticky){ |
| 27 | setTimeout(() => this.fadeOut(), this.notificationSetting.duration); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | private fadeOut = ():void => { |
| 32 | this.fade = true; |
| 33 | } |
| 34 | |
| 35 | private destroyMe() { |
| 36 | /*Only destroy on fade out, not on entry animation */ |
| 37 | if(this.fade){ |
| 38 | this.destroyComponent.emit(this.notificationSetting); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | } |