-
Notifications
You must be signed in to change notification settings - Fork 24.8k
/
animation_group_player.ts
160 lines (136 loc) Β· 4.14 KB
/
animation_group_player.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @license
* Copyright Google Inc. 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 {scheduleMicroTask} from '../util';
import {AnimationPlayer} from './animation_player';
/**
* A programmatic controller for a group of reusable animations.
* Used internally to control animations.
*
* @see `AnimationPlayer`
* @see `{@link animations/group group()}`
*
*/
export class AnimationGroupPlayer implements AnimationPlayer {
private _onDoneFns: Function[] = [];
private _onStartFns: Function[] = [];
private _finished = false;
private _started = false;
private _destroyed = false;
private _onDestroyFns: Function[] = [];
public parentPlayer: AnimationPlayer|null = null;
public totalTime: number = 0;
public readonly players: AnimationPlayer[];
constructor(_players: AnimationPlayer[]) {
this.players = _players;
let doneCount = 0;
let destroyCount = 0;
let startCount = 0;
const total = this.players.length;
if (total == 0) {
scheduleMicroTask(() => this._onFinish());
} else {
this.players.forEach(player => {
player.onDone(() => {
if (++doneCount == total) {
this._onFinish();
}
});
player.onDestroy(() => {
if (++destroyCount == total) {
this._onDestroy();
}
});
player.onStart(() => {
if (++startCount == total) {
this._onStart();
}
});
});
}
this.totalTime = this.players.reduce((time, player) => Math.max(time, player.totalTime), 0);
}
private _onFinish() {
if (!this._finished) {
this._finished = true;
this._onDoneFns.forEach(fn => fn());
this._onDoneFns = [];
}
}
init(): void { this.players.forEach(player => player.init()); }
onStart(fn: () => void): void { this._onStartFns.push(fn); }
private _onStart() {
if (!this.hasStarted()) {
this._started = true;
this._onStartFns.forEach(fn => fn());
this._onStartFns = [];
}
}
onDone(fn: () => void): void { this._onDoneFns.push(fn); }
onDestroy(fn: () => void): void { this._onDestroyFns.push(fn); }
hasStarted() { return this._started; }
play() {
if (!this.parentPlayer) {
this.init();
}
this._onStart();
this.players.forEach(player => player.play());
}
pause(): void { this.players.forEach(player => player.pause()); }
restart(): void { this.players.forEach(player => player.restart()); }
finish(): void {
this._onFinish();
this.players.forEach(player => player.finish());
}
destroy(): void { this._onDestroy(); }
private _onDestroy() {
if (!this._destroyed) {
this._destroyed = true;
this._onFinish();
this.players.forEach(player => player.destroy());
this._onDestroyFns.forEach(fn => fn());
this._onDestroyFns = [];
}
}
reset(): void {
this.players.forEach(player => player.reset());
this._destroyed = false;
this._finished = false;
this._started = false;
}
setPosition(p: number): void {
const timeAtPosition = p * this.totalTime;
this.players.forEach(player => {
const position = player.totalTime ? Math.min(1, timeAtPosition / player.totalTime) : 1;
player.setPosition(position);
});
}
getPosition(): number {
const longestPlayer = this.players.reduce((longestSoFar, player) => {
const newPlayerIsLongest = player.totalTime > (longestSoFar! as AnimationPlayer).totalTime;
if (longestSoFar === undefined || newPlayerIsLongest) {
return player;
} else {
return longestSoFar;
}
}, undefined);
return longestPlayer !== undefined ? longestPlayer.getPosition() / longestPlayer.totalTime : 0;
}
beforeDestroy(): void {
this.players.forEach(player => {
if (player.beforeDestroy) {
player.beforeDestroy();
}
});
}
/** @internal */
triggerCallback(phaseName: string): void {
const methods = phaseName == 'start' ? this._onStartFns : this._onDoneFns;
methods.forEach(fn => fn());
methods.length = 0;
}
}