diff --git a/packages/animations/src/players/animation_group_player.ts b/packages/animations/src/players/animation_group_player.ts index a5fa06ced5..692f0b971c 100644 --- a/packages/animations/src/players/animation_group_player.ts +++ b/packages/animations/src/players/animation_group_player.ts @@ -148,12 +148,13 @@ export class AnimationGroupPlayer implements AnimationPlayer { } getPosition(): number { - let min = 0; - this.players.forEach(player => { - const p = player.getPosition(); - min = Math.min(p, min); - }); - return min; + const longestPlayer = + this.players.reduce((longestSoFar: AnimationPlayer|null, player: AnimationPlayer) => { + const newPlayerIsLongest = + longestSoFar === null || player.totalTime > longestSoFar.totalTime; + return newPlayerIsLongest ? player : longestSoFar; + }, null); + return longestPlayer != null ? longestPlayer.getPosition() : 0; } beforeDestroy(): void { diff --git a/packages/animations/src/players/animation_player.ts b/packages/animations/src/players/animation_player.ts index 3d3472c0c4..607a996b6a 100644 --- a/packages/animations/src/players/animation_player.ts +++ b/packages/animations/src/players/animation_player.ts @@ -124,6 +124,7 @@ export class NoopAnimationPlayer implements AnimationPlayer { private _started = false; private _destroyed = false; private _finished = false; + private _position = 0; public parentPlayer: AnimationPlayer|null = null; public readonly totalTime: number; constructor(duration: number = 0, delay: number = 0) { @@ -184,9 +185,11 @@ export class NoopAnimationPlayer implements AnimationPlayer { } } reset(): void {} - setPosition(position: number): void {} + setPosition(position: number): void { + this._position = this.totalTime ? position * this.totalTime : 1; + } getPosition(): number { - return 0; + return this.totalTime ? this._position / this.totalTime : 1; } /** @internal */ diff --git a/packages/animations/test/animation_group_player_spec.ts b/packages/animations/test/animation_group_player_spec.ts new file mode 100644 index 0000000000..549eec5c28 --- /dev/null +++ b/packages/animations/test/animation_group_player_spec.ts @@ -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); + })); +}); diff --git a/packages/platform-browser/animations/src/animation_builder.ts b/packages/platform-browser/animations/src/animation_builder.ts index 2e0080104c..057b1b5d55 100644 --- a/packages/platform-browser/animations/src/animation_builder.ts +++ b/packages/platform-browser/animations/src/animation_builder.ts @@ -111,7 +111,7 @@ export class RendererAnimationPlayer implements AnimationPlayer { } getPosition(): number { - return 0; + return this._renderer.engine.players[+this.id]?.getPosition() ?? 0; } public totalTime = 0;