Skip to content

Commit 3fc7fba

Browse files
committed
feat(animations): implement query selector for ani driver
- using createSelector from core modules - custom matching for dynamically added classes - requires next version of tns-core-modules
1 parent cb8f8ba commit 3fc7fba

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

Diff for: nativescript-angular/animations/animation-driver.ts

+31-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
import { NgView } from "../element-registry";
1111
import { animationsLog as traceLog } from "../trace";
1212

13+
import { createSelector, SelectorCore } from "tns-core-modules/ui/styling/css-selector";
14+
1315
export class NativeScriptAnimationDriver implements AnimationDriver {
1416
matchesElement(_element: any, _selector: string): boolean {
1517
// this method is never called since NG 4.2.5
@@ -34,28 +36,50 @@ export class NativeScriptAnimationDriver implements AnimationDriver {
3436
return found;
3537
}
3638

37-
// traverse children and check if they have the provided class
38-
query(element: any, selector: string, multi: boolean): any[] {
39+
query(element: NgView, selector: string, multi: boolean): NgView[] {
3940
traceLog(
4041
`NativeScriptAnimationDriver.query ` +
4142
`element: ${element}, selector: ${selector} ` +
4243
`multi: ${multi}`
4344
);
4445

46+
const selectors = selector.split(",").map(s => s.trim());
47+
48+
const nsSelectors: SelectorCore[] = selectors.map(createSelector);
49+
const classSelectors = selectors
50+
.filter(s => s.startsWith("."))
51+
.map(s => s.substring(1));
52+
53+
return this.visitDescendants(element, nsSelectors, classSelectors, multi);
54+
}
55+
56+
private visitDescendants(
57+
element: NgView,
58+
nsSelectors: SelectorCore[],
59+
classSelectors: string[],
60+
multi: boolean): NgView[] {
61+
4562
let results = [];
46-
element.eachChild(child => {
47-
if (child[selector]) {
63+
eachDescendant(element, child => {
64+
if (nsSelectors.some(s => s.match(child)) ||
65+
classSelectors.some(s => this.hasClass(child, s))) {
4866
results.push(child);
49-
50-
return !multi;
67+
return multi;
5168
}
5269

53-
return false;
70+
return true;
5471
});
5572

5673
return results;
5774
}
5875

76+
// we're using that instead of match for classes
77+
// that are dynamically added by the animation engine
78+
// such as .ng-trigger, that's added for every :enter view
79+
private hasClass(element: any, cls: string) {
80+
return element["$$classes"][cls];
81+
}
82+
5983
computeStyle(element: NgView, prop: string): string {
6084
traceLog(
6185
`NativeScriptAnimationDriver.computeStyle ` +

0 commit comments

Comments
 (0)