primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 27.3 kB
JavaScript
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, Renderer2, NgZone, ChangeDetectorRef, Input, ContentChild, ContentChildren, ViewChild, Output, NgModule } from '@angular/core';
import { animation, style, animate, trigger, transition, useAnimation } from '@angular/animations';
import { CommonModule } from '@angular/common';
import { DomHandler } from 'primeng/dom';
import { Header, Footer, PrimeTemplate, SharedModule } from 'primeng/api';
import { FocusTrapModule } from 'primeng/focustrap';
import { RippleModule } from 'primeng/ripple';
import { UniqueComponentId } from 'primeng/utils';
const showAnimation = animation([
style({ transform: '{{transform}}', opacity: 0 }),
animate('{{transition}}')
]);
const hideAnimation = animation([
animate('{{transition}}', style({ transform: '{{transform}}', opacity: 0 }))
]);
class Dialog {
constructor(el, renderer, zone, cd) {
this.el = el;
this.renderer = renderer;
this.zone = zone;
this.cd = cd;
this.draggable = true;
this.resizable = true;
this.closeOnEscape = true;
this.closable = true;
this.showHeader = true;
this.blockScroll = false;
this.autoZIndex = true;
this.baseZIndex = 0;
this.minX = 0;
this.minY = 0;
this.focusOnShow = true;
this.keepInViewport = true;
this.focusTrap = true;
this.transitionOptions = '150ms cubic-bezier(0, 0, 0.2, 1)';
this.closeIcon = 'pi pi-times';
this.minimizeIcon = 'pi pi-window-minimize';
this.maximizeIcon = 'pi pi-window-maximize';
this.onShow = new EventEmitter();
this.onHide = new EventEmitter();
this.visibleChange = new EventEmitter();
this.onResizeInit = new EventEmitter();
this.onResizeEnd = new EventEmitter();
this.onDragEnd = new EventEmitter();
this.onMaximize = new EventEmitter();
this.id = UniqueComponentId();
this._style = {};
this._position = "center";
this.transformOptions = "scale(0.7)";
}
get positionLeft() {
return 0;
}
;
set positionLeft(_positionLeft) {
console.log("positionLeft property is deprecated.");
}
get positionTop() {
return 0;
}
;
set positionTop(_positionTop) {
console.log("positionTop property is deprecated.");
}
get responsive() {
return false;
}
;
set responsive(_responsive) {
console.log("Responsive property is deprecated.");
}
get breakpoint() {
return 649;
}
;
set breakpoint(_breakpoint) {
console.log("Breakpoint property is not utilized and deprecated, use breakpoints or CSS media queries instead.");
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'header':
this.headerTemplate = item.template;
break;
case 'content':
this.contentTemplate = item.template;
break;
case 'footer':
this.footerTemplate = item.template;
break;
default:
this.contentTemplate = item.template;
break;
}
});
}
ngOnInit() {
if (this.breakpoints) {
this.createStyle();
}
}
get visible() {
return this._visible;
}
set visible(value) {
this._visible = value;
if (this._visible && !this.maskVisible) {
this.maskVisible = true;
}
}
get style() {
return this._style;
}
set style(value) {
if (value) {
this._style = Object.assign({}, value);
this.originalStyle = value;
}
}
get position() {
return this._position;
}
;
set position(value) {
this._position = value;
switch (value) {
case 'topleft':
case 'bottomleft':
case 'left':
this.transformOptions = "translate3d(-100%, 0px, 0px)";
break;
case 'topright':
case 'bottomright':
case 'right':
this.transformOptions = "translate3d(100%, 0px, 0px)";
break;
case 'bottom':
this.transformOptions = "translate3d(0px, 100%, 0px)";
break;
case 'top':
this.transformOptions = "translate3d(0px, -100%, 0px)";
break;
default:
this.transformOptions = "scale(0.7)";
break;
}
}
focus() {
let focusable = DomHandler.findSingle(this.container, '[autofocus]');
if (focusable) {
this.zone.runOutsideAngular(() => {
setTimeout(() => focusable.focus(), 5);
});
}
}
close(event) {
this.visibleChange.emit(false);
event.preventDefault();
}
enableModality() {
if (this.closable && this.dismissableMask) {
this.maskClickListener = this.renderer.listen(this.wrapper, 'mousedown', (event) => {
if (this.wrapper && this.wrapper.isSameNode(event.target)) {
this.close(event);
}
});
}
if (this.modal) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
}
}
disableModality() {
if (this.wrapper) {
if (this.dismissableMask) {
this.unbindMaskClickListener();
}
if (this.modal) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
if (!this.cd.destroyed) {
this.cd.detectChanges();
}
}
}
maximize() {
this.maximized = !this.maximized;
if (!this.modal && !this.blockScroll) {
if (this.maximized)
DomHandler.addClass(document.body, 'p-overflow-hidden');
else
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
this.onMaximize.emit({ 'maximized': this.maximized });
}
unbindMaskClickListener() {
if (this.maskClickListener) {
this.maskClickListener();
this.maskClickListener = null;
}
}
moveOnTop() {
if (this.autoZIndex) {
this.container.style.zIndex = String(this.baseZIndex + (++DomHandler.zindex));
this.wrapper.style.zIndex = String(this.baseZIndex + (DomHandler.zindex - 1));
}
}
createStyle() {
if (!this.styleElement) {
this.styleElement = document.createElement('style');
this.styleElement.type = 'text/css';
document.head.appendChild(this.styleElement);
let innerHTML = '';
for (let breakpoint in this.breakpoints) {
innerHTML += `
@media screen and (max-width: ${breakpoint}) {
.p-dialog[${this.id}] {
width: ${this.breakpoints[breakpoint]} !important;
}
}
`;
}
this.styleElement.innerHTML = innerHTML;
}
}
initDrag(event) {
if (DomHandler.hasClass(event.target, 'p-dialog-header-icon') || DomHandler.hasClass(event.target.parentElement, 'p-dialog-header-icon')) {
return;
}
if (this.draggable) {
this.dragging = true;
this.lastPageX = event.pageX;
this.lastPageY = event.pageY;
this.container.style.margin = '0';
DomHandler.addClass(document.body, 'p-unselectable-text');
}
}
onKeydown(event) {
if (this.focusTrap) {
if (event.which === 9) {
event.preventDefault();
let focusableElements = DomHandler.getFocusableElements(this.container);
if (focusableElements && focusableElements.length > 0) {
if (!focusableElements[0].ownerDocument.activeElement) {
focusableElements[0].focus();
}
else {
let focusedIndex = focusableElements.indexOf(focusableElements[0].ownerDocument.activeElement);
if (event.shiftKey) {
if (focusedIndex == -1 || focusedIndex === 0)
focusableElements[focusableElements.length - 1].focus();
else
focusableElements[focusedIndex - 1].focus();
}
else {
if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
focusableElements[0].focus();
else
focusableElements[focusedIndex + 1].focus();
}
}
}
}
}
}
onDrag(event) {
if (this.dragging) {
let containerWidth = DomHandler.getOuterWidth(this.container);
let containerHeight = DomHandler.getOuterHeight(this.container);
let deltaX = event.pageX - this.lastPageX;
let deltaY = event.pageY - this.lastPageY;
let offset = DomHandler.getOffset(this.container);
let leftPos = offset.left + deltaX;
let topPos = offset.top + deltaY;
let viewport = DomHandler.getViewport();
this.container.style.position = 'fixed';
if (this.keepInViewport) {
if (leftPos >= this.minX && (leftPos + containerWidth) < viewport.width) {
this._style.left = leftPos + 'px';
this.lastPageX = event.pageX;
this.container.style.left = leftPos + 'px';
}
if (topPos >= this.minY && (topPos + containerHeight) < viewport.height) {
this._style.top = topPos + 'px';
this.lastPageY = event.pageY;
this.container.style.top = topPos + 'px';
}
}
else {
this.lastPageX = event.pageX;
this.container.style.left = leftPos + 'px';
this.lastPageY = event.pageY;
this.container.style.top = topPos + 'px';
}
}
}
endDrag(event) {
if (this.dragging) {
this.dragging = false;
DomHandler.removeClass(document.body, 'p-unselectable-text');
this.cd.detectChanges();
this.onDragEnd.emit(event);
}
}
resetPosition() {
this.container.style.position = '';
this.container.style.left = '';
this.container.style.top = '';
this.container.style.margin = '';
}
//backward compatibility
center() {
this.resetPosition();
}
initResize(event) {
if (this.resizable) {
this.resizing = true;
this.lastPageX = event.pageX;
this.lastPageY = event.pageY;
DomHandler.addClass(document.body, 'p-unselectable-text');
this.onResizeInit.emit(event);
}
}
onResize(event) {
if (this.resizing) {
let deltaX = event.pageX - this.lastPageX;
let deltaY = event.pageY - this.lastPageY;
let containerWidth = DomHandler.getOuterWidth(this.container);
let containerHeight = DomHandler.getOuterHeight(this.container);
let contentHeight = DomHandler.getOuterHeight(this.contentViewChild.nativeElement);
let newWidth = containerWidth + deltaX;
let newHeight = containerHeight + deltaY;
let minWidth = this.container.style.minWidth;
let minHeight = this.container.style.minHeight;
let offset = DomHandler.getOffset(this.container);
let viewport = DomHandler.getViewport();
let hasBeenDragged = !parseInt(this.container.style.top) || !parseInt(this.container.style.left);
if (hasBeenDragged) {
newWidth += deltaX;
newHeight += deltaY;
}
if ((!minWidth || newWidth > parseInt(minWidth)) && (offset.left + newWidth) < viewport.width) {
this._style.width = newWidth + 'px';
this.container.style.width = this._style.width;
}
if ((!minHeight || newHeight > parseInt(minHeight)) && (offset.top + newHeight) < viewport.height) {
this.contentViewChild.nativeElement.style.height = contentHeight + newHeight - containerHeight + 'px';
if (this._style.height) {
this._style.height = newHeight + 'px';
this.container.style.height = this._style.height;
}
}
this.lastPageX = event.pageX;
this.lastPageY = event.pageY;
}
}
resizeEnd(event) {
if (this.resizing) {
this.resizing = false;
DomHandler.removeClass(document.body, 'p-unselectable-text');
this.onResizeEnd.emit(event);
}
}
bindGlobalListeners() {
if (this.draggable) {
this.bindDocumentDragListener();
this.bindDocumentDragEndListener();
}
if (this.resizable) {
this.bindDocumentResizeListeners();
}
if (this.closeOnEscape && this.closable) {
this.bindDocumentEscapeListener();
}
}
unbindGlobalListeners() {
this.unbindDocumentDragListener();
this.unbindDocumentDragEndListener();
this.unbindDocumentResizeListeners();
this.unbindDocumentEscapeListener();
}
bindDocumentDragListener() {
this.zone.runOutsideAngular(() => {
this.documentDragListener = this.onDrag.bind(this);
window.document.addEventListener('mousemove', this.documentDragListener);
});
}
unbindDocumentDragListener() {
if (this.documentDragListener) {
window.document.removeEventListener('mousemove', this.documentDragListener);
this.documentDragListener = null;
}
}
bindDocumentDragEndListener() {
this.zone.runOutsideAngular(() => {
this.documentDragEndListener = this.endDrag.bind(this);
window.document.addEventListener('mouseup', this.documentDragEndListener);
});
}
unbindDocumentDragEndListener() {
if (this.documentDragEndListener) {
window.document.removeEventListener('mouseup', this.documentDragEndListener);
this.documentDragEndListener = null;
}
}
bindDocumentResizeListeners() {
this.zone.runOutsideAngular(() => {
this.documentResizeListener = this.onResize.bind(this);
this.documentResizeEndListener = this.resizeEnd.bind(this);
window.document.addEventListener('mousemove', this.documentResizeListener);
window.document.addEventListener('mouseup', this.documentResizeEndListener);
});
}
unbindDocumentResizeListeners() {
if (this.documentResizeListener && this.documentResizeEndListener) {
window.document.removeEventListener('mousemove', this.documentResizeListener);
window.document.removeEventListener('mouseup', this.documentResizeEndListener);
this.documentResizeListener = null;
this.documentResizeEndListener = null;
}
}
bindDocumentEscapeListener() {
const documentTarget = this.el ? this.el.nativeElement.ownerDocument : 'document';
this.documentEscapeListener = this.renderer.listen(documentTarget, 'keydown', (event) => {
if (event.which == 27) {
if (parseInt(this.container.style.zIndex) === (DomHandler.zindex + this.baseZIndex)) {
this.close(event);
}
}
});
}
unbindDocumentEscapeListener() {
if (this.documentEscapeListener) {
this.documentEscapeListener();
this.documentEscapeListener = null;
}
}
appendContainer() {
if (this.appendTo) {
if (this.appendTo === 'body')
document.body.appendChild(this.wrapper);
else
DomHandler.appendChild(this.wrapper, this.appendTo);
}
}
restoreAppend() {
if (this.container && this.appendTo) {
this.el.nativeElement.appendChild(this.wrapper);
}
}
onAnimationStart(event) {
switch (event.toState) {
case 'visible':
this.container = event.element;
this.wrapper = this.container.parentElement;
this.appendContainer();
this.moveOnTop();
this.bindGlobalListeners();
this.container.setAttribute(this.id, '');
if (this.modal) {
this.enableModality();
}
if (!this.modal && this.blockScroll) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
}
if (this.focusOnShow) {
this.focus();
}
break;
}
}
onAnimationEnd(event) {
switch (event.toState) {
case 'void':
this.onContainerDestroy();
this.onHide.emit({});
break;
case 'visible':
this.onShow.emit({});
break;
}
}
onContainerDestroy() {
this.unbindGlobalListeners();
this.dragging = false;
this.maskVisible = false;
if (this.maximized) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
this.maximized = false;
}
if (this.modal) {
this.disableModality();
}
if (this.blockScroll) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
this.container = null;
this.wrapper = null;
this._style = this.originalStyle ? Object.assign({}, this.originalStyle) : {};
}
destroyStyle() {
if (this.styleElement) {
document.head.removeChild(this.styleElement);
this.styleElement = null;
}
}
ngOnDestroy() {
if (this.container) {
this.restoreAppend();
this.onContainerDestroy();
}
this.destroyStyle();
}
}
Dialog.decorators = [
{ type: Component, args: [{
selector: 'p-dialog',
template: `
<div *ngIf="maskVisible" [class]="maskStyleClass"
[ngClass]="{'p-dialog-mask': true, 'p-component-overlay': this.modal, 'p-dialog-mask-scrollblocker': this.modal || this.blockScroll,
'p-dialog-left': position === 'left',
'p-dialog-right': position === 'right',
'p-dialog-top': position === 'top',
'p-dialog-top-left': position === 'topleft' || position === 'top-left',
'p-dialog-top-right': position === 'topright' || position === 'top-right',
'p-dialog-bottom': position === 'bottom',
'p-dialog-bottom-left': position === 'bottomleft' || position === 'bottom-left',
'p-dialog-bottom-right': position === 'bottomright' || position === 'bottom-right'}">
<div #container [ngClass]="{'p-dialog p-component':true, 'p-dialog-rtl':rtl,'p-dialog-draggable':draggable,'p-dialog-resizable':resizable, 'p-dialog-maximized': maximized}"
[ngStyle]="style" [class]="styleClass" *ngIf="visible" pFocusTrap [pFocusTrapDisabled]="focusTrap === false"
[@animation]="{value: 'visible', params: {transform: transformOptions, transition: transitionOptions}}" (@animation.start)="onAnimationStart($event)" (@animation.done)="onAnimationEnd($event)" role="dialog" [attr.aria-labelledby]="id + '-label'">
<div #titlebar class="p-dialog-header" (mousedown)="initDrag($event)" *ngIf="showHeader">
<span [attr.id]="id + '-label'" class="p-dialog-title" *ngIf="header">{{header}}</span>
<span [attr.id]="id + '-label'" class="p-dialog-title" *ngIf="headerFacet">
<ng-content select="p-header"></ng-content>
</span>
<ng-container *ngTemplateOutlet="headerTemplate"></ng-container>
<div class="p-dialog-header-icons">
<button *ngIf="maximizable" type="button" [ngClass]="{'p-dialog-header-icon p-dialog-header-maximize p-link':true}" (click)="maximize()" (keydown.enter)="maximize()" tabindex="-1" pRipple>
<span class="p-dialog-header-maximize-icon" [ngClass]="maximized ? minimizeIcon : maximizeIcon"></span>
</button>
<button *ngIf="closable" type="button" [ngClass]="{'p-dialog-header-icon p-dialog-header-close p-link':true}" (click)="close($event)" (keydown.enter)="close($event)" tabindex="-1" pRipple>
<span class="p-dialog-header-close-icon" [ngClass]="closeIcon"></span>
</button>
</div>
</div>
<div #content [ngClass]="'p-dialog-content'" [ngStyle]="contentStyle" [class]="contentStyleClass">
<ng-content></ng-content>
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</div>
<div #footer class="p-dialog-footer" *ngIf="footerFacet || footerTemplate">
<ng-content select="p-footer"></ng-content>
<ng-container *ngTemplateOutlet="footerTemplate"></ng-container>
</div>
<div *ngIf="resizable" class="p-resizable-handle" style="z-index: 90;" (mousedown)="initResize($event)"></div>
</div>
</div>
`,
animations: [
trigger('animation', [
transition('void => visible', [
useAnimation(showAnimation)
]),
transition('visible => void', [
useAnimation(hideAnimation)
])
])
],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
styles: [".p-dialog-mask{align-items:center;background-color:transparent;display:flex;height:100%;justify-content:center;left:0;pointer-events:none;position:fixed;top:0;transition-property:background-color;width:100%}.p-dialog,.p-dialog-mask.p-component-overlay{pointer-events:auto}.p-dialog{display:flex;flex-direction:column;max-height:90%;position:relative;transform:scale(1)}.p-dialog-content{overflow-y:auto}.p-dialog-header{align-items:center;display:flex;flex-shrink:0;justify-content:space-between}.p-dialog-footer{flex-shrink:0}.p-dialog .p-dialog-header-icons{align-items:center;display:flex}.p-dialog .p-dialog-header-icon{align-items:center;display:flex;justify-content:center;overflow:hidden;position:relative}.p-dialog-mask.p-dialog-mask-leave{background-color:transparent}.p-fluid .p-dialog-footer .p-button{width:auto}.p-dialog-bottom-left .p-dialog,.p-dialog-bottom-right .p-dialog,.p-dialog-bottom .p-dialog,.p-dialog-left .p-dialog,.p-dialog-right .p-dialog,.p-dialog-top-left .p-dialog,.p-dialog-top-right .p-dialog,.p-dialog-top .p-dialog{margin:.75rem;transform:translateZ(0)}.p-dialog-maximized{height:100%;left:0!important;max-height:100%;top:0!important;transform:none;transition:none;width:100vw!important}.p-dialog-maximized .p-dialog-content{flex-grow:1}.p-dialog-left{justify-content:flex-start}.p-dialog-right{justify-content:flex-end}.p-dialog-top,.p-dialog-top-left{align-items:flex-start}.p-dialog-top-left{justify-content:flex-start}.p-dialog-top-right{align-items:flex-start;justify-content:flex-end}.p-dialog-bottom{align-items:flex-end}.p-dialog-bottom-left{align-items:flex-end;justify-content:flex-start}.p-dialog-bottom-right{align-items:flex-end;justify-content:flex-end}.p-dialog .p-resizable-handle{bottom:1px;cursor:se-resize;display:block;font-size:.1px;height:12px;position:absolute;right:1px;width:12px}.p-confirm-dialog .p-dialog-content{align-items:center;display:flex}"]
},] }
];
Dialog.ctorParameters = () => [
{ type: ElementRef },
{ type: Renderer2 },
{ type: NgZone },
{ type: ChangeDetectorRef }
];
Dialog.propDecorators = {
header: [{ type: Input }],
draggable: [{ type: Input }],
resizable: [{ type: Input }],
positionLeft: [{ type: Input }],
positionTop: [{ type: Input }],
contentStyle: [{ type: Input }],
contentStyleClass: [{ type: Input }],
modal: [{ type: Input }],
closeOnEscape: [{ type: Input }],
dismissableMask: [{ type: Input }],
rtl: [{ type: Input }],
closable: [{ type: Input }],
responsive: [{ type: Input }],
appendTo: [{ type: Input }],
breakpoints: [{ type: Input }],
styleClass: [{ type: Input }],
maskStyleClass: [{ type: Input }],
showHeader: [{ type: Input }],
breakpoint: [{ type: Input }],
blockScroll: [{ type: Input }],
autoZIndex: [{ type: Input }],
baseZIndex: [{ type: Input }],
minX: [{ type: Input }],
minY: [{ type: Input }],
focusOnShow: [{ type: Input }],
maximizable: [{ type: Input }],
keepInViewport: [{ type: Input }],
focusTrap: [{ type: Input }],
transitionOptions: [{ type: Input }],
closeIcon: [{ type: Input }],
minimizeIcon: [{ type: Input }],
maximizeIcon: [{ type: Input }],
headerFacet: [{ type: ContentChild, args: [Header,] }],
footerFacet: [{ type: ContentChild, args: [Footer,] }],
templates: [{ type: ContentChildren, args: [PrimeTemplate,] }],
headerViewChild: [{ type: ViewChild, args: ['titlebar',] }],
contentViewChild: [{ type: ViewChild, args: ['content',] }],
footerViewChild: [{ type: ViewChild, args: ['footer',] }],
onShow: [{ type: Output }],
onHide: [{ type: Output }],
visibleChange: [{ type: Output }],
onResizeInit: [{ type: Output }],
onResizeEnd: [{ type: Output }],
onDragEnd: [{ type: Output }],
onMaximize: [{ type: Output }],
visible: [{ type: Input }],
style: [{ type: Input }],
position: [{ type: Input }]
};
class DialogModule {
}
DialogModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, FocusTrapModule, RippleModule],
exports: [Dialog, SharedModule],
declarations: [Dialog]
},] }
];
/**
* Generated bundle index. Do not edit.
*/
export { Dialog, DialogModule };
//# sourceMappingURL=primeng-dialog.js.map