From 93ce735241ccd7951b66bccda24e9ae0a4c90074 Mon Sep 17 00:00:00 2001 From: perceptron8 Date: Sat, 5 Oct 2024 09:54:39 +0200 Subject: [PATCH] Rewrite `findIndexInSelection` (`Tree` and `TreeSelect`) --- src/app/components/tree/tree.ts | 19 +++---------------- src/app/components/treeselect/treeselect.ts | 20 +++----------------- 2 files changed, 6 insertions(+), 33 deletions(-) diff --git a/src/app/components/tree/tree.ts b/src/app/components/tree/tree.ts index 0222846f457..e75d59cbded 100755 --- a/src/app/components/tree/tree.ts +++ b/src/app/components/tree/tree.ts @@ -1435,24 +1435,11 @@ export class Tree implements OnInit, AfterContentInit, OnChanges, OnDestroy, Blo } findIndexInSelection(node: TreeNode) { - let index: number = -1; if (this.selectionMode && this.selection) { - if (this.isSingleSelectionMode()) { - let areNodesEqual = (this.selection.key !== undefined && this.selection.key === node.key) || this.selection == node; - index = areNodesEqual ? 0 : -1; - } else { - for (let i = 0; i < this.selection.length; i++) { - let selectedNode = this.selection[i]; - let areNodesEqual = (selectedNode.key !== undefined && selectedNode.key === node.key) || selectedNode == node; - if (areNodesEqual) { - index = i; - break; - } - } - } + const selection = this.isSingleSelectionMode() ? [this.selection] : this.selection; + return selection.findIndex(selectedNode => selectedNode === node || selectedNode.key === node.key && selectedNode.key !== undefined); } - - return index; + return -1; } syncNodeOption(node: TreeNode, parentNodes: TreeNode[], option: any, value?: any) { diff --git a/src/app/components/treeselect/treeselect.ts b/src/app/components/treeselect/treeselect.ts index d97b5baf5c1..68afb1bd6ad 100755 --- a/src/app/components/treeselect/treeselect.ts +++ b/src/app/components/treeselect/treeselect.ts @@ -907,25 +907,11 @@ export class TreeSelect implements AfterContentInit { } findIndexInSelection(node: TreeNode) { - let index: number = -1; - if (this.value) { - if (this.selectionMode === 'single') { - let areNodesEqual = (this.value.key !== undefined && this.value.key === node.key) || this.value === node; - index = areNodesEqual ? 0 : -1; - } else { - for (let i = 0; i < this.value.length; i++) { - let selectedNode = this.value[i]; - let areNodesEqual = (selectedNode.key !== undefined && selectedNode.key === node.key) || selectedNode === node; - if (areNodesEqual) { - index = i; - break; - } - } - } + const value = this.selectionMode === 'single' ? [this.value] : this.value; + return value.findIndex(selectedNode => selectedNode === node || selectedNode.key === node.key && selectedNode.key !== undefined); } - - return index; + return -1; } onSelect(event: TreeNodeSelectEvent) {