formations/cesi/nosql/Slides/slides.js

149 lines
2.9 KiB
JavaScript

var slides;
var current_slide = 0;
var current_slide_items;
var current_item = 0;
var slide_window;
var clock;
var timer;
var timer_interval;
var slideCounter;
var planned_duration = 30;
function changeSlide(i, hide_items) {
if (current_slide + i < 0 || current_slide + i >= slides.length)
{
return true;
}
if (slide_window)
{
slide_window.window.changeSlide(i, hide_items);
}
slides[current_slide].classList.remove('current');
current_slide += i;
// Change URL
window.location.hash = "#" + current_slide;
// Get all items in a list
current_slide_items = slides[current_slide].getElementsByTagName('li');
current_item = 0;
for (var i = 0; i < current_slide_items.length; i++)
{
if (hide_items)
{
current_slide_items[i].classList.remove('shown');
}
else
{
current_slide_items[i].classList.add('shown');
}
}
slides[current_slide].classList.add('current');
slideCounter.innerHTML = current_slide + 1;
return true;
}
function progressSlide() {
if (current_item + 1 > current_slide_items.length || current_slide_items[current_item].classList.contains('shown'))
{
return changeSlide(1, true);
}
current_slide_items[current_item++].classList.add('shown');
if (slide_window)
{
slide_window.window.progressSlide();
}
return true;
}
function addTime() {
if (!clock)
{
clock = document.createElement('div');
clock.className = 'clock';
document.body.appendChild(clock);
}
function zerofill(v) {
return ("0" + v).slice(-2);
}
timer = 0;
window.clearInterval(timer_interval);
timer_interval = window.setInterval(function () {
timer += 1;
clock.style.width = Math.round(timer / (planned_duration * 60) * 100) + '%';
}, 1000);
}
function initSlides () {
slides = document.getElementsByTagName('section');
var current = window.location.hash.substr(1);
if (current > 0)
{
current_slide = parseInt(current, 10);
}
slides[current_slide].classList.add('current');
current_slide_items = slides[current_slide].getElementsByTagName('li');
slideCounter = document.createElement('div');
slideCounter.className = 'counter';
document.body.appendChild(slideCounter);
document.onkeydown = function (e) {
var prevent = false;
if (e.key == " ")
{
prevent = progressSlide();
}
else if (e.key == "ArrowRight" || e.key == "ArrowDown" || e.key == "PageDown")
{
prevent = changeSlide(1, false);
}
else if (e.key == "ArrowLeft" || e.key == "ArrowUp" || e.key == "PageUp")
{
prevent = changeSlide(-1, false);
}
else if (e.key == "Home")
{
prevent = changeSlide(-(current_slide), false);
}
else if (e.key == "End")
{
prevent = changeSlide(slides.length - 1, false);
}
else if (e.key == "o")
{
slide_window = window.open(document.location.href);
document.body.className = "notes";
prevent = true;
}
else if (e.key == "s")
{
addTime();
}
if (prevent)
{
e.preventDefault();
return false;
}
return true;
};
}
window.onload = initSlides;