-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeDisplay.ts
135 lines (115 loc) · 4.96 KB
/
TimeDisplay.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
import * as shadyCss from '@webcomponents/shadycss';
import textDisplayCss from './TextDisplay.css';
import { StateReceiverMixin } from './StateReceiverMixin';
import type { ChromelessPlayer } from 'theoplayer/chromeless';
import { setTextContent } from '../util/CommonUtils';
import { formatAsTimePhrase, formatTime } from '../util/TimeUtils';
import { Attribute } from '../util/Attribute';
import type { StreamType } from '../util/StreamType';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate('theoplayer-time-display', `<style>${textDisplayCss}</style><span></span>`);
const PLAYER_EVENTS = ['timeupdate', 'seeking', 'seeked', 'durationchange'] as const;
const DEFAULT_MISSING_TIME_PHRASE = 'video not loaded, unknown time';
/**
* `<theoplayer-time-display>` - A control that displays the current time of the stream.
*
* @attribute `show-duration` - If set, also shows the duration of the stream.
* @attribute `remaining` - If set, shows the remaining time of the stream. Not compatible with `show-duration`.
* @attribute `remaining-when-live` - If set, and the stream is a livestream, shows the remaining time
* (until the live point) of the stream.
* @group Components
*/
export class TimeDisplay extends StateReceiverMixin(HTMLElement, ['player', 'streamType']) {
private readonly _spanEl: HTMLElement;
private _player: ChromelessPlayer | undefined;
static get observedAttributes() {
return [Attribute.REMAINING, Attribute.REMAINING_WHEN_LIVE, Attribute.SHOW_DURATION, Attribute.STREAM_TYPE];
}
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.appendChild(template().content.cloneNode(true));
this._spanEl = shadowRoot.querySelector('span')!;
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.hasAttribute(Attribute.ARIA_LIVE)) {
// Tell screen readers not to automatically read the time as it changes
this.setAttribute(Attribute.ARIA_LIVE, 'off');
}
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);
}
}
get streamType(): StreamType {
return (this.getAttribute(Attribute.STREAM_TYPE) || 'vod') as StreamType;
}
set streamType(streamType: StreamType) {
this.setAttribute(Attribute.STREAM_TYPE, streamType);
}
private readonly _updateFromPlayer = () => {
const currentTime = this._player ? this._player.currentTime : 0;
const duration = this._player ? this._player.duration : NaN;
const seekable = this._player?.seekable;
const endTime = isFinite(duration) ? duration : seekable && seekable.length > 0 ? seekable.end(0) : NaN;
const remaining = this.hasAttribute(Attribute.REMAINING) || (this.hasAttribute(Attribute.REMAINING_WHEN_LIVE) && this.streamType !== 'vod');
let time = currentTime;
if (remaining) {
time = -((endTime || 0) - currentTime);
}
const showDuration = this.hasAttribute(Attribute.SHOW_DURATION);
let text: string;
if (showDuration && !remaining) {
text = `${formatTime(time, endTime, remaining)} / ${formatTime(endTime)}`;
} else {
text = formatTime(time, endTime, remaining);
}
let ariaValueText: string;
if (isNaN(duration)) {
ariaValueText = DEFAULT_MISSING_TIME_PHRASE;
} else if (showDuration) {
ariaValueText = `${formatAsTimePhrase(time, remaining)} of ${formatAsTimePhrase(endTime)}`;
} else {
ariaValueText = formatAsTimePhrase(time, remaining);
}
setTextContent(this._spanEl, text);
this.setAttribute('aria-valuetext', ariaValueText);
};
attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
if (newValue === oldValue) {
return;
}
if (TimeDisplay.observedAttributes.indexOf(attrName as Attribute) >= 0) {
this._updateFromPlayer();
shadyCss.styleSubtree(this);
}
}
}
customElements.define('theoplayer-time-display', TimeDisplay);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-time-display': TimeDisplay;
}
}