blob: 8b894676c6d1a82a80f42ea384cf49c0906d5f2b [file] [log] [blame]
Israel Lavi1994c982018-05-21 17:42:00 +03001import { Component, Input, Output, EventEmitter, OnInit, ViewContainerRef, ViewChild } from "@angular/core";
2import { NotificationSettings } from "../utilities/notification.config";
3import { CreateDynamicComponentService } from "../../utils/create-dynamic-component.service";
Israel Lavib2a3ace2018-08-07 10:54:17 +03004import { template } from "./notification.component.html";
Israel Lavi1994c982018-05-21 17:42:00 +03005
6@Component({
7 selector: 'sdc-notification',
8 template: template
9})
10
11export class NotificationComponent implements OnInit {
12
Israel Lavib2a3ace2018-08-07 10:54:17 +030013 @Input() notificationSetting: NotificationSettings;
Israel Lavi1994c982018-05-21 17:42:00 +030014 @Output() destroyComponent = new EventEmitter<any>();
Israel Lavib2a3ace2018-08-07 10:54:17 +030015 @ViewChild("dynamicContentContainer", { read: ViewContainerRef }) contentContainer: ViewContainerRef;
16 public fade: boolean = false;
Israel Lavi1994c982018-05-21 17:42:00 +030017
18 constructor(private createDynamicComponentService: CreateDynamicComponentService) {
19 }
20
21 public ngOnInit() {
Israel Lavib2a3ace2018-08-07 10:54:17 +030022 if (this.notificationSetting.hasCustomContent) {
Israel Lavi1994c982018-05-21 17:42:00 +030023 this.createDynamicComponentService.insertComponentDynamically(this.notificationSetting.innerComponentType, this.notificationSetting.innerComponentOptions, this.contentContainer);
24 }
25
Israel Lavib2a3ace2018-08-07 10:54:17 +030026 if (!this.notificationSetting.sticky) {
Israel Lavi1994c982018-05-21 17:42:00 +030027 setTimeout(() => this.fadeOut(), this.notificationSetting.duration);
28 }
29 }
30
Israel Lavib2a3ace2018-08-07 10:54:17 +030031 public fadeOut = (): void => {
Israel Lavi1994c982018-05-21 17:42:00 +030032 this.fade = true;
33 }
34
Israel Lavib2a3ace2018-08-07 10:54:17 +030035 public destroyMe() {
Israel Lavi1994c982018-05-21 17:42:00 +030036 /*Only destroy on fade out, not on entry animation */
Israel Lavib2a3ace2018-08-07 10:54:17 +030037 if (this.fade) {
Israel Lavi1994c982018-05-21 17:42:00 +030038 this.destroyComponent.emit(this.notificationSetting);
39 }
40 }
41
42}