UNPKG

primeng

Version:

[![npm version](https://badge.fury.io/js/primeng.svg)](https://badge.fury.io/js/primeng) [![npm downloads](https://img.shields.io/npm/dm/primeng.svg)](https://www.npmjs.com/package/primeng) [![Actions CI](https://github.com/primefaces/primeng/workflows/No

351 lines (347 loc) 19.5 kB
import { trigger, transition, style, animate } from '@angular/animations'; import * as i2 from '@angular/common'; import { CommonModule } from '@angular/common'; import * as i0 from '@angular/core'; import { EventEmitter, booleanAttribute, Component, ChangeDetectionStrategy, ViewEncapsulation, Optional, Input, Output, ContentChildren, NgModule } from '@angular/core'; import * as i1 from 'primeng/api'; import { PrimeTemplate } from 'primeng/api'; import { CheckIcon } from 'primeng/icons/check'; import { ExclamationTriangleIcon } from 'primeng/icons/exclamationtriangle'; import { InfoCircleIcon } from 'primeng/icons/infocircle'; import { TimesIcon } from 'primeng/icons/times'; import { TimesCircleIcon } from 'primeng/icons/timescircle'; import * as i3 from 'primeng/ripple'; import { RippleModule } from 'primeng/ripple'; import { timer } from 'rxjs'; /** * Messages is used to display alerts inline. * @group Components */ class Messages { messageService; el; cd; config; /** * An array of messages to display. * @group Props */ set value(messages) { this.messages = messages; this.startMessageLifes(this.messages); } /** * Defines if message box can be closed by the click icon. * @group Props */ closable = true; /** * Inline style of the component. * @group Props */ style; /** * Style class of the component. * @group Props */ styleClass; /** * Whether displaying services messages are enabled. * @group Props */ enableService = true; /** * Id to match the key of the message to enable scoping in service based messaging. * @group Props */ key; /** * Whether displaying messages would be escaped or not. * @group Props */ escape = true; /** * Severity level of the message. * @group Props */ severity; /** * Transition options of the show animation. * @group Props */ showTransitionOptions = '300ms ease-out'; /** * Transition options of the hide animation. * @group Props */ hideTransitionOptions = '200ms cubic-bezier(0.86, 0, 0.07, 1)'; /** * This function is executed when the value changes. * @param {Message[]} value - messages value. * @group Emits */ valueChange = new EventEmitter(); /** * This function is executed when a message is closed. * @param {Message} value - Closed message. * @group Emits */ onClose = new EventEmitter(); templates; messages; messageSubscription; clearSubscription; timerSubscriptions = []; contentTemplate; constructor(messageService, el, cd, config) { this.messageService = messageService; this.el = el; this.cd = cd; this.config = config; } 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 (!Array.isArray(messages)) { messages = [messages]; } const filteredMessages = messages.filter((m) => this.key === m.key); this.messages = this.messages ? [...this.messages, ...filteredMessages] : [...filteredMessages]; this.startMessageLifes(filteredMessages); this.cd.markForCheck(); } }); this.clearSubscription = this.messageService.clearObserver.subscribe((key) => { if (key) { if (this.key === key) { this.messages = null; } } else { this.messages = null; } this.cd.markForCheck(); }); } } hasMessages() { let parentEl = this.el.nativeElement.parentElement; if (parentEl && parentEl.offsetParent) { return this.contentTemplate != null || (this.messages && this.messages.length > 0); } return false; } clear() { this.messages = []; this.valueChange.emit(this.messages); } removeMessage(i) { const removedMessage = this.messages[i]; this.messages = this.messages?.filter((msg, index) => index !== i); removedMessage && this.onClose.emit(removedMessage); this.valueChange.emit(this.messages); } get icon() { const severity = this.severity || (this.hasMessages() ? this.messages[0].severity : null); if (this.hasMessages()) { switch (severity) { case 'success': return 'pi-check'; case 'info': return 'pi-info-circle'; case 'error': return 'pi-times'; case 'warn': return 'pi-exclamation-triangle'; default: return 'pi-info-circle'; } } return null; } get closeAriaLabel() { return this.config.translation.aria ? this.config.translation.aria.close : undefined; } ngOnDestroy() { if (this.messageSubscription) { this.messageSubscription.unsubscribe(); } if (this.clearSubscription) { this.clearSubscription.unsubscribe(); } this.timerSubscriptions?.forEach((subscription) => subscription.unsubscribe()); } startMessageLifes(messages) { messages?.forEach((message) => message.life && this.startMessageLife(message)); } startMessageLife(message) { const timerSubsctiption = timer(message.life).subscribe(() => { this.messages = this.messages?.filter((msgEl) => msgEl !== message); this.timerSubscriptions = this.timerSubscriptions?.filter((timerEl) => timerEl !== timerSubsctiption); this.valueChange.emit(this.messages); this.cd.markForCheck(); }); this.timerSubscriptions.push(timerSubsctiption); } static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: Messages, deps: [{ token: i1.MessageService, optional: true }, { token: i0.ElementRef }, { token: i0.ChangeDetectorRef }, { token: i1.PrimeNGConfig }], target: i0.ɵɵFactoryTarget.Component }); static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "16.1.0", version: "18.0.1", type: Messages, selector: "p-messages", inputs: { value: "value", closable: ["closable", "closable", booleanAttribute], style: "style", styleClass: "styleClass", enableService: ["enableService", "enableService", booleanAttribute], key: "key", escape: ["escape", "escape", booleanAttribute], severity: "severity", showTransitionOptions: "showTransitionOptions", hideTransitionOptions: "hideTransitionOptions" }, outputs: { valueChange: "valueChange", onClose: "onClose" }, host: { classAttribute: "p-element" }, queries: [{ propertyName: "templates", predicate: PrimeTemplate }], ngImport: i0, template: ` <div class="p-messages p-component" role="alert" [ngStyle]="style" [class]="styleClass" [attr.aria-atomic]="true" [attr.aria-live]="'assertive'" [attr.data-pc-name]="'message'"> <ng-container *ngIf="!contentTemplate; else staticMessage"> <div *ngFor="let msg of messages; let i = index" [class]="'p-message p-message-' + msg.severity" role="alert" [@messageAnimation]="{ value: 'visible', params: { showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions } }" > <div class="p-message-wrapper" [attr.data-pc-section]="'wrapper'" [attr.id]="msg.id || null"> <span *ngIf="msg.icon" [class]="'p-message-icon pi ' + msg.icon" [attr.data-pc-section]="'icon'"> </span> <span class="p-message-icon" *ngIf="!msg.icon"> <ng-container> <CheckIcon *ngIf="msg.severity === 'success'" [attr.data-pc-section]="'icon'" /> <InfoCircleIcon *ngIf="msg.severity === 'info'" [attr.data-pc-section]="'icon'" /> <TimesCircleIcon *ngIf="msg.severity === 'error'" [attr.data-pc-section]="'icon'" /> <ExclamationTriangleIcon *ngIf="msg.severity === 'warn'" [attr.data-pc-section]="'icon'" /> </ng-container> </span> <ng-container *ngIf="!escape; else escapeOut"> <span *ngIf="msg.summary" class="p-message-summary" [innerHTML]="msg.summary" [attr.data-pc-section]="'summary'"></span> <span *ngIf="msg.detail" class="p-message-detail" [innerHTML]="msg.detail" [attr.data-pc-section]="'detail'"></span> </ng-container> <ng-template #escapeOut> <span *ngIf="msg.summary" class="p-message-summary" [attr.data-pc-section]="'summary'">{{ msg.summary }}</span> <span *ngIf="msg.detail" class="p-message-detail" [attr.data-pc-section]="'detail'">{{ msg.detail }}</span> </ng-template> <button class="p-message-close p-link" (click)="removeMessage(i)" *ngIf="closable && (msg.closable ?? true)" type="button" pRipple [attr.aria-label]="closeAriaLabel" [attr.data-pc-section]="'closebutton'"> <TimesIcon [styleClass]="'p-message-close-icon'" [attr.data-pc-section]="'closeicon'" /> </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> `, isInline: true, styles: ["@layer primeng{.p-message-wrapper{display:flex;align-items:center}.p-message-close{display:flex;align-items:center;justify-content:center;flex:none}.p-message-close.p-link{margin-left:auto;overflow:hidden;position:relative}.p-messages .p-message.ng-animating{overflow:hidden}}\n"], dependencies: [{ kind: "directive", type: i0.forwardRef(() => i2.NgClass), selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: i0.forwardRef(() => i2.NgForOf), selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i0.forwardRef(() => i2.NgIf), selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i0.forwardRef(() => i2.NgTemplateOutlet), selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "directive", type: i0.forwardRef(() => i2.NgStyle), selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "directive", type: i0.forwardRef(() => i3.Ripple), selector: "[pRipple]" }, { kind: "component", type: i0.forwardRef(() => CheckIcon), selector: "CheckIcon" }, { kind: "component", type: i0.forwardRef(() => InfoCircleIcon), selector: "InfoCircleIcon" }, { kind: "component", type: i0.forwardRef(() => TimesCircleIcon), selector: "TimesCircleIcon" }, { kind: "component", type: i0.forwardRef(() => ExclamationTriangleIcon), selector: "ExclamationTriangleIcon" }, { kind: "component", type: i0.forwardRef(() => TimesIcon), selector: "TimesIcon" }], 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, opacity: 0 }))]) ]) ], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: Messages, decorators: [{ type: Component, args: [{ selector: 'p-messages', template: ` <div class="p-messages p-component" role="alert" [ngStyle]="style" [class]="styleClass" [attr.aria-atomic]="true" [attr.aria-live]="'assertive'" [attr.data-pc-name]="'message'"> <ng-container *ngIf="!contentTemplate; else staticMessage"> <div *ngFor="let msg of messages; let i = index" [class]="'p-message p-message-' + msg.severity" role="alert" [@messageAnimation]="{ value: 'visible', params: { showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions } }" > <div class="p-message-wrapper" [attr.data-pc-section]="'wrapper'" [attr.id]="msg.id || null"> <span *ngIf="msg.icon" [class]="'p-message-icon pi ' + msg.icon" [attr.data-pc-section]="'icon'"> </span> <span class="p-message-icon" *ngIf="!msg.icon"> <ng-container> <CheckIcon *ngIf="msg.severity === 'success'" [attr.data-pc-section]="'icon'" /> <InfoCircleIcon *ngIf="msg.severity === 'info'" [attr.data-pc-section]="'icon'" /> <TimesCircleIcon *ngIf="msg.severity === 'error'" [attr.data-pc-section]="'icon'" /> <ExclamationTriangleIcon *ngIf="msg.severity === 'warn'" [attr.data-pc-section]="'icon'" /> </ng-container> </span> <ng-container *ngIf="!escape; else escapeOut"> <span *ngIf="msg.summary" class="p-message-summary" [innerHTML]="msg.summary" [attr.data-pc-section]="'summary'"></span> <span *ngIf="msg.detail" class="p-message-detail" [innerHTML]="msg.detail" [attr.data-pc-section]="'detail'"></span> </ng-container> <ng-template #escapeOut> <span *ngIf="msg.summary" class="p-message-summary" [attr.data-pc-section]="'summary'">{{ msg.summary }}</span> <span *ngIf="msg.detail" class="p-message-detail" [attr.data-pc-section]="'detail'">{{ msg.detail }}</span> </ng-template> <button class="p-message-close p-link" (click)="removeMessage(i)" *ngIf="closable && (msg.closable ?? true)" type="button" pRipple [attr.aria-label]="closeAriaLabel" [attr.data-pc-section]="'closebutton'"> <TimesIcon [styleClass]="'p-message-close-icon'" [attr.data-pc-section]="'closeicon'" /> </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, opacity: 0 }))]) ]) ], changeDetection: ChangeDetectionStrategy.OnPush, encapsulation: ViewEncapsulation.None, host: { class: 'p-element' }, styles: ["@layer primeng{.p-message-wrapper{display:flex;align-items:center}.p-message-close{display:flex;align-items:center;justify-content:center;flex:none}.p-message-close.p-link{margin-left:auto;overflow:hidden;position:relative}.p-messages .p-message.ng-animating{overflow:hidden}}\n"] }] }], ctorParameters: () => [{ type: i1.MessageService, decorators: [{ type: Optional }] }, { type: i0.ElementRef }, { type: i0.ChangeDetectorRef }, { type: i1.PrimeNGConfig }], propDecorators: { value: [{ type: Input }], closable: [{ type: Input, args: [{ transform: booleanAttribute }] }], style: [{ type: Input }], styleClass: [{ type: Input }], enableService: [{ type: Input, args: [{ transform: booleanAttribute }] }], key: [{ type: Input }], escape: [{ type: Input, args: [{ transform: booleanAttribute }] }], severity: [{ type: Input }], showTransitionOptions: [{ type: Input }], hideTransitionOptions: [{ type: Input }], valueChange: [{ type: Output }], onClose: [{ type: Output }], templates: [{ type: ContentChildren, args: [PrimeTemplate] }] } }); class MessagesModule { static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: MessagesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "18.0.1", ngImport: i0, type: MessagesModule, declarations: [Messages], imports: [CommonModule, RippleModule, CheckIcon, InfoCircleIcon, TimesCircleIcon, ExclamationTriangleIcon, TimesIcon], exports: [Messages] }); static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: MessagesModule, imports: [CommonModule, RippleModule, CheckIcon, InfoCircleIcon, TimesCircleIcon, ExclamationTriangleIcon, TimesIcon] }); } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.0.1", ngImport: i0, type: MessagesModule, decorators: [{ type: NgModule, args: [{ imports: [CommonModule, RippleModule, CheckIcon, InfoCircleIcon, TimesCircleIcon, ExclamationTriangleIcon, TimesIcon], exports: [Messages], declarations: [Messages] }] }] }); /** * Generated bundle index. Do not edit. */ export { Messages, MessagesModule }; //# sourceMappingURL=primeng-messages.mjs.map