primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 8.08 kB
JavaScript
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, Optional, ElementRef, ChangeDetectorRef, Input, ContentChildren, Output, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { trigger, transition, style, animate } from '@angular/animations';
import { MessageService, PrimeTemplate } from 'primeng/api';
import { RippleModule } from 'primeng/ripple';
class Messages {
constructor(messageService, el, cd) {
this.messageService = messageService;
this.el = el;
this.cd = cd;
this.closable = true;
this.enableService = true;
this.escape = true;
this.showTransitionOptions = '300ms ease-out';
this.hideTransitionOptions = '200ms cubic-bezier(0.86, 0, 0.07, 1)';
this.valueChange = new EventEmitter();
}
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) => {
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() {
this.value = [];
this.valueChange.emit(this.value);
}
removeMessage(i) {
this.value = this.value.filter((msg, index) => index !== i);
this.valueChange.emit(this.value);
}
get icon() {
const severity = this.severity || (this.hasMessages() ? this.value[0].severity : null);
if (this.hasMessages()) {
switch (severity) {
case 'success':
return 'pi-check';
break;
case 'info':
return 'pi-info-circle';
break;
case 'error':
return 'pi-times';
break;
case 'warn':
return 'pi-exclamation-triangle';
break;
default:
return 'pi-info-circle';
break;
}
}
return null;
}
ngOnDestroy() {
if (this.messageSubscription) {
this.messageSubscription.unsubscribe();
}
if (this.clearSubscription) {
this.clearSubscription.unsubscribe();
}
}
}
Messages.decorators = [
{ type: Component, args: [{
selector: 'p-messages',
template: `
<div class="p-messages p-component" role="alert" [ngStyle]="style" [class]="styleClass">
<ng-container *ngIf="!contentTemplate; else staticMessage">
<div *ngFor="let msg of value; let i=index" [ngClass]="'p-message p-message-' + msg.severity" role="alert"
[@messageAnimation]="{value: 'visible', params: {showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}">
<div class="p-message-wrapper">
<span [class]="'p-message-icon pi' + (msg.icon ? ' ' + msg.icon : '')" [ngClass]="{'pi-info-circle': msg.severity === 'info',
'pi-check': msg.severity === 'success',
'pi-exclamation-triangle': msg.severity === 'warn',
'pi-times-circle': msg.severity === 'error'}"></span>
<ng-container *ngIf="!escape; else escapeOut">
<span *ngIf="msg.summary" class="p-message-summary" [innerHTML]="msg.summary"></span>
<span *ngIf="msg.detail" class="p-message-detail" [innerHTML]="msg.detail"></span>
</ng-container>
<ng-template #escapeOut>
<span *ngIf="msg.summary" class="p-message-summary">{{msg.summary}}</span>
<span *ngIf="msg.detail" class="p-message-detail">{{msg.detail}}</span>
</ng-template>
<button class="p-message-close p-link" (click)="removeMessage(i)" *ngIf="closable" type="button" pRipple>
<i class="p-message-close-icon pi pi-times"></i>
</button>
</div>
</div>
</ng-container>
<ng-template #staticMessage>
<div [ngClass]="'p-message p-message-' + severity" role="alert">
<div class="p-message-wrapper">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</div>
</div>
</ng-template>
</div>
`,
animations: [
trigger('messageAnimation', [
transition(':enter', [
style({ opacity: 0, transform: 'translateY(-25%)' }),
animate('{{showTransitionParams}}')
]),
transition(':leave', [
animate('{{hideTransitionParams}}', style({ height: 0, marginTop: 0, marginBottom: 0, marginLeft: 0, marginRight: 0, overflow: 'hidden', opacity: 0 }))
])
])
],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
styles: [".p-message-close,.p-message-wrapper{align-items:center;display:flex}.p-message-close{justify-content:center}.p-message-close.p-link{margin-left:auto;overflow:hidden;position:relative}"]
},] }
];
Messages.ctorParameters = () => [
{ type: MessageService, decorators: [{ type: Optional }] },
{ type: ElementRef },
{ type: ChangeDetectorRef }
];
Messages.propDecorators = {
value: [{ type: Input }],
closable: [{ type: Input }],
style: [{ type: Input }],
styleClass: [{ type: Input }],
enableService: [{ type: Input }],
key: [{ type: Input }],
escape: [{ type: Input }],
severity: [{ type: Input }],
showTransitionOptions: [{ type: Input }],
hideTransitionOptions: [{ type: Input }],
templates: [{ type: ContentChildren, args: [PrimeTemplate,] }],
valueChange: [{ type: Output }]
};
class MessagesModule {
}
MessagesModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, RippleModule],
exports: [Messages],
declarations: [Messages]
},] }
];
/**
* Generated bundle index. Do not edit.
*/
export { Messages, MessagesModule };
//# sourceMappingURL=primeng-messages.js.map