-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveQualityDisplay.ts
83 lines (71 loc) · 3.02 KB
/
ActiveQualityDisplay.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
import { StateReceiverMixin } from './StateReceiverMixin';
import { setTextContent } from '../util/CommonUtils';
import type { VideoQuality } from 'theoplayer/chromeless';
import { formatQualityLabel } from '../util/TrackUtils';
import * as shadyCss from '@webcomponents/shadycss';
/**
* `<theoplayer-active-quality-display>` - A control that displays the name of the active video quality.
*
* @group Components
*/
export class ActiveQualityDisplay extends StateReceiverMixin(HTMLElement, ['activeVideoQuality', 'targetVideoQualities']) {
private readonly _spanEl: HTMLSpanElement;
private _activeVideoQuality: VideoQuality | undefined = undefined;
private _targetVideoQualities: VideoQuality[] | undefined = undefined;
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
this._spanEl = document.createElement('span');
shadowRoot.appendChild(this._spanEl);
this._upgradeProperty('activeVideoQuality');
this._upgradeProperty('targetVideoQualities');
}
connectedCallback(): void {
shadyCss.styleElement(this);
}
protected _upgradeProperty(prop: keyof this) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
get activeVideoQuality(): VideoQuality | undefined {
return this._activeVideoQuality;
}
set activeVideoQuality(quality: VideoQuality | undefined) {
this._activeVideoQuality = quality;
this.update_();
}
get targetVideoQualities(): VideoQuality[] | undefined {
return this._targetVideoQualities;
}
set targetVideoQualities(qualities: VideoQuality[] | undefined) {
this._targetVideoQualities = qualities;
this.update_();
}
private update_(): void {
// If no target quality is selected, or more than one target quality is selected,
// treat as "automatic" quality selection.
const hasSingleTargetQuality = this._targetVideoQualities !== undefined && this._targetVideoQualities.length === 1;
const targetQuality = hasSingleTargetQuality ? this._targetVideoQualities![0] : undefined;
// Always show the target quality immediately, even if it's not the active quality yet.
const selectedQuality = targetQuality ?? this._activeVideoQuality;
const qualityLabel = formatQualityLabel(selectedQuality);
let label: string;
if (hasSingleTargetQuality) {
// Manual quality selection: "720p" or "Unknown"
label = qualityLabel ?? `Unknown`;
} else {
// Automatic quality selection: "Automatic" or "Automatic (720p)"
label = `Automatic${qualityLabel ? ` (${qualityLabel})` : ''}`;
}
setTextContent(this._spanEl, label);
}
}
customElements.define('theoplayer-active-quality-display', ActiveQualityDisplay);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-active-quality-display': ActiveQualityDisplay;
}
}