Skip to content

Commit

Permalink
feat(animations): expose the triggerName within the transition event
Browse files Browse the repository at this point in the history
Closes #13600
  • Loading branch information
matsko committed Jan 7, 2017
1 parent 4bae4b3 commit 3f67ab0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
4 changes: 2 additions & 2 deletions modules/@angular/compiler/src/animation/animation_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ class _AnimationBuilder implements AnimationAstVisitor {

statements.push(new o.ReturnStatement(
o.importExpr(createIdentifier(Identifiers.AnimationTransition)).instantiate([
_ANIMATION_PLAYER_VAR, _ANIMATION_FACTORY_ELEMENT_VAR, _ANIMATION_CURRENT_STATE_VAR,
_ANIMATION_NEXT_STATE_VAR, _ANIMATION_TIME_VAR
_ANIMATION_PLAYER_VAR, _ANIMATION_FACTORY_ELEMENT_VAR, o.literal(this.animationName),
_ANIMATION_CURRENT_STATE_VAR, _ANIMATION_NEXT_STATE_VAR, _ANIMATION_TIME_VAR
])));

return o.fn(
Expand Down
7 changes: 4 additions & 3 deletions modules/@angular/core/src/animation/animation_transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ import {AnimationTransitionEvent} from './animation_transition_event';

export class AnimationTransition {
constructor(
private _player: AnimationPlayer, private _element: ElementRef, private _fromState: string,
private _toState: string, private _totalTime: number) {}
private _player: AnimationPlayer, private _element: ElementRef, private _triggerName: string,
private _fromState: string, private _toState: string, private _totalTime: number) {}

private _createEvent(phaseName: string): AnimationTransitionEvent {
return new AnimationTransitionEvent({
fromState: this._fromState,
toState: this._toState,
totalTime: this._totalTime,
phaseName: phaseName,
element: this._element
element: this._element,
triggerName: this._triggerName
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ export class AnimationTransitionEvent {
public totalTime: number;
public phaseName: string;
public element: ElementRef;
public triggerName: string;

constructor({fromState, toState, totalTime, phaseName, element}: {
constructor({fromState, toState, totalTime, phaseName, element, triggerName}: {
fromState: string,
toState: string,
totalTime: number,
phaseName: string,
element: any
element: any,
triggerName: string
}) {
this.fromState = fromState;
this.toState = toState;
this.totalTime = totalTime;
this.phaseName = phaseName;
this.element = new ElementRef(element);
this.triggerName = triggerName;
}
}
42 changes: 42 additions & 0 deletions modules/@angular/core/test/animation/animation_integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,48 @@ function declareTests({useJit}: {useJit: boolean}) {
expect(elements[i].nativeElement).toBe(targetElements[i]);
}
}));

it('should expose the trigger associated with the animation within the callback event',
fakeAsync(() => {
TestBed.overrideComponent(DummyIfCmp, {
set: {
template: `
<div *ngIf="exp"
@t1
(@t1.start)="callback($event)"
(@t1.done)="callback($event)"></div>
<div *ngIf="exp"
@t2
(@t2.start)="callback($event)"
(@t2.done)="callback($event)"></div>
`,
animations: [
trigger('t1', [transition('* => *', [animate(1000)])]),
trigger('t2', [transition('* => *', [animate(1000)])])
]
}
});

const driver = TestBed.get(AnimationDriver) as InnerContentTrackingAnimationDriver;
const fixture = TestBed.createComponent(DummyIfCmp);
const cmp = fixture.componentInstance;

let triggers: string[] = [];
cmp.callback = (e: AnimationTransitionEvent) =>
triggers.push(`${e.triggerName}-${e.phaseName}`);

cmp.exp = true;
fixture.detectChanges();
flushMicrotasks();

expect(triggers).toEqual(['t1-start', 't2-start']);
triggers = [];

driver.log.shift()['player'].finish();
driver.log.shift()['player'].finish();

expect(triggers).toEqual(['t1-done', 't2-done']);
}));
});

describe('ng directives', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import {AUTO_STYLE, AnimationTransitionEvent, Component, Injector, ViewChild, animate, state, style, transition, trigger} from '@angular/core';
import {DebugDomRootRenderer} from '@angular/core/src/debug/debug_renderer';
import {ElementRef} from '@angular/core/src/linker/element_ref';
import {ViewChild} from '@angular/core/src/metadata/di';
import {RootRenderer} from '@angular/core/src/render/api';
import {TestBed, fakeAsync, flushMicrotasks} from '@angular/core/testing';
import {MockAnimationPlayer} from '@angular/core/testing/testing_internal';
Expand Down Expand Up @@ -207,12 +206,14 @@ export function main() {
uiDriver.log.shift()['player'].finish();

const [triggerOneStart, triggerOneDone] = log['one'];
expect(triggerOneStart.triggerName).toEqual('one');
expect(triggerOneStart.fromState).toEqual('a');
expect(triggerOneStart.toState).toEqual('b');
expect(triggerOneStart.totalTime).toEqual(500);
expect(triggerOneStart.phaseName).toEqual('start');
expect(triggerOneStart.element instanceof ElementRef).toEqual(true);

expect(triggerOneDone.triggerName).toEqual('one');
expect(triggerOneDone.fromState).toEqual('a');
expect(triggerOneDone.toState).toEqual('b');
expect(triggerOneDone.totalTime).toEqual(500);
Expand All @@ -222,12 +223,14 @@ export function main() {
uiDriver.log.shift()['player'].finish();

const [triggerTwoStart, triggerTwoDone] = log['two'];
expect(triggerTwoStart.triggerName).toEqual('two');
expect(triggerTwoStart.fromState).toEqual('c');
expect(triggerTwoStart.toState).toEqual('d');
expect(triggerTwoStart.totalTime).toEqual(1000);
expect(triggerTwoStart.phaseName).toEqual('start');
expect(triggerTwoStart.element instanceof ElementRef).toEqual(true);

expect(triggerTwoDone.triggerName).toEqual('two');
expect(triggerTwoDone.fromState).toEqual('c');
expect(triggerTwoDone.toState).toEqual('d');
expect(triggerTwoDone.totalTime).toEqual(1000);
Expand Down Expand Up @@ -263,13 +266,15 @@ export function main() {
uiDriver.log.shift()['player'].finish();

const start1 = cmp1Log['start'];
expect(start1.triggerName).toEqual('myTrigger');
expect(start1.fromState).toEqual('void');
expect(start1.toState).toEqual('off');
expect(start1.totalTime).toEqual(500);
expect(start1.phaseName).toEqual('start');
expect(start1.element instanceof ElementRef).toBe(true);

const done1 = cmp1Log['done'];
expect(done1.triggerName).toEqual('myTrigger');
expect(done1.fromState).toEqual('void');
expect(done1.toState).toEqual('off');
expect(done1.totalTime).toEqual(500);
Expand All @@ -281,13 +286,15 @@ export function main() {
uiDriver.log.shift()['player'].finish();

const start2 = cmp2Log['start'];
expect(start2.triggerName).toEqual('myTrigger');
expect(start2.fromState).toEqual('void');
expect(start2.toState).toEqual('on');
expect(start2.totalTime).toEqual(1000);
expect(start2.phaseName).toEqual('start');
expect(start2.element instanceof ElementRef).toBe(true);

const done2 = cmp2Log['done'];
expect(done2.triggerName).toEqual('myTrigger');
expect(done2.fromState).toEqual('void');
expect(done2.toState).toEqual('on');
expect(done2.totalTime).toEqual(1000);
Expand Down
4 changes: 3 additions & 1 deletion tools/public_api_guard/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ export declare class AnimationTransitionEvent {
phaseName: string;
toState: string;
totalTime: number;
constructor({fromState, toState, totalTime, phaseName, element}: {
triggerName: string;
constructor({fromState, toState, totalTime, phaseName, element, triggerName}: {
fromState: string;
toState: string;
totalTime: number;
phaseName: string;
element: any;
triggerName: string;
});
}

Expand Down

0 comments on commit 3f67ab0

Please sign in to comment.