-
Notifications
You must be signed in to change notification settings - Fork 4
/
p2p-media.js
138 lines (118 loc) · 3.25 KB
/
p2p-media.js
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
import { supportsP2P, resolveP2PUrl, isP2P } from './db.js'
function addStylesheet () {
if (!document.getElementById('p2p-media-styles')) {
const style = document.createElement('link')
style.id = 'p2p-media-styles'
style.rel = 'stylesheet'
style.href = './p2p-media.css'
document.head.appendChild(style)
}
}
class P2PImage extends HTMLElement {
constructor () {
super()
this.img = document.createElement('img')
addStylesheet()
}
static get observedAttributes () {
return ['src', 'class']
}
connectedCallback () {
this.attachImage()
}
attributeChangedCallback (name, oldValue, newValue) {
if (name === 'src' && newValue !== oldValue) {
this.attachImage(newValue)
} else if (name === 'class' && newValue !== oldValue) {
this.syncClass()
}
}
async attachImage (src) {
try {
src = src || this.getAttribute('src')
const p2pSupported = await supportsP2P(src)
if (p2pSupported) {
this.img.src = src
} else {
this.handleError(src)
}
} catch (error) {
this.handleError(src)
}
this.syncClass()
this.clearAndAppend(this.img)
}
syncClass () {
const classes = this.className.split(' ')
this.img.className = classes.join(' ')
}
handleError (src) {
const fallbackSrc = isP2P(src) ? resolveP2PUrl(src) : './assets/profile.png'
this.img.src = fallbackSrc
this.setAttribute('src', fallbackSrc)
this.dispatchEvent(new CustomEvent('load-error', { detail: { fallbackSrc } }))
}
clearAndAppend (element) {
this.innerHTML = ''
this.appendChild(element)
}
}
class P2PVideo extends HTMLElement {
constructor () {
super()
this.video = document.createElement('video')
this.video.controls = true
addStylesheet()
}
static get observedAttributes () {
return ['src', 'class']
}
connectedCallback () {
this.attachVideo()
}
attributeChangedCallback (name, oldValue, newValue) {
if (name === 'src' && newValue !== oldValue) {
this.attachVideo(newValue)
} else if (name === 'class' && newValue !== oldValue) {
this.syncClass()
}
}
async attachVideo (src) {
this.video.innerHTML = '' // Clear any existing sources
const source = document.createElement('source')
source.src = src || this.getAttribute('src')
try {
const p2pSupported = await supportsP2P(source.src)
if (!p2pSupported) {
this.handleError(source)
return
}
} catch (error) {
this.handleError(source)
return
}
source.onerror = () => this.handleError(source)
this.video.appendChild(source)
this.syncClass()
this.clearAndAppend(this.video)
}
syncClass () {
const classes = this.className.split(' ')
this.video.className = classes.join(' ')
}
handleError (source) {
const src = source.src
if (isP2P(src)) {
const fallbackSrc = resolveP2PUrl(src)
source.src = fallbackSrc
this.setAttribute('src', fallbackSrc)
this.dispatchEvent(new CustomEvent('load-error', { detail: { fallbackSrc } }))
}
}
clearAndAppend (element) {
this.innerHTML = ''
this.appendChild(element)
}
}
customElements.define('p2p-image', P2PImage)
customElements.define('p2p-video', P2PVideo)