250 lines
11 KiB
JavaScript
250 lines
11 KiB
JavaScript
import { Component, ViewEncapsulation, ChangeDetectionStrategy, Inject, forwardRef, ChangeDetectorRef, Input, EventEmitter, ElementRef, Output, ContentChildren, NgModule } from '@angular/core';
|
|
import { trigger, state, style, transition, animate } from '@angular/animations';
|
|
import { CommonModule } from '@angular/common';
|
|
import { PrimeTemplate, SharedModule } from 'primeng/api';
|
|
import { Subject } from 'rxjs';
|
|
|
|
class OrganizationChartNode {
|
|
constructor(chart, cd) {
|
|
this.cd = cd;
|
|
this.chart = chart;
|
|
this.subscription = this.chart.selectionSource$.subscribe(() => {
|
|
this.cd.markForCheck();
|
|
});
|
|
}
|
|
get leaf() {
|
|
return this.node.leaf == false ? false : !(this.node.children && this.node.children.length);
|
|
}
|
|
get colspan() {
|
|
return (this.node.children && this.node.children.length) ? this.node.children.length * 2 : null;
|
|
}
|
|
onNodeClick(event, node) {
|
|
this.chart.onNodeClick(event, node);
|
|
}
|
|
toggleNode(event, node) {
|
|
node.expanded = !node.expanded;
|
|
if (node.expanded)
|
|
this.chart.onNodeExpand.emit({ originalEvent: event, node: this.node });
|
|
else
|
|
this.chart.onNodeCollapse.emit({ originalEvent: event, node: this.node });
|
|
event.preventDefault();
|
|
}
|
|
isSelected() {
|
|
return this.chart.isSelected(this.node);
|
|
}
|
|
ngOnDestroy() {
|
|
this.subscription.unsubscribe();
|
|
}
|
|
}
|
|
OrganizationChartNode.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: '[pOrganizationChartNode]',
|
|
template: `
|
|
<tbody *ngIf="node">
|
|
<tr>
|
|
<td [attr.colspan]="colspan">
|
|
<div [class]="node.styleClass" [ngClass]="{'p-organizationchart-node-content': true, 'p-organizationchart-selectable-node': chart.selectionMode && node.selectable !== false,'p-highlight':isSelected()}"
|
|
(click)="onNodeClick($event,node)">
|
|
<div *ngIf="!chart.getTemplateForNode(node)">{{node.label}}</div>
|
|
<div *ngIf="chart.getTemplateForNode(node)">
|
|
<ng-container *ngTemplateOutlet="chart.getTemplateForNode(node); context: {$implicit: node}"></ng-container>
|
|
</div>
|
|
<a *ngIf="!leaf" tabindex="0" class="p-node-toggler" (click)="toggleNode($event, node)" (keydown.enter)="toggleNode($event, node)">
|
|
<i class="p-node-toggler-icon pi" [ngClass]="{'pi-chevron-down': node.expanded, 'pi-chevron-up': !node.expanded}"></i>
|
|
</a>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<tr [ngClass]="!leaf&&node.expanded ? 'p-organizationchart-node-visible' : 'p-organizationchart-node-hidden'" class="p-organizationchart-lines" [@childState]="'in'">
|
|
<td [attr.colspan]="colspan">
|
|
<div class="p-organizationchart-line-down"></div>
|
|
</td>
|
|
</tr>
|
|
<tr [ngClass]="!leaf&&node.expanded ? 'p-organizationchart-node-visible' : 'p-organizationchart-node-hidden'" class="p-organizationchart-lines" [@childState]="'in'">
|
|
<ng-container *ngIf="node.children && node.children.length === 1">
|
|
<td [attr.colspan]="colspan">
|
|
<div class="p-organizationchart-line-down"></div>
|
|
</td>
|
|
</ng-container>
|
|
<ng-container *ngIf="node.children && node.children.length > 1">
|
|
<ng-template ngFor let-child [ngForOf]="node.children" let-first="first" let-last="last">
|
|
<td class="p-organizationchart-line-left" [ngClass]="{'p-organizationchart-line-top':!first}"> </td>
|
|
<td class="p-organizationchart-line-right" [ngClass]="{'p-organizationchart-line-top':!last}"> </td>
|
|
</ng-template>
|
|
</ng-container>
|
|
</tr>
|
|
<tr [ngClass]="!leaf&&node.expanded ? 'p-organizationchart-node-visible' : 'p-organizationchart-node-hidden'" class="p-organizationchart-nodes" [@childState]="'in'">
|
|
<td *ngFor="let child of node.children" colspan="2">
|
|
<table class="p-organizationchart-table" pOrganizationChartNode [node]="child"></table>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
`,
|
|
animations: [
|
|
trigger('childState', [
|
|
state('in', style({ opacity: 1 })),
|
|
transition('void => *', [
|
|
style({ opacity: 0 }),
|
|
animate(150)
|
|
]),
|
|
transition('* => void', [
|
|
animate(150, style({ opacity: 0 }))
|
|
])
|
|
])
|
|
],
|
|
encapsulation: ViewEncapsulation.None,
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
styles: [".p-organizationchart-table{border-collapse:separate;border-spacing:0;margin:0 auto}.p-organizationchart-table>tbody>tr>td{padding:0 .75rem;text-align:center;vertical-align:top}.p-organizationchart-node-content{display:inline-block;position:relative}.p-organizationchart-node-content .p-node-toggler{-moz-user-select:none;-ms-user-select:none;-webkit-user-select:none;bottom:-.75rem;cursor:pointer;height:1.5rem;left:50%;margin-left:-.75rem;position:absolute;user-select:none;width:1.5rem;z-index:2}.p-organizationchart-node-content .p-node-toggler .p-node-toggler-icon{position:relative;top:.25rem}.p-organizationchart-line-down{height:20px;margin:0 auto;width:1px}.p-organizationchart-line-left,.p-organizationchart-line-right{border-radius:0}.p-organizationchart-selectable-node{cursor:pointer}.p-organizationchart .p-organizationchart-node-hidden{display:none}.p-organizationchart-preservespace .p-organizationchart-node-hidden{display:inherit;visibility:hidden}"]
|
|
},] }
|
|
];
|
|
OrganizationChartNode.ctorParameters = () => [
|
|
{ type: undefined, decorators: [{ type: Inject, args: [forwardRef(() => OrganizationChart),] }] },
|
|
{ type: ChangeDetectorRef }
|
|
];
|
|
OrganizationChartNode.propDecorators = {
|
|
node: [{ type: Input }],
|
|
root: [{ type: Input }],
|
|
first: [{ type: Input }],
|
|
last: [{ type: Input }]
|
|
};
|
|
class OrganizationChart {
|
|
constructor(el, cd) {
|
|
this.el = el;
|
|
this.cd = cd;
|
|
this.preserveSpace = true;
|
|
this.selectionChange = new EventEmitter();
|
|
this.onNodeSelect = new EventEmitter();
|
|
this.onNodeUnselect = new EventEmitter();
|
|
this.onNodeExpand = new EventEmitter();
|
|
this.onNodeCollapse = new EventEmitter();
|
|
this.selectionSource = new Subject();
|
|
this.selectionSource$ = this.selectionSource.asObservable();
|
|
}
|
|
get selection() {
|
|
return this._selection;
|
|
}
|
|
set selection(val) {
|
|
this._selection = val;
|
|
if (this.initialized)
|
|
this.selectionSource.next();
|
|
}
|
|
get root() {
|
|
return this.value && this.value.length ? this.value[0] : null;
|
|
}
|
|
ngAfterContentInit() {
|
|
if (this.templates.length) {
|
|
this.templateMap = {};
|
|
}
|
|
this.templates.forEach((item) => {
|
|
this.templateMap[item.getType()] = item.template;
|
|
});
|
|
this.initialized = true;
|
|
}
|
|
getTemplateForNode(node) {
|
|
if (this.templateMap)
|
|
return node.type ? this.templateMap[node.type] : this.templateMap['default'];
|
|
else
|
|
return null;
|
|
}
|
|
onNodeClick(event, node) {
|
|
let eventTarget = event.target;
|
|
if (eventTarget.className && (eventTarget.className.indexOf('p-node-toggler') !== -1 || eventTarget.className.indexOf('p-node-toggler-icon') !== -1)) {
|
|
return;
|
|
}
|
|
else if (this.selectionMode) {
|
|
if (node.selectable === false) {
|
|
return;
|
|
}
|
|
let index = this.findIndexInSelection(node);
|
|
let selected = (index >= 0);
|
|
if (this.selectionMode === 'single') {
|
|
if (selected) {
|
|
this.selection = null;
|
|
this.onNodeUnselect.emit({ originalEvent: event, node: node });
|
|
}
|
|
else {
|
|
this.selection = node;
|
|
this.onNodeSelect.emit({ originalEvent: event, node: node });
|
|
}
|
|
}
|
|
else if (this.selectionMode === 'multiple') {
|
|
if (selected) {
|
|
this.selection = this.selection.filter((val, i) => i != index);
|
|
this.onNodeUnselect.emit({ originalEvent: event, node: node });
|
|
}
|
|
else {
|
|
this.selection = [...this.selection || [], node];
|
|
this.onNodeSelect.emit({ originalEvent: event, node: node });
|
|
}
|
|
}
|
|
this.selectionChange.emit(this.selection);
|
|
this.selectionSource.next();
|
|
}
|
|
}
|
|
findIndexInSelection(node) {
|
|
let index = -1;
|
|
if (this.selectionMode && this.selection) {
|
|
if (this.selectionMode === 'single') {
|
|
index = (this.selection == node) ? 0 : -1;
|
|
}
|
|
else if (this.selectionMode === 'multiple') {
|
|
for (let i = 0; i < this.selection.length; i++) {
|
|
if (this.selection[i] == node) {
|
|
index = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return index;
|
|
}
|
|
isSelected(node) {
|
|
return this.findIndexInSelection(node) != -1;
|
|
}
|
|
}
|
|
OrganizationChart.decorators = [
|
|
{ type: Component, args: [{
|
|
selector: 'p-organizationChart',
|
|
template: `
|
|
<div [ngStyle]="style" [class]="styleClass" [ngClass]="{'p-organizationchart p-component': true, 'p-organizationchart-preservespace': preserveSpace}">
|
|
<table class="p-organizationchart-table" pOrganizationChartNode [node]="root" *ngIf="root"></table>
|
|
</div>
|
|
`,
|
|
changeDetection: ChangeDetectionStrategy.OnPush
|
|
},] }
|
|
];
|
|
OrganizationChart.ctorParameters = () => [
|
|
{ type: ElementRef },
|
|
{ type: ChangeDetectorRef }
|
|
];
|
|
OrganizationChart.propDecorators = {
|
|
value: [{ type: Input }],
|
|
style: [{ type: Input }],
|
|
styleClass: [{ type: Input }],
|
|
selectionMode: [{ type: Input }],
|
|
preserveSpace: [{ type: Input }],
|
|
selection: [{ type: Input }],
|
|
selectionChange: [{ type: Output }],
|
|
onNodeSelect: [{ type: Output }],
|
|
onNodeUnselect: [{ type: Output }],
|
|
onNodeExpand: [{ type: Output }],
|
|
onNodeCollapse: [{ type: Output }],
|
|
templates: [{ type: ContentChildren, args: [PrimeTemplate,] }]
|
|
};
|
|
class OrganizationChartModule {
|
|
}
|
|
OrganizationChartModule.decorators = [
|
|
{ type: NgModule, args: [{
|
|
imports: [CommonModule],
|
|
exports: [OrganizationChart, SharedModule],
|
|
declarations: [OrganizationChart, OrganizationChartNode]
|
|
},] }
|
|
];
|
|
|
|
/**
|
|
* Generated bundle index. Do not edit.
|
|
*/
|
|
|
|
export { OrganizationChart, OrganizationChartModule, OrganizationChartNode };
|
|
//# sourceMappingURL=primeng-organizationchart.js.map
|