-
Notifications
You must be signed in to change notification settings - Fork 4
/
player.js
153 lines (130 loc) · 3.8 KB
/
player.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var elemVideo;
var elemPlaylistItems;
var elemProgress;
var elemStart;
var elemLoader;
var btnPlay;
var btnPrev;
var btnNext;
var btnFullscreen;
var btnReset;
var currentVideo;
reset = (evt) => {
evt.preventDefault()
if (currentVideo) {
currentVideo.classList.remove('playing')
}
elemVideo.pause();
elemVideo.innerHTML = ""
elemVideo.load();
elemStart.textContent = "0:00"
}
selectVideo = (evt) => {
const el = evt.target.tagName.toLowerCase() == 'tr'
? evt.target
: evt.target.parentNode
// handle 'playing'
if (currentVideo) {
currentVideo.classList.remove('playing')
}
currentVideo = el
currentVideo.classList.add('playing')
// update src
elemVideo.innerHTML = '<source src="https://siasky.net/' + el.dataset.skylink + '" type="video/mp4">'
elemVideo.load();
btnPlay.click()
// set spinner until we've reloaded
elemLoader.classList.add('spinner')
}
onCanPlayVideo = () => {
elemLoader.classList.remove('spinner')
}
onTimeUpdateVideo = () => {
var minutes = Math.floor(elemVideo.currentTime / 60),
seconds = Math.floor(elemVideo.currentTime - minutes * 60),
x = minutes < 10 ? "0" + minutes : minutes,
y = seconds < 10 ? "0" + seconds : seconds;
elemStart.textContent = x + " : " + y;
}
onClickVideo = (evt) => {
evt.preventDefault();
if (elemVideo.paused || elemVideo.ended) {
elemVideo.play();
return;
}
elemVideo.pause();
}
onClickPlay = (evt) => {
evt.preventDefault();
if (elemVideo.paused || elemVideo.ended) {
btnPlay.childNodes[0].setAttribute("class", "icon pause");
elemVideo.play();
return
}
elemVideo.pause();
btnPlay.childNodes[0].setAttribute("class", "icon play");
}
onClickPrev = (evt) => {
evt.preventDefault();
let prev = -1;
for (let i = elemPlaylistItems.childNodes.length - 1; i > 0; i--) {
if (currentVideo == elemPlaylistItems.childNodes[i]) {
prev = i - 1;
}
}
if (prev != -1) {
elemPlaylistItems.childNodes[prev].click()
}
}
onClickNext = (evt) => {
evt.preventDefault();
let next = -1;
for (let i = 0; i < elemPlaylistItems.childNodes.length - 1; i++) {
if (currentVideo == elemPlaylistItems.childNodes[i]) {
next = i + 1;
}
}
if (next != -1) {
elemPlaylistItems.childNodes[next].click()
}
}
onClickFullscreen = (evt) => {
evt.preventDefault();
const functions = [
'requestFullscreen',
'webkitRequestFullscreen',
'mozRequestFullScreen',
'msRequestFullscreen',
]
for (const f of functions) {
if (elemVideo[f]) {
elemVideo[f]()
break;
}
}
}
window.addEventListener('DOMContentLoaded', () => {
elemVideo = document.getElementById('video')
elemPlaylistItems = document.getElementById('playlist-items')
for (const el of elemPlaylistItems.childNodes) {
el.onclick = selectVideo
}
elemPlaylistTitle = document.getElementById('title');
elemProgress = document.getElementById('progress');
elemStart = document.getElementById('start');
elemLoader = document.getElementById('loader');
btnPlay = document.getElementById("btn-play")
btnPrev = document.getElementById("btn-prev")
btnNext = document.getElementById("btn-next")
btnFullscreen = document.getElementById("btn-full")
btnReset = document.getElementById("btn-reset")
elemVideo.oncanplay = onCanPlayVideo
elemVideo.ontimeupdate = onTimeUpdateVideo
elemVideo.onclick = onClickVideo
elemVideo.onended = reset
btnPlay.onclick = onClickPlay
btnPrev.onclick = onClickPrev
btnNext.onclick = onClickNext
btnFullscreen.onclick = onClickFullscreen
btnReset.onclick = reset
});