All files / src/lib/common/transition transition.directive.ts

100% Statements 65/65
100% Branches 31/31
100% Functions 9/9
100% Lines 61/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1221x                           1x               1x   1x   1x   1x   50x   50x 50x 50x 50x 50x   50x 50x     1x 53x   53x 51x 2x 1x   1x         1x 6x 5x 5x   5x 2x   3x 1x   2x           1x 52x     1x 7x 6x 6x 6x 6x 6x 6x   1x 1x       5x 5x   5x 3x 3x   2x 2x   2x 1x 1x           1x 58x   58x 58x 58x 58x   58x 57x     1x  
import {
    Attribute,
    Directive,
    ElementRef,
    EventEmitter,
    HostListener,
    Input,
    OnChanges,
    Output,
    Renderer2,
    SimpleChanges
} from '@angular/core';
 
import {TransitionMode} from '../types/TransitionMode';
import {TransitionState} from '../types/TransitionState';
 
/**
 * @experimental
 */
@Directive({
    selector: '[anjTransition]'
})
export class TransitionDirective implements OnChanges {
 
    private static supportedTransitions = ['opacity', 'transform'];
 
    @Input() public anjTransition: any;
 
    @Output() public end: EventEmitter<TransitionState>;
 
    private state: TransitionState = TransitionState.EnterTo;
 
    constructor(private renderer: Renderer2,
                private element: ElementRef,
                @Attribute('mode') public mode: TransitionMode,
                @Attribute('name') public name: string) {
        this.end = new EventEmitter<TransitionState>();
 
        this.mode = this.mode ? this.mode : 'in-out';
        this.name = this.name ? this.name : 'transition';
    }
 
    public ngOnChanges(changes: SimpleChanges) {
        this.endTransition();
 
        if (!changes.anjTransition.previousValue && !changes.anjTransition.currentValue && this.mode !== 'out') {
            this.initEnter();
        } else if (!changes.anjTransition.previousValue && changes.anjTransition.currentValue) {
            this.transitionIn();
        } else {
            this.transitionOut();
        }
    }
 
    @HostListener('transitiend', ['$event'])
    public handleTransition(event: TransitionEvent): void {
        if (event.target === this.element.nativeElement && TransitionDirective.supportedTransitions.indexOf(event.propertyName) > -1) {
            event.stopPropagation();
            this.end.emit(this.state);
 
            if (this.state === TransitionState.Enter) {
                this.endTransition();
            } else {
                if (this.mode !== 'out' && this.anjTransition) {
                    this.transitionIn();
                } else {
                    this.endTransition();
                }
            }
        }
    }
 
    private initEnter(): void {
        this.renderer.addClass(this.element.nativeElement, `anj-${this.name}-enter-to`);
    }
 
    private transitionIn(): void {
        if (this.mode !== 'out') {
            this.state = TransitionState.Enter;
            this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-enter-to`);
            this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-leave`);
            this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-leave-active`);
            this.renderer.addClass(this.element.nativeElement, `anj-${this.name}-enter-active`);
            this.renderer.addClass(this.element.nativeElement, `anj-${this.name}-enter`);
        } else {
            this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-enter-to`);
            this.endTransition();
        }
    }
 
    private transitionOut(): void {
        this.state = TransitionState.Leave;
 
        if (this.mode !== 'in') {
            this.renderer.addClass(this.element.nativeElement, `anj-${this.name}-leave-active`);
            this.renderer.addClass(this.element.nativeElement, `anj-${this.name}-leave`);
        } else {
            this.renderer.addClass(this.element.nativeElement, `anj-${this.name}-enter-to`);
            this.state = TransitionState.EnterTo;
 
            if (this.anjTransition) {
                window.setTimeout(() => {
                    this.transitionIn();
                });
            }
        }
    }
 
    private endTransition(): void {
        this.state = TransitionState.Inactive;
 
        this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-enter-active`);
        this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-leave-active`);
        this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-enter`);
        this.renderer.removeClass(this.element.nativeElement, `anj-${this.name}-leave`);
 
        if (!this.anjTransition) {
            this.renderer.addClass(this.element.nativeElement, `anj-${this.name}-enter-to`);
        }
    }
}