import {NgModule,Component,OnDestroy,Input,Output,EventEmitter,AfterContentInit,Optional,ElementRef,ChangeDetectionStrategy,ContentChildren,QueryList,TemplateRef, ViewEncapsulation, ChangeDetectorRef} from '@angular/core'; import {CommonModule} from '@angular/common'; import {trigger,state,style,transition,animate} from '@angular/animations'; import {Message,PrimeTemplate,MessageService} from 'mirra-ui/api'; import {Subscription} from 'rxjs'; import {RippleModule} from 'mirra-ui/ripple'; @Component({ selector: 'p-messages', template: ` `, animations: [ trigger('messageAnimation', [ state('visible', style({ transform: 'translateY(0)', opacity: 1 })), transition('void => *', [ style({transform: 'translateY(-25%)', opacity: 0}), animate('{{showTransitionParams}}') ]), transition('* => void', [ animate(('{{hideTransitionParams}}'), style({ opacity: 0, transform: 'translateY(-25%)' })) ]) ]) ], changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, styleUrls: ['./messages.scss'] }) export class Messages implements AfterContentInit, OnDestroy { @Input() value: Message[]; @Input() closable: boolean = true; @Input() style: any; @Input() styleClass: string; @Input() enableService: boolean = true; @Input() key: string; @Input() escape: boolean = true; @Input() severity: string; @Input() showTransitionOptions: string = '300ms ease-out'; @Input() hideTransitionOptions: string = '250ms ease-in'; @ContentChildren(PrimeTemplate) templates: QueryList; @Output() valueChange: EventEmitter = new EventEmitter(); messageSubscription: Subscription; clearSubscription: Subscription; contentTemplate: TemplateRef; constructor(@Optional() public messageService: MessageService, public el: ElementRef, public cd: ChangeDetectorRef) {} ngAfterContentInit() { this.templates.forEach((item) => { switch(item.getType()) { case 'content': this.contentTemplate = item.template; break; default: this.contentTemplate = item.template; break; } }); if (this.messageService && this.enableService && !this.contentTemplate) { this.messageSubscription = this.messageService.messageObserver.subscribe((messages: any) => { if (messages) { if (messages instanceof Array) { let filteredMessages = messages.filter(m => this.key === m.key); this.value = this.value ? [...this.value, ...filteredMessages] : [...filteredMessages]; } else if (this.key === messages.key) { this.value = this.value ? [...this.value, ...[messages]] : [messages]; } this.cd.markForCheck(); } }); this.clearSubscription = this.messageService.clearObserver.subscribe(key => { if (key) { if (this.key === key) { this.value = null; } } else { this.value = null; } this.cd.markForCheck(); }); } } hasMessages() { let parentEl = this.el.nativeElement.parentElement; if (parentEl && parentEl.offsetParent) { return this.contentTemplate != null || this.value && this.value.length > 0; } return false; } clear(event) { this.value = []; this.valueChange.emit(this.value); event.preventDefault(); } get icon(): string { const severity = this.severity || (this.hasMessages() ? this.value[0].severity : null); if (this.hasMessages()) { switch(severity) { case 'success': return 'fa-check'; break; case 'info': return 'fa-info-circle'; break; case 'error': return 'fa-times'; break; case 'warn': return 'fa-exclamation-triangle'; break; default: return 'fa-info-circle'; break; } } return null; } ngOnDestroy() { if (this.messageSubscription) { this.messageSubscription.unsubscribe(); } if (this.clearSubscription) { this.clearSubscription.unsubscribe(); } } } @NgModule({ imports: [CommonModule,RippleModule], exports: [Messages], declarations: [Messages] }) export class MessagesModule { }