add notification service + component
This commit is contained in:
1 parent
939a6e32ef
commit
9512e5bd28
5 files changed
+189
No files matched your search
@@ -0,0 +1,16 @@
|
|||||||
|
@keyframes fadeIn {
|
||||||
|
from { opacity: 0; }
|
||||||
|
to { opacity: 1; }
|
||||||
|
}
|
||||||
|
|
||||||
|
.notification {
|
||||||
|
position: fixed;
|
||||||
|
right: 20px;
|
||||||
|
bottom: 20px;
|
||||||
|
z-index: 1050; /* Ensure it's above other content */
|
||||||
|
margin-bottom: 20px; /* Adjust based on size and desired spacing */
|
||||||
|
animation: fadeIn 1s ease-out; /* Use the fadeIn animation, lasts 1 second */
|
||||||
|
}
|
||||||
|
.notif-back {
|
||||||
|
background-color: aliceblue;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<div class="notification">
|
||||||
|
<div class="alert alert-border-success alert-dismissible fade show notif-back">
|
||||||
|
<div class="d-flex align-items-center">
|
||||||
|
<div class="font-35 text-success"><span class="material-icons-outlined fs-2">check_circle</span>
|
||||||
|
</div>
|
||||||
|
<div class="ms-3">
|
||||||
|
<h6 class="mb-0 text-success">{{title}}</h6>
|
||||||
|
<div class="">{{message}}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="button" class="btn-close"></button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { NotificationPopupComponent } from './notification-popup.component';
|
||||||
|
|
||||||
|
describe('NotificationPopupComponent', () => {
|
||||||
|
let component: NotificationPopupComponent;
|
||||||
|
let fixture: ComponentFixture<NotificationPopupComponent>;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [NotificationPopupComponent]
|
||||||
|
});
|
||||||
|
fixture = TestBed.createComponent(NotificationPopupComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { Component } from '@angular/core';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-notification-popup',
|
||||||
|
templateUrl: './notification-popup.component.html',
|
||||||
|
styleUrls: ['./notification-popup.component.css'],
|
||||||
|
})
|
||||||
|
export class NotificationPopupComponent {
|
||||||
|
show: boolean = false;
|
||||||
|
title: string = 'Alert';
|
||||||
|
message: string = 'Alert Message...';
|
||||||
|
type: string = 'alert';
|
||||||
|
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
|
ngOnInit(): void {}
|
||||||
|
|
||||||
|
public showNotification(
|
||||||
|
title: string,
|
||||||
|
message: string,
|
||||||
|
type: string = 'alert',
|
||||||
|
timeout: number = 3000
|
||||||
|
): void {
|
||||||
|
this.title = title;
|
||||||
|
this.message = message;
|
||||||
|
this.type = type;
|
||||||
|
this.show = true;
|
||||||
|
setTimeout(() => (this.show = false), timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
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`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user