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