-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextTrackOffRadioButton.ts
80 lines (66 loc) · 2.54 KB
/
TextTrackOffRadioButton.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
import { RadioButton } from './RadioButton';
import { buttonTemplate } from './Button';
import type { TextTracksList } from 'theoplayer/chromeless';
import { isSubtitleTrack } from '../util/TrackUtils';
import { Attribute } from '../util/Attribute';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate('theoplayer-text-track-off-radio-button', buttonTemplate(`<slot>Off</slot>`));
const TRACK_EVENTS = ['change'] as const;
/**
* `<theoplayer-text-track-off-radio-button>` - A radio button that disables the active subtitle track when clicked.
*
* @group Components
*/
export class TextTrackOffRadioButton extends RadioButton {
private _trackList: TextTracksList | undefined = undefined;
constructor() {
super({ template: template() });
this._upgradeProperty('trackList');
}
get trackList(): TextTracksList | undefined {
return this._trackList;
}
set trackList(trackList: TextTracksList | undefined) {
if (this._trackList) {
this._trackList.removeEventListener(TRACK_EVENTS, this._onTrackChange);
}
this._trackList = trackList;
this._updateFromTrackList();
if (trackList) {
trackList.addEventListener(TRACK_EVENTS, this._onTrackChange);
}
}
private _updateFromTrackList(): void {
this.checked = this._trackList ? !hasShowingSubtitleTrack(this._trackList) : true;
}
private _updateTrackList(): void {
if (this._trackList && this.checked) {
disableSubtitleTracks(this._trackList);
}
}
private readonly _onTrackChange = () => {
this._updateFromTrackList();
};
override attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
super.attributeChangedCallback(attrName, oldValue, newValue);
if (attrName === Attribute.ARIA_CHECKED && oldValue !== newValue) {
this._updateTrackList();
}
}
}
function hasShowingSubtitleTrack(trackList: TextTracksList): boolean {
return trackList.filter(isSubtitleTrack).some((x) => x.mode === 'showing');
}
function disableSubtitleTracks(trackList: TextTracksList): void {
for (const track of trackList) {
if (isSubtitleTrack(track) && track.mode === 'showing') {
track.mode = 'disabled';
}
}
}
customElements.define('theoplayer-text-track-off-radio-button', TextTrackOffRadioButton);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-text-track-off-radio-button': TextTrackOffRadioButton;
}
}