110 lines
3.5 KiB
TypeScript
110 lines
3.5 KiB
TypeScript
import {
|
|
Injectable,
|
|
ComponentFactoryResolver,
|
|
ApplicationRef,
|
|
Injector,
|
|
EmbeddedViewRef,
|
|
ComponentRef,
|
|
} from '@angular/core';
|
|
import { NotificationPopupComponent } from '../notification-popup/notification-popup.component';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class NotificationService {
|
|
private notificationComponents: ComponentRef<NotificationPopupComponent>[] =
|
|
[];
|
|
private notificationBottomMargin = 10; // Margin between notifications
|
|
private baseBottomPosition = 20; // Starting bottom position for the first notification
|
|
|
|
constructor(
|
|
private componentFactoryResolver: ComponentFactoryResolver,
|
|
private appRef: ApplicationRef,
|
|
private injector: Injector
|
|
) {}
|
|
|
|
public showNotification(
|
|
title: string,
|
|
message: string,
|
|
type: string = 'alert',
|
|
timeout: number = 5000
|
|
): void {
|
|
// Create a component reference for the notification
|
|
const componentRef = this.componentFactoryResolver
|
|
.resolveComponentFactory(NotificationPopupComponent)
|
|
.create(this.injector);
|
|
|
|
// Assign the message and type to the instance
|
|
componentRef.instance.title = title;
|
|
componentRef.instance.message = message;
|
|
componentRef.instance.type = type;
|
|
|
|
// Attach the component to the application
|
|
this.appRef.attachView(componentRef.hostView);
|
|
|
|
// Convert the component's host view into a DOM element and append it to the body
|
|
const domElem = (componentRef.hostView as EmbeddedViewRef<any>)
|
|
.rootNodes[0] as HTMLElement;
|
|
document.body.appendChild(domElem);
|
|
|
|
const position =
|
|
this.baseBottomPosition +
|
|
this.notificationComponents.length *
|
|
(domElem.offsetHeight + this.notificationBottomMargin);
|
|
domElem.style.bottom = `${position}px`;
|
|
domElem.style.position = 'fixed';
|
|
domElem.style.right = '20px';
|
|
|
|
// Store the component reference for later removal
|
|
this.notificationComponents.push(componentRef);
|
|
|
|
// Remove the notification after a delay
|
|
setTimeout(() => {
|
|
domElem.style.visibility = 'hidden';
|
|
}, timeout);
|
|
|
|
setTimeout(() => {
|
|
this.removeNotification(componentRef);
|
|
}, timeout + 1000); // Customize this value as needed
|
|
}
|
|
|
|
// private removeNotification(
|
|
// componentRef: ComponentRef<NotificationPopupComponent>
|
|
// ): void {
|
|
// this.appRef.detachView(componentRef.hostView);
|
|
// componentRef.destroy();
|
|
// this.notificationComponents = this.notificationComponents.filter(
|
|
// (c) => c !== componentRef
|
|
// );
|
|
// }
|
|
|
|
private removeNotification(
|
|
componentRef: ComponentRef<NotificationPopupComponent>
|
|
): void {
|
|
const domElem = (componentRef.hostView as EmbeddedViewRef<any>)
|
|
.rootNodes[0] as HTMLElement;
|
|
this.appRef.detachView(componentRef.hostView);
|
|
document.body.removeChild(domElem); // Make sure to remove the element from the body
|
|
componentRef.destroy();
|
|
// Filter out the destroyed notification
|
|
this.notificationComponents = this.notificationComponents.filter(
|
|
(c) => c !== componentRef
|
|
);
|
|
|
|
// Adjust positions of remaining notifications
|
|
this.adjustPositions();
|
|
}
|
|
|
|
private adjustPositions(): void {
|
|
// Start from the initial position again and reposition each remaining notification
|
|
this.notificationComponents.forEach((componentRef, index) => {
|
|
const domElem = (componentRef.hostView as EmbeddedViewRef<any>)
|
|
.rootNodes[0] as HTMLElement;
|
|
const position =
|
|
this.baseBottomPosition +
|
|
index * (domElem.offsetHeight + this.notificationBottomMargin);
|
|
domElem.style.bottom = `${position}px`;
|
|
});
|
|
}
|
|
}
|