-
Notifications
You must be signed in to change notification settings - Fork 0
/
QualityRadioGroup.ts
125 lines (107 loc) · 4.41 KB
/
QualityRadioGroup.ts
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
import * as shadyCss from '@webcomponents/shadycss';
import { RadioGroup } from './RadioGroup';
import verticalRadioGroupCss from './VerticalRadioGroup.css';
import { StateReceiverMixin } from './StateReceiverMixin';
import type { ChromelessPlayer, MediaTrack, MediaTrackList, Quality, VideoQuality } from 'theoplayer/chromeless';
import { arrayFind, fromArrayLike } from '../util/CommonUtils';
import { QualityRadioButton } from './QualityRadioButton';
import { createEvent } from '../util/EventUtils';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate(
'theoplayer-quality-radio-group',
`<style>${verticalRadioGroupCss}</style><theoplayer-radio-group></theoplayer-radio-group>`
);
const TRACK_EVENTS = ['addtrack', 'removetrack', 'change'] as const;
/**
* `<theoplayer-quality-radio-group>` - A radio group that shows a list of available video qualities,
* from which the user can choose a desired target quality.
*
* @group Components
*/
export class QualityRadioGroup extends StateReceiverMixin(HTMLElement, ['player']) {
private readonly _radioGroup: RadioGroup;
private _player: ChromelessPlayer | undefined;
private _videoTracks: MediaTrackList | undefined;
private _track: MediaTrack | undefined;
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template().content.cloneNode(true));
this._radioGroup = shadowRoot.querySelector('theoplayer-radio-group')!;
this._upgradeProperty('player');
}
protected _upgradeProperty(prop: keyof this) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
connectedCallback(): void {
shadyCss.styleElement(this);
if (!(this._radioGroup instanceof RadioGroup)) {
customElements.upgrade(this._radioGroup);
}
this._updateTrack();
this.shadowRoot!.addEventListener('change', this._onChange);
}
disconnectedCallback(): void {
this.shadowRoot!.removeEventListener('change', this._onChange);
}
get player(): ChromelessPlayer | undefined {
return this._player;
}
set player(player: ChromelessPlayer | undefined) {
if (this._player === player) {
return;
}
this._videoTracks?.removeEventListener(TRACK_EVENTS, this._updateTrack);
this._player = player;
this._videoTracks = player?.videoTracks;
this._updateTrack();
this._videoTracks?.addEventListener(TRACK_EVENTS, this._updateTrack);
}
private readonly _onChange = () => {
this.dispatchEvent(createEvent('change', { bubbles: true }));
};
private readonly _updateTrack = (): void => {
const track = this._player ? arrayFind(this._player.videoTracks, (track) => track.enabled) : undefined;
if (this._track === track) {
return;
}
this._track = track;
this._update();
};
private readonly _update = (): void => {
const buttons = fromArrayLike(this._radioGroup.children) as QualityRadioButton[];
const availableQualities: VideoQuality[] = this._track ? (this._track.qualities as Quality[] as VideoQuality[]) : [];
// If there is only one available quality, *only* show the "Automatic" option (without the single quality).
// Otherwise, add an "Automatic" option at the front.
let qualities: (VideoQuality | undefined)[] = availableQualities.length === 1 ? [undefined] : [undefined, ...availableQualities];
let i = 0;
// Add buttons for each quality
while (i < buttons.length && i < qualities.length) {
const button = buttons[i];
button.track = this._track;
button.quality = qualities[i];
i++;
}
while (i < buttons.length) {
this._radioGroup.removeChild(buttons[i]);
i++;
}
while (i < qualities.length) {
const button = new QualityRadioButton();
button.track = this._track;
button.quality = qualities[i];
this._radioGroup.appendChild(button);
i++;
}
};
}
customElements.define('theoplayer-quality-radio-group', QualityRadioGroup);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-quality-radio-group': QualityRadioGroup;
}
}