Skip to content

Commit

Permalink
fix(module: tree): fix default keys bug with setTimeOut
Browse files Browse the repository at this point in the history
fix(module: tree): fix default keys bug with setTimeOut

close NG-ZORRO#3001

fix(module: tree): reset nzTreeNode component Input

fix(module: tree): rollback nzTreeNode component Input

fix(module: tree): rollback nzTreeNode component Input
  • Loading branch information
simplejason committed Mar 2, 2019
1 parent 623a9ff commit b9f99c1
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 229 deletions.
2 changes: 1 addition & 1 deletion components/tree-select/nz-tree-select.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class NzTreeSelectComponent implements ControlValueAccessor, OnInit, OnDe
if (this.selectedNodes.length) {
const removeNode = this.selectedNodes[ this.selectedNodes.length - 1 ];
this.removeSelected(removeNode);
this.nzTreeService.$statusChange.next({
this.nzTreeService.triggerEventChange$.next({
'eventName': 'removeSelect',
'node' : removeNode
});
Expand Down
13 changes: 7 additions & 6 deletions components/tree/demo/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ import { NzFormatEmitEvent, NzTreeNodeOptions } from 'ng-zorro-antd';
<nz-tree
#treeCom
[nzData]="nodes"
[nzDraggable]="true"
nzCheckable
nzMultiple
[nzCheckedKeys]="defaultCheckedKeys"
[nzExpandedKeys]="defaultExpandedKeys"
[nzSelectedKeys]="defaultSelectedKeys"
(nzClick)="nzClick($event)"
(nzCheckBoxChange)="nzCheck($event)">
(nzCheckBoxChange)="nzCheck($event)"
(nzExpandChange)="nzCheck($event)">
</nz-tree>
`
})

export class NzDemoTreeBasicComponent implements OnInit {
@ViewChild('treeCom') treeCom;
defaultCheckedKeys = [ '1001', '1002' ];
defaultSelectedKeys = [ '10010', '10020' ];
defaultCheckedKeys = [ '10020' ];
defaultSelectedKeys = [ '10010' ];
defaultExpandedKeys = [ '100', '1001' ];

nodes: NzTreeNodeOptions[] = [ {
Expand All @@ -33,7 +34,7 @@ export class NzDemoTreeBasicComponent implements OnInit {
disabled: true,
children: [
{ title: 'leaf 1-0-0', key: '10010', disableCheckbox: true, isLeaf: true },
{ title: 'leaf 1-0-1', key: '10011', isLeaf: true, checked: true }
{ title: 'leaf 1-0-1', key: '10011', isLeaf: true }
]
}, {
title : 'parent 1-1',
Expand All @@ -50,7 +51,7 @@ export class NzDemoTreeBasicComponent implements OnInit {
}

nzCheck(event: NzFormatEmitEvent): void {
console.log(event, event.checkedKeys, event.keys, event.nodes);
console.log(event);
}

// nzSelectedKeys change
Expand Down
5 changes: 2 additions & 3 deletions components/tree/demo/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,13 @@ export class NzDemoTreeDirectoryComponent {
openFolder(data: NzTreeNode | NzFormatEmitEvent): void {
// do something if u want
if (data instanceof NzTreeNode) {
data.setExpanded(!data.isExpanded);
data.isExpanded = !data.isExpanded;
} else {
data.node.setExpanded(!data.node.isExpanded);
data.node.isExpanded = !data.node.isExpanded;
}
}

activeNode(data: NzFormatEmitEvent): void {
data.node.setExpanded(true);
this.activedNode = data.node;
}

Expand Down
8 changes: 7 additions & 1 deletion components/tree/demo/draggable.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Component, OnInit } from '@angular/core';
import { NzFormatEmitEvent } from 'ng-zorro-antd';

@Component({
selector: 'nz-demo-tree-draggable',
template: `
<nz-tree
[nzData]="nodes"
nzDraggable="true">
nzDraggable="true"
(nzOnDrop)="afterDrop($event)">
</nz-tree>
`,
styles : [ `
Expand Down Expand Up @@ -67,6 +69,10 @@ export class NzDemoTreeDraggableComponent implements OnInit {
isLeaf: true
} ];

afterDrop(event: NzFormatEmitEvent): void {
console.log(event);
}

ngOnInit(): void {
}
}
1 change: 0 additions & 1 deletion components/tree/doc/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ Almost anything can be represented in a tree structure. Examples include directo
| isDisableCheckbox | Whether treeNode's checkbox can not be clicked | `boolean` | `true` / `false` |
| isSelectable | Set whether the treeNode can be selected | `boolean` | `true``false` |
| isChecked | Whether treeNode is checked | `boolean` | `true` / `false` |
| isAllChecked | Whether all treeNode's children are checked | `boolean` | `true` / `false` |
| isHalfChecked | Part of treeNode's children are checked | `boolean` | `true` / `false` |
| isSelected | Whether treeNode is selected | `boolean` | `true` / `false` |
| isLoading | Whether treeNode is loading(when nzAsyncData is true) | `boolean` | `true` / `false` |
Expand Down
1 change: 0 additions & 1 deletion components/tree/doc/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ subtitle: 树形控件
| isDisableCheckbox | 是否禁用 checkBox | `boolean` | `true``false` |
| isSelectable | 是否可选中 | `boolean` | `true``false` |
| isChecked | 是否选中 checkBox | `boolean` | `true``false` |
| isAllChecked | 子节点是否全选 | `boolean` | `true``false` |
| isHalfChecked | 子节点有选中但未全选 | `boolean` | `true``false` |
| isSelected | 是否已选中 | `boolean` | `true``false` |
| isLoading | 是否异步加载状态(影响展开图标展示) | `boolean` | `true``false` |
Expand Down
83 changes: 48 additions & 35 deletions components/tree/nz-tree-base.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ export class NzTreeBaseService implements OnDestroy {
checkedNodeList: NzTreeNode[] = [];
halfCheckedNodeList: NzTreeNode[] = [];
matchedNodeList: NzTreeNode[] = [];
$statusChange = new Subject<NzFormatEmitEvent>();
triggerEventChange$ = new Subject<NzFormatEmitEvent>();

statusChanged(): Observable<NzFormatEmitEvent> {
return this.$statusChange.asObservable();
/**
* trigger event
*/
eventTriggerChanged(): Observable<NzFormatEmitEvent> {
return this.triggerEventChange$.asObservable();
}

/**
Expand Down Expand Up @@ -89,17 +92,17 @@ export class NzTreeBaseService implements OnDestroy {
const calc = (nodes: NzTreeNode[]): boolean => {
return nodes.every(node => {
if (isInArray(node.key, selectedKeys)) {
node.setSelected(true, isMulti);
node.isSelected = true;
if (!isMulti) {
// if not support multi select
return false;
}
} else {
node.setSelected(false, isMulti);
node.isSelected = false;
}
if (node.getChildren().length > 0) {
if (node.children.length > 0) {
// Recursion
return calc(node.getChildren());
return calc(node.children);
}
return true;
});
Expand All @@ -115,13 +118,12 @@ export class NzTreeBaseService implements OnDestroy {
const calc = (nodes: NzTreeNode[]) => {
nodes.forEach(node => {
if (isInArray(node.key, expandedKeys)) {
node.setExpanded(true);
this.setExpandedNodeList(node);
node.isExpanded = true;
} else {
node.setExpanded(false);
node.isExpanded = false;
}
if (node.getChildren().length > 0) {
calc(node.getChildren());
if (node.children.length > 0) {
calc(node.children);
}
});
};
Expand All @@ -137,12 +139,14 @@ export class NzTreeBaseService implements OnDestroy {
const calc = (nodes: NzTreeNode[]) => {
nodes.forEach(node => {
if (isInArray(node.key, checkedKeys)) {
node.setChecked(true);
node.isChecked = true;
node.isHalfChecked = false;
} else {
node.setChecked(false);
node.isChecked = false;
node.isHalfChecked = false;
}
if (node.getChildren().length > 0) {
calc(node.getChildren());
if (node.children.length > 0) {
calc(node.children);
}
});
};
Expand All @@ -164,19 +168,18 @@ export class NzTreeBaseService implements OnDestroy {
/**
* set node selected status
*/
setNodeActive(node: NzTreeNode, isMultiple: boolean = false): void {
if (!isMultiple && node.isSelected) {
setNodeActive(node: NzTreeNode): void {
if (!this.isMultiple && node.isSelected) {
this.selectedNodeList.forEach(n => {
if (node.key !== n.key) {
// reset other nodes
n.origin.selected = false;
n.isSelected = false;
}
});
// single mode: remove pre node
this.selectedNodeList = [];
}
this.setSelectedNodeList(node, isMultiple);
this.setSelectedNodeList(node, this.isMultiple);
}

/**
Expand Down Expand Up @@ -288,6 +291,7 @@ export class NzTreeBaseService implements OnDestroy {
});
}

// reset other node checked state based current node
conduct(node: NzTreeNode): void {
const isChecked = node.isChecked;
if (node) {
Expand All @@ -306,14 +310,19 @@ export class NzTreeBaseService implements OnDestroy {
// 全禁用节点不选中
if (parentNode) {
if (!isCheckDisabled(parentNode)) {
if (parentNode.getChildren().every(child => isCheckDisabled(child) || (!child.isHalfChecked && child.isChecked))) {
parentNode.setChecked(true);
} else if (parentNode.getChildren().some(child => child.isHalfChecked || child.isChecked)) {
parentNode.setChecked(false, true);
if (parentNode.children.every(child => isCheckDisabled(child) || (!child.isHalfChecked && child.isChecked))) {
parentNode.isChecked = true;
parentNode.isHalfChecked = false;
} else if (parentNode.children.some(child => child.isHalfChecked || child.isChecked)) {
parentNode.isChecked = false;
parentNode.isHalfChecked = true;
} else {
parentNode.setChecked(false);
parentNode.isChecked = false;
parentNode.isHalfChecked = false;
}
}
this.setCheckedNodeList(parentNode);
this.setHalfCheckedNodeList(parentNode);
this.conductUp(parentNode);
}
}
Expand All @@ -323,7 +332,10 @@ export class NzTreeBaseService implements OnDestroy {
*/
conductDown(node: NzTreeNode, value: boolean): void {
if (!isCheckDisabled(node)) {
node.setChecked(value);
node.isChecked = value;
node.isHalfChecked = false;
this.setCheckedNodeList(node);
this.setHalfCheckedNodeList(node);
node.children.forEach(n => {
this.conductDown(n, value);
});
Expand Down Expand Up @@ -358,7 +370,7 @@ export class NzTreeBaseService implements OnDestroy {
} else {
n.isMatched = false;
}
n.getChildren().forEach(child => {
n.children.forEach(child => {
searchChild(child);
});
};
Expand Down Expand Up @@ -392,8 +404,8 @@ export class NzTreeBaseService implements OnDestroy {
this.checkedNodeList.splice(index, 1);
}

if (n.getChildren()) {
n.getChildren().forEach(child => {
if (n.children) {
n.children.forEach(child => {
loopNode(child);
});
}
Expand All @@ -408,11 +420,11 @@ export class NzTreeBaseService implements OnDestroy {
* drag event
*/
refreshDragNode(node: NzTreeNode): void {
if (node.getChildren().length === 0) {
if (node.children.length === 0) {
// until root
this.conductUp(node);
} else {
node.getChildren().forEach((child) => {
node.children.forEach((child) => {
this.refreshDragNode(child);
});
}
Expand All @@ -425,7 +437,7 @@ export class NzTreeBaseService implements OnDestroy {
} else {
node.level = 0;
}
for (const child of node.getChildren()) {
for (const child of node.children) {
this.resetNodeLevel(child);
}
}
Expand Down Expand Up @@ -457,7 +469,7 @@ export class NzTreeBaseService implements OnDestroy {
const isSelectedRootNode = this.selectedNode.getParentNode();
// remove the dragNode
if (isSelectedRootNode) {
isSelectedRootNode.getChildren().splice(isSelectedRootNode.getChildren().indexOf(this.selectedNode), 1);
isSelectedRootNode.children.splice(isSelectedRootNode.children.indexOf(this.selectedNode), 1);
} else {
this.rootNodes.splice(this.rootNodes.indexOf(this.selectedNode), 1);
}
Expand Down Expand Up @@ -519,6 +531,7 @@ export class NzTreeBaseService implements OnDestroy {
break;
case 'check':
const checkedNodeList = this.getCheckedNodeList();

Object.assign(emitStructure, { 'checkedKeys': checkedNodeList });
Object.assign(emitStructure, { 'nodes': checkedNodeList });
Object.assign(emitStructure, { 'keys': checkedNodeList.map(n => n.key) });
Expand All @@ -537,8 +550,8 @@ export class NzTreeBaseService implements OnDestroy {
}

ngOnDestroy(): void {
this.$statusChange.complete();
this.$statusChange = null;
this.triggerEventChange$.complete();
this.triggerEventChange$ = null;
}

}
11 changes: 3 additions & 8 deletions components/tree/nz-tree-node.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
</ng-container>
<ng-container *ngIf="!nzTreeTemplate">
<span
title="{{nzTreeNode.title}}"
title="{{title}}"
[attr.draggable]="canDraggable"
[attr.aria-grabbed]="canDraggable"
[ngClass]="nzNodeContentClass"
[class.draggable]="canDraggable">
<span
*ngIf="nzTreeNode.origin.icon && nzShowIcon"
*ngIf="icon && nzShowIcon"
[class.ant-tree-icon__open]="isSwitcherOpen"
[class.ant-tree-icon__close]="isSwitcherClose"
[class.ant-tree-icon_loading]="nzTreeNode.isLoading"
[class.ant-tree-icon_loading]="isLoading"
[ngClass]="nzNodeContentLoadingClass">
<span
[ngClass]="nzNodeContentIconClass">
Expand Down Expand Up @@ -90,11 +90,6 @@
[nzBeforeDrop]="nzBeforeDrop"
[nzCheckStrictly]="nzCheckStrictly"
[nzTreeTemplate]="nzTreeTemplate"
(clickNode)="clickNode.emit($event)"
(dblClick)="dblClick.emit($event)"
(contextMenu)="contextMenu.emit($event)"
(clickExpand)="clickExpand.emit($event)"
(clickCheckBox)="clickCheckBox.emit($event)"
(nzDragStart)="nzDragStart.emit($event)"
(nzDragEnter)="nzDragEnter.emit($event)"
(nzDragOver)="nzDragOver.emit($event)"
Expand Down
Loading

0 comments on commit b9f99c1

Please sign in to comment.