2019-10-04 16:14:14 +09:00
|
|
|
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
|
2019-10-04 16:14:14 +09:00
|
|
|
} from '@angular/core';
|
|
|
|
import { MatTreeNestedDataSource, MatTree } from '@angular/material';
|
|
|
|
import { NestedTreeControl } from '@angular/cdk/tree';
|
|
|
|
import { BehaviorSubject } from 'rxjs';
|
|
|
|
import { NGXLogger } from 'ngx-logger';
|
|
|
|
|
2019-10-31 11:58:39 +09:00
|
|
|
import { DeptInfo } from '@ucap-webmessenger/protocol-query';
|
|
|
|
import { LoginResponse } from '@ucap-webmessenger/protocol-authentication';
|
|
|
|
|
2019-10-04 16:14:14 +09:00
|
|
|
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-31 11:58:39 +09:00
|
|
|
@Input()
|
|
|
|
loginRes: LoginResponse;
|
2019-10-04 13:45:02 +09:00
|
|
|
@Input()
|
2019-10-04 16:14:14 +09:00
|
|
|
set oraganizationList(deptInfo: DeptInfo[]) {
|
|
|
|
const nodeMap = new Map<number, OraganizationNode>();
|
|
|
|
const rootNodeList: OraganizationNode[] = [];
|
|
|
|
const remainChildNodeList: OraganizationNode[] = [];
|
2019-10-31 11:58:39 +09:00
|
|
|
let myNode: OraganizationNode;
|
2019-10-04 16:14:14 +09:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-10-31 11:58:39 +09:00
|
|
|
if (value.seq === this.loginRes.departmentCode) {
|
|
|
|
myNode = node;
|
|
|
|
}
|
|
|
|
|
2019-10-04 16:14:14 +09:00
|
|
|
if (0 === value.parentSeq) {
|
|
|
|
rootNodeList.push(node);
|
|
|
|
return;
|
|
|
|
}
|
2019-10-04 13:45:02 +09:00
|
|
|
|
2019-10-04 16:14:14 +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 11:58:39 +09:00
|
|
|
// console.log('myNode', myNode);
|
|
|
|
// console.log('this.dataSource.data', this.dataSource.data[0]);
|
|
|
|
// this.treeControl.expandDescendants(this.dataSource.data[2]);
|
|
|
|
// console.log('this.dataSource', this.dataSource);
|
|
|
|
// console.log('this.dataSource.data', this.dataSource.data);
|
|
|
|
// console.log('this.treeControl', this.treeControl);
|
|
|
|
// console.log('this.treeControl.dataNodes', this.treeControl.dataNodes);
|
|
|
|
|
|
|
|
// const myNode = this.treeControl.dataNodes.filter(
|
|
|
|
// node => node.deptInfo.seq === this.loginRes.departmentCode
|
|
|
|
// );
|
|
|
|
// if (!!myNode && myNode.length > 0) {
|
|
|
|
// this.treeControl.expand(myNode[0]);
|
|
|
|
// }
|
2019-10-04 16:14:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@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-04 16:14:14 +09:00
|
|
|
|
2019-10-31 10:13:30 +09:00
|
|
|
ngAfterViewInit(): void {}
|
|
|
|
|
2019-10-04 16:14:14 +09:00
|
|
|
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
|
|
|
}
|