Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5a765f0

Browse files
literalpiemhevery
authored andcommitted
fix(animations): implement getPosition in browser animation builder (#39983)
Forward `getPosition` to `animation_group_player`. PR Close #39983
1 parent 03c3e07 commit 5a765f0

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

‎packages/animations/src/players/animation_group_player.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,13 @@ export class AnimationGroupPlayer implements AnimationPlayer {
148148
}
149149

150150
getPosition(): number {
151-
let min = 0;
152-
this.players.forEach(player => {
153-
const p = player.getPosition();
154-
min = Math.min(p, min);
155-
});
156-
return min;
151+
const longestPlayer =
152+
this.players.reduce((longestSoFar: AnimationPlayer|null, player: AnimationPlayer) => {
153+
const newPlayerIsLongest =
154+
longestSoFar === null || player.totalTime > longestSoFar.totalTime;
155+
return newPlayerIsLongest ? player : longestSoFar;
156+
}, null);
157+
return longestPlayer != null ? longestPlayer.getPosition() : 0;
157158
}
158159

159160
beforeDestroy(): void {

‎packages/animations/src/players/animation_player.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export class NoopAnimationPlayer implements AnimationPlayer {
124124
private _started = false;
125125
private _destroyed = false;
126126
private _finished = false;
127+
private _position = 0;
127128
public parentPlayer: AnimationPlayer|null = null;
128129
public readonly totalTime: number;
129130
constructor(duration: number = 0, delay: number = 0) {
@@ -184,9 +185,11 @@ export class NoopAnimationPlayer implements AnimationPlayer {
184185
}
185186
}
186187
reset(): void {}
187-
setPosition(position: number): void {}
188+
setPosition(position: number): void {
189+
this._position = this.totalTime ? position * this.totalTime : 1;
190+
}
188191
getPosition(): number {
189-
return 0;
192+
return this.totalTime ? this._position / this.totalTime : 1;
190193
}
191194

192195
/** @internal */
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {fakeAsync} from '@angular/core/testing';
9+
import {NoopAnimationPlayer} from '../src/animations';
10+
import {AnimationGroupPlayer} from '../src/players/animation_group_player';
11+
12+
13+
describe('AnimationGroupPlayer', () => {
14+
it('should getPosition of an empty group', fakeAsync(() => {
15+
const players: NoopAnimationPlayer[] = [];
16+
const groupPlayer = new AnimationGroupPlayer(players);
17+
expect(groupPlayer.getPosition()).toBe(0);
18+
}));
19+
20+
it('should getPosition of a single player in a group', fakeAsync(() => {
21+
const player = new NoopAnimationPlayer(5, 5);
22+
player.setPosition(0.2);
23+
const players = [player];
24+
const groupPlayer = new AnimationGroupPlayer(players);
25+
expect(groupPlayer.getPosition()).toBe(0.2);
26+
}));
27+
28+
it('should getPosition based on the longest player in the group', fakeAsync(() => {
29+
const longestPlayer = new NoopAnimationPlayer(5, 5);
30+
longestPlayer.setPosition(0.2);
31+
const players = [
32+
new NoopAnimationPlayer(1, 4),
33+
new NoopAnimationPlayer(4, 1),
34+
new NoopAnimationPlayer(7, 0),
35+
longestPlayer,
36+
new NoopAnimationPlayer(1, 1),
37+
];
38+
const groupPlayer = new AnimationGroupPlayer(players);
39+
expect(groupPlayer.getPosition()).toBe(0.2);
40+
}));
41+
});

‎packages/platform-browser/animations/src/animation_builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class RendererAnimationPlayer implements AnimationPlayer {
111111
}
112112

113113
getPosition(): number {
114-
return 0;
114+
return this._renderer.engine.players[+this.id]?.getPosition() ?? 0;
115115
}
116116

117117
public totalTime = 0;

0 commit comments

Comments
 (0)