fix(animations): implement getPosition in browser animation builder (#39983)

Forward `getPosition` to `animation_group_player`.

PR Close #39983
This commit is contained in:
Benjamin Kindle 2020-12-02 17:17:08 -05:00 committed by Misko Hevery
parent 6cc9ab120b
commit ca08625227
4 changed files with 54 additions and 9 deletions

View File

@ -148,12 +148,13 @@ export class AnimationGroupPlayer implements AnimationPlayer {
} }
getPosition(): number { getPosition(): number {
let min = 0; const longestPlayer =
this.players.forEach(player => { this.players.reduce((longestSoFar: AnimationPlayer|null, player: AnimationPlayer) => {
const p = player.getPosition(); const newPlayerIsLongest =
min = Math.min(p, min); longestSoFar === null || player.totalTime > longestSoFar.totalTime;
}); return newPlayerIsLongest ? player : longestSoFar;
return min; }, null);
return longestPlayer != null ? longestPlayer.getPosition() : 0;
} }
beforeDestroy(): void { beforeDestroy(): void {

View File

@ -124,6 +124,7 @@ export class NoopAnimationPlayer implements AnimationPlayer {
private _started = false; private _started = false;
private _destroyed = false; private _destroyed = false;
private _finished = false; private _finished = false;
private _position = 0;
public parentPlayer: AnimationPlayer|null = null; public parentPlayer: AnimationPlayer|null = null;
public readonly totalTime: number; public readonly totalTime: number;
constructor(duration: number = 0, delay: number = 0) { constructor(duration: number = 0, delay: number = 0) {
@ -184,9 +185,11 @@ export class NoopAnimationPlayer implements AnimationPlayer {
} }
} }
reset(): void {} reset(): void {}
setPosition(position: number): void {} setPosition(position: number): void {
this._position = this.totalTime ? position * this.totalTime : 1;
}
getPosition(): number { getPosition(): number {
return 0; return this.totalTime ? this._position / this.totalTime : 1;
} }
/** @internal */ /** @internal */

View File

@ -0,0 +1,41 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {fakeAsync} from '@angular/core/testing';
import {NoopAnimationPlayer} from '../src/animations';
import {AnimationGroupPlayer} from '../src/players/animation_group_player';
describe('AnimationGroupPlayer', () => {
it('should getPosition of an empty group', fakeAsync(() => {
const players: NoopAnimationPlayer[] = [];
const groupPlayer = new AnimationGroupPlayer(players);
expect(groupPlayer.getPosition()).toBe(0);
}));
it('should getPosition of a single player in a group', fakeAsync(() => {
const player = new NoopAnimationPlayer(5, 5);
player.setPosition(0.2);
const players = [player];
const groupPlayer = new AnimationGroupPlayer(players);
expect(groupPlayer.getPosition()).toBe(0.2);
}));
it('should getPosition based on the longest player in the group', fakeAsync(() => {
const longestPlayer = new NoopAnimationPlayer(5, 5);
longestPlayer.setPosition(0.2);
const players = [
new NoopAnimationPlayer(1, 4),
new NoopAnimationPlayer(4, 1),
new NoopAnimationPlayer(7, 0),
longestPlayer,
new NoopAnimationPlayer(1, 1),
];
const groupPlayer = new AnimationGroupPlayer(players);
expect(groupPlayer.getPosition()).toBe(0.2);
}));
});

View File

@ -111,7 +111,7 @@ export class RendererAnimationPlayer implements AnimationPlayer {
} }
getPosition(): number { getPosition(): number {
return 0; return this._renderer.engine.players[+this.id]?.getPosition() ?? 0;
} }
public totalTime = 0; public totalTime = 0;