/
animation_player.ts
203 lines (197 loc) Β· 4.94 KB
/
animation_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @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 {scheduleMicroTask} from '../util';
/**
* Provides programmatic control of a reusable animation sequence,
* built using the `build()` method of `AnimationBuilder`. The `build()` method
* returns a factory, whose `create()` method instantiates and initializes this interface.
*
* @see `AnimationBuilder`
* @see `AnimationFactory`
* @see `animate()`
*
* @publicApi
*/
export interface AnimationPlayer {
/**
* Provides a callback to invoke when the animation finishes.
* @param fn The callback function.
* @see `finish()`
*/
onDone(fn: () => void): void;
/**
* Provides a callback to invoke when the animation starts.
* @param fn The callback function.
* @see `run()`
*/
onStart(fn: () => void): void;
/**
* Provides a callback to invoke after the animation is destroyed.
* @param fn The callback function.
* @see `destroy()`
* @see `beforeDestroy()`
*/
onDestroy(fn: () => void): void;
/**
* Initializes the animation.
*/
init(): void;
/**
* Reports whether the animation has started.
* @returns True if the animation has started, false otherwise.
*/
hasStarted(): boolean;
/**
* Runs the animation, invoking the `onStart()` callback.
*/
play(): void;
/**
* Pauses the animation.
*/
pause(): void;
/**
* Restarts the paused animation.
*/
restart(): void;
/**
* Ends the animation, invoking the `onDone()` callback.
*/
finish(): void;
/**
* Destroys the animation, after invoking the `beforeDestroy()` callback.
* Calls the `onDestroy()` callback when destruction is completed.
*/
destroy(): void;
/**
* Resets the animation to its initial state.
*/
reset(): void;
/**
* Sets the position of the animation.
* @param position A 0-based offset into the duration, in milliseconds.
*/
setPosition(position: any /** TODO #9100 */): void;
/**
* Reports the current position of the animation.
* @returns A 0-based offset into the duration, in milliseconds.
*/
getPosition(): number;
/**
* The parent of this player, if any.
*/
parentPlayer: AnimationPlayer|null;
/**
* The total run time of the animation, in milliseconds.
*/
readonly totalTime: number;
/**
* Provides a callback to invoke before the animation is destroyed.
*/
beforeDestroy?: () => any;
/**
* @internal
* Internal
*/
triggerCallback?: (phaseName: string) => void;
/**
* @internal
* Internal
*/
disabled?: boolean;
}
/**
* An empty programmatic controller for reusable animations.
* Used internally when animations are disabled, to avoid
* checking for the null case when an animation player is expected.
*
* @see `animate()`
* @see `AnimationPlayer`
* @see `GroupPlayer`
*
* @publicApi
*/
export class NoopAnimationPlayer implements AnimationPlayer {
private _onDoneFns: Function[] = [];
private _onStartFns: Function[] = [];
private _onDestroyFns: Function[] = [];
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) {
this.totalTime = duration + delay;
}
private _onFinish() {
if (!this._finished) {
this._finished = true;
this._onDoneFns.forEach(fn => fn());
this._onDoneFns = [];
}
}
onStart(fn: () => void): void {
this._onStartFns.push(fn);
}
onDone(fn: () => void): void {
this._onDoneFns.push(fn);
}
onDestroy(fn: () => void): void {
this._onDestroyFns.push(fn);
}
hasStarted(): boolean {
return this._started;
}
init(): void {}
play(): void {
if (!this.hasStarted()) {
this._onStart();
this.triggerMicrotask();
}
this._started = true;
}
/** @internal */
triggerMicrotask() {
scheduleMicroTask(() => this._onFinish());
}
private _onStart() {
this._onStartFns.forEach(fn => fn());
this._onStartFns = [];
}
pause(): void {}
restart(): void {}
finish(): void {
this._onFinish();
}
destroy(): void {
if (!this._destroyed) {
this._destroyed = true;
if (!this.hasStarted()) {
this._onStart();
}
this.finish();
this._onDestroyFns.forEach(fn => fn());
this._onDestroyFns = [];
}
}
reset(): void {
this._started = false;
}
setPosition(position: number): void {
this._position = this.totalTime ? position * this.totalTime : 1;
}
getPosition(): number {
return this.totalTime ? this._position / this.totalTime : 1;
}
/** @internal */
triggerCallback(phaseName: string): void {
const methods = phaseName == 'start' ? this._onStartFns : this._onDoneFns;
methods.forEach(fn => fn());
methods.length = 0;
}
}