-
Notifications
You must be signed in to change notification settings - Fork 24.7k
/
timeline_animation_engine.ts
165 lines (145 loc) · 5.35 KB
/
timeline_animation_engine.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
/**
* @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 {AnimationMetadata, AnimationMetadataType, AnimationOptions, AnimationPlayer, AUTO_STYLE, ɵStyleDataMap} from '@angular/animations';
import {Ast} from '../dsl/animation_ast';
import {buildAnimationAst} from '../dsl/animation_ast_builder';
import {AnimationStyleNormalizer} from '../dsl/animation_style_normalizer';
import {buildAnimationTimelines} from '../dsl/animation_timeline_builder';
import {AnimationTimelineInstruction} from '../dsl/animation_timeline_instruction';
import {ElementInstructionMap} from '../dsl/element_instruction_map';
import {createAnimationFailed, missingOrDestroyedAnimation, missingPlayer, registerFailed} from '../error_helpers';
import {ENTER_CLASSNAME, LEAVE_CLASSNAME} from '../util';
import {warnRegister} from '../warning_helpers';
import {AnimationDriver} from './animation_driver';
import {getOrSetDefaultValue, listenOnPlayer, makeAnimationEvent, normalizeKeyframes, optimizeGroupPlayer} from './shared';
const EMPTY_INSTRUCTION_MAP = new ElementInstructionMap();
export class TimelineAnimationEngine {
private _animations = new Map<string, Ast<AnimationMetadataType>>();
private _playersById = new Map<string, AnimationPlayer>();
public players: AnimationPlayer[] = [];
constructor(
public bodyNode: any, private _driver: AnimationDriver,
private _normalizer: AnimationStyleNormalizer) {}
register(id: string, metadata: AnimationMetadata|AnimationMetadata[]) {
const errors: Error[] = [];
const warnings: string[] = [];
const ast = buildAnimationAst(this._driver, metadata, errors, warnings);
if (errors.length) {
throw registerFailed(errors);
} else {
if (warnings.length) {
warnRegister(warnings);
}
this._animations.set(id, ast);
}
}
private _buildPlayer(
i: AnimationTimelineInstruction, preStyles: ɵStyleDataMap,
postStyles?: ɵStyleDataMap): AnimationPlayer {
const element = i.element;
const keyframes = normalizeKeyframes(this._normalizer, i.keyframes, preStyles, postStyles);
return this._driver.animate(element, keyframes, i.duration, i.delay, i.easing, [], true);
}
create(id: string, element: any, options: AnimationOptions = {}): AnimationPlayer {
const errors: Error[] = [];
const ast = this._animations.get(id);
let instructions: AnimationTimelineInstruction[];
const autoStylesMap = new Map<any, ɵStyleDataMap>();
if (ast) {
instructions = buildAnimationTimelines(
this._driver, element, ast, ENTER_CLASSNAME, LEAVE_CLASSNAME, new Map(), new Map(),
options, EMPTY_INSTRUCTION_MAP, errors);
instructions.forEach(inst => {
const styles = getOrSetDefaultValue(
autoStylesMap, inst.element, new Map<string, string|number|null>());
inst.postStyleProps.forEach(prop => styles.set(prop, null));
});
} else {
errors.push(missingOrDestroyedAnimation());
instructions = [];
}
if (errors.length) {
throw createAnimationFailed(errors);
}
autoStylesMap.forEach((styles, element) => {
styles.forEach((_, prop) => {
styles.set(prop, this._driver.computeStyle(element, prop, AUTO_STYLE));
});
});
const players = instructions.map(i => {
const styles = autoStylesMap.get(i.element);
return this._buildPlayer(i, new Map(), styles);
});
const player = optimizeGroupPlayer(players);
this._playersById.set(id, player);
player.onDestroy(() => this.destroy(id));
this.players.push(player);
return player;
}
destroy(id: string) {
const player = this._getPlayer(id);
player.destroy();
this._playersById.delete(id);
const index = this.players.indexOf(player);
if (index >= 0) {
this.players.splice(index, 1);
}
}
private _getPlayer(id: string): AnimationPlayer {
const player = this._playersById.get(id);
if (!player) {
throw missingPlayer(id);
}
return player;
}
listen(id: string, element: string, eventName: string, callback: (event: any) => any):
() => void {
// triggerName, fromState, toState are all ignored for timeline animations
const baseEvent = makeAnimationEvent(element, '', '', '');
listenOnPlayer(this._getPlayer(id), eventName, baseEvent, callback);
return () => {};
}
command(id: string, element: any, command: string, args: any[]): void {
if (command == 'register') {
this.register(id, args[0] as AnimationMetadata | AnimationMetadata[]);
return;
}
if (command == 'create') {
const options = (args[0] || {}) as AnimationOptions;
this.create(id, element, options);
return;
}
const player = this._getPlayer(id);
switch (command) {
case 'play':
player.play();
break;
case 'pause':
player.pause();
break;
case 'reset':
player.reset();
break;
case 'restart':
player.restart();
break;
case 'finish':
player.finish();
break;
case 'init':
player.init();
break;
case 'setPosition':
player.setPosition(parseFloat(args[0] as string));
break;
case 'destroy':
this.destroy(id);
break;
}
}
}