|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import {AnimationPlayer, AnimationStyles, Injector} from '@angular/core'; |
| 9 | +import {StyleData} from '../common/style_data'; |
| 10 | +import {normalizeStyles} from '../common/util'; |
| 11 | +import {AnimationDriver} from '../engine/animation_driver'; |
| 12 | +import {DomAnimationTransitionEngine} from '../engine/dom_animation_transition_engine'; |
| 13 | +import {AnimationMetadata, sequence} from './animation_metadata'; |
| 14 | +import {AnimationTimelineInstruction} from './animation_timeline_instruction'; |
| 15 | +import {buildAnimationKeyframes} from './animation_timeline_visitor'; |
| 16 | +import {validateAnimationSequence} from './animation_validator_visitor'; |
| 17 | +import {AnimationStyleNormalizer} from './style_normalization/animation_style_normalizer'; |
| 18 | + |
| 19 | +/** |
| 20 | + * @experimental Animation support is experimental. |
| 21 | + */ |
| 22 | +export class Animation { |
| 23 | + private _animationAst: AnimationMetadata; |
| 24 | + constructor(input: AnimationMetadata|AnimationMetadata[]) { |
| 25 | + const ast = |
| 26 | + Array.isArray(input) ? sequence(<AnimationMetadata[]>input) : <AnimationMetadata>input; |
| 27 | + const errors = validateAnimationSequence(ast); |
| 28 | + if (errors.length) { |
| 29 | + const errorMessage = `animation validation failed:\n${errors.join("\n")}`; |
| 30 | + throw new Error(errorMessage); |
| 31 | + } |
| 32 | + this._animationAst = ast; |
| 33 | + } |
| 34 | + |
| 35 | + buildTimelines(startingStyles: StyleData|StyleData[], destinationStyles: StyleData|StyleData[]): |
| 36 | + AnimationTimelineInstruction[] { |
| 37 | + const start = Array.isArray(startingStyles) ? |
| 38 | + normalizeStyles(new AnimationStyles(<StyleData[]>startingStyles)) : |
| 39 | + <StyleData>startingStyles; |
| 40 | + const dest = Array.isArray(destinationStyles) ? |
| 41 | + normalizeStyles(new AnimationStyles(<StyleData[]>destinationStyles)) : |
| 42 | + <StyleData>destinationStyles; |
| 43 | + return buildAnimationKeyframes(this._animationAst, start, dest); |
| 44 | + } |
| 45 | + |
| 46 | + // this is only used for development demo purposes for now |
| 47 | + private create( |
| 48 | + injector: Injector, element: any, startingStyles: StyleData = {}, |
| 49 | + destinationStyles: StyleData = {}): AnimationPlayer { |
| 50 | + const instructions = this.buildTimelines(startingStyles, destinationStyles); |
| 51 | + |
| 52 | + // note the code below is only here to make the tests happy (once the new renderer is |
| 53 | + // within core then the code below will interact with Renderer.transition(...)) |
| 54 | + const driver: AnimationDriver = injector.get(AnimationDriver); |
| 55 | + const normalizer: AnimationStyleNormalizer = injector.get(AnimationStyleNormalizer); |
| 56 | + const engine = new DomAnimationTransitionEngine(driver, normalizer); |
| 57 | + return engine.process(element, instructions); |
| 58 | + } |
| 59 | +} |
0 commit comments