120 lines
3.1 KiB
TypeScript
Raw Normal View History

import {
Component,
OnInit,
Input,
ChangeDetectorRef,
2019-10-07 10:08:14 +09:00
ViewChild,
Output,
2019-10-31 10:13:30 +09:00
EventEmitter,
AfterViewInit
} from '@angular/core';
import { DeptInfo } from '@ucap-webmessenger/protocol-query';
import { MatTreeNestedDataSource, MatTree } from '@angular/material';
import { NestedTreeControl } from '@angular/cdk/tree';
import { BehaviorSubject } from 'rxjs';
import { NGXLogger } from 'ngx-logger';
export class OraganizationNode {
private childNodeBehaviorSubject: BehaviorSubject<OraganizationNode[]>;
private childNodeList: OraganizationNode[];
get title(): string {
return this.deptInfo.name;
}
get children(): BehaviorSubject<OraganizationNode[]> {
if (!this.childNodeBehaviorSubject) {
this.childNodeBehaviorSubject = new BehaviorSubject(
undefined === this.childNodeList ? [] : this.childNodeList
);
}
return this.childNodeBehaviorSubject;
}
constructor(public deptInfo: DeptInfo) {}
addChild(childNode: OraganizationNode) {
if (!this.childNodeList) {
this.childNodeList = [];
}
this.childNodeList.push(childNode);
}
}
2019-10-04 13:45:02 +09:00
@Component({
selector: 'ucap-organization-tree',
templateUrl: './tree.component.html',
styleUrls: ['./tree.component.scss']
})
2019-10-31 10:13:30 +09:00
export class TreeComponent implements OnInit, AfterViewInit {
2019-10-07 10:08:14 +09:00
@Output()
selected = new EventEmitter<DeptInfo>();
2019-10-04 13:45:02 +09:00
@Input()
set oraganizationList(deptInfo: DeptInfo[]) {
const nodeMap = new Map<number, OraganizationNode>();
const rootNodeList: OraganizationNode[] = [];
const remainChildNodeList: OraganizationNode[] = [];
deptInfo.forEach(value => {
const node = new OraganizationNode(value);
if (nodeMap.has(value.seq)) {
this.logger.warn('duplicate seq', value.seq);
return;
}
nodeMap.set(value.seq, node);
if (0 === value.parentSeq) {
rootNodeList.push(node);
return;
}
2019-10-04 13:45:02 +09:00
if (nodeMap.has(value.parentSeq)) {
nodeMap.get(value.parentSeq).addChild(node);
} else {
remainChildNodeList.push(node);
}
});
remainChildNodeList.forEach(value => {
if (nodeMap.has(value.deptInfo.parentSeq)) {
nodeMap.get(value.deptInfo.parentSeq).addChild(value);
}
});
this.dataSource.data = rootNodeList;
2019-10-31 10:13:30 +09:00
this.treeControl.expand(this.dataSource.data[0]);
}
@ViewChild('orgranizationTree', { static: true })
orgranizationTree: MatTree<OraganizationNode>;
levels = new Map<OraganizationNode, number>();
treeControl: NestedTreeControl<OraganizationNode>;
dataSource: MatTreeNestedDataSource<OraganizationNode>;
constructor(
private changeDetectorRef: ChangeDetectorRef,
private logger: NGXLogger
) {
this.treeControl = new NestedTreeControl<OraganizationNode>(
this.getChildren
);
this.dataSource = new MatTreeNestedDataSource();
}
2019-10-04 13:45:02 +09:00
ngOnInit() {}
2019-10-31 10:13:30 +09:00
ngAfterViewInit(): void {}
getChildren = (node: OraganizationNode) => node.children;
hasChildren = (index: number, node: OraganizationNode) =>
0 < node.children.value.length;
2019-10-07 10:08:14 +09:00
onClickNode(node: OraganizationNode) {
this.selected.emit(node.deptInfo);
}
2019-10-04 13:45:02 +09:00
}