-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.js
209 lines (186 loc) · 5.24 KB
/
control.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { addListener, removeListener, loadXMLDocument, nodeIsInDocument } from './compatability.js'
import { UIInterface } from './UIInterface.js'
import { Slideshow } from './Slideshow.js'
const hasParam = (name) => (
!!window.location.search.substring(1).split(/&/)
.find((q) => (q === name))
)
export const slideshow = new Slideshow()
export let ui
export let audioPlayer
export let slider
export let timeout = 50 // timeout between updates in milliseconds
export const debug = hasParam('debug')
export const verbose = hasParam('verbose')
export async function setup(
slideshowConfig, containerName, sliderName, playerName,
) {
slider = setupSlider(sliderName)
ui = new UIInterface(setupContainer(containerName))
audioPlayer = setupPlayer(playerName)
addListener(slideshow, 'configure', showConfigured, true)
const config = await loadXMLDocument(slideshowConfig)
await slideshow.load(config, ui)
const audioSrc = document.createElement('source')
audioSrc.src = slideshow.backgroundMusic
audioPlayer.appendChild(audioSrc)
}
function showConfigured() {
if(debug) {
console.debug({
'Slideshow Configured': audioPlayer
})
}
if(audioPlayer == null || audioPlayer.loaded) {
resetShow()
}
}
function songLoadedCallback(filename) {
if(slideshow.configured) {
resetShow()
}
}
function resetShow() {
ui.container.appendChild(ui.startLink)
audioPlayer?.pause()
slideshow.reset()
}
function startShow() {
const { startLink } = ui
if(nodeIsInDocument(startLink)) {
startLink.parentNode.removeChild(startLink)
}
slideshow.paused = false
if(audioPlayer) {
audioPlayer.play()
if(
typeof(slideshow.currentTime) !== 'number'
|| isNaN(slideshow.currentTime)
) {
console.warn({
'Slideshow Time': slideshow.currentTime,
})
} else {
audioPlayer.currentTime = slideshow.currentTime / 1000
}
}
step()
}
function stopShow() {
audioPlayer?.pause()
slideshow.stop()
}
function seekToTime(time) {
const barStart = 0
const barLength = slider.parentElement.clientHeight
slideshow.currentTime = time
const { presentationTime: total } = slideshow
const barSize = barLength - slider.clientHeight / 2
const scaledBarSize = barSize * time / total
slider.style.top = (
`${barStart + Math.round(scaledBarSize)}px`
)
}
function step() {
if(slideshow.playing) {
const { currentTime } = slideshow
seekToTime(currentTime)
if(currentTime + timeout < slideshow.presentationTime) {
let interval = timeout
if(
slideshow.stopIndex != null
&& slideshow.stopIndex < slideshow.events.length - 1
) {
const { startTime: nextStart } = (
slideshow.events[slideshow.stopIndex + 1]
)
interval = Math.min(
nextStart - currentTime, interval
)
}
setTimeout(step, interval)
}
}
}
function setupContainer(containerName) {
const container = document.getElementById(containerName)
if(!container) throw new Error('Container not found.')
return container
}
function setupSlider(sliderName) {
const slider = document.getElementById(sliderName)
if(!slider) {
throw new Error(`Slider "${sliderName}" not found.`)
}
addListener(slider.parentNode, 'click', slideClicked, true)
addListener(slider, 'mousedown', sliderSelected, true)
addListener(slider, 'click', sliderClicked, true)
return slider
}
function slideClicked(event) {
sliderMouseAt(event.clientY)
}
function sliderClicked() {
slideshow.togglePlaying()
}
function sliderSelected(event) {
slider.classList.add('active')
addListener(document, 'mousemove', sliderDrag, true)
addListener(document, 'mouseup', sliderRelease, true)
const { startLink } = ui
if(nodeIsInDocument(startLink)) {
ui.container.removeChild(startLink)
}
stopShow()
}
function sliderDrag(event) {
sliderMouseAt(event.clientY)
}
function sliderMouseAt(yPosition) {
const barStart = 0
const barLength = slider.parentElement.clientHeight
const { presentationTime: total } = slideshow
if(isNaN(total) || total < 0) {
throw new Error(`Invalid \`presentationTime\`: ${total}`)
}
const sliderSize = slider.offsetHeight
const center = Math.round(sliderSize / 2)
const position = yPosition - center
if(
position >= barStart // after the start
&& position <= barLength - sliderSize // before the end
) {
slider.style.top = `${position}px`
const time = Math.round(total * position / barLength)
if(debug && verbose) {
console.debug({
'Seeking': {
yPosition, time, total,
slider: {
click: event.clientY, size: sliderSize,
center, position,
},
bar: {
start: barStart, length: barLength,
},
},
})
}
slideshow.currentTime = time
if(audioPlayer) {
audioPlayer.currentTime = time / 1000
}
}
}
function sliderRelease() {
slider.classList.remove('active')
removeListener(document, 'mousemove', sliderDrag, true)
removeListener(document, 'mouseup', sliderRelease, true)
}
function setupPlayer(playerName) {
const player = document.getElementById(playerName)
player.addEventListener('timeupdate', () => {
slideshow.currentTime = player.currentTime * 1000
})
return player
}