Skip to content

Commit

Permalink
Find non-children
Browse files Browse the repository at this point in the history
Maybe fixed findAncestor?
  • Loading branch information
MSGhero committed Jun 8, 2017
1 parent 6d2e177 commit 03fbdb7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
6 changes: 5 additions & 1 deletion haxe/ui/containers/TabView.hx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ class TabView extends Component {
var match = super.findComponent(criteria, type, recursive, searchType);
if (match == null) {
for (view in _views) {
match = view.findComponent(criteria, type, recursive, searchType);
if (view.matchesSearch(criteria, type, searchType)) {
return cast view;
} else {
match = view.findComponent(criteria, type, recursive, searchType);
}
if (match != null) {
break;
}
Expand Down
79 changes: 42 additions & 37 deletions haxe/ui/core/Component.hx
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,29 @@ class Component extends ComponentBase implements IComponentBase implements IClon
return _children;
}

/**
Checks if this component meets the search criteria
- `criteria` - The criteria by which to search, the interpretation of this is defined using `searchType` (the default search type is _id_)
- `type` - The component class you wish to cast the result to (defaults to _null_)
- `searchType` - Allows you specify how to consider a parent a match (defaults to _id_), can be either:
- `id` - The first component that has the id specified in `criteria` will be considered a match
- `css` - The first component that contains a style name specified by `criteria` will be considered a match
**/
@:dox(group = "Display tree related properties and methods")
public function matchesSearch<T>(criteria:String = null, type:Class<T> = null, searchType:String = "id"):Bool {
if (criteria != null) {
return searchType == "id" && id == criteria || searchType == "css" && hasClass(criteria) == true;
} else if (type != null) {
return Std.is(this, type) == true;
}
return false;
}

/**
Finds a specific child in this components display tree (recusively if desired) and can optionally cast the result
Expand All @@ -606,19 +629,9 @@ class Component extends ComponentBase implements IComponentBase implements IClon

var match:Component = null;
for (child in childComponents) {
if (criteria != null) {
if (searchType == "id" && child.id == criteria) {
match = cast child;
break;
} else if (searchType == "css" && child.hasClass(criteria) == true) {
match = cast child;
break;
}
} else if (type != null) {
if (Std.is(child, type) == true) {
match = cast child;
break;
}
if (child.matchesSearch(criteria, type, searchType)) {
match = child;
break;
}
}
if (match == null && recursive == true) {
Expand Down Expand Up @@ -652,19 +665,11 @@ class Component extends ComponentBase implements IComponentBase implements IClon
var match:Component = null;
var p = this.parentComponent;
while (p != null) {
if (criteria != null) {
if (searchType == "id" && p.id == criteria) {
match = cast p;
break;
} else if (searchType == "css" && p.hasClass(criteria) == true) {
match = cast p;
break;
}
} else if (type != null) {
if (Std.is(p, type) == true) {
match = cast p;
break;
}
if (p.matchesSearch(criteria, type, searchType)) {
match = p;
break;
} else {
p = p.parentComponent;
}
}
return cast match;
Expand Down Expand Up @@ -803,12 +808,12 @@ class Component extends ComponentBase implements IComponentBase implements IClon
invalidateStyle();
}
}
if (recursive == true) {
for (child in childComponents) {
child.addClass(name, invalidate, recursive);
}
}
if (recursive == true) {
for (child in childComponents) {
child.addClass(name, invalidate, recursive);
}
}
}

/**
Expand All @@ -823,11 +828,11 @@ class Component extends ComponentBase implements IComponentBase implements IClon
}
}

if (recursive == true) {
for (child in childComponents) {
child.removeClass(name, invalidate, recursive);
}
}
if (recursive == true) {
for (child in childComponents) {
child.removeClass(name, invalidate, recursive);
}
}
}

/**
Expand Down

0 comments on commit 03fbdb7

Please sign in to comment.