all files / controls/vg-scrub-bar/vg-scrub-bar-buffering-time/ vg-scrub-bar-buffering-time.ts

69.57% Statements 16/23
66.67% Branches 6/9
42.86% Functions 3/7
75% Lines 15/20
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                                                                                                                              
import { Component, Input, ElementRef, OnInit, ViewEncapsulation, OnDestroy } from '@angular/core';
import { VgAPI } from '../../../core/services/vg-api';
import { Subscription } from 'rxjs/Subscription';
 
@Component({
    selector: 'vg-scrub-bar-buffering-time',
    encapsulation: ViewEncapsulation.None,
    template: `<div class="background" [style.width]="getBufferTime()"></div>`,
    styles: [ `
        vg-scrub-bar-buffering-time {
            display: flex;
            width: 100%;
            height: 5px;
            pointer-events: none;
            position: absolute;
        }
 
        vg-scrub-bar-buffering-time .background {
            background-color: rgba(255, 255, 255, 0.3);
        }
 
        vg-controls vg-scrub-bar-buffering-time {
            position: absolute;
            top: calc(50% - 3px);
        }
 
        vg-controls vg-scrub-bar-buffering-time .background {
            -webkit-border-radius: 2px;
            -moz-border-radius: 2px;
            border-radius: 2px;
        }
    ` ]
})
export class VgScrubBarBufferingTime implements OnInit, OnDestroy {
    @Input() vgFor: string;
 
    elem: HTMLElement;
    target: any;
 
    subscriptions: Subscription[] = [];
 
    constructor(ref: ElementRef, public API: VgAPI) {
        this.elem = ref.nativeElement;
    }
 
    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);
    }
 
    getBufferTime() {
        let bufferTime = "0%";
 
        if (this.target && this.target.buffer && this.target.buffered.length) {
            Iif (this.target.time.total === 0) {
                bufferTime = '0%';
            }
            else {
                bufferTime = ((this.target.buffer.end / this.target.time.total) * 100) + '%';
            }
        }
 
        return bufferTime;
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
}