all files / controls/vg-playback-button/ vg-playback-button.ts

63.33% Statements 19/30
20% Branches 2/10
40% Functions 4/10
66.67% Lines 18/27
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                                                                                                                                                                                  
import { Component, Input, ElementRef, HostListener, OnInit, ViewEncapsulation, OnDestroy } from '@angular/core';
import { VgAPI } from '../../core/services/vg-api';
import { Subscription } from 'rxjs/Subscription';
 
@Component({
    selector: 'vg-playback-button',
    encapsulation: ViewEncapsulation.None,
    template: `
    <span class="button"
          tabindex="0"
          role="button"
          aria-label="playback speed button"
          [attr.aria-valuetext]="ariaValue">
        {{getPlaybackRate()}}x
    </span>`,
    styles: [ `
        vg-playback-button {
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            display: flex;
            justify-content: center;
            height: 50px;
            width: 50px;
            cursor: pointer;
            color: white;
            line-height: 50px;
            font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
        }
 
        vg-playback-button .button {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 50px;
        }
    ` ]
})
export class VgPlaybackButton implements OnInit, OnDestroy {
    @Input() vgFor: string;
 
    elem: HTMLElement;
    target: any;
 
    @Input() playbackValues: Array<string>;
    playbackIndex: number;
 
    subscriptions: Subscription[] = [];
 
    ariaValue = 1;
 
    constructor(ref: ElementRef, public API: VgAPI) {
        this.elem = ref.nativeElement;
        this.playbackValues = [ '0.5', '1.0', '1.5', '2.0' ];
        this.playbackIndex = 1;
    }
 
    ngOnInit() {
        if (this.API.isPlayerReady) {
            this.onPlayerReady();
        }
        else {
            this.subscriptions.push(this.API.playerReadyEvent.subscribe(() => this.onPlayerReady()));
        }
    }
 
    onPlayerReady() {
        this.target = this.API.getMediaById(this.vgFor);
    }
 
    @HostListener('click')
    onClick() {
        this.updatePlaybackSpeed();
    }
 
    @HostListener('keydown', ['$event'])
    onKeyDown(event: KeyboardEvent) {
        // On press Enter (13) or Space (32)
        if (event.keyCode === 13 || event.keyCode === 32) {
            event.preventDefault();
            this.updatePlaybackSpeed();
        }
    }
 
    updatePlaybackSpeed() {
        this.playbackIndex = ++this.playbackIndex % this.playbackValues.length;
 
        if (this.target instanceof VgAPI) {
            this.target.playbackRate = (this.playbackValues[ this.playbackIndex ]);
        }
        else {
            this.target.playbackRate[ this.vgFor ] = (this.playbackValues[ this.playbackIndex ]);
        }
    }
 
    getPlaybackRate() {
        this.ariaValue = this.target ? this.target.playbackRate : 1.0;
        return this.ariaValue;
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
}