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

47.62% Statements 10/21
0% Branches 0/6
12.5% Functions 1/8
55.56% Lines 10/18
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 1321×                   1×       1×                                                                                                                                                                 1×                           1×       1×     1×           1×         1×     1×  
import {
    Component,
    ElementRef,
    OnInit,
    Input,
    ViewEncapsulation,
    OnDestroy,
    SimpleChanges,
    OnChanges, Output, EventEmitter
} from '@angular/core';
import { VgAPI } from '../../core/services/vg-api';
import { Subscription } from 'rxjs';
import { BitrateOption } from '../../core/core';
 
@Component({
    selector: 'vg-quality-selector',
    encapsulation: ViewEncapsulation.None,
    template: `
        <div class="container">
            <div class="quality-selected"
                 [class.vg-icon-hd]="!bitrateSelected">
                {{ bitrateSelected?.label }}
            </div>
            
            <select class="quality-selector" 
                    (change)="selectBitrate($event.target.value)"
                    tabindex="0"
                    aria-label="quality selector"
                    [attr.aria-valuetext]="ariaValue">
                <option 
                    *ngFor="let bitrate of bitrates"
                    [value]="bitrate.qualityIndex"
                    [selected]="bitrate.qualityIndex === bitrateSelected?.qualityIndex">
                    {{ bitrate.label }}
                </option>
            </select>
        </div>
    `,
    styles: [ `
        vg-quality-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-quality-selector .container {
            position: relative;
            display: flex;
            flex-grow: 1;
            align-items: center;
            
            padding: 0;
            margin: 5px;
        }
        vg-quality-selector select.quality-selector {
            width: 50px;
            padding: 5px 8px;
            border: none;
            background: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            color: transparent;
            font-size: 16px;
        }
        vg-quality-selector select.quality-selector::-ms-expand {
            display: none;
        }
        vg-quality-selector select.quality-selector option {
            color: #000;
        }
        vg-quality-selector .quality-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-quality-selector .vg-icon-closed_caption:before {
            width: 100%;
        }
    ` ]
})
export class VgQualitySelector implements OnInit, OnChanges, OnDestroy {
    @Input() bitrates: BitrateOption[];
 
    @Output() onBitrateChange: EventEmitter<BitrateOption> = new EventEmitter();
 
    bitrateSelected: BitrateOption;
 
    elem: HTMLElement;
    target: any;
 
    subscriptions: Subscription[] = [];
 
    ariaValue: string;
 
    constructor(ref: ElementRef, public API: VgAPI) {
        this.elem = ref.nativeElement;
    }
 
    ngOnInit() {
    }
 
    ngOnChanges(changes: SimpleChanges) {
        if (changes['bitrates'].currentValue && changes['bitrates'].currentValue.length) {
            this.bitrates.forEach(item => item.label = item.label || Math.round(item.bitrate / 1000).toString())
        }
    }
 
    selectBitrate(index: number) {
        this.bitrateSelected = this.bitrates[index];
        this.onBitrateChange.emit(this.bitrates[index]);
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
}