primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 23.4 kB
JavaScript
import { EventEmitter, ElementRef, Renderer2, NgZone, ChangeDetectorRef, Input, ContentChildren, ViewChild, Output, Component, ChangeDetectionStrategy, 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, SharedModule } from 'primeng/api';
import { FocusTrapModule } from 'primeng/focustrap';
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
let idx = 0;
const showAnimation = animation([
style({ transform: '{{transform}}', opacity: 0 }),
animate('{{transition}}', style({ transform: 'none', opacity: 1 }))
]);
const hideAnimation = animation([
animate('{{transition}}', style({ transform: '{{transform}}', opacity: 0 }))
]);
let Dialog = 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.id = `ui-dialog-${idx++}`;
this._style = {};
this._position = "center";
this.transformOptions = "scale(0.7)";
}
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 CSS media queries instead.");
}
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, 'button');
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, 'click', (event) => {
if (!this.container.isSameNode(event.target) && !this.container.contains(event.target)) {
this.close(event);
}
});
}
if (this.modal) {
DomHandler.addClass(document.body, 'ui-overflow-hidden');
}
}
disableModality() {
if (this.wrapper) {
if (this.dismissableMask) {
this.unbindMaskClickListener();
}
if (this.modal) {
DomHandler.removeClass(document.body, 'ui-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, 'ui-overflow-hidden');
else
DomHandler.removeClass(document.body, 'ui-overflow-hidden');
}
}
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));
}
}
getMaskClass() {
let maskClass = { 'ui-dialog-mask': true, 'ui-widget-overlay': this.modal, 'ui-dialog-visible': this.maskVisible, 'ui-dialog-mask-scrollblocker': this.modal || this.blockScroll };
maskClass[this.getPositionClass().toString()] = true;
return maskClass;
}
getPositionClass() {
const positions = ['left', 'right', 'top', 'topleft', 'topright', 'bottom', 'bottomleft', 'bottomright'];
const pos = positions.find(item => item === this.position);
return pos ? `ui-dialog-${pos}` : '';
}
initDrag(event) {
if (DomHandler.hasClass(event.target, 'ui-dialog-titlebar-icon') || DomHandler.hasClass(event.target.parentElement, 'ui-dialog-titlebar-icon')) {
return;
}
if (this.draggable) {
this.dragging = true;
this.lastPageX = event.pageX;
this.lastPageY = event.pageY;
DomHandler.addClass(document.body, 'ui-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 (!document.activeElement) {
focusableElements[0].focus();
}
else {
let focusedIndex = focusableElements.indexOf(document.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();
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.draggable) {
this.dragging = false;
DomHandler.removeClass(document.body, 'ui-unselectable-text');
}
}
initResize(event) {
if (this.resizable) {
this.resizing = true;
this.lastPageX = event.pageX;
this.lastPageY = event.pageY;
DomHandler.addClass(document.body, 'ui-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();
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 + deltaY + 'px';
}
this.lastPageX = event.pageX;
this.lastPageY = event.pageY;
}
}
resizeEnd(event) {
if (this.resizing) {
this.resizing = false;
DomHandler.removeClass(document.body, 'ui-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() {
this.documentEscapeListener = this.renderer.listen('document', '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.onShow.emit({});
this.appendContainer();
this.moveOnTop();
this.bindGlobalListeners();
if (this.modal) {
this.enableModality();
}
if (!this.modal && this.blockScroll) {
DomHandler.addClass(document.body, 'ui-overflow-hidden');
}
if (this.focusOnShow) {
this.focus();
}
break;
}
}
onAnimationEnd(event) {
switch (event.toState) {
case 'void':
this.onContainerDestroy();
this.onHide.emit({});
break;
}
}
onContainerDestroy() {
this.unbindGlobalListeners();
this.dragging = false;
this.maskVisible = false;
if (this.maximized) {
DomHandler.removeClass(document.body, 'ui-overflow-hidden');
this.maximized = false;
}
if (this.modal) {
this.disableModality();
}
if (this.blockScroll) {
DomHandler.removeClass(document.body, 'ui-overflow-hidden');
}
this.container = null;
this.wrapper = null;
this._style = this.originalStyle ? Object.assign({}, this.originalStyle) : {};
}
ngOnDestroy() {
if (this.container) {
this.restoreAppend();
this.onContainerDestroy();
}
}
};
Dialog.ctorParameters = () => [
{ type: ElementRef },
{ type: Renderer2 },
{ type: NgZone },
{ type: ChangeDetectorRef }
];
__decorate([
Input()
], Dialog.prototype, "header", void 0);
__decorate([
Input()
], Dialog.prototype, "draggable", void 0);
__decorate([
Input()
], Dialog.prototype, "resizable", void 0);
__decorate([
Input()
], Dialog.prototype, "positionLeft", void 0);
__decorate([
Input()
], Dialog.prototype, "positionTop", void 0);
__decorate([
Input()
], Dialog.prototype, "contentStyle", void 0);
__decorate([
Input()
], Dialog.prototype, "modal", void 0);
__decorate([
Input()
], Dialog.prototype, "closeOnEscape", void 0);
__decorate([
Input()
], Dialog.prototype, "dismissableMask", void 0);
__decorate([
Input()
], Dialog.prototype, "rtl", void 0);
__decorate([
Input()
], Dialog.prototype, "closable", void 0);
__decorate([
Input()
], Dialog.prototype, "responsive", null);
__decorate([
Input()
], Dialog.prototype, "appendTo", void 0);
__decorate([
Input()
], Dialog.prototype, "styleClass", void 0);
__decorate([
Input()
], Dialog.prototype, "maskStyleClass", void 0);
__decorate([
Input()
], Dialog.prototype, "showHeader", void 0);
__decorate([
Input()
], Dialog.prototype, "breakpoint", null);
__decorate([
Input()
], Dialog.prototype, "blockScroll", void 0);
__decorate([
Input()
], Dialog.prototype, "autoZIndex", void 0);
__decorate([
Input()
], Dialog.prototype, "baseZIndex", void 0);
__decorate([
Input()
], Dialog.prototype, "minX", void 0);
__decorate([
Input()
], Dialog.prototype, "minY", void 0);
__decorate([
Input()
], Dialog.prototype, "focusOnShow", void 0);
__decorate([
Input()
], Dialog.prototype, "maximizable", void 0);
__decorate([
Input()
], Dialog.prototype, "keepInViewport", void 0);
__decorate([
Input()
], Dialog.prototype, "focusTrap", void 0);
__decorate([
Input()
], Dialog.prototype, "transitionOptions", void 0);
__decorate([
Input()
], Dialog.prototype, "closeIcon", void 0);
__decorate([
Input()
], Dialog.prototype, "minimizeIcon", void 0);
__decorate([
Input()
], Dialog.prototype, "maximizeIcon", void 0);
__decorate([
ContentChildren(Header, { descendants: false })
], Dialog.prototype, "headerFacet", void 0);
__decorate([
ContentChildren(Footer, { descendants: false })
], Dialog.prototype, "footerFacet", void 0);
__decorate([
ViewChild('titlebar')
], Dialog.prototype, "headerViewChild", void 0);
__decorate([
ViewChild('content')
], Dialog.prototype, "contentViewChild", void 0);
__decorate([
ViewChild('footer')
], Dialog.prototype, "footerViewChild", void 0);
__decorate([
Output()
], Dialog.prototype, "onShow", void 0);
__decorate([
Output()
], Dialog.prototype, "onHide", void 0);
__decorate([
Output()
], Dialog.prototype, "visibleChange", void 0);
__decorate([
Output()
], Dialog.prototype, "onResizeInit", void 0);
__decorate([
Output()
], Dialog.prototype, "onResizeEnd", void 0);
__decorate([
Input()
], Dialog.prototype, "visible", null);
__decorate([
Input()
], Dialog.prototype, "style", null);
__decorate([
Input()
], Dialog.prototype, "position", null);
Dialog = __decorate([
Component({
selector: 'p-dialog',
template: `
<div [class]="maskStyleClass" [ngClass]="getMaskClass()" *ngIf="maskVisible">
<div #container [ngClass]="{'ui-dialog ui-widget ui-widget-content ui-corner-all ui-shadow':true, 'ui-dialog-rtl':rtl,'ui-dialog-draggable':draggable,'ui-dialog-resizable':resizable, 'ui-dialog-maximized': maximized}"
[ngStyle]="style" [class]="styleClass" *ngIf="visible" pFocusTrap [pFocusTrapDisabled]="focusTrap === false"
[]="{value: 'visible', params: {transform: transformOptions, transition: transitionOptions}}" (.start)="onAnimationStart($event)" (.done)="onAnimationEnd($event)" role="dialog" [attr.aria-labelledby]="id + '-label'">
<div #titlebar class="ui-dialog-titlebar ui-widget-header ui-helper-clearfix ui-corner-top" (mousedown)="initDrag($event)" *ngIf="showHeader">
<span [attr.id]="id + '-label'" class="ui-dialog-title" *ngIf="header">{{header}}</span>
<span [attr.id]="id + '-label'" class="ui-dialog-title" *ngIf="headerFacet && headerFacet.first">
<ng-content select="p-header"></ng-content>
</span>
<div class="ui-dialog-titlebar-icons">
<a *ngIf="maximizable" [ngClass]="{'ui-dialog-titlebar-icon ui-dialog-titlebar-maximize ui-corner-all':true}" tabindex="0" role="button" (click)="maximize()" (keydown.enter)="maximize()">
<span [ngClass]="maximized ? minimizeIcon : maximizeIcon"></span>
</a>
<a *ngIf="closable" [ngClass]="{'ui-dialog-titlebar-icon ui-dialog-titlebar-close ui-corner-all':true}" tabindex="0" role="button" (click)="close($event)" (keydown.enter)="close($event)">
<span [class]="closeIcon"></span>
</a>
</div>
</div>
<div #content class="ui-dialog-content ui-widget-content" [ngStyle]="contentStyle">
<ng-content></ng-content>
</div>
<div #footer class="ui-dialog-footer ui-widget-content" *ngIf="footerFacet && footerFacet.first">
<ng-content select="p-footer"></ng-content>
</div>
<div *ngIf="resizable" class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se" 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.Default
})
], Dialog);
let DialogModule = class DialogModule {
};
DialogModule = __decorate([
NgModule({
imports: [CommonModule, FocusTrapModule],
exports: [Dialog, SharedModule],
declarations: [Dialog]
})
], DialogModule);
/**
* Generated bundle index. Do not edit.
*/
export { Dialog, DialogModule };
//# sourceMappingURL=primeng-dialog.js.map