var cache = cache || {} var layer = 'descriptions' var baseURL = 'https://pad.ma' var imageResolution = 480 let lastKnownScrollPosition = 0 let ticking = false; async function pandoraAPI(action, data) { var url = baseURL + '/api/' //var url = '/pandoraAPI/' var key = JSON.stringify([action, data]) if (!cache[key]) { var response = await fetch(url, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ action: action, data: data }) }) cache[key] = await response.json() } return cache[key] } function updatePlayer(video, frame, currentTime, out, src) { var rect = frame.getBoundingClientRect(); video.style.opacity = 0 console.log('update player', rect) video.style.top = (rect.top + window.scrollY) + 'px' video.style.display = 'block'; if (src) { video.src = src } //video.poster = frame.querySelector('img').src video.currentTime = currentTime video.dataset.in = currentTime video.dataset.out = out video.controls = true video.play() video.style.opacity = 1 } function isElementInViewport (el) { var rect = el.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /* or $(window).height() */ rect.right <= (window.innerWidth || document.documentElement.clientWidth) /* or $(window).width() */ ); } function onVisibilityChange(el, callback) { var old_visible; return function () { var visible = isElementInViewport(el); if (visible != old_visible) { old_visible = visible; if (typeof callback == 'function') { callback(visible); } } } } function timeupdate(event) { if (event.target.dataset.out && event.target.dataset.in) { var in_= parseFloat(event.target.dataset.in) var out_= parseFloat(event.target.dataset.out) if (event.target.currentTime >= out_) { event.target.currentTime = in_ } } } function loadItem(config) { pandoraAPI('get', {id: config.item, keys: ['id', 'title', 'layers']}).then(response => { var ascroll = document.querySelector('#ascroll') var loaded = false var video = document.createElement('video') video.classList.add('player') video.muted = true video.src = `${baseURL}/${config.item}/480p.webm` video.addEventListener('timeupdate', timeupdate) ascroll.appendChild(video) var h1 = document.createElement('h1') var first h1.innerHTML = response.data.title ascroll.appendChild(h1) response.data.layers[layer].forEach(annotation => { if (config.user && annotation.user != config.user) { return } if (!first) { first = annotation } var div = document.createElement('div') div.classList.add('annotation') div.innerHTML = `