primeng
Version:
[](https://opensource.org/licenses/MIT) [](https://badge.fury.io/js/primeng) [ • 11 kB
JavaScript
import { EventEmitter, Inject, forwardRef, ChangeDetectorRef, Input, Output, ContentChildren, Component, ChangeDetectionStrategy, ElementRef, NgModule } from '@angular/core';
import { trigger, state, style, transition, animate } from '@angular/animations';
import { CommonModule } from '@angular/common';
import { Header, PrimeTemplate, SharedModule } from 'primeng/api';
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;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
let idx = 0;
let AccordionTab = class AccordionTab {
constructor(accordion, changeDetector) {
this.changeDetector = changeDetector;
this.cache = true;
this.selectedChange = new EventEmitter();
this.transitionOptions = '400ms cubic-bezier(0.86, 0, 0.07, 1)';
this.id = `ui-accordiontab-${idx++}`;
this.accordion = accordion;
}
get selected() {
return this._selected;
}
set selected(val) {
this._selected = val;
if (!this.loaded) {
this.changeDetector.detectChanges();
}
}
get animating() {
return this._animating;
}
set animating(val) {
this._animating = val;
if (!this.changeDetector.destroyed) {
this.changeDetector.detectChanges();
}
}
ngAfterContentInit() {
this.templates.forEach((item) => {
switch (item.getType()) {
case 'content':
this.contentTemplate = item.template;
break;
default:
this.contentTemplate = item.template;
break;
}
});
}
toggle(event) {
if (this.disabled || this.animating) {
return false;
}
this.animating = true;
let index = this.findTabIndex();
if (this.selected) {
this.selected = false;
this.accordion.onClose.emit({ originalEvent: event, index: index });
}
else {
if (!this.accordion.multiple) {
for (var i = 0; i < this.accordion.tabs.length; i++) {
this.accordion.tabs[i].selected = false;
this.accordion.tabs[i].selectedChange.emit(false);
}
}
this.selected = true;
this.loaded = true;
this.accordion.onOpen.emit({ originalEvent: event, index: index });
}
this.selectedChange.emit(this.selected);
this.accordion.updateActiveIndex();
event.preventDefault();
}
findTabIndex() {
let index = -1;
for (var i = 0; i < this.accordion.tabs.length; i++) {
if (this.accordion.tabs[i] == this) {
index = i;
break;
}
}
return index;
}
get hasHeaderFacet() {
return this.headerFacet && this.headerFacet.length > 0;
}
onToggleDone(event) {
this.animating = false;
}
onKeydown(event) {
if (event.which === 32 || event.which === 13) {
this.toggle(event);
event.preventDefault();
}
}
ngOnDestroy() {
this.accordion.tabs.splice(this.findTabIndex(), 1);
}
};
AccordionTab.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Inject, args: [forwardRef(() => Accordion),] }] },
{ type: ChangeDetectorRef }
];
__decorate([
Input()
], AccordionTab.prototype, "header", void 0);
__decorate([
Input()
], AccordionTab.prototype, "disabled", void 0);
__decorate([
Input()
], AccordionTab.prototype, "cache", void 0);
__decorate([
Output()
], AccordionTab.prototype, "selectedChange", void 0);
__decorate([
Input()
], AccordionTab.prototype, "transitionOptions", void 0);
__decorate([
ContentChildren(Header)
], AccordionTab.prototype, "headerFacet", void 0);
__decorate([
ContentChildren(PrimeTemplate)
], AccordionTab.prototype, "templates", void 0);
__decorate([
Input()
], AccordionTab.prototype, "selected", null);
AccordionTab = __decorate([
Component({
selector: 'p-accordionTab',
template: `
<div class="ui-accordion-header ui-state-default ui-corner-all" [ngClass]="{'ui-state-active': selected,'ui-state-disabled':disabled}">
<a [attr.tabindex]="disabled ? -1 : 0" [attr.id]="id" [attr.aria-controls]="id + '-content'" role="tab" [attr.aria-expanded]="selected" (click)="toggle($event)"
(keydown)="onKeydown($event)">
<span class="ui-accordion-toggle-icon" [ngClass]="selected ? accordion.collapseIcon : accordion.expandIcon"></span>
<span class="ui-accordion-header-text" *ngIf="!hasHeaderFacet">
{{header}}
</span>
<ng-content select="p-header" *ngIf="hasHeaderFacet"></ng-content>
</a>
</div>
<div [attr.id]="id + '-content'" class="ui-accordion-content-wrapper" []="selected ? {value: 'visible', params: {transitionParams: animating ? transitionOptions : '0ms', height: '*'}} : {value: 'hidden', params: {transitionParams: transitionOptions, height: '0'}}" (.done)="onToggleDone($event)"
[ngClass]="{'ui-accordion-content-wrapper-overflown': !selected||animating}"
role="region" [attr.aria-hidden]="!selected" [attr.aria-labelledby]="id">
<div class="ui-accordion-content ui-widget-content">
<ng-content></ng-content>
<ng-container *ngIf="contentTemplate && (cache ? loaded : selected)">
<ng-container *ngTemplateOutlet="contentTemplate"></ng-container>
</ng-container>
</div>
</div>
`,
animations: [
trigger('tabContent', [
state('hidden', style({
height: '0'
})),
state('void', style({
height: '{{height}}'
}), { params: { height: '0' } }),
state('visible', style({
height: '*'
})),
transition('visible <=> hidden', animate('{{transitionParams}}')),
transition('void => hidden', animate('{{transitionParams}}')),
transition('void => visible', animate('{{transitionParams}}'))
])
],
changeDetection: ChangeDetectionStrategy.Default
}),
__param(0, Inject(forwardRef(() => Accordion)))
], AccordionTab);
let Accordion = class Accordion {
constructor(el, changeDetector) {
this.el = el;
this.changeDetector = changeDetector;
this.onClose = new EventEmitter();
this.onOpen = new EventEmitter();
this.expandIcon = 'pi pi-fw pi-chevron-right';
this.collapseIcon = 'pi pi-fw pi-chevron-down';
this.activeIndexChange = new EventEmitter();
this.tabs = [];
}
ngAfterContentInit() {
this.initTabs();
this.tabListSubscription = this.tabList.changes.subscribe(_ => {
this.initTabs();
this.changeDetector.markForCheck();
});
}
initTabs() {
this.tabs = this.tabList.toArray();
this.updateSelectionState();
}
getBlockableElement() {
return this.el.nativeElement.children[0];
}
get activeIndex() {
return this._activeIndex;
}
set activeIndex(val) {
this._activeIndex = val;
if (this.preventActiveIndexPropagation) {
this.preventActiveIndexPropagation = false;
return;
}
this.updateSelectionState();
}
updateSelectionState() {
if (this.tabs && this.tabs.length && this._activeIndex != null) {
for (let i = 0; i < this.tabs.length; i++) {
let selected = this.multiple ? this._activeIndex.includes(i) : (i === this._activeIndex);
let changed = selected !== this.tabs[i].selected;
if (changed) {
this.tabs[i].animating = true;
this.tabs[i].selected = selected;
this.tabs[i].selectedChange.emit(selected);
}
}
}
}
updateActiveIndex() {
let index = this.multiple ? [] : null;
this.tabs.forEach((tab, i) => {
if (tab.selected) {
if (this.multiple) {
index.push(i);
}
else {
index = i;
return;
}
}
});
this.preventActiveIndexPropagation = true;
this.activeIndexChange.emit(index);
}
ngOnDestroy() {
if (this.tabListSubscription) {
this.tabListSubscription.unsubscribe();
}
}
};
Accordion.ctorParameters = () => [
{ type: ElementRef },
{ type: ChangeDetectorRef }
];
__decorate([
Input()
], Accordion.prototype, "multiple", void 0);
__decorate([
Output()
], Accordion.prototype, "onClose", void 0);
__decorate([
Output()
], Accordion.prototype, "onOpen", void 0);
__decorate([
Input()
], Accordion.prototype, "style", void 0);
__decorate([
Input()
], Accordion.prototype, "styleClass", void 0);
__decorate([
Input()
], Accordion.prototype, "expandIcon", void 0);
__decorate([
Input()
], Accordion.prototype, "collapseIcon", void 0);
__decorate([
Output()
], Accordion.prototype, "activeIndexChange", void 0);
__decorate([
ContentChildren(AccordionTab)
], Accordion.prototype, "tabList", void 0);
__decorate([
Input()
], Accordion.prototype, "activeIndex", null);
Accordion = __decorate([
Component({
selector: 'p-accordion',
template: `
<div [ngClass]="'ui-accordion ui-widget ui-helper-reset'" [ngStyle]="style" [class]="styleClass" role="tablist">
<ng-content></ng-content>
</div>
`
})
], Accordion);
let AccordionModule = class AccordionModule {
};
AccordionModule = __decorate([
NgModule({
imports: [CommonModule],
exports: [Accordion, AccordionTab, SharedModule],
declarations: [Accordion, AccordionTab]
})
], AccordionModule);
/**
* Generated bundle index. Do not edit.
*/
export { Accordion, AccordionModule, AccordionTab };
//# sourceMappingURL=primeng-accordion.js.map