import { Component, Injector, OnInit, ViewEncapsulation, NgZone, ViewChild, Input } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { NotificationServiceProxy, UserNotification } from '@shared/service-proxies/service-proxies'; import { IFormattedUserNotification, UserNotificationHelper } from './UserNotificationHelper'; import * as _ from 'lodash'; import { UrlHelper } from '@shared/helpers/UrlHelper'; import { ProgressBarService } from '../progressbar/progressbar.service'; import { EmergencyNotifModalComponent } from './emegency-notifications.component'; @Component({ encapsulation: ViewEncapsulation.None, templateUrl: './header-notifications.component.html', selector: '[headerNotifications]', styleUrls: [ './header-notifications.component.less' ] }) export class HeaderNotificationsComponent extends AppComponentBase implements OnInit { notifications: IFormattedUserNotification[] = []; unreadNotificationCount = 0; show: boolean; logs: string = ''; isPending: boolean; isClosed: boolean; isEmergency: boolean; @ViewChild('emergencyNotifModalComponent', { static: true }) emergencyNotifModalComponent: EmergencyNotifModalComponent; constructor( injector: Injector, private _notificationService: NotificationServiceProxy, private _userNotificationHelper: UserNotificationHelper, public _zone: NgZone, private _progressBar: ProgressBarService ) { super(injector); } ngOnInit(): void { this._progressBar.currentToggle.subscribe(show => this.show = show); this._progressBar.currentMessage.subscribe(logs => this.logs = logs); this.loadNotifications(); this.registerToEvents(); } ngAfterViewInit() { } loadNotifications(): void { if (UrlHelper.isInstallUrl(location.href)) { return; } this._notificationService.getUserNotifications(undefined, undefined, undefined, 3, 0).subscribe(result => { this.unreadNotificationCount = result.unreadCount; this.notifications = []; _.forEach(result.items, (item: UserNotification) => { this.notifications.push(this._userNotificationHelper.format(item)); }); if(this.notifications!=null){ var emergency = this.notifications.filter(o => o.isEmergency==true && o.isUnread==true); var _emergency = emergency.length > 0 ? true : false; if(_emergency){ this.isEmergency = emergency[0].isEmergency; this.isClosed = emergency[0].isClosed; } } }); } registerToEvents() { let self = this; function onNotificationReceived(userNotification) { self._userNotificationHelper.show(userNotification); self.loadNotifications(); } abp.event.on('abp.notifications.received', userNotification => { self._zone.run(() => { onNotificationReceived(userNotification); }); }); function onNotificationsRefresh() { self.loadNotifications(); } abp.event.on('app.notifications.refresh', () => { self._zone.run(() => { onNotificationsRefresh(); }); }); function onNotificationsRead(userNotificationId) { for (let i = 0; i < self.notifications.length; i++) { if (self.notifications[i].userNotificationId === userNotificationId) { self.notifications[i].state = 'READ'; } } self.unreadNotificationCount -= 1; } abp.event.on('app.notifications.read', userNotificationId => { self._zone.run(() => { onNotificationsRead(userNotificationId); }); }); } setAllNotificationsAsRead(): void { this._userNotificationHelper.setAllAsRead(); } openNotificationSettingsModal(): void { this._userNotificationHelper.openSettingsModal(); } setNotificationAsRead(userNotification: IFormattedUserNotification): void { this._userNotificationHelper.setAsRead(userNotification.userNotificationId); } gotoUrl(notif : IFormattedUserNotification): void { if(notif.isEmergency){ if(notif.data.properties!=undefined){ if(notif.data.properties.ticketId!=undefined){ this._userNotificationHelper.openEmergenecy(notif.isPending, notif.data.properties.ticketId, notif); } } }else{ if (notif) { location.href = notif.url; } } } toggle (toggle: boolean) { this.show = toggle; } }