Skip to content

Commit

Permalink
fix(tree): fix set default key error when node is null
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerkay committed Dec 25, 2019
1 parent acaaa9a commit 88a1435
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
18 changes: 9 additions & 9 deletions src/tree/tree-node.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ThyTreeNodeData, TreeNodeCheckState } from './tree.class';
import { ThyTreeNodeData, ThyTreeNodeCheckState } from './tree.class';
import { ThyTreeService } from './tree.service';
import { helpers } from '../util';

Expand All @@ -17,7 +17,7 @@ export class ThyTreeNode<T = any> {

isExpanded: boolean;

isChecked: TreeNodeCheckState;
isChecked: ThyTreeNodeCheckState;

isLoading: boolean;

Expand All @@ -42,7 +42,7 @@ export class ThyTreeNode<T = any> {
this.origin = node;
this.isDisabled = node.disabled || false;
this.isExpanded = node.expanded || false;
this.isChecked = TreeNodeCheckState.unchecked;
this.isChecked = ThyTreeNodeCheckState.unchecked;
this.isLoading = false;
if (node.children) {
node.children.forEach(childNode => {
Expand Down Expand Up @@ -71,7 +71,7 @@ export class ThyTreeNode<T = any> {
}

public setChecked(checked: boolean, propagateUp = true, propagateDown = true) {
this.isChecked = checked ? TreeNodeCheckState.checked : TreeNodeCheckState.unchecked;
this.isChecked = checked ? ThyTreeNodeCheckState.checked : ThyTreeNodeCheckState.unchecked;
if (propagateDown && this.children) {
this.children.forEach(node => {
node.setChecked(checked, false, true);
Expand All @@ -85,14 +85,14 @@ export class ThyTreeNode<T = any> {
public setParentCheck() {
const parent = this.parentNode;
if (parent) {
const checkedNodes = parent.children.filter(n => n.isChecked === TreeNodeCheckState.checked);
const unCheckedNodes = parent.children.filter(n => n.isChecked === TreeNodeCheckState.unchecked);
const checkedNodes = parent.children.filter(n => n.isChecked === ThyTreeNodeCheckState.checked);
const unCheckedNodes = parent.children.filter(n => n.isChecked === ThyTreeNodeCheckState.unchecked);
if (checkedNodes.length === parent.children.length) {
parent.isChecked = TreeNodeCheckState.checked;
parent.isChecked = ThyTreeNodeCheckState.checked;
} else if (unCheckedNodes.length === parent.children.length) {
parent.isChecked = TreeNodeCheckState.unchecked;
parent.isChecked = ThyTreeNodeCheckState.unchecked;
} else {
parent.isChecked = TreeNodeCheckState.indeterminate;
parent.isChecked = ThyTreeNodeCheckState.indeterminate;
}
parent.setParentCheck();
}
Expand Down
13 changes: 10 additions & 3 deletions src/tree/tree-node.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ChangeDetectorRef
} from '@angular/core';
import { ThyTreeComponent } from './tree.component';
import { ThyTreeNodeData, TreeNodeCheckState, ThyTreeEmitEvent } from './tree.class';
import { ThyTreeNodeData, ThyTreeNodeCheckState, ThyTreeEmitEvent } from './tree.class';
import { ThyTreeNode } from './tree-node.class';
import { ThyTreeService } from './tree.service';
import { takeUntil, filter } from 'rxjs/operators';
Expand Down Expand Up @@ -75,7 +75,7 @@ export class ThyTreeNodeComponent implements OnDestroy {

destroy$ = new Subject();

checkState = TreeNodeCheckState;
checkState = ThyTreeNodeCheckState;

markForCheck(): void {
this.cdr.markForCheck();
Expand Down Expand Up @@ -113,7 +113,14 @@ export class ThyTreeNodeComponent implements OnDestroy {

public clickNodeCheck(event: Event) {
event.stopPropagation();
this.node.setChecked(this.node.isChecked === TreeNodeCheckState.unchecked ? true : false);
if (
this.node.isChecked === ThyTreeNodeCheckState.unchecked ||
this.node.isChecked === ThyTreeNodeCheckState.indeterminate
) {
this.node.setChecked(true);
} else {
this.node.setChecked(false);
}
this.thyOnCheckboxChange.emit({
eventName: 'checkboxChange',
event: event,
Expand Down
18 changes: 9 additions & 9 deletions src/tree/tree.class.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ThyDragDropEvent } from '../drag-drop/drag-drop.class';
import { ThyTreeNode } from './tree-node.class';

export enum TreeNodeCheckState {
export enum ThyTreeNodeCheckState {
unchecked = 0,
checked = 1,
indeterminate = 2
Expand Down Expand Up @@ -31,28 +31,28 @@ export interface ThyTreeNodeData<T = any> {
[key: string]: any;
}

export interface ThyTreeEmitEvent {
export interface ThyTreeEmitEvent<T = any> {
eventName: string;

node?: ThyTreeNode;
node?: ThyTreeNode<T>;

event?: Event | any;

dragNode?: ThyTreeNode;
dragNode?: ThyTreeNode<T>;

targetNode?: ThyTreeNode;
targetNode?: ThyTreeNode<T>;
}

export interface ThyTreeDragDropEvent {
export interface ThyTreeDragDropEvent<T = any> {
event?: ThyDragDropEvent;

currentIndex?: number;

dragNode?: ThyTreeNode;
dragNode?: ThyTreeNode<T>;

targetNode?: ThyTreeNode;
targetNode?: ThyTreeNode<T>;

afterNode?: ThyTreeNode;
afterNode?: ThyTreeNode<T>;
}

export class ThyTreeIcons {
Expand Down
5 changes: 4 additions & 1 deletion src/tree/tree.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ export class ThyTreeComponent implements ControlValueAccessor, OnInit, OnChanges

private _setDefaultSelectedKeys() {
(this.thySelectedKeys || []).forEach(key => {
this.selectTreeNode(this.thyTreeService.getTreeNode(key));
const node = this.thyTreeService.getTreeNode(key);
if (node) {
this.selectTreeNode(this.thyTreeService.getTreeNode(key));
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/tree/tree.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, OnDestroy } from '@angular/core';
import { TreeNodeCheckState } from './tree.class';
import { ThyTreeNodeCheckState } from './tree.class';
import { Subject } from 'rxjs';
import { ThyTreeNode } from './tree-node.class';

Expand Down Expand Up @@ -40,7 +40,7 @@ export class ThyTreeService implements OnDestroy {

public getCheckedNodes(): ThyTreeNode[] {
const allNodes = this._getParallelTreeNodes(this.treeNodes);
return allNodes.filter(n => n.isChecked === TreeNodeCheckState.checked);
return allNodes.filter(n => n.isChecked === ThyTreeNodeCheckState.checked);
}

public deleteTreeNode(node: ThyTreeNode) {
Expand Down

0 comments on commit 88a1435

Please sign in to comment.