padmaEmbeds/example/example.js

221 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

2018-03-09 16:16:02 +00:00
var slides = [],
slideData = [],
current = 0,
activeAudio,
2018-03-09 16:16:02 +00:00
timeout;
init()
function init() {
document.querySelectorAll('.slide').forEach(function(slide) {
slides.push(slide)
2018-03-09 16:41:27 +00:00
var data = slideData[slides.length - 1] = load_slide(slide);
data.idx = slides.length - 1;
2018-03-09 16:16:02 +00:00
if (data.video && data.video.length) {
data.embed = init_embed(data)
}
2018-03-16 12:55:14 +00:00
if (data.audio) {
data.audioEmbed = init_audio_embed(data)
}
2018-03-09 16:16:02 +00:00
})
go(0)
}
function go(idx) {
var old = current
slides[current].style.zIndex = 0
slides[idx].style.zIndex = 10
current = idx
if (timeout) {
clearTimeout(timeout)
timeout = null
}
2018-03-09 16:41:27 +00:00
timeout = setTimeout(next, Math.round(slides[current].dataset.duration) * 1000)
2018-03-09 16:17:37 +00:00
2018-03-09 16:16:02 +00:00
stop(old)
start(current)
}
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 start(idx) {
var data = slideData[idx]
console.log('start', current, data)
if (data.zooms) {
2018-03-09 16:16:02 +00:00
start_zoom(data)
} else if (data.video && data.video.length) {
start_video(data)
}
if (data.audioContainer) { // if there is an audio container, we always stop current audio
reset_active_audio();
}
2018-03-16 12:55:14 +00:00
if (data.audio) {
start_audio(data)
}
2018-03-09 16:16:02 +00:00
}
function stop(idx) {
var data = slideData[idx]
console.log(current, data)
2018-03-14 17:51:14 +00:00
if (data.zooms) {
2018-03-09 16:16:02 +00:00
reset_zoom(data)
} else if (data.video && data.video.length) {
2018-03-09 16:41:27 +00:00
reset_video(data)
2018-03-09 16:16:02 +00:00
}
if (data.audio && !data.audioContinue) {
2018-03-16 12:55:14 +00:00
reset_audio(data)
}
2018-03-09 16:16:02 +00:00
}
function load_urls(dataset) {
var urls = [], idx = 0
while (dataset['url_' + idx]) {
urls.push(dataset['url_' + idx])
idx += 1
}
return urls
}
function load_slide(slide) {
var data = {}
data.duration = slide.dataset.duration
var video = slide.querySelector('.video')
if (video) {
data.video = load_urls(video.dataset)
data.videoVolume = video.dataset.volume ? parseFloat(video.dataset.volume) : 1
2018-03-09 16:16:02 +00:00
data.container = video
// assumes documents if length > 1
if (data.video.length > 1) {
data.zooms = data.video.map(url => url.split('/').pop().split('#')[0].split(',').map(a => Math.round(a)))
2018-03-09 16:16:02 +00:00
}
}
2018-03-16 12:55:14 +00:00
var audio = slide.querySelector('.audio')
if (audio) {
data.audio = audio.dataset.url // audio does not need to be an array
data.audioVolume = audio.dataset.volume ? parseFloat(audio.dataset.volume) : 1
2018-03-16 12:55:14 +00:00
data.audioContainer = audio
console.log('continue', audio.dataset.continue)
data.audioContinue = !!audio.dataset.continue
2018-03-16 12:55:14 +00:00
}
2018-03-09 16:16:02 +00:00
return data
}
function init_embed(data) {
var embed = window.currentEmbed = new PandoraEmbed({
2018-03-09 16:41:27 +00:00
id: 'slide-' + data.idx,
2018-03-09 16:16:02 +00:00
url: data.video[0],
container: data.container
});
embed.on('init', function(data) {
2018-03-09 16:41:27 +00:00
});
embed.on('playing', function(positionData) {
if (!slideData[data.idx].start) {
slideData[data.idx].start = positionData.position;
}
2018-03-09 16:16:02 +00:00
});
2018-03-09 16:41:27 +00:00
2018-03-09 16:16:02 +00:00
return embed;
}
2018-03-16 12:55:14 +00:00
function init_audio_embed(data) {
return new PandoraEmbed({
id: 'slide-audio-' + data.idx,
url: data.audio,
container: data.audioContainer
})
}
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_zoom(data) {
2018-03-14 17:51:14 +00:00
data.zoomTimeouts.forEach(timeout => clearTimeout(timeout))
2018-03-09 16:16:02 +00:00
data.embed.postMessage('options', {
2018-03-14 17:51:14 +00:00
'area': data.zooms[0]
2018-03-09 16:16:02 +00:00
})
}
function start_zoom(data) {
// console.log('start zoon', Math.round(1000 * data.duration / 2))
for (var i=1; i<data.zooms.length; i++) {
2018-03-14 17:51:14 +00:00
data.zoomTimeouts = [];
(function(j) {
2018-03-14 17:51:14 +00:00
data.zoomTimeouts.push(setTimeout(function() {
data.embed.postMessage('options', {
'area': data.zooms[j]
})
2018-03-14 17:51:14 +00:00
}, Math.round(1000 * (data.duration / data.zooms.length) * j)))
})(i)
}
2018-03-09 16:16:02 +00:00
}
function start_video(data) {
data.embed.postMessage('options', {
'paused': false,
'volume': data.videoVolume
2018-03-09 16:16:02 +00:00
})
}
2018-03-16 12:55:14 +00:00
function start_audio(data) {
data.audioEmbed.postMessage('options', {
'paused': false,
'volume': data.audioVolume
2018-03-16 12:55:14 +00:00
})
activeAudio = data
2018-03-16 12:55:14 +00:00
}
2018-03-09 16:41:27 +00:00
function reset_video(data) {
2018-03-09 16:16:02 +00:00
data.embed.postMessage('options', {
2018-03-09 16:41:27 +00:00
'paused': true,
'position': data.start || 0
2018-03-09 16:16:02 +00:00
})
}
2018-03-16 12:55:14 +00:00
function reset_audio(data) {
data.audioEmbed.postMessage('options', {
'paused': true,
'position': 0 //FIXME: figure correct audio reset
})
activeAudio = null;
}
function reset_active_audio() {
if (activeAudio) {
reset_audio(activeAudio)
}
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()
}
}, false)