-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlaybackRateRadioGroup.ts
132 lines (113 loc) · 4.3 KB
/
PlaybackRateRadioGroup.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
126
127
128
129
130
131
132
import * as shadyCss from '@webcomponents/shadycss';
import { RadioGroup } from './RadioGroup';
import verticalRadioGroupCss from './VerticalRadioGroup.css';
import { StateReceiverMixin } from './StateReceiverMixin';
import type { ChromelessPlayer } from 'theoplayer/chromeless';
import type { RadioButton } from './RadioButton';
import { createEvent } from '../util/EventUtils';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate(
'theoplayer-playback-rate-radio-group',
`<style>${verticalRadioGroupCss}</style><theoplayer-radio-group><slot></slot></theoplayer-radio-group>`
);
/**
* `<theoplayer-playback-rate-radio-group>` - A radio group that shows a list of playback rates,
* from which the user can choose a desired playback rate.
*
* @slot {@link RadioButton} - The possible options for the playback rate.
* The value of each radio button must be a valid number.
* For example: `<theoplayer-radio-button value="2">2x</theoplayer-radio-button>`
* @group Components
*/
export class PlaybackRateRadioGroup extends StateReceiverMixin(HTMLElement, ['player']) {
private readonly _radioGroup: RadioGroup;
private readonly _optionsSlot: HTMLSlotElement;
private _player: ChromelessPlayer | undefined;
private _value: number = 1;
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template().content.cloneNode(true));
this._radioGroup = shadowRoot.querySelector('theoplayer-radio-group')!;
this._optionsSlot = shadowRoot.querySelector('slot')!;
this._upgradeProperty('value');
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._updateChecked();
this.shadowRoot!.addEventListener('change', this._onChange);
this._optionsSlot.addEventListener('slotchange', this._updateChecked);
}
disconnectedCallback(): void {
this.shadowRoot!.removeEventListener('change', this._onChange);
this._optionsSlot.removeEventListener('slotchange', this._updateChecked);
}
/**
* The currently chosen playback rate.
*/
get value(): number {
return this._value;
}
set value(value: number) {
value = Number(value);
if (this._value === value) {
return;
}
this._value = value;
if (this._player !== undefined) {
this._player.playbackRate = value;
}
this._updateChecked();
this.dispatchEvent(createEvent('change', { bubbles: true }));
}
get player(): ChromelessPlayer | undefined {
return this._player;
}
set player(player: ChromelessPlayer | undefined) {
if (this._player === player) {
return;
}
if (this._player !== undefined) {
this._player.removeEventListener('ratechange', this._updateFromPlayer);
}
this._player = player;
this._updateFromPlayer();
if (this._player !== undefined) {
this._player.addEventListener('ratechange', this._updateFromPlayer);
}
}
private readonly _updateChecked = (): void => {
const buttons = this._radioGroup.allRadioButtons();
for (const button of buttons) {
button.checked = Number(button.value) === this.value;
}
};
private readonly _onChange = (): void => {
const button = this._radioGroup.checkedRadioButton as RadioButton | null;
if (button !== null && this.value !== Number(button.value)) {
this.value = button.value;
}
};
private readonly _updateFromPlayer = (): void => {
if (this._player !== undefined) {
this.value = this._player.playbackRate;
}
};
}
customElements.define('theoplayer-playback-rate-radio-group', PlaybackRateRadioGroup);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-playback-rate-radio-group': PlaybackRateRadioGroup;
}
}