Skip to content

Commit

Permalink
feat(module:tree-select): add the public methods (#3335)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsuanxyz authored and vthinkxie committed Apr 25, 2019
1 parent cb68c9d commit ee6d18b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 68 deletions.
94 changes: 94 additions & 0 deletions components/core/tree/nz-tree-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { NzTreeNode } from './nz-tree-base-node';
import { NzTreeBaseService } from './nz-tree-base.service';

export class NzTreeBase {
constructor(public nzTreeService: NzTreeBaseService) {}

/**
* Coerces a value({@link any[]}) to a TreeNodes({@link NzTreeNode[]})
*/
coerceTreeNodes(
// tslint:disable-next-line:no-any
value: any[]
): NzTreeNode[] {
let nodes: NzTreeNode[] = [];
if (!this.nzTreeService.isArrayOfNzTreeNode(value)) {
// has not been new NzTreeNode
nodes = value.map(item => new NzTreeNode(item, null, this.nzTreeService));
} else {
nodes = value.map((item: NzTreeNode) => {
item.service = this.nzTreeService;
return item;
});
}
return nodes;
}

/**
* Get all nodes({@link NzTreeNode})
*/
getTreeNodes(): NzTreeNode[] {
return this.nzTreeService.rootNodes;
}

/**
* Get {@link NzTreeNode} with key
*/
getTreeNodeByKey(key: string): NzTreeNode | null {
// flat tree nodes
const nodes: NzTreeNode[] = [];
const getNode = (node: NzTreeNode): void => {
nodes.push(node);
node.getChildren().forEach(n => {
getNode(n);
});
};
this.getTreeNodes().forEach(n => {
getNode(n);
});
return nodes.find(n => n.key === key) || null;
}

/**
* Get checked nodes(merged)
*/
getCheckedNodeList(): NzTreeNode[] {
return this.nzTreeService.getCheckedNodeList();
}

/**
* Get selected nodes
*/
getSelectedNodeList(): NzTreeNode[] {
return this.nzTreeService.getSelectedNodeList();
}

/**
* Get half checked nodes
*/
getHalfCheckedNodeList(): NzTreeNode[] {
return this.nzTreeService.getHalfCheckedNodeList();
}

/**
* Get expanded nodes
*/
getExpandedNodeList(): NzTreeNode[] {
return this.nzTreeService.getExpandedNodeList();
}

/**
* Get matched nodes(if nzSearchValue is not null)
*/
getMatchedNodeList(): NzTreeNode[] {
return this.nzTreeService.getMatchedNodeList();
}
}
1 change: 1 addition & 0 deletions components/core/tree/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './nz-tree-base-node';
export * from './nz-tree-base.definitions';
export * from './nz-tree-base.service';
export * from './nz-tree-service.resolver';
export * from './nz-tree-base';
12 changes: 12 additions & 0 deletions components/tree-select/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ import { NzTreeSelectModule } from 'ng-zorro-antd';
| `[nzMaxTagCount]` | Max tag count to show| number | - |
| `[nzMaxTagPlaceholder]` | Placeholder for not showing tags | TemplateRef<{ $implicit: NzTreeNode[] }> | - |
| `(nzExpandChange)` | Callback function for when a treeNode is expanded or collapsed |`EventEmitter<NzFormatEmitEvent>` | - |

#### Methods

| Property | Description | Type |
| -------- | ----------- | ---- |
| getTreeNodes | get all nodes(NzTreeNode) | `NzTreeNode[]` |
| getTreeNodeByKey | get NzTreeNode with key | `NzTreeNode` |
| getCheckedNodeList | get checked nodes(merged) | `NzTreeNode[]` |
| getSelectedNodeList | get selected nodes | `NzTreeNode[]` |
| getHalfCheckedNodeList | get half checked nodes | `NzTreeNode[]` |
| getExpandedNodeList | get expanded nodes | `NzTreeNode[]` |
| getMatchedNodeList | get matched nodes(if nzSearchValue is not null) | `NzTreeNode[]` |
12 changes: 12 additions & 0 deletions components/tree-select/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ import { NzTreeSelectModule } from 'ng-zorro-antd';
| `[nzMaxTagCount]` | 最多显示多少个 tag | number | - |
| `[nzMaxTagPlaceholder]` | 隐藏 tag 时显示的内容 | TemplateRef<{ $implicit: NzTreeNode[] }> | - |
| `(nzExpandChange)` | 点击展开树节点图标调用 | `EventEmitter<NzFormatEmitEvent>` | - |

#### 方法

| 方法名 | 说明 | 返回值
| --- | --- | --- |
| getTreeNodes | 获取组件 NzTreeNode 节点 | `NzTreeNode[]` |
| getTreeNodeByKey | 按 key 获取 NzTreeNode 节点 | `NzTreeNode` |
| getCheckedNodeList | 获取组件 checkBox 被点击选中的节点 | `NzTreeNode[]` |
| getSelectedNodeList | 获取组件被选中的节点 | `NzTreeNode[]` |
| getHalfCheckedNodeList | 获取组件半选状态节点 | `NzTreeNode[]` |
| getExpandedNodeList | 获取组件展开状态节点 | `NzTreeNode[]` |
| getMatchedNodeList | 获取组搜索匹配到的节点 | `NzTreeNode[]` |
18 changes: 6 additions & 12 deletions components/tree-select/nz-tree-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
NzFormatEmitEvent,
NzNoAnimationDirective,
NzSizeLDSType,
NzTreeBase,
NzTreeBaseService,
NzTreeHigherOrderServiceToken,
NzTreeNode,
Expand Down Expand Up @@ -95,7 +96,7 @@ export function higherOrderServiceFactory(injector: Injector): NzTreeBaseService
`
]
})
export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, OnDestroy, OnChanges {
export class NzTreeSelectComponent extends NzTreeBase implements ControlValueAccessor, OnInit, OnDestroy, OnChanges {
@Input() @InputBoolean() nzAllowClear = true;
@Input() @InputBoolean() nzShowExpand = true;
@Input() @InputBoolean() nzShowLine = false;
Expand Down Expand Up @@ -176,12 +177,13 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, OnDe
}

constructor(
nzTreeService: NzTreeSelectService,
private renderer: Renderer2,
private cdr: ChangeDetectorRef,
private nzTreeService: NzTreeSelectService,
private elementRef: ElementRef,
@Host() @Optional() public noAnimation?: NzNoAnimationDirective
) {
super(nzTreeService);
this.renderer.addClass(this.elementRef.nativeElement, 'ant-select');
}

Expand Down Expand Up @@ -356,24 +358,16 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, OnDe

updateSelectedNodes(init: boolean = false): void {
if (init) {
let nodes;
const nodes = this.coerceTreeNodes(this.nzNodes);
this.nzTreeService.isMultiple = this.isMultiple;
if (!this.nzTreeService.isArrayOfNzTreeNode(this.nzNodes)) {
// has not been new NzTreeNode
nodes = this.nzNodes.map(item => new NzTreeNode(item, undefined, this.nzTreeService));
} else {
nodes = this.nzNodes.map(item => new NzTreeNode({ ...item.origin }, undefined, this.nzTreeService));
}
this.nzTreeService.initTree(nodes);
if (this.nzCheckable) {
this.nzTreeService.calcCheckedKeys(this.value, nodes);
} else {
this.nzTreeService.calcSelectedKeys(this.value, nodes, this.isMultiple);
}
}
this.selectedNodes = [
...(this.nzCheckable ? this.nzTreeService.getCheckedNodeList() : this.nzTreeService.getSelectedNodeList())
];
this.selectedNodes = [...(this.nzCheckable ? this.getCheckedNodeList() : this.getSelectedNodeList())];
}

updatePosition(): void {
Expand Down
63 changes: 7 additions & 56 deletions components/tree/nz-tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
NzFormatBeforeDropEvent,
NzFormatEmitEvent,
NzNoAnimationDirective,
NzTreeBase,
NzTreeBaseService,
NzTreeHigherOrderServiceToken,
NzTreeNode
Expand Down Expand Up @@ -68,7 +69,7 @@ export function NzTreeServiceFactory(
}
]
})
export class NzTreeComponent implements OnInit, OnDestroy, ControlValueAccessor, OnChanges {
export class NzTreeComponent extends NzTreeBase implements OnInit, OnDestroy, ControlValueAccessor, OnChanges {
@Input() @InputBoolean() nzShowIcon = false;
@Input() @InputBoolean() nzShowExpand = true;
@Input() @InputBoolean() nzShowLine = false;
Expand Down Expand Up @@ -205,48 +206,6 @@ export class NzTreeComponent implements OnInit, OnDestroy, ControlValueAccessor,
onChange: (value: NzTreeNode[]) => void = () => null;
onTouched: () => void = () => null;

getTreeNodes(): NzTreeNode[] {
return this.nzTreeService.rootNodes;
}

getTreeNodeByKey(key: string): NzTreeNode | null {
// flat tree nodes
const nodes: NzTreeNode[] = [];
const getNode = (node: NzTreeNode): void => {
nodes.push(node);
node.getChildren().forEach(n => {
getNode(n);
});
};
this.nzNodes.forEach(n => {
getNode(n);
});
return nodes.find(n => n.key === key) || null;
}

/**
* public function
*/
getCheckedNodeList(): NzTreeNode[] {
return this.nzTreeService.getCheckedNodeList();
}

getSelectedNodeList(): NzTreeNode[] {
return this.nzTreeService.getSelectedNodeList();
}

getHalfCheckedNodeList(): NzTreeNode[] {
return this.nzTreeService.getHalfCheckedNodeList();
}

getExpandedNodeList(): NzTreeNode[] {
return this.nzTreeService.getExpandedNodeList();
}

getMatchedNodeList(): NzTreeNode[] {
return this.nzTreeService.getMatchedNodeList();
}

setClassMap(): void {
this.classMap = {
[this.prefixCls]: true,
Expand All @@ -272,28 +231,20 @@ export class NzTreeComponent implements OnInit, OnDestroy, ControlValueAccessor,

// tslint:disable-next-line:no-any
initNzData(value: any[]): void {
let nzNodes: NzTreeNode[] = [];
if (Array.isArray(value)) {
if (!this.nzTreeService.isArrayOfNzTreeNode(value)) {
// has not been new NzTreeNode
nzNodes = value.map(item => new NzTreeNode(item, null, this.nzTreeService));
} else {
nzNodes = value.map((item: NzTreeNode) => {
item.service = this.nzTreeService;
return item;
});
}
this.nzTreeService.isCheckStrictly = this.nzCheckStrictly;
this.nzTreeService.isMultiple = this.nzMultiple;
this.nzTreeService.initTree(nzNodes);
this.nzTreeService.initTree(this.coerceTreeNodes(value));
}
}

constructor(
public nzTreeService: NzTreeBaseService,
nzTreeService: NzTreeBaseService,
private cdr: ChangeDetectorRef,
@Host() @Optional() public noAnimation?: NzNoAnimationDirective
) {}
) {
super(nzTreeService);
}

ngOnInit(): void {
this.setClassMap();
Expand Down

0 comments on commit ee6d18b

Please sign in to comment.