all files / controls/vg-volume/ vg-volume.ts

71.11% Statements 32/45
44.44% Branches 8/18
57.14% Functions 8/14
73.81% Lines 31/42
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 166 167 168 169 170 171 1721×       1×                                                                                                                                                         1×                         15× 15× 15×     1×                 1× 2× 2×     1×       1× 2× 2×         2× 1×           4× 3× 3× 2×                                 1× 1× 1× 1× 1×     1× 3× 3×     1× 4×     1×        
import {
    Component, Input, ElementRef, HostListener, OnInit, ViewEncapsulation, ViewChild,
    OnDestroy
} from '@angular/core';
import { VgAPI } from '../../core/services/vg-api';
import { Subscription } from 'rxjs/Subscription';
 
@Component({
    selector: 'vg-volume',
    encapsulation: ViewEncapsulation.None,
    template: `
        <div #volumeBar
            class="volumeBar"
            tabindex="0"
            role="slider"
            aria-label="volume level"
            aria-level="polite"
            [attr.aria-valuenow]="ariaValue"
            aria-valuemin="0"
            aria-valuemax="100"
            [attr.aria-valuetext]="ariaValue + '%'"
            (click)="onClick($event)"
            (mousedown)="onMouseDown($event)"
            [style.width]="hidden ? '0px' : '100px'">
            <div class="volumeBackground" [ngClass]="{dragging: isDragging}">
                <div class="volumeValue" [style.width]="hidden ? '0px' : (getVolume() * (100-15)) + '%'"></div>
                <div [style.width]="hidden ? '0px' : '15px'" class="volumeKnob" [style.left]="(getVolume() * (100-15)) + '%'"></div>
            </div>
        </div>
    `,
    styles: [ `
        vg-volume {
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            display: flex;
            justify-content: center;
            height: 50px
            cursor: pointer;
            color: white;
            line-height: 50px;
        }
        vg-volume .volumeBar {
            position: relative;
            display: flex;
            flex-grow: 1;
            align-items: center;
            cursor: pointer;
        }
        vg-volume .volumeBackground {
            display: flex;
            flex-grow: 1;
            height: 5px;
            pointer-events: none;
            background-color: #333;
        }
        vg-volume .volumeValue {
            display: flex;
            height: 5px;
            pointer-events: none;
            background-color: #FFF;
            transition:all 0.2s ease-out;
        }
        vg-volume .volumeKnob {
            position: absolute;
            width: 15px; height: 15px;
            left: 0; top: 50%;
            transform: translateY(-50%);
            border-radius: 15px;
            pointer-events: none;
            background-color: #FFF;
            transition:all 0.2s ease-out;
        }
        vg-volume .volumeBackground.dragging .volumeValue,
        vg-volume .volumeBackground.dragging .volumeKnob {
            transition: none;
        }
    ` ]
})
export class VgVolume implements OnInit, OnDestroy {
    @Input() hidden: boolean;
    @Input() vgFor: string;
    @ViewChild('volumeBar') volumeBarRef: ElementRef;
 
    elem: HTMLElement;
    target: any;
    isDragging: boolean;
    mouseDownPosX: number;
    ariaValue: number;
 
    subscriptions: Subscription[] = [];
 
    constructor(ref: ElementRef, public API: VgAPI) {
        this.elem = ref.nativeElement;
        this.isDragging = false;
    }
 
    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);
        this.ariaValue = this.getVolume() * 100;
    }
 
    onClick(event: { clientX: number }) {
        this.setVolume(this.calculateVolume(event.clientX));
    }
 
    onMouseDown(event: { clientX: number }) {
        this.mouseDownPosX = event.clientX;
        this.isDragging = true;
    }
 
    @HostListener('document:mousemove', [ '$event' ])
    onDrag(event: { clientX: number }) {
        if (this.isDragging) {
            this.setVolume(this.calculateVolume(event.clientX));
        }
    }
 
    @HostListener('document:mouseup', [ '$event' ])
    onStopDrag(event: { clientX: number }) {
        if (this.isDragging) {
            this.isDragging = false;
            if (this.mouseDownPosX === event.clientX) {
                this.setVolume(this.calculateVolume(event.clientX));
            }
        }
    }
 
    @HostListener('keydown', ['$event'])
    arrowAdjustVolume(event: KeyboardEvent) {
        if (event.keyCode === 38 || event.keyCode === 39) {
            event.preventDefault();
            this.setVolume(Math.max(0, Math.min(100,(this.getVolume() * 100) + 10)));
        }
        else if (event.keyCode === 37 || event.keyCode === 40) {
            event.preventDefault();
            this.setVolume(Math.max(0, Math.min(100,(this.getVolume() * 100) - 10)));
        }
    }
 
    calculateVolume(mousePosX: number) {
        const recObj = this.volumeBarRef.nativeElement.getBoundingClientRect();
        const volumeBarOffsetLeft: number = recObj.left;
        const volumeBarWidth: number = recObj.width;
        return (mousePosX - volumeBarOffsetLeft) / volumeBarWidth * 100;
    }
 
    setVolume(vol: number) {
        this.target.volume = Math.max(0, Math.min(1, vol / 100));
        this.ariaValue = this.target.volume * 100;
    }
 
    getVolume(): number {
        return this.target ? this.target.volume : 0;
    }
 
    ngOnDestroy() {
        this.subscriptions.forEach(s => s.unsubscribe());
    }
}