-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultUI.ts
336 lines (296 loc) · 12.5 KB
/
DefaultUI.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import * as shadyCss from '@webcomponents/shadycss';
import type { ChromelessPlayer, PlayerConfiguration, SourceDescription } from 'theoplayer/chromeless';
import type { UIContainer } from './UIContainer';
import defaultUiCss from './DefaultUI.css';
import defaultUiHtml from './DefaultUI.html';
import { Attribute } from './util/Attribute';
import { applyExtensions } from './extensions/ExtensionRegistry';
import { isMobile, isTv } from './util/Environment';
import type { DeviceType } from './util/DeviceType';
import type { StreamType } from './util/StreamType';
import type { TimeRange } from './components/TimeRange';
import { STREAM_TYPE_CHANGE_EVENT } from './events/StreamTypeChangeEvent';
import { READY_EVENT } from './events/ReadyEvent';
import { toggleAttribute } from './util/CommonUtils';
import { createCustomEvent } from './util/EventUtils';
import { createTemplate } from './util/TemplateUtils';
const template = createTemplate('theoplayer-default-ui', `<style>${defaultUiCss}</style>${defaultUiHtml}`);
/**
* `<theoplayer-default-ui>` - A default UI for THEOplayer.
*
* This default UI provides a great player experience out-of-the-box, that works well on all types of devices
* and for all types of streams. It provides all the common playback controls for playing, seeking,
* changing languages and qualities. It also supports advertisements and casting.
*
* ## Usage
*
* 1. Create a `<theoplayer-default-ui>` element.
* 2. Set its `configuration` attribute or property to a valid player configuration.
* 3. Set its `source` attribute or property to a valid stream source.
* 4. Optionally, customize the player using CSS custom properties and/or extra controls.
*
* ## Customization
*
* The styling can be controlled using CSS custom properties (see {@link UIContainer | `<theoplayer-ui>`}).
* Additional controls can be added to the `top-control-bar` and `bottom-control-bar` slots.
* For more extensive customizations, we recommend defining your own custom UI using
* a {@link UIContainer | `<theoplayer-ui>`}.
*
* @attribute `configuration` - The THEOplayer {@link theoplayer!PlayerConfiguration | PlayerConfiguration}, as a JSON string.
* @attribute `source` - The THEOplayer {@link theoplayer!SourceDescription | SourceDescription}, as a JSON string.
* @attribute `fluid` - If set, the player automatically adjusts its height to fit the video's aspect ratio.
* @attribute `muted` - If set, the player starts out as muted. Reflects `ui.player.muted`.
* @attribute `autoplay` - If set, the player attempts to automatically start playing (if allowed).
* @attribute `device-type` - The device type, either "desktop", "mobile" or "tv".
* Can be used in CSS to show/hide certain device-specific UI controls.
* @attribute `mobile` - Whether the user is on a mobile device. Equivalent to `device-type == "mobile"`.
* @attribute `tv` - Whether the user is on a TV device. Equivalent to `device-type == "tv"`.
* @attribute `stream-type` - The stream type, either "vod", "live" or "dvr".
* Can be used to show/hide certain UI controls specific for livestreams, such as
* a {@link LiveButton | `<theoplayer-live-button>`}.
* If you know in advance that the source will be a livestream, you can set this attribute to avoid a screen flicker
* when the player switches between its VOD-specific and live-only controls.
* @attribute `user-idle-timeout` - The timeout (in seconds) between when the user stops interacting with the UI,
* and when the user is considered to be "idle".
* @attribute `dvr-threshold` - The minimum length (in seconds) of a livestream's sliding window for the stream to be DVR
* and its stream type to be set to "dvr".
*
* @slot `title` - A slot for the stream's title in the top control bar.
* @slot `top-control-bar` - A slot for extra UI controls in the top control bar.
* @slot `bottom-control-bar` - A slot for extra UI controls in the bottom control bar.
* @slot `menu` - A slot for extra menus (see {@link Menu | `<theoplayer-menu>`}).
* @group Components
*/
export class DefaultUI extends HTMLElement {
/**
* Fired when the backing player is created, and the {@link DefaultUI.player} property is set.
*
* @group Events
*/
static READY_EVENT: typeof READY_EVENT = READY_EVENT;
static get observedAttributes() {
return [
Attribute.CONFIGURATION,
Attribute.SOURCE,
Attribute.MUTED,
Attribute.AUTOPLAY,
Attribute.FLUID,
Attribute.DEVICE_TYPE,
Attribute.STREAM_TYPE,
Attribute.USER_IDLE_TIMEOUT,
Attribute.DVR_THRESHOLD,
Attribute.HAS_TITLE
];
}
protected readonly _shadowRoot: ShadowRoot;
protected readonly _ui: UIContainer;
private readonly _titleSlot: HTMLSlotElement | undefined;
private readonly _timeRange: TimeRange | undefined;
private _appliedExtensions: boolean = false;
/**
* Creates a new THEOplayer default UI.
*
* @param configuration - The player configuration.
* Will be passed to the {@link theoplayer!ChromelessPlayer | ChromelessPlayer} constructor to create
* the underlying THEOplayer instance.
* Can also be set later on through the {@link configuration} property.
*/
constructor(configuration: PlayerConfiguration = {}) {
super();
this._shadowRoot = this.initShadowRoot();
this._ui = this._shadowRoot.querySelector('theoplayer-ui')!;
this._ui.addEventListener(READY_EVENT, this._dispatchReadyEvent);
this._ui.addEventListener(STREAM_TYPE_CHANGE_EVENT, this._updateStreamType);
this.setConfiguration_(configuration);
this._titleSlot = this._shadowRoot.querySelector<HTMLSlotElement>('slot[name="title"]') ?? undefined;
this._titleSlot?.addEventListener('slotchange', this._onTitleSlotChange);
this._timeRange = this._shadowRoot.querySelector('theoplayer-time-range') ?? undefined;
this._upgradeProperty('configuration');
this._upgradeProperty('source');
this._upgradeProperty('fluid');
this._upgradeProperty('muted');
this._upgradeProperty('autoplay');
this._upgradeProperty('streamType');
this._upgradeProperty('userIdleTimeout');
this._upgradeProperty('dvrThreshold');
}
protected initShadowRoot(): ShadowRoot {
const shadowRoot = this.attachShadow({ mode: 'open', delegatesFocus: true });
shadowRoot.appendChild(template().content.cloneNode(true));
return shadowRoot;
}
private _upgradeProperty(prop: keyof this) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
/**
* The underlying THEOplayer player instance.
*
* This is constructed automatically as soon as a valid {@link configuration} is set.
*/
get player(): ChromelessPlayer | undefined {
return this._ui.player;
}
/**
* The player configuration.
*
* Used to create the underlying THEOplayer instance.
*/
get configuration(): PlayerConfiguration {
return this._ui.configuration;
}
set configuration(configuration: PlayerConfiguration) {
this.removeAttribute(Attribute.CONFIGURATION);
this.setConfiguration_(configuration);
}
private setConfiguration_(configuration: PlayerConfiguration) {
this._ui.configuration = {
...configuration,
ads: {
...(configuration.ads ?? {}),
// Always disable Google IMA's own ad countdown,
// since we show our own countdown in the default UI.
showCountdown: false
}
};
}
/**
* The player's current source.
*/
get source(): SourceDescription | undefined {
return this._ui.source;
}
set source(value: SourceDescription | undefined) {
this.removeAttribute(Attribute.SOURCE);
this._ui.source = value;
}
/**
* Whether to automatically adjusts the player's height to fit the video's aspect ratio.
*/
get fluid(): boolean {
return this._ui.fluid;
}
set fluid(value: boolean) {
this._ui.fluid = value;
}
/**
* Whether the player's audio is muted.
*/
get muted(): boolean {
return this._ui.muted;
}
set muted(value: boolean) {
this._ui.muted = value;
}
/**
* Whether the player should attempt to automatically start playback.
*/
get autoplay(): boolean {
return this._ui.autoplay;
}
set autoplay(value: boolean) {
this._ui.autoplay = value;
}
/**
* The stream type, either "vod", "live" or "dvr".
*
* If you know in advance that the source will be a livestream, you can set this property to avoid a screen flicker
* when the player switches between its VOD-specific and live-only controls.
*/
get streamType(): StreamType {
return this._ui.streamType;
}
set streamType(value: StreamType) {
this._ui.streamType = value;
}
/**
* The timeout (in seconds) between when the user stops interacting with the UI,
* and when the user is considered to be "idle".
*/
get userIdleTimeout(): number {
return this._ui.userIdleTimeout;
}
set userIdleTimeout(value: number) {
this._ui.userIdleTimeout = value;
}
/**
* The minimum length (in seconds) of a livestream's sliding window for the stream to be DVR
* and its stream type to be set to "dvr".
*/
get dvrThreshold(): number {
return this._ui.dvrThreshold;
}
set dvrThreshold(value: number) {
this._ui.dvrThreshold = value;
}
connectedCallback(): void {
shadyCss.styleElement(this);
if (!this.hasAttribute(Attribute.DEVICE_TYPE)) {
const deviceType: DeviceType = isMobile() ? 'mobile' : isTv() ? 'tv' : 'desktop';
this.setAttribute(Attribute.DEVICE_TYPE, deviceType);
}
if (!this._appliedExtensions) {
this._appliedExtensions = true;
applyExtensions(this);
shadyCss.styleSubtree(this);
}
this._onTitleSlotChange();
}
disconnectedCallback(): void {
return;
}
attributeChangedCallback(attrName: string, oldValue: any, newValue: any): void {
if (newValue === oldValue) {
return;
}
const hasValue = newValue != null;
if (attrName === Attribute.SOURCE) {
this._ui.source = newValue ? (JSON.parse(newValue) as SourceDescription) : undefined;
} else if (attrName === Attribute.CONFIGURATION) {
this.setConfiguration_(newValue ? (JSON.parse(newValue) as PlayerConfiguration) : {});
} else if (attrName === Attribute.MUTED) {
this.muted = hasValue;
} else if (attrName === Attribute.AUTOPLAY) {
this.autoplay = hasValue;
} else if (attrName === Attribute.FLUID) {
this.fluid = hasValue;
} else if (attrName === Attribute.DEVICE_TYPE) {
toggleAttribute(this, Attribute.MOBILE, newValue === 'mobile');
toggleAttribute(this, Attribute.TV, newValue === 'tv');
this._ui.setAttribute(Attribute.DEVICE_TYPE, newValue);
} else if (attrName === Attribute.STREAM_TYPE) {
this.streamType = newValue;
} else if (attrName === Attribute.USER_IDLE_TIMEOUT) {
this.userIdleTimeout = Number(newValue);
} else if (attrName === Attribute.DVR_THRESHOLD) {
this.dvrThreshold = Number(newValue);
}
if (DefaultUI.observedAttributes.indexOf(attrName as Attribute) >= 0) {
shadyCss.styleSubtree(this);
}
}
private readonly _updateStreamType = () => {
this.setAttribute(Attribute.STREAM_TYPE, this.streamType);
if (this._timeRange) {
// Hide seekbar when stream is live with no DVR
toggleAttribute(this._timeRange, Attribute.HIDDEN, this.streamType === 'live');
}
};
private readonly _dispatchReadyEvent = () => {
this.dispatchEvent(createCustomEvent(READY_EVENT));
};
private readonly _onTitleSlotChange = () => {
if (this._titleSlot) {
toggleAttribute(this, Attribute.HAS_TITLE, this._titleSlot.assignedNodes().length > 0);
}
};
}
customElements.define('theoplayer-default-ui', DefaultUI);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-default-ui': DefaultUI;
}
}