all files / controls/ vg-controls.ts

80.33% Statements 49/61
75% Branches 9/12
60% Functions 9/15
81.82% Lines 45/55
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                                                                                           10× 10×                                                                                                    
import {
    Component, Input, OnInit, ElementRef, HostBinding, AfterViewInit, ViewEncapsulation,
    EventEmitter, Output
} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { VgAPI } from '../core/services/vg-api';
import { VgControlsHidden } from './../core/services/vg-controls-hidden';
import 'rxjs/add/observable/fromEvent';
import { VgStates } from '../core/states/vg-states';
import { Subscription } from 'rxjs/Subscription';
 
@Component({
    selector: 'vg-controls',
    encapsulation: ViewEncapsulation.None,
    template: `<ng-content></ng-content>`,
    styles: [ `
        vg-controls {
            position: absolute;
            display: flex;
            width: 100%;
            height: 50px;
            z-index: 300;
            bottom: 0;
            background-color: rgba(0, 0, 0, 0.5);
            -webkit-transition: bottom 1s;
            -khtml-transition: bottom 1s;
            -moz-transition: bottom 1s;
            -ms-transition: bottom 1s;
            transition: bottom 1s;
        }
 
        vg-controls.hide {  
            bottom: -50px;
        }
    `]
})
export class VgControls implements OnInit, AfterViewInit {
    elem: HTMLElement;
    target: any;
 
    @HostBinding('style.pointer-events') isAdsPlaying: string = 'initial';
    @HostBinding('class.hide') hideControls: boolean = false;
 
    @Input() vgFor: string;
    @Input() vgAutohide: boolean = false;
    @Input() vgAutohideTime: number = 3;
 
    private timer: any;
    private hideTimer: any;
 
    subscriptions: Subscription[] = [];
 
    constructor(private API: VgAPI, private ref: ElementRef, private hidden: VgControlsHidden) {
        this.elem = ref.nativeElement;
    }
 
    ngOnInit() {
        let mouseMove = Observable.fromEvent(this.API.videogularElement, 'mousemove');
        this.subscriptions.push(mouseMove.subscribe(this.show.bind(this)));
 
        let touchStart = Observable.fromEvent(this.API.videogularElement, 'touchstart');
        this.subscriptions.push(touchStart.subscribe(this.show.bind(this)));
 
        Iif (this.API.isPlayerReady) {
            this.onPlayerReady();
        }
        else {
            this.subscriptions.push(this.API.playerReadyEvent.subscribe(() => this.onPlayerReady()));
        }
    }
 
    onPlayerReady() {
        this.target = this.API.getMediaById(this.vgFor);
 
        this.subscriptions.push(this.target.subscriptions.play.subscribe(this.onPlay.bind(this)));
        this.subscriptions.push(this.target.subscriptions.pause.subscribe(this.onPause.bind(this)));
        this.subscriptions.push(this.target.subscriptions.startAds.subscribe(this.onStartAds.bind(this)));
        this.subscriptions.push(this.target.subscriptions.endAds.subscribe(this.onEndAds.bind(this)));
    }
 
    ngAfterViewInit() {
        if (this.vgAutohide) {
            this.hide();
        }
        else {
            this.show();
        }
    }
 
    onPlay() {
        Eif (this.vgAutohide) {
            this.hide();
        }
    }
 
    onPause() {
        clearTimeout(this.timer);
        this.hideControls = false;
        this.hidden.state(false);
    }
 
    onStartAds() {
        this.isAdsPlaying = 'none';
    }
 
    onEndAds() {
        this.isAdsPlaying = 'initial';
    }
 
    hide() {
        if (this.vgAutohide) {
            clearTimeout(this.timer);
            this.hideAsync();
        }
    }
 
    show() {
        clearTimeout(this.timer);
        this.hideControls = false;
        this.hidden.state(false);
 
        Iif (this.vgAutohide) {
            this.hideAsync();
        }
    }
 
    private hideAsync() {
        if (this.API.state === VgStates.VG_PLAYING) {
            this.timer = setTimeout(() => {
                this.hideControls = true;
                this.hidden.state(true);
            }, this.vgAutohideTime * 1000);
        }
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
}