268 lines
11 KiB
JavaScript
268 lines
11 KiB
JavaScript
import { EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, NgZone, Input, Output, ViewChild, ChangeDetectorRef, ContentChildren, NgModule } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { DomHandler } from 'primeng/dom';
|
|
import { MessageService, PrimeTemplate, SharedModule } from 'primeng/api';
|
|
import { RippleModule } from 'primeng/ripple';
|
|
import { trigger, state, style, transition, animate, query, animateChild } from '@angular/animations';
|
|
|
|
class ToastItem {
|
|
constructor(zone) {
|
|
this.zone = zone;
|
|
this.onClose = new EventEmitter();
|
|
}
|
|
ngAfterViewInit() {
|
|
this.initTimeout();
|
|
}
|
|
initTimeout() {
|
|
if (!this.message.sticky) {
|
|
this.zone.runOutsideAngular(() => {
|
|
this.timeout = setTimeout(() => {
|
|
this.onClose.emit({
|
|
index: this.index,
|
|
message: this.message
|
|
});
|
|
}, this.message.life || 3000);
|
|
});
|
|
}
|
|
}
|
|
clearTimeout() {
|
|
if (this.timeout) {
|
|
clearTimeout(this.timeout);
|
|
this.timeout = null;
|
|
}
|
|
}
|
|
onMouseEnter() {
|
|
this.clearTimeout();
|
|
}
|
|
onMouseLeave() {
|
|
this.initTimeout();
|
|
}
|
|
onCloseIconClick(event) {
|
|
this.clearTimeout();
|
|
this.onClose.emit({
|
|
index: this.index,
|
|
message: this.message
|
|
});
|
|
event.preventDefault();
|
|
}
|
|
ngOnDestroy() {
|
|
this.clearTimeout();
|
|
}
|
|
}
|
|
ToastItem.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: 'p-toastItem',
|
|
template: `
|
|
<div #container [attr.id]="message.id" class="p-toast-message" [ngClass]="'p-toast-message-' + message.severity" [@messageState]="{value: 'visible', params: {showTransformParams: showTransformOptions, hideTransformParams: hideTransformOptions, showTransitionParams: showTransitionOptions, hideTransitionParams: hideTransitionOptions}}"
|
|
(mouseenter)="onMouseEnter()" (mouseleave)="onMouseLeave()">
|
|
<div class="p-toast-message-content" role="alert" aria-live="assertive" aria-atomic="true">
|
|
<ng-container *ngIf="!template">
|
|
<span class="p-toast-message-icon pi" [ngClass]="{'pi-info-circle': message.severity == 'info', 'pi-exclamation-triangle': message.severity == 'warn',
|
|
'pi-times-circle': message.severity == 'error', 'pi-check' :message.severity == 'success'}"></span>
|
|
<div class="p-toast-message-text">
|
|
<div class="p-toast-summary">{{message.summary}}</div>
|
|
<div class="p-toast-detail">{{message.detail}}</div>
|
|
</div>
|
|
</ng-container>
|
|
<button type="button" class="p-toast-icon-close p-link" (click)="onCloseIconClick($event)" (keydown.enter)="onCloseIconClick($event)" *ngIf="message.closable !== false" pRipple>
|
|
<span class="p-toast-icon-close-icon pi pi-times"></span>
|
|
</button>
|
|
<ng-container *ngTemplateOutlet="template; context: {$implicit: message}"></ng-container>
|
|
</div>
|
|
</div>
|
|
`,
|
|
animations: [
|
|
trigger('messageState', [
|
|
state('visible', style({
|
|
transform: 'translateY(0)',
|
|
opacity: 1
|
|
})),
|
|
transition('void => *', [
|
|
style({ transform: '{{showTransformParams}}', opacity: 0 }),
|
|
animate('{{showTransitionParams}}')
|
|
]),
|
|
transition('* => void', [
|
|
animate(('{{hideTransitionParams}}'), style({
|
|
height: 0,
|
|
opacity: 0,
|
|
transform: '{{hideTransformParams}}'
|
|
}))
|
|
])
|
|
])
|
|
],
|
|
encapsulation: ViewEncapsulation.None,
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
},] }
|
|
];
|
|
ToastItem.ctorParameters = () => [
|
|
{ type: NgZone }
|
|
];
|
|
ToastItem.propDecorators = {
|
|
message: [{ type: Input }],
|
|
index: [{ type: Input }],
|
|
template: [{ type: Input }],
|
|
showTransformOptions: [{ type: Input }],
|
|
hideTransformOptions: [{ type: Input }],
|
|
showTransitionOptions: [{ type: Input }],
|
|
hideTransitionOptions: [{ type: Input }],
|
|
onClose: [{ type: Output }],
|
|
containerViewChild: [{ type: ViewChild, args: ['container',] }]
|
|
};
|
|
class Toast {
|
|
constructor(messageService, cd) {
|
|
this.messageService = messageService;
|
|
this.cd = cd;
|
|
this.autoZIndex = true;
|
|
this.baseZIndex = 0;
|
|
this.position = 'top-right';
|
|
this.preventOpenDuplicates = false;
|
|
this.preventDuplicates = false;
|
|
this.showTransformOptions = 'translateY(100%)';
|
|
this.hideTransformOptions = 'translateY(-100%)';
|
|
this.showTransitionOptions = '300ms ease-out';
|
|
this.hideTransitionOptions = '250ms ease-in';
|
|
this.onClose = new EventEmitter();
|
|
}
|
|
ngOnInit() {
|
|
this.messageSubscription = this.messageService.messageObserver.subscribe(messages => {
|
|
if (messages) {
|
|
if (messages instanceof Array) {
|
|
const filteredMessages = messages.filter(m => this.canAdd(m));
|
|
this.add(filteredMessages);
|
|
}
|
|
else if (this.canAdd(messages)) {
|
|
this.add([messages]);
|
|
}
|
|
}
|
|
});
|
|
this.clearSubscription = this.messageService.clearObserver.subscribe(key => {
|
|
if (key) {
|
|
if (this.key === key) {
|
|
this.messages = null;
|
|
}
|
|
}
|
|
else {
|
|
this.messages = null;
|
|
}
|
|
this.cd.markForCheck();
|
|
});
|
|
}
|
|
add(messages) {
|
|
this.messages = this.messages ? [...this.messages, ...messages] : [...messages];
|
|
if (this.preventDuplicates) {
|
|
this.messagesArchieve = this.messagesArchieve ? [...this.messagesArchieve, ...messages] : [...messages];
|
|
}
|
|
this.cd.markForCheck();
|
|
}
|
|
canAdd(message) {
|
|
let allow = this.key === message.key;
|
|
if (allow && this.preventOpenDuplicates) {
|
|
allow = !this.containsMessage(this.messages, message);
|
|
}
|
|
if (allow && this.preventDuplicates) {
|
|
allow = !this.containsMessage(this.messagesArchieve, message);
|
|
}
|
|
return allow;
|
|
}
|
|
containsMessage(collection, message) {
|
|
if (!collection) {
|
|
return false;
|
|
}
|
|
return collection.find(m => {
|
|
return ((m.summary === message.summary) && (m.detail == message.detail) && (m.severity === message.severity));
|
|
}) != null;
|
|
}
|
|
ngAfterContentInit() {
|
|
this.templates.forEach((item) => {
|
|
switch (item.getType()) {
|
|
case 'message':
|
|
this.template = item.template;
|
|
break;
|
|
default:
|
|
this.template = item.template;
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
onMessageClose(event) {
|
|
this.messages.splice(event.index, 1);
|
|
this.onClose.emit({
|
|
message: event.message
|
|
});
|
|
this.cd.detectChanges();
|
|
}
|
|
onAnimationStart(event) {
|
|
if (event.fromState === 'void' && this.autoZIndex) {
|
|
this.containerViewChild.nativeElement.style.zIndex = String(this.baseZIndex + (++DomHandler.zindex));
|
|
}
|
|
}
|
|
ngOnDestroy() {
|
|
if (this.messageSubscription) {
|
|
this.messageSubscription.unsubscribe();
|
|
}
|
|
if (this.clearSubscription) {
|
|
this.clearSubscription.unsubscribe();
|
|
}
|
|
}
|
|
}
|
|
Toast.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: 'p-toast',
|
|
template: `
|
|
<div #container [ngClass]="'p-toast p-component p-toast-' + position" [ngStyle]="style" [class]="styleClass">
|
|
<p-toastItem *ngFor="let msg of messages; let i=index" [message]="msg" [index]="i" (onClose)="onMessageClose($event)"
|
|
[template]="template" @toastAnimation (@toastAnimation.start)="onAnimationStart($event)"
|
|
[showTransformOptions]="showTransformOptions" [hideTransformOptions]="hideTransformOptions"
|
|
[showTransitionOptions]="showTransitionOptions" [hideTransitionOptions]="hideTransitionOptions"></p-toastItem>
|
|
</div>
|
|
`,
|
|
animations: [
|
|
trigger('toastAnimation', [
|
|
transition(':enter, :leave', [
|
|
query('@*', animateChild())
|
|
])
|
|
])
|
|
],
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
encapsulation: ViewEncapsulation.None,
|
|
styles: [".p-toast{position:fixed;width:25rem}.p-toast-message{overflow:hidden}.p-toast-message-content{-ms-flex-align:start;align-items:flex-start;display:-ms-flexbox;display:flex}.p-toast-message-text{-ms-flex:1 1 auto;flex:1 1 auto}.p-toast-top-right{right:20px;top:20px}.p-toast-top-left{left:20px;top:20px}.p-toast-bottom-left{bottom:20px;left:20px}.p-toast-bottom-right{bottom:20px;right:20px}.p-toast-top-center{left:50%;margin-left:-10em;top:20px}.p-toast-bottom-center{bottom:20px;left:50%;margin-left:-10em}.p-toast-center{-ms-transform:translate(-50%,-50%);left:50%;min-width:20vw;top:50%;transform:translate(-50%,-50%)}.p-toast-icon-close{-ms-flex-align:center;-ms-flex-pack:center;align-items:center;display:-ms-flexbox;display:flex;justify-content:center;overflow:hidden;position:relative}.p-toast-icon-close.p-link{cursor:pointer}"]
|
|
},] }
|
|
];
|
|
Toast.ctorParameters = () => [
|
|
{ type: MessageService },
|
|
{ type: ChangeDetectorRef }
|
|
];
|
|
Toast.propDecorators = {
|
|
key: [{ type: Input }],
|
|
autoZIndex: [{ type: Input }],
|
|
baseZIndex: [{ type: Input }],
|
|
style: [{ type: Input }],
|
|
styleClass: [{ type: Input }],
|
|
position: [{ type: Input }],
|
|
preventOpenDuplicates: [{ type: Input }],
|
|
preventDuplicates: [{ type: Input }],
|
|
showTransformOptions: [{ type: Input }],
|
|
hideTransformOptions: [{ type: Input }],
|
|
showTransitionOptions: [{ type: Input }],
|
|
hideTransitionOptions: [{ type: Input }],
|
|
onClose: [{ type: Output }],
|
|
containerViewChild: [{ type: ViewChild, args: ['container',] }],
|
|
templates: [{ type: ContentChildren, args: [PrimeTemplate,] }]
|
|
};
|
|
class ToastModule {
|
|
}
|
|
ToastModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
imports: [CommonModule, RippleModule],
|
|
exports: [Toast, SharedModule],
|
|
declarations: [Toast, ToastItem]
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { Toast, ToastItem, ToastModule };
|
|
//# sourceMappingURL=primeng-toast.js.map
|