-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorDisplay.ts
92 lines (76 loc) · 2.82 KB
/
ErrorDisplay.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
import * as shadyCss from '@webcomponents/shadycss';
import errorDisplayCss from './ErrorDisplay.css';
import errorIcon from '../icons/error.svg';
import { StateReceiverMixin } from './StateReceiverMixin';
import type { THEOplayerError } from 'theoplayer/chromeless';
import { setTextContent, toggleAttribute } from '../util/CommonUtils';
import { Attribute } from '../util/Attribute';
import { createTemplate } from '../util/TemplateUtils';
const template = createTemplate(
'theoplayer-error-display',
`<style>${errorDisplayCss}</style>` +
`<div part="icon"><slot name="icon">${errorIcon}</slot></div>` +
`<div part="text">` +
`<h1 part="heading"><slot name="heading">An error occurred</slot></h1>` +
`<p part="message"><slot name="message"></slot></p>` +
`</div>` +
`<div part="fullscreen-controls">` +
`<slot name="fullscreen-controls"><theoplayer-fullscreen-button></theoplayer-fullscreen-button></slot>` +
`</div>`
);
/**
* `<theoplayer-error-display>` - A screen that shows the details of a fatal player error.
*
* @group Components
*/
export class ErrorDisplay extends StateReceiverMixin(HTMLElement, ['error', 'fullscreen']) {
private readonly _messageSlot: HTMLSlotElement;
private _error: THEOplayerError | undefined;
static get observedAttributes() {
return [Attribute.FULLSCREEN];
}
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open', delegatesFocus: true });
shadowRoot.appendChild(template().content.cloneNode(true));
this._messageSlot = shadowRoot.querySelector('slot[name="message"]')!;
this._upgradeProperty('error');
}
protected _upgradeProperty(prop: keyof this) {
if (this.hasOwnProperty(prop)) {
let value = this[prop];
delete this[prop];
this[prop] = value;
}
}
connectedCallback(): void {
shadyCss.styleElement(this);
}
/**
* The error.
*/
get error(): THEOplayerError | undefined {
return this._error;
}
set error(error: THEOplayerError | undefined) {
this._error = error;
setTextContent(this._messageSlot, error ? error.message : '');
}
get fullscreen(): boolean {
return this.hasAttribute(Attribute.FULLSCREEN);
}
set fullscreen(fullscreen: boolean) {
toggleAttribute(this, Attribute.FULLSCREEN, fullscreen);
}
attributeChangedCallback(attrName: string, oldValue: any, newValue: any) {
if (ErrorDisplay.observedAttributes.indexOf(attrName as Attribute) >= 0) {
shadyCss.styleSubtree(this);
}
}
}
customElements.define('theoplayer-error-display', ErrorDisplay);
declare global {
interface HTMLElementTagNameMap {
'theoplayer-error-display': ErrorDisplay;
}
}