Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions components/core/tree/nz-tree-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { NzTreeNode, NzTreeNodeKey } from './nz-tree-base-node';
import { flattenTreeData, isCheckDisabled, isInArray } from './nz-tree-base-util';
import { NzFormatEmitEvent } from './nz-tree-base.definitions';

type BooleanKeys<T> = {
[K in keyof T]-?: T[K] extends boolean ? K : never;
}[keyof T];

@Injectable()
export class NzTreeBaseService {
DRAG_SIDE_RANGE = 0.25;
Expand All @@ -37,11 +41,11 @@ export class NzTreeBaseService {
*/
initTree(nzNodes: NzTreeNode[]): void {
this.rootNodes = nzNodes;
this.expandedNodeList = [];
this.selectedNodeList = [];
this.halfCheckedNodeList = [];
this.checkedNodeList = [];
this.matchedNodeList = [];
this.expandedNodeList = this.filterNodesRecursively(nzNodes, 'isExpanded');
this.selectedNodeList = this.filterNodesRecursively(nzNodes, 'isSelected');
this.halfCheckedNodeList = this.filterNodesRecursively(nzNodes, 'isHalfChecked');
this.checkedNodeList = this.filterNodesRecursively(nzNodes, 'isChecked');
this.matchedNodeList = this.filterNodesRecursively(nzNodes, 'isMatched');
}

flattenTreeData(nzNodes: NzTreeNode[], expandedKeys: NzTreeNodeKey[] | true = []): void {
Expand Down Expand Up @@ -567,4 +571,14 @@ export class NzTreeBaseService {
};
calc(node.getParentNode());
}

filterNodesRecursively(nodes: NzTreeNode[], propertyName: BooleanKeys<NzTreeNode>): NzTreeNode[] {
if (nodes == null) return [];
const reducer = (acc: NzTreeNode[], node: NzTreeNode): NzTreeNode[] => {
if (node[propertyName]) acc.push(node);
if (node.children.length > 0) node.children.reduce(reducer, acc);
return acc;
};
return nodes.reduce(reducer, []);
}
}