2017-01-26 11:16:51 -08:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2017-04-26 10:44:28 -07:00
|
|
|
import {AnimationOptions, ɵStyleData} from '@angular/animations';
|
2017-03-01 17:13:06 -08:00
|
|
|
|
2017-05-02 15:45:48 -07:00
|
|
|
import {AnimationDriver} from '../render/animation_driver';
|
2017-04-26 10:44:28 -07:00
|
|
|
import {getOrSetAsInMap} from '../render/shared';
|
|
|
|
import {iteratorToArray, mergeAnimationOptions} from '../util';
|
|
|
|
|
|
|
|
import {TransitionAst} from './animation_ast';
|
|
|
|
import {buildAnimationTimelines} from './animation_timeline_builder';
|
2017-01-26 11:16:51 -08:00
|
|
|
import {TransitionMatcherFn} from './animation_transition_expr';
|
|
|
|
import {AnimationTransitionInstruction, createTransitionInstruction} from './animation_transition_instruction';
|
2017-04-26 10:44:28 -07:00
|
|
|
import {ElementInstructionMap} from './element_instruction_map';
|
2017-01-26 11:16:51 -08:00
|
|
|
|
2017-02-22 15:14:49 -08:00
|
|
|
export class AnimationTransitionFactory {
|
2017-01-26 11:16:51 -08:00
|
|
|
constructor(
|
2017-04-26 10:44:28 -07:00
|
|
|
private _triggerName: string, public ast: TransitionAst,
|
|
|
|
private _stateStyles: {[stateName: string]: ɵStyleData}) {}
|
|
|
|
|
|
|
|
match(currentState: any, nextState: any): boolean {
|
|
|
|
return oneOrMoreTransitionsMatch(this.ast.matchers, currentState, nextState);
|
2017-01-26 11:16:51 -08:00
|
|
|
}
|
|
|
|
|
2017-04-26 10:44:28 -07:00
|
|
|
build(
|
2017-05-02 15:45:48 -07:00
|
|
|
driver: AnimationDriver, element: any, currentState: any, nextState: any,
|
|
|
|
options?: AnimationOptions,
|
2017-06-29 14:17:39 -07:00
|
|
|
subInstructions?: ElementInstructionMap): AnimationTransitionInstruction {
|
2017-04-26 10:44:28 -07:00
|
|
|
const animationOptions = mergeAnimationOptions(this.ast.options || {}, options || {});
|
2017-01-26 11:16:51 -08:00
|
|
|
|
|
|
|
const backupStateStyles = this._stateStyles['*'] || {};
|
|
|
|
const currentStateStyles = this._stateStyles[currentState] || backupStateStyles;
|
|
|
|
const nextStateStyles = this._stateStyles[nextState] || backupStateStyles;
|
2017-06-29 14:17:39 -07:00
|
|
|
const queriedElements = new Set<any>();
|
|
|
|
const preStyleMap = new Map<any, {[prop: string]: boolean}>();
|
|
|
|
const postStyleMap = new Map<any, {[prop: string]: boolean}>();
|
|
|
|
const isRemoval = nextState === 'void';
|
2017-01-26 11:16:51 -08:00
|
|
|
|
2017-04-26 10:44:28 -07:00
|
|
|
const errors: any[] = [];
|
|
|
|
const timelines = buildAnimationTimelines(
|
2017-05-02 15:45:48 -07:00
|
|
|
driver, element, this.ast.animation, currentStateStyles, nextStateStyles, animationOptions,
|
2017-04-26 10:44:28 -07:00
|
|
|
subInstructions, errors);
|
|
|
|
|
|
|
|
if (errors.length) {
|
2017-06-29 14:17:39 -07:00
|
|
|
return createTransitionInstruction(
|
|
|
|
element, this._triggerName, currentState, nextState, isRemoval, currentStateStyles,
|
|
|
|
nextStateStyles, [], [], preStyleMap, postStyleMap, errors);
|
2017-04-26 10:44:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
timelines.forEach(tl => {
|
|
|
|
const elm = tl.element;
|
|
|
|
const preProps = getOrSetAsInMap(preStyleMap, elm, {});
|
|
|
|
tl.preStyleProps.forEach(prop => preProps[prop] = true);
|
|
|
|
|
|
|
|
const postProps = getOrSetAsInMap(postStyleMap, elm, {});
|
|
|
|
tl.postStyleProps.forEach(prop => postProps[prop] = true);
|
|
|
|
|
|
|
|
if (elm !== element) {
|
|
|
|
queriedElements.add(elm);
|
|
|
|
}
|
|
|
|
});
|
2017-01-26 11:16:51 -08:00
|
|
|
|
2017-04-26 10:44:28 -07:00
|
|
|
const queriedElementsList = iteratorToArray(queriedElements.values());
|
2017-01-26 11:16:51 -08:00
|
|
|
return createTransitionInstruction(
|
2017-06-29 14:17:39 -07:00
|
|
|
element, this._triggerName, currentState, nextState, isRemoval, currentStateStyles,
|
|
|
|
nextStateStyles, timelines, queriedElementsList, preStyleMap, postStyleMap);
|
2017-01-26 11:16:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function oneOrMoreTransitionsMatch(
|
|
|
|
matchFns: TransitionMatcherFn[], currentState: any, nextState: any): boolean {
|
|
|
|
return matchFns.some(fn => fn(currentState, nextState));
|
|
|
|
}
|