import {
Component,
Input,
Output,
EventEmitter,
ChangeDetectionStrategy,
ChangeDetectorRef,
HostListener,
ElementRef,
inject,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { NotificationItemConfig } from 'mayvio-ui/notificationcenter';
import 'mayvio-ui/notificationcenter/css';
@Component({
selector: 'mv-notification-center',
standalone: true,
imports: [CommonModule],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
`,
})
export class NotificationCenterComponent {
@Input() title = 'Notifications';
@Input() notifications: NotificationItemConfig[] = [];
@Input() markAllText = 'Mark all as read';
@Input() emptyText = 'No notifications';
@Input() className = '';
@Output() markAllAsRead = new EventEmitter();
isOpen = false;
private el = inject(ElementRef);
private cdr = inject(ChangeDetectorRef);
get unreadCount(): number {
return this.notifications.filter((n) => n.unread).length;
}
toggleOpen() {
this.isOpen = !this.isOpen;
this.cdr.markForCheck();
}
@HostListener('document:mousedown', ['$event.target'])
onClickOutside(target: EventTarget | null) {
if (this.isOpen && !this.el.nativeElement.contains(target as Node)) {
this.isOpen = false;
this.cdr.markForCheck();
}
}
}