-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadingIndicator.ts
85 lines (70 loc) · 2.87 KB
/
LoadingIndicator.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
import * as shadyCss from '@webcomponents/shadycss';
import loadingIndicatorCss from './LoadingIndicator.css';
import loadingIndicatorHtml from './LoadingIndicator.html';
import { StateReceiverMixin } from './StateReceiverMixin';
import type { ChromelessPlayer } from 'theoplayer/chromeless';
import { Attribute } from '../util/Attribute';
import { toggleAttribute } from '../util/CommonUtils';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate('theoplayer-loading-indicator', `<style>${loadingIndicatorCss}</style>${loadingIndicatorHtml}`);
const PLAYER_EVENTS = ['readystatechange', 'play', 'pause', 'playing', 'seeking', 'seeked'] as const;
/**
* `<theoplayer-loading-indicator>` - An indicator that shows whether the player is currently waiting for more data to resume playback.
*
* @attribute `loading` (readonly) - Whether the player is waiting for more data. If set, the indicator is shown.
* @group Components
*/
export class LoadingIndicator extends StateReceiverMixin(HTMLElement, ['player']) {
private _player: ChromelessPlayer | undefined;
static get observedAttributes() {
return [Attribute.LOADING];
}
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template().content.cloneNode(true));
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);
this._updateFromPlayer();
}
get player(): ChromelessPlayer | undefined {
return this._player;
}
set player(player: ChromelessPlayer | undefined) {
if (this._player === player) {
return;
}
if (this._player !== undefined) {
this._player.removeEventListener(PLAYER_EVENTS, this._updateFromPlayer);
}
this._player = player;
this._updateFromPlayer();
if (this._player !== undefined) {
this._player.addEventListener(PLAYER_EVENTS, this._updateFromPlayer);
}
}
private readonly _updateFromPlayer = () => {
const loading = this._player !== undefined && !this._player.paused && (this._player.seeking || this._player.readyState < 3);
toggleAttribute(this, Attribute.LOADING, loading);
};
attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
if (LoadingIndicator.observedAttributes.indexOf(attrName as Attribute) >= 0) {
shadyCss.styleSubtree(this);
}
}
}
customElements.define('theoplayer-loading-indicator', LoadingIndicator);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-loading-indicator': LoadingIndicator;
}
}