-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
animation-player.ts
126 lines (100 loc) · 3.17 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
import { AnimationPlayer } from "@angular/animations";
import { KeyframeAnimation }
from "tns-core-modules/ui/animation/keyframe-animation";
import { Keyframe, createKeyframeAnimation } from "./utils";
import { NgView } from "../element-registry";
import { animationsLog as traceLog, isLogEnabled } from "../trace";
export class NativeScriptAnimationPlayer implements AnimationPlayer {
public parentPlayer: AnimationPlayer = null;
private _startSubscriptions: Function[] = [];
private _doneSubscriptions: Function[] = [];
private _finished = false;
private _started = false;
private animation: KeyframeAnimation;
constructor(
private target: NgView,
keyframes: Keyframe[],
private duration: number,
private delay: number,
easing: string
) {
this.initKeyframeAnimation(keyframes, duration, delay, easing);
}
get totalTime(): number {
return this.delay + this.duration;
}
init(): void {
}
hasStarted(): boolean {
return this._started;
}
onStart(fn: Function): void { this._startSubscriptions.push(fn); }
onDone(fn: Function): void { this._doneSubscriptions.push(fn); }
onDestroy(fn: Function): void { this._doneSubscriptions.push(fn); }
play(): void {
if (isLogEnabled()) {
traceLog(`NativeScriptAnimationPlayer.play`);
}
if (!this.animation) {
return;
}
if (!this._started) {
this._started = true;
this._startSubscriptions.forEach(fn => fn());
this._startSubscriptions = [];
}
this.animation.play(this.target)
.then(() => this.onFinish())
.catch((_e) => {});
}
pause(): void {
}
finish(): void {
this.onFinish();
}
reset(): void {
if (isLogEnabled()) {
traceLog(`NativeScriptAnimationPlayer.reset`);
}
if (this.animation && this.animation.isPlaying) {
this.animation.cancel();
}
}
restart(): void {
if (isLogEnabled()) {
traceLog(`NativeScriptAnimationPlayer.restart`);
}
this.reset();
this.play();
}
destroy(): void {
if (isLogEnabled()) {
traceLog(`NativeScriptAnimationPlayer.destroy`);
}
this.onFinish();
}
setPosition(_p: any): void {
throw new Error("AnimationPlayer.setPosition method is not supported!");
}
getPosition(): number {
return 0;
}
private initKeyframeAnimation(keyframes: Keyframe[], duration: number, delay: number, easing: string) {
if (isLogEnabled()) {
traceLog(`NativeScriptAnimationPlayer.initKeyframeAnimation`);
}
this.animation = createKeyframeAnimation(keyframes, duration, delay, easing);
}
private onFinish() {
if (isLogEnabled()) {
traceLog(`NativeScriptAnimationPlayer.onFinish`);
}
if (this._finished) {
return;
}
this._finished = true;
this._started = false;
this._doneSubscriptions.forEach(fn => fn());
this._doneSubscriptions = [];
}
}