primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 10.7 kB
JavaScript
import { EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ElementRef, Renderer2, ChangeDetectorRef, Input, ContentChildren, Output, NgModule } from '@angular/core';
import { animation, style, animate, trigger, transition, useAnimation } from '@angular/animations';
import { CommonModule } from '@angular/common';
import { RippleModule } from 'primeng/ripple';
import { DomHandler } from 'primeng/dom';
import { PrimeTemplate } from 'primeng/api';
const showAnimation = animation([
style({ transform: '{{transform}}', opacity: 0 }),
animate('{{transition}}')
]);
const hideAnimation = animation([
animate('{{transition}}', style({ transform: '{{transform}}', opacity: 0 }))
]);
class Sidebar {
constructor(el, renderer, cd) {
this.el = el;
this.renderer = renderer;
this.cd = cd;
this.blockScroll = false;
this.autoZIndex = true;
this.baseZIndex = 0;
this.modal = true;
this.dismissible = true;
this.showCloseIcon = true;
this.closeOnEscape = true;
this.transitionOptions = '150ms cubic-bezier(0, 0, 0.2, 1)';
this.onShow = new EventEmitter();
this.onHide = new EventEmitter();
this.visibleChange = new EventEmitter();
this._position = "left";
this._fullScreen = false;
this.transformOptions = "translate3d(-100%, 0px, 0px)";
}
ngAfterViewInit() {
this.initialized = true;
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'content':
this.contentTemplate = item.template;
break;
default:
this.contentTemplate = item.template;
break;
}
});
}
get visible() {
return this._visible;
}
set visible(val) {
this._visible = val;
}
get position() {
return this._position;
}
;
set position(value) {
this._position = value;
switch (value) {
case 'left':
this.transformOptions = "translate3d(100%, 0px, 0px)";
break;
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;
}
}
get fullScreen() {
return this._fullScreen;
}
set fullScreen(value) {
this._fullScreen = value;
if (value)
this.transformOptions = "none";
}
show() {
if (this.autoZIndex) {
this.container.style.zIndex = String(this.baseZIndex + (++DomHandler.zindex));
}
if (this.modal) {
this.enableModality();
}
this.onShow.emit({});
}
hide() {
this.onHide.emit({});
if (this.modal) {
this.disableModality();
}
}
close(event) {
this.hide();
this.visibleChange.emit(false);
event.preventDefault();
}
enableModality() {
if (!this.mask) {
this.mask = document.createElement('div');
this.mask.style.zIndex = String(parseInt(this.container.style.zIndex) - 1);
DomHandler.addMultipleClasses(this.mask, 'p-component-overlay p-sidebar-mask');
if (this.dismissible) {
this.maskClickListener = this.renderer.listen(this.mask, 'click', (event) => {
if (this.dismissible) {
this.close(event);
}
});
}
document.body.appendChild(this.mask);
if (this.blockScroll) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
}
}
}
disableModality() {
if (this.mask) {
this.unbindMaskClickListener();
document.body.removeChild(this.mask);
if (this.blockScroll) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
this.mask = null;
}
}
onAnimationStart(event) {
switch (event.toState) {
case 'visible':
this.container = event.element;
this.appendContainer();
this.show();
if (this.closeOnEscape) {
this.bindDocumentEscapeListener();
}
break;
case 'void':
this.hide();
this.unbindGlobalListeners();
break;
}
}
appendContainer() {
if (this.appendTo) {
if (this.appendTo === 'body')
document.body.appendChild(this.container);
else
DomHandler.appendChild(this.container, this.appendTo);
}
}
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;
}
}
unbindMaskClickListener() {
if (this.maskClickListener) {
this.maskClickListener();
this.maskClickListener = null;
}
}
unbindGlobalListeners() {
this.unbindMaskClickListener();
this.unbindDocumentEscapeListener();
}
ngOnDestroy() {
this.initialized = false;
if (this.visible) {
this.hide();
}
if (this.appendTo) {
this.el.nativeElement.appendChild(this.container);
}
this.unbindGlobalListeners();
}
}
Sidebar.decorators = [
{ type: Component, args: [{
selector: 'p-sidebar',
template: `
<div #container [ngClass]="{'p-sidebar':true, 'p-sidebar-active': visible,
'p-sidebar-left': (position === 'left' && !fullScreen), 'p-sidebar-right': (position === 'right' && !fullScreen),
'p-sidebar-top': (position === 'top' && !fullScreen), 'p-sidebar-bottom': (position === 'bottom' && !fullScreen),
'p-sidebar-full': fullScreen}" *ngIf="visible" [@panelState]="{value: 'visible', params: {transform: transformOptions, transition: transitionOptions}}" (@panelState.start)="onAnimationStart($event)" [ngStyle]="style" [class]="styleClass" role="complementary" [attr.aria-modal]="modal">
<div class="p-sidebar-header">
<button type="button" class="p-sidebar-close p-sidebar-icon p-link" *ngIf="showCloseIcon" (click)="close($event)" (keydown.enter)="close($event)" [attr.aria-label]="ariaCloseLabel" pRipple>
<span class="p-sidebar-close-icon pi pi-times"></span>
</button>
</div>
<div class="p-sidebar-content">
<ng-content></ng-content>
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</div>
</div>
`,
animations: [
trigger('panelState', [
transition('void => visible', [
useAnimation(showAnimation)
]),
transition('visible => void', [
useAnimation(hideAnimation)
])
])
],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
styles: [".p-sidebar{display:flex;flex-direction:column;position:fixed;transition:transform .3s}.p-sidebar-content{overflow-y:auto;position:relative}.p-sidebar-header{align-items:center;display:flex;justify-content:flex-end}.p-sidebar-icon{align-items:center;display:flex;justify-content:center}.p-sidebar-mask{transition-property:background-color}.p-sidebar-mask,.p-sidebar-mask.p-sidebar-mask-leave.p-component-overlay{background-color:transparent}.p-sidebar-left{height:100%;left:0;top:0;width:20rem}.p-sidebar-right{height:100%;right:0;top:0;width:20rem}.p-sidebar-top{height:10rem;left:0;top:0;width:100%}.p-sidebar-bottom{bottom:0;height:10rem;left:0;width:100%}.p-sidebar-full{height:100%;left:0;top:0;transition:none;width:100%}.p-sidebar-left.p-sidebar-sm,.p-sidebar-right.p-sidebar-sm{width:20rem}.p-sidebar-left.p-sidebar-md,.p-sidebar-right.p-sidebar-md{width:40rem}.p-sidebar-left.p-sidebar-lg,.p-sidebar-right.p-sidebar-lg{width:60rem}.p-sidebar-bottom.p-sidebar-sm,.p-sidebar-top.p-sidebar-sm{height:10rem}.p-sidebar-bottom.p-sidebar-md,.p-sidebar-top.p-sidebar-md{height:20rem}.p-sidebar-bottom.p-sidebar-lg,.p-sidebar-top.p-sidebar-lg{height:30rem}@media screen and (max-width:64em){.p-sidebar-left.p-sidebar-lg,.p-sidebar-left.p-sidebar-md,.p-sidebar-right.p-sidebar-lg,.p-sidebar-right.p-sidebar-md{width:20rem}}"]
},] }
];
Sidebar.ctorParameters = () => [
{ type: ElementRef },
{ type: Renderer2 },
{ type: ChangeDetectorRef }
];
Sidebar.propDecorators = {
appendTo: [{ type: Input }],
blockScroll: [{ type: Input }],
style: [{ type: Input }],
styleClass: [{ type: Input }],
ariaCloseLabel: [{ type: Input }],
autoZIndex: [{ type: Input }],
baseZIndex: [{ type: Input }],
modal: [{ type: Input }],
dismissible: [{ type: Input }],
showCloseIcon: [{ type: Input }],
closeOnEscape: [{ type: Input }],
transitionOptions: [{ type: Input }],
templates: [{ type: ContentChildren, args: [PrimeTemplate,] }],
onShow: [{ type: Output }],
onHide: [{ type: Output }],
visibleChange: [{ type: Output }],
visible: [{ type: Input }],
position: [{ type: Input }],
fullScreen: [{ type: Input }]
};
class SidebarModule {
}
SidebarModule.decorators = [
{ type: NgModule, args: [{
imports: [CommonModule, RippleModule],
exports: [Sidebar],
declarations: [Sidebar]
},] }
];
/**
* Generated bundle index. Do not edit.
*/
export { Sidebar, SidebarModule };
//# sourceMappingURL=primeng-sidebar.js.map