all files / controls/vg-scrub-bar/vg-scrub-bar-cue-points/ vg-scrub-bar-cue-points.ts

70.21% Statements 33/47
60% Branches 12/20
50% Functions 5/10
72.73% Lines 32/44
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 1371×                   1×                                                                 1×                         4× 4×     1×                 1× 1×   1× 1×   1×         1× 3×     2×   2× 6× 6× 6× 6×   6× 3× 3×     6×         6×         1× 2×       2×     1× 3× 2×       1×                     1×        
import {
    Component,
    ElementRef,
    Input,
    OnChanges,
    OnDestroy,
    OnInit,
    SimpleChange,
    ViewEncapsulation
} from '@angular/core';
import { VgAPI } from '../../../core/services/vg-api';
import { Subscription } from 'rxjs/Subscription';
 
@Component({
    selector: 'vg-scrub-bar-cue-points',
    encapsulation: ViewEncapsulation.None,
    template: `
        <div class="cue-point-container">
            <span *ngFor="let cp of cuePoints" [style.width]="cp.$$style?.width" [style.left]="cp.$$style?.left"
                  class="cue-point"></span>
        </div>
    `,
    styles: [ `
        vg-scrub-bar-cue-points {
            display: flex;
            width: 100%;
            height: 5px;
            pointer-events: none;
            position: absolute;
        }
 
        vg-scrub-bar-cue-points .cue-point-container .cue-point {
            position: absolute;
            height: 5px;
            background-color: rgba(255, 204, 0, 0.7);
        }
 
        vg-controls vg-scrub-bar-cue-points {
            position: absolute;
            top: calc(50% - 3px);
        }
    ` ]
})
export class VgScrubBarCuePoints implements OnInit, OnChanges, OnDestroy {
    @Input() vgCuePoints: TextTrackCueList;
    @Input() vgFor: string;
 
    elem: HTMLElement;
    target: any;
    onLoadedMetadataCalled: boolean = false;
    cuePoints: Array<any> = [];
 
    subscriptions: Subscription[] = [];
 
    totalCues = 0;
 
    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);
 
        let onTimeUpdate = this.target.subscriptions.loadedMetadata;
        this.subscriptions.push(onTimeUpdate.subscribe(this.onLoadedMetadata.bind(this)));
 
        Iif (this.onLoadedMetadataCalled) {
            this.onLoadedMetadata();
        }
    }
 
    onLoadedMetadata() {
        if (this.vgCuePoints) {
            // We need to transform the TextTrackCueList to Array or it doesn't work on IE11/Edge.
            // See: https://github.com/videogular/videogular2/issues/369
            this.cuePoints = [];
 
            for (let i = 0, l = this.vgCuePoints.length; i < l; i++) {
                let end = (this.vgCuePoints[ i ].endTime >= 0) ? this.vgCuePoints[ i ].endTime : this.vgCuePoints[ i ].startTime + 1;
                let cuePointDuration = (end - this.vgCuePoints[ i ].startTime) * 1000;
                let position = '0';
                let percentWidth = '0';
 
                if (typeof cuePointDuration === 'number' && this.target.time.total) {
                    percentWidth = ((cuePointDuration * 100) / this.target.time.total) + "%";
                    position = (this.vgCuePoints[ i ].startTime * 100 / (Math.round(this.target.time.total / 1000))) + "%";
                }
 
                (<any>this.vgCuePoints[ i ]).$$style = {
                    width: percentWidth,
                    left: position
                };
 
                this.cuePoints.push(this.vgCuePoints[ i ]);
            }
        }
    }
 
    updateCuePoints() {
        Iif (!this.target) {
            this.onLoadedMetadataCalled = true;
            return;
        }
        this.onLoadedMetadata();
    }
 
    ngOnChanges(changes: { [propName: string]: SimpleChange }) {
        if (changes[ 'vgCuePoints' ].currentValue) {
            this.updateCuePoints();
        }
    }
 
    ngDoCheck() {
        if (this.vgCuePoints) {
            const changes = this.totalCues !== this.vgCuePoints.length;
 
            if (changes) {
                this.totalCues = this.vgCuePoints.length;
                this.updateCuePoints();
            }
        }
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
}