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

83.33% Statements 30/36
50% Branches 3/6
71.43% Functions 10/14
87.1% Lines 27/31
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 157 158 159 160 161 162 163 164 165                                                         17×                         12×                                                                                                                                                                                                    
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;
}
 
 
export class VgTrackSelector implements OnInit, OnDestroy {
     vgFor: string;
 
    elem: HTMLElement;
    target: any;
    tracks: Array<Option>;
    trackSelected: string;
 
    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);
 
        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)
            }
        ];
 
        this.trackSelected = this.tracks.filter((item: Option) => item.selected === true)[ 0 ].id;
    }
 
    selectTrack(trackId: string) {
        this.trackSelected = (trackId === 'null') ? null : trackId;
 
        Array.from((this.API.getMasterMedia().elem as HTMLMediaElement).textTracks)
            .forEach((item: TextTrack) => {
                if (item.language === trackId) {
                    item.mode = 'showing';
                } else {
                    item.mode = 'hidden';
                }
            });
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
static decorators: DecoratorInvocation[] = [
{ type: Component, args: [{
    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)">
                <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 select.trackSelector:focus {
            outline: none;
        }
        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%;
        }
    ` ]
}, ] },
];
/** @nocollapse */
static ctorParameters: ({type: any, decorators?: DecoratorInvocation[]}|null)[] = [
{type: ElementRef, },
{type: VgAPI, },
];
static propDecorators: {[key: string]: DecoratorInvocation[]} = {
'vgFor': [{ type: Input },],
};
}
 
interface DecoratorInvocation {
  type: Function;
  args?: any[];
}