all files / controls/vg-track-selector/ vg-track-selector.ts

82.35% Statements 28/34
50% Branches 3/6
69.23% Functions 9/13
86.67% Lines 26/30
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571× 1×                                                                                                                                                                       1×                       6× 6×     1×                 1× 4×   4× 17× 8× 8×           4×         8×       12× 4× 4×     3× 2×   2×   2×   4× 1× 1×   3×         1×        
import { Component, ElementRef, OnInit, Input, ViewEncapsulation, OnDestroy } from '@angular/core';
import { VgAPI } from '../../core/services/vg-api';
import { Subscription } from 'rxjs/Subscription';
 
export interface Option {
    id: string;
    label: string;
    selected: boolean;
}
 
@Component({
    selector: 'vg-track-selector',
    encapsulation: ViewEncapsulation.None,
    template: `
        <div class="container">
            <div class="track-selected"
                 [class.vg-icon-closed_caption]="!trackSelected">
                {{ trackSelected || '' }}
            </div>
            
            <select class="trackSelector" 
                    (change)="selectTrack($event.target.value)"
                    tabindex="0"
                    aria-label="track selector"
                    [attr.aria-valuetext]="ariaValue">
                <option 
                    *ngFor="let track of tracks"
                    [value]="track.id"
                    [selected]="track.selected === true">
                    {{ track.label }}
                </option>
            </select>
        </div>
    `,
    styles: [ `
        vg-track-selector {
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            display: flex;
            justify-content: center;
            width: 50px;
            height: 50px;
            cursor: pointer;
            color: white;
            line-height: 50px;
        }
        vg-track-selector .container {
            position: relative;
            display: flex;
            flex-grow: 1;
            align-items: center;
            
            padding: 0;
            margin: 5px;
        }
        vg-track-selector select.trackSelector {
            width: 50px;
            padding: 5px 8px;
            border: none;
            background: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            color: transparent;
            font-size: 16px;
        }
        vg-track-selector .track-selected {
            position: absolute;
            width: 100%;
            height: 50px;
            top: -6px;
            text-align: center;
            text-transform: uppercase;
            font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
            padding-top: 2px;
            pointer-events: none;
        }
        vg-track-selector .vg-icon-closed_caption:before {
            width: 100%;
        }
    ` ]
})
export class VgTrackSelector implements OnInit, OnDestroy {
    @Input() vgFor: string;
 
    elem: HTMLElement;
    target: any;
    tracks: Array<Option>;
    trackSelected: string;
 
    subscriptions: Subscription[] = [];
 
    ariaValue: string;
 
    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);
 
        const subs: Array<Option> = Array.from((this.API.getMasterMedia().elem as HTMLMediaElement).children)
            .filter((item: HTMLElement) => item.tagName === 'TRACK')
            .filter((item: HTMLTrackElement) => item.kind === 'subtitles')
            .map((item: HTMLTrackElement) => ({
                label: item.label,
                selected: item.default === true,
                id: item.srclang
            }));
 
        this.tracks = [
            ...subs,
            {
                id: null,
                label: 'Off',
                selected: subs.every((item: Option) => item.selected === false)
            }
        ];
 
        const track: Option = this.tracks.filter((item: Option) => item.selected === true)[ 0 ];
        this.trackSelected = track.id;
        this.ariaValue = track.label;
    }
 
    selectTrack(trackId: string) {
        this.trackSelected = (trackId === 'null') ? null : trackId;
 
        this.ariaValue = 'No track selected';
 
        Array.from((this.API.getMasterMedia().elem as HTMLMediaElement).textTracks)
            .forEach((item: TextTrack) => {
                if (item.language === trackId) {
                    this.ariaValue = item.label;
                    item.mode = 'showing';
                } else {
                    item.mode = 'hidden';
                }
            });
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
}