Skip to content

Commit

Permalink
findNode / findNodes in TreeView
Browse files Browse the repository at this point in the history
  • Loading branch information
ianharrigan committed Aug 4, 2024
1 parent 22f7c5a commit e94d766
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
24 changes: 24 additions & 0 deletions haxe/ui/containers/TreeView.hx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ class TreeView extends ScrollView implements IDataComponent {
return value;
}

public function findNodes(fieldValue:Any, fieldName:String = null):Array<TreeViewNode> {
var foundNodes = [];
for (child in getNodes()) {
var childNodesFound = child.findNodes(fieldValue, fieldName);
if (childNodesFound != null && childNodesFound.length > 0) {
foundNodes = foundNodes.concat(childNodesFound);
}
}
return foundNodes;
}

public function findNode(fieldValue:Any, fieldName:String = null):TreeViewNode {
var foundNode = null;

for (child in getNodes()) {
foundNode = child.findNode(fieldValue, fieldName);
if (foundNode != null) {
break;
}
}

return foundNode;
}

public function findNodeByPath(path:String, field:String = null):TreeViewNode {
var foundNode = null;

Expand Down
46 changes: 46 additions & 0 deletions haxe/ui/containers/TreeViewNode.hx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,52 @@ class TreeViewNode extends VBox {
return parts.join("/");
}

public function findNodes(fieldValue:Any, fieldName:String = null):Array<TreeViewNode> {
if (fieldName == null) { // lets try to guess a field to use
fieldName = "text";
}

var foundNodes = [];
if (Reflect.hasField(this.data, fieldName)) {
var nodeFieldValue = Reflect.field(this.data, fieldName);
if (nodeFieldValue == fieldValue) {
foundNodes.push(this);
}
}

for (child in getNodes()) {
var childNodesFound = child.findNodes(fieldValue, fieldName);
if (childNodesFound != null && childNodesFound.length > 0) {
foundNodes = foundNodes.concat(childNodesFound);
}
}

return foundNodes;
}

public function findNode(fieldValue:Any, fieldName:String = null):TreeViewNode {
if (fieldName == null) { // lets try to guess a field to use
fieldName = "text";
}

if (Reflect.hasField(this.data, fieldName)) {
var nodeFieldValue = Reflect.field(this.data, fieldName);
if (nodeFieldValue == fieldValue) {
return this;
}
}

var foundNode = null;
for (child in getNodes()) {
foundNode = child.findNode(fieldValue, fieldName);
if (foundNode != null) {
break;
}
}

return foundNode;
}

public function findNodeByPath(path:String, field:String = null):TreeViewNode {
var foundNode = null;

Expand Down

0 comments on commit e94d766

Please sign in to comment.