padmaEmbeds/example/example.js

406 lines
9.9 KiB
JavaScript
Raw Normal View History

2018-03-17 09:12:14 +00:00
class Slide {
constructor(slide, idx) {
this.isAudioLoaded = this.isVideoLoaded = false
2018-03-17 09:12:14 +00:00
this.el = slide
this.idx = idx
this.duration = slide.dataset.duration
this.durationMs = parseInt(this.duration) * 1000
2018-03-17 09:12:14 +00:00
const video = slide.querySelector('.video')
if (video) {
this.video = load_urls(video.dataset)
2018-03-17 12:51:39 +00:00
if (this.video.length > 1 || this.video[0].includes('document')) {
this.isDocument = true
}
2018-03-17 09:12:14 +00:00
this.videoVolume = video.dataset.volume ? parseFloat(video.dataset.volume) : 1
this.videoContainer = video
if (this.video.length > 1) {
this.zooms = this.video.map(url => url.split('/').pop().split('#')[0].split(',').map(a => Math.round(a)))
}
}
const audio = slide.querySelector('.audio')
if (audio) {
this.audio = audio.dataset.url // audio does not need to be an array
this.audioVolume = audio.dataset.volume ? parseFloat(audio.dataset.volume) : 1
this.audioContainer = audio
this.audioContinue = !!audio.dataset.continue
}
this.initEmbeds()
return this
}
initEmbeds() {
if (this.video) {
const videoEmbed = this.videoEmbed = new PandoraEmbed({
id: 'slide-' + this.idx,
url: this.video[0],
container: this.videoContainer
})
2018-03-17 12:51:39 +00:00
videoEmbed.on('init', () => {
if (this.isDocument) {
this.isVideoLoaded = true
}
})
videoEmbed.on('loaded', () => {
2018-03-17 12:51:39 +00:00
console.log('loaded called on embed')
this.isVideoLoaded = true
2018-03-17 09:12:14 +00:00
})
videoEmbed.on('playing', (positionData) => {
if (!this.videoStart) {
this.videoStart = positionData.position
}
})
}
if (this.audio) {
const audioEmbed = this.audioEmbed = new PandoraEmbed({
id: 'audio-' + this.idx,
url: this.audio,
container: this.audioContainer
})
audioEmbed.on('loaded', () => {
2018-03-17 12:51:39 +00:00
console.log('loaded called on audio embed')
this.isAudioLoaded = true
2018-03-17 09:12:14 +00:00
})
audioEmbed.on('playing', (positionData) => {
if (!this.audioStart) {
this.audioStart = positionData.position
}
})
}
return this;
}
isReady() {
console.log('isReady called');
if (this.video && this.audio) {
return this.isVideoLoaded && this.isAudioLoaded
} else if (this.video) {
return this.isVideoLoaded
} else if (this.audio) {
return this.isAudioLoaded
} else {
return true
}
}
2018-03-17 09:12:14 +00:00
sendToBack() {
this.el.style.zIndex = 0
return this
}
bringToFront() {
this.el.style.zIndex = 10
return this
}
start() {
console.log('called start', this.isReady())
this.timeout = setTimeout(next, this.durationMs)
this.startTime = new Date()
2018-03-17 09:12:14 +00:00
if (this.zooms) {
this.startZoom()
} else if (this.video && this.video.length) {
this.startVideo()
}
if (this.audioContainer) { // if there is an audio container, we always stop current audio
reset_active_audio()
}
if (this.audio) {
this.startAudio()
}
return this
}
stop() {
if (this.zooms) {
this.resetZoom()
} else if (this.video && this.video.length) {
this.resetVideo()
}
if (this.audio && !this.audioContinue) {
this.resetAudio()
}
return this
}
pause() {
clearTimeout(this.timeout)
this.pauseTime = new Date()
if (this.zooms) {
this.pauseZoom()
} else if (this.video && this.video.length) {
this.pauseVideo()
}
if (this.audio) {
this.pauseAudio()
}
}
resume() {
const offset = this.pauseTime - this.startTime
const timeout = this.durationMs - offset
this.timeout = setTimeout(next, timeout)
// "Fake" the start time in case the user pauses and resumes again.
// NOTE: This seems like a bit of a hack, but the other way seemed convoluted
this.startTime = new Date() - offset
if (this.zooms) {
this.resumeZoom()
} else if (this.video && this.video.length) {
this.resumeVideo()
}
if (this.audio) {
this.resumeAudio()
}
}
startZoom(offset) {
offset = offset || 0
2018-03-17 09:12:14 +00:00
for (var i=1; i<this.zooms.length; i++) {
this.zoomTimeouts = [];
((j) => {
const timeout = Math.round(1000 * (this.duration / this.zooms.length) * j) - offset
if (timeout < 0) return
2018-03-17 09:12:14 +00:00
this.zoomTimeouts.push(setTimeout(() => {
this.videoEmbed.postMessage('options', {
'area': this.zooms[j]
})
}, timeout))
2018-03-17 09:12:14 +00:00
})(i);
}
return this
}
startVideo() {
this.videoEmbed.postMessage('options', {
'paused': false,
'volume': this.videoVolume
})
return this
}
startAudio() {
this.audioEmbed.postMessage('options', {
'paused': false,
'volume': this.audioVolume
})
activeAudio = this
return this
}
pauseZoom() {
this.zoomTimeouts.forEach(clearTimeout)
return this
}
pauseVideo() {
this.videoEmbed.postMessage('options', {
'paused': true
})
return this
}
pauseAudio() {
this.audioEmbed.postMessage('options', {
'paused': true
})
return this
}
resumeZoom() {
const offset = this.pauseTime - this.startTime
this.startZoom(offset)
return this
}
resumeVideo() {
this.videoEmbed.postMessage('options', {
'paused': false
})
return this
}
resumeAudio() {
this.audioEmbed.postMessage('options', {
'paused': false
})
return this
}
2018-03-17 09:12:14 +00:00
resetZoom() {
this.zoomTimeouts.forEach(clearTimeout)
2018-03-17 09:12:14 +00:00
this.videoEmbed.postMessage('options', {
'area': this.zooms[0]
})
return this
}
resetVideo() {
this.videoEmbed.postMessage('options', {
'paused': true,
'position': this.start || 0
})
return this
}
resetAudio() {
this.audioEmbed.postMessage('options', {
'paused': true,
'position': 0 //FIXME: figure correct audio reset
})
activeAudio = null;
return this
}
}
2018-03-09 16:16:02 +00:00
var slides = [],
current = 0,
activeAudio,
2018-03-09 16:16:02 +00:00
timeout;
let globalIsPaused = false
let textbUrl = new URLSearchParams(window.location.search).get('textb') || 'https://textb.org/r/housingplaylist2/'
// use raw version of page
textbUrl = textbUrl.replace('/t/', '/r/')
if (!textbUrl.endsWith('/')) textbUrl += '/'
fetch(textbUrl)
.then(response => response.text())
.then(loadYaml)
.then(init)
.catch(err => {
console.log('error', err)
alert('error loading YAML')
})
function loadYaml(txt) {
const html = txt.split('\n\n')
.filter(chunk => chunk.trim())
.map(chunk => {
const obj = jsyaml.load(chunk)
return obj
})
.map(jsonToHTML)
.join('')
document.body.innerHTML = `
<div class="base"></div>
<div id="overlay"></div>
${html}
`
return true
}
2018-03-09 16:16:02 +00:00
function globalPause() {
if (globalIsPaused) return;
slides[current].pause()
globalIsPaused = true
}
function globalResume() {
if (!globalIsPaused) return;
slides[current].resume()
globalIsPaused = false
}
function togglePause() {
if (globalIsPaused) {
globalResume()
} else {
globalPause()
}
}
2018-03-09 16:16:02 +00:00
function init() {
2018-03-17 09:12:14 +00:00
document.querySelectorAll('.slide').forEach(function(slide, idx) {
slides.push(new Slide(slide, idx))
2018-03-09 16:16:02 +00:00
})
go(0)
}
function go(idx) {
if (!slides[idx].isReady()) {
console.log('slide not ready');
return setTimeout(() => {
go(idx)
}, 250)
}
2018-03-09 16:16:02 +00:00
var old = current
2018-03-17 09:12:14 +00:00
slides[current].sendToBack()
slides[idx].bringToFront()
2018-03-09 16:16:02 +00:00
current = idx
if (timeout) {
clearTimeout(timeout)
timeout = null
}
// timeout = setTimeout(next, Math.round(slides[current].duration) * 1000)
2018-03-09 16:17:37 +00:00
2018-03-17 09:12:14 +00:00
slides[old].stop()
slides[current].start()
2018-03-09 16:16:02 +00:00
}
function next() {
var idx = (current + 1) % slides.length
console.log(current, idx)
go(idx)
}
function previous() {
var idx = (current - 1) % slides.length
if (idx < 0) {
idx += slides.length
}
go(idx)
}
function load_urls(dataset) {
var urls = [], idx = 0
while (dataset['url_' + idx]) {
urls.push(dataset['url_' + idx])
idx += 1
}
return urls
}
2018-03-16 12:55:14 +00:00
2018-03-09 16:16:02 +00:00
window.addEventListener('blur', function(){
setTimeout(function(){
// using the 'setTimout' to let the event pass the run loop
if (document.activeElement instanceof HTMLIFrameElement) {
window.focus();
}
},0);
}, false);
function reset_active_audio() {
if (activeAudio) {
2018-03-17 09:12:14 +00:00
activeAudio.resetAudio()
}
2018-03-16 12:55:14 +00:00
}
2018-03-09 16:16:02 +00:00
document.addEventListener('keydown', function(event) {
if (event.key == 'ArrowRight') {
next()
event.preventDefault()
} else if (event.key == 'ArrowLeft') {
previous()
event.preventDefault()
} else if (event.keyCode === 32) { // spacebar
togglePause()
2018-03-09 16:16:02 +00:00
}
}, false)