forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation_transition_factory.ts
200 lines (172 loc) · 7.91 KB
/
animation_transition_factory.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
/**
* @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 {AnimationOptions, ɵStyleDataMap} from '@angular/animations';
import {AnimationDriver} from '../render/animation_driver';
import {getOrSetDefaultValue} from '../render/shared';
import {copyObj, interpolateParams, iteratorToArray} from '../util';
import {StyleAst, TransitionAst} from './animation_ast';
import {buildAnimationTimelines} from './animation_timeline_builder';
import {AnimationTimelineInstruction} from './animation_timeline_instruction';
import {TransitionMatcherFn} from './animation_transition_expr';
import {AnimationTransitionInstruction, createTransitionInstruction} from './animation_transition_instruction';
import {ElementInstructionMap} from './element_instruction_map';
import {AnimationStyleNormalizer} from './style_normalization/animation_style_normalizer';
const EMPTY_OBJECT = {};
export class AnimationTransitionFactory {
constructor(
private _triggerName: string, public ast: TransitionAst,
private _stateStyles: Map<string, AnimationStateStyles>) {}
match(currentState: any, nextState: any, element: any, params: {[key: string]: any}): boolean {
return oneOrMoreTransitionsMatch(this.ast.matchers, currentState, nextState, element, params);
}
buildStyles(stateName: string|boolean|undefined, params: {[key: string]: any}, errors: Error[]):
ɵStyleDataMap {
let styler = this._stateStyles.get('*');
if (stateName !== undefined) {
styler = this._stateStyles.get(stateName?.toString()) || styler;
}
return styler ? styler.buildStyles(params, errors) : new Map();
}
build(
driver: AnimationDriver, element: any, currentState: any, nextState: any,
enterClassName: string, leaveClassName: string, currentOptions?: AnimationOptions,
nextOptions?: AnimationOptions, subInstructions?: ElementInstructionMap,
skipAstBuild?: boolean): AnimationTransitionInstruction {
const errors: Error[] = [];
const transitionAnimationParams = this.ast.options && this.ast.options.params || EMPTY_OBJECT;
const currentAnimationParams = currentOptions && currentOptions.params || EMPTY_OBJECT;
const currentStateStyles = this.buildStyles(currentState, currentAnimationParams, errors);
const nextAnimationParams = nextOptions && nextOptions.params || EMPTY_OBJECT;
const nextStateStyles = this.buildStyles(nextState, nextAnimationParams, errors);
const queriedElements = new Set<any>();
const preStyleMap = new Map<any, Set<string>>();
const postStyleMap = new Map<any, Set<string>>();
const isRemoval = nextState === 'void';
const animationOptions: AnimationOptions = {
params: applyParamDefaults(nextAnimationParams, transitionAnimationParams),
delay: this.ast.options?.delay,
};
const timelines = skipAstBuild ?
[] :
buildAnimationTimelines(
driver, element, this.ast.animation, enterClassName, leaveClassName, currentStateStyles,
nextStateStyles, animationOptions, subInstructions, errors);
let totalTime = 0;
timelines.forEach(tl => {
totalTime = Math.max(tl.duration + tl.delay, totalTime);
});
if (errors.length) {
return createTransitionInstruction(
element, this._triggerName, currentState, nextState, isRemoval, currentStateStyles,
nextStateStyles, [], [], preStyleMap, postStyleMap, totalTime, errors);
}
timelines.forEach(tl => {
const elm = tl.element;
const preProps = getOrSetDefaultValue(preStyleMap, elm, new Set<string>());
tl.preStyleProps.forEach(prop => preProps.add(prop));
const postProps = getOrSetDefaultValue(postStyleMap, elm, new Set<string>());
tl.postStyleProps.forEach(prop => postProps.add(prop));
if (elm !== element) {
queriedElements.add(elm);
}
});
if (typeof ngDevMode === 'undefined' || ngDevMode) {
checkNonAnimatableInTimelines(timelines, this._triggerName, driver);
}
const queriedElementsList = iteratorToArray(queriedElements.values());
return createTransitionInstruction(
element, this._triggerName, currentState, nextState, isRemoval, currentStateStyles,
nextStateStyles, timelines, queriedElementsList, preStyleMap, postStyleMap, totalTime);
}
}
/**
* Checks inside a set of timelines if they try to animate a css property which is not considered
* animatable, in that case it prints a warning on the console.
* Besides that the function doesn't have any other effect.
*
* Note: this check is done here after the timelines are built instead of doing on a lower level so
* that we can make sure that the warning appears only once per instruction (we can aggregate here
* all the issues instead of finding them separately).
*
* @param timelines The built timelines for the current instruction.
* @param triggerName The name of the trigger for the current instruction.
* @param driver Animation driver used to perform the check.
*
*/
function checkNonAnimatableInTimelines(
timelines: AnimationTimelineInstruction[], triggerName: string, driver: AnimationDriver): void {
if (!driver.validateAnimatableStyleProperty) {
return;
}
const invalidNonAnimatableProps = new Set<string>();
timelines.forEach(({keyframes}) => {
const nonAnimatablePropsInitialValues = new Map<string, string|number>();
keyframes.forEach(keyframe => {
for (const [prop, value] of keyframe.entries()) {
if (!driver.validateAnimatableStyleProperty!(prop)) {
if (nonAnimatablePropsInitialValues.has(prop) && !invalidNonAnimatableProps.has(prop)) {
const propInitialValue = nonAnimatablePropsInitialValues.get(prop);
if (propInitialValue !== value) {
invalidNonAnimatableProps.add(prop);
}
} else {
nonAnimatablePropsInitialValues.set(prop, value);
}
}
}
});
});
if (invalidNonAnimatableProps.size > 0) {
console.warn(
`Warning: The animation trigger "${triggerName}" is attempting to animate the following` +
' not animatable properties: ' + Array.from(invalidNonAnimatableProps).join(', ') + '\n' +
'(to check the list of all animatable properties visit https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)');
}
}
function oneOrMoreTransitionsMatch(
matchFns: TransitionMatcherFn[], currentState: any, nextState: any, element: any,
params: {[key: string]: any}): boolean {
return matchFns.some(fn => fn(currentState, nextState, element, params));
}
function applyParamDefaults(userParams: Record<string, any>, defaults: Record<string, any>) {
const result: Record<string, any> = copyObj(defaults);
for (const key in userParams) {
if (userParams.hasOwnProperty(key) && userParams[key] != null) {
result[key] = userParams[key];
}
}
return result;
}
export class AnimationStateStyles {
constructor(
private styles: StyleAst, private defaultParams: {[key: string]: any},
private normalizer: AnimationStyleNormalizer) {}
buildStyles(params: {[key: string]: any}, errors: Error[]): ɵStyleDataMap {
const finalStyles: ɵStyleDataMap = new Map();
const combinedParams = copyObj(this.defaultParams);
Object.keys(params).forEach(key => {
const value = params[key];
if (value !== null) {
combinedParams[key] = value;
}
});
this.styles.styles.forEach(value => {
if (typeof value !== 'string') {
value.forEach((val, prop) => {
if (val) {
val = interpolateParams(val, combinedParams, errors);
}
const normalizedProp = this.normalizer.normalizePropertyName(prop, errors);
val = this.normalizer.normalizeStyleValue(prop, normalizedProp, val, errors);
finalStyles.set(prop, val);
});
}
});
return finalStyles;
}
}