2011-07-20 22:08:31 +04:00
|
|
|
(function () {
|
2011-07-15 04:53:20 +04:00
|
|
|
var url = window.location,
|
2011-07-16 04:52:21 +04:00
|
|
|
body = document.body,
|
2011-07-15 04:53:20 +04:00
|
|
|
slides = document.querySelectorAll('div.slide'),
|
2010-11-15 16:45:40 +03:00
|
|
|
progress = document.querySelector('div.progress div'),
|
2011-07-20 22:08:31 +04:00
|
|
|
slideList = [],
|
2011-08-09 20:47:32 +04:00
|
|
|
l = slides.length, i;
|
2010-10-30 04:16:19 +04:00
|
|
|
|
2011-07-20 22:08:31 +04:00
|
|
|
for (i = 0; i < l; i++) {
|
2011-08-09 20:47:32 +04:00
|
|
|
slideList.push({
|
|
|
|
id: slides[i].id,
|
|
|
|
hasInnerNavigation: null !== slides[i].querySelector('.inner')
|
|
|
|
});
|
2010-11-08 04:34:56 +03:00
|
|
|
}
|
2011-07-16 05:13:35 +04:00
|
|
|
|
2011-07-20 22:08:31 +04:00
|
|
|
function getTransform() {
|
|
|
|
var denominator = Math.max(
|
|
|
|
body.clientWidth / window.innerWidth,
|
|
|
|
body.clientHeight / window.innerHeight
|
|
|
|
);
|
|
|
|
|
|
|
|
return 'scale(' + (1 / denominator) + ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
function applyTransform(transform) {
|
2011-07-12 15:08:29 +04:00
|
|
|
body.style.MozTransform = transform;
|
|
|
|
body.style.WebkitTransform = transform;
|
|
|
|
body.style.OTransform = transform;
|
|
|
|
body.style.msTransform = transform;
|
|
|
|
body.style.transform = transform;
|
2010-11-08 04:34:56 +03:00
|
|
|
}
|
2011-07-12 15:18:02 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
function enterSlideMode() {
|
2011-07-20 22:08:31 +04:00
|
|
|
body.className = 'full';
|
|
|
|
applyTransform(getTransform());
|
2010-11-08 02:26:01 +03:00
|
|
|
}
|
2010-10-30 04:16:19 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
function enterListMode() {
|
2011-07-20 22:08:31 +04:00
|
|
|
body.className = 'list';
|
|
|
|
applyTransform('none');
|
2011-07-18 12:03:49 +04:00
|
|
|
}
|
2011-07-20 22:08:31 +04:00
|
|
|
|
|
|
|
function getCurrentSlideNumber() {
|
2011-08-09 20:47:32 +04:00
|
|
|
var i, l = slideList.length,
|
|
|
|
currentSlideId = url.hash.substr(1);
|
|
|
|
|
|
|
|
for (i = 0; i < l; ++i) {
|
|
|
|
if (currentSlideId === slideList[i].id) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2011-07-12 15:08:29 +04:00
|
|
|
}
|
|
|
|
|
2011-07-20 22:08:31 +04:00
|
|
|
function scrollToCurrentSlide() {
|
2011-08-09 20:47:32 +04:00
|
|
|
var currentSlide = document.getElementById(
|
|
|
|
slideList[getCurrentSlideNumber()].id
|
|
|
|
);
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
if (null != currentSlide) {
|
|
|
|
window.scrollTo(0, currentSlide.offsetTop);
|
2011-07-20 22:08:31 +04:00
|
|
|
}
|
2011-07-12 15:08:29 +04:00
|
|
|
}
|
2011-07-16 05:13:35 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
function isListMode() {
|
2011-07-20 22:08:31 +04:00
|
|
|
return 'full' !== url.search.substr(1);
|
2010-11-08 04:34:56 +03:00
|
|
|
}
|
2011-07-12 15:08:29 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
function normalizeSlideNumber(slideNumber) {
|
|
|
|
if (0 > slideNumber) {
|
2011-07-20 22:08:31 +04:00
|
|
|
return slideList.length - 1;
|
2011-08-09 20:47:32 +04:00
|
|
|
} else if (slideList.length <= slideNumber) {
|
2011-07-20 22:08:31 +04:00
|
|
|
return 0;
|
|
|
|
} else {
|
2011-08-09 20:47:32 +04:00
|
|
|
return slideNumber;
|
2011-07-20 22:08:31 +04:00
|
|
|
}
|
2010-11-15 16:45:40 +03:00
|
|
|
}
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
function updateProgress(slideNumber) {
|
|
|
|
if (null === progress) { return; }
|
|
|
|
progress.style.width = (100 / (slideList.length - 1) * normalizeSlideNumber(slideNumber)).toFixed(2) + '%';
|
2011-07-18 16:40:01 +04:00
|
|
|
}
|
2010-10-30 04:16:19 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
function getSlideHash(slideNumber) {
|
|
|
|
return '#' + slideList[normalizeSlideNumber(slideNumber)].id;
|
2011-07-16 14:01:16 +04:00
|
|
|
}
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
function goToSlide(slideNumber) {
|
|
|
|
url.hash = getSlideHash(slideNumber);
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
if (!isListMode()) {
|
|
|
|
updateProgress(slideNumber);
|
2011-07-20 22:08:31 +04:00
|
|
|
}
|
|
|
|
}
|
2011-08-01 16:06:35 +04:00
|
|
|
|
|
|
|
function getContainingSlideId(el) {
|
|
|
|
var node = el;
|
2011-08-09 20:47:32 +04:00
|
|
|
while ('BODY' !== node.nodeName && 'HTML' !== node.nodeName) {
|
2011-08-01 16:06:35 +04:00
|
|
|
if (-1 !== node.className.indexOf('slide')) {
|
|
|
|
return node.id;
|
|
|
|
} else {
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2011-08-09 20:47:32 +04:00
|
|
|
|
2011-08-01 03:39:03 +04:00
|
|
|
function dispatchSingleSlideMode(e) {
|
2011-08-01 16:06:35 +04:00
|
|
|
var slideId = getContainingSlideId(e.target);
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
if ('' !== slideId && isListMode()) {
|
2011-08-01 03:39:03 +04:00
|
|
|
e.preventDefault();
|
2011-08-09 20:47:32 +04:00
|
|
|
|
2011-08-01 03:39:03 +04:00
|
|
|
// NOTE: we should update hash to get things work properly
|
2011-08-01 16:06:35 +04:00
|
|
|
url.hash = '#' + slideId;
|
|
|
|
history.replaceState(null, null, url.pathname + '?full#' + slideId);
|
2011-08-09 20:47:32 +04:00
|
|
|
enterSlideMode();
|
|
|
|
|
2011-08-01 03:39:03 +04:00
|
|
|
updateProgress(getCurrentSlideNumber());
|
|
|
|
}
|
|
|
|
}
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
// Increases inner navigation by adding 'active' class to next inactive
|
|
|
|
// inner navigation item.
|
|
|
|
function increaseInnerNavigation(slideNumber) {
|
|
|
|
// Shortcut for slides without inner navigation
|
|
|
|
if (true !== slideList[slideNumber].hasInnerNavigation) { return -1; }
|
|
|
|
|
|
|
|
var activeNodes = document.querySelectorAll(getSlideHash(slideNumber) + ' .active'),
|
|
|
|
// NOTE: we assume there is no other elements in inner navigation
|
|
|
|
node = activeNodes[activeNodes.length - 1].nextElementSibling;
|
|
|
|
|
|
|
|
if (null !== node) {
|
|
|
|
node.classList.add('active');
|
|
|
|
// NOTE: 'auto-added' class is used in resetInnerNavigation - to
|
|
|
|
// overcome problems with manually added 'active' class.
|
|
|
|
node.classList.add('auto-added');
|
|
|
|
|
|
|
|
return activeNodes.length + 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resets inner navigation by removing 'active' class from all previously
|
|
|
|
// activated elements.
|
|
|
|
function resetInnerNavigation(slideNumber) {
|
|
|
|
// Shortcut for slides without inner navigation
|
|
|
|
if (true !== slideList[slideNumber].hasInnerNavigation) { return; }
|
|
|
|
|
|
|
|
var activeNodes = document.querySelectorAll(
|
|
|
|
getSlideHash(slideNumber) + ' .active.auto-added'
|
|
|
|
),
|
|
|
|
i = 0, l;
|
|
|
|
|
|
|
|
for (l = activeNodes.length; i < l; i++) {
|
|
|
|
activeNodes[i].classList.remove('active');
|
|
|
|
activeNodes[i].classList.remove('auto-added');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event handlers
|
|
|
|
|
2011-07-20 22:08:31 +04:00
|
|
|
window.addEventListener('DOMContentLoaded', function () {
|
2011-08-09 20:47:32 +04:00
|
|
|
if (!isListMode()) {
|
|
|
|
// "?full" is present without slide hash, so we should display first
|
2011-07-21 16:28:06 +04:00
|
|
|
// slide
|
2011-08-09 20:47:32 +04:00
|
|
|
if (-1 === getCurrentSlideNumber()) {
|
|
|
|
history.replaceState(null, null, url.pathname + '?full' + getSlideHash(0));
|
2011-07-21 16:28:06 +04:00
|
|
|
}
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
enterSlideMode();
|
2011-07-21 16:28:06 +04:00
|
|
|
updateProgress(getCurrentSlideNumber());
|
2011-07-20 22:08:31 +04:00
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
window.addEventListener('popstate', function (e) {
|
2011-08-09 20:47:32 +04:00
|
|
|
if (isListMode()) {
|
|
|
|
enterListMode();
|
2011-07-20 22:08:31 +04:00
|
|
|
scrollToCurrentSlide();
|
|
|
|
} else {
|
2011-08-09 20:47:32 +04:00
|
|
|
enterSlideMode();
|
2011-07-20 22:08:31 +04:00
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
|
|
|
|
window.addEventListener('resize', function (e) {
|
2011-08-09 20:47:32 +04:00
|
|
|
if (!isListMode()) {
|
2011-07-20 22:08:31 +04:00
|
|
|
applyTransform(getTransform());
|
|
|
|
}
|
2011-07-16 04:52:21 +04:00
|
|
|
}, false);
|
2011-07-20 22:08:31 +04:00
|
|
|
|
|
|
|
document.addEventListener('keydown', function (e) {
|
2011-08-09 20:47:32 +04:00
|
|
|
// Shortcut for alt, shift and meta keys
|
|
|
|
if (e.altKey || e.ctrlKey || e.metaKey) { return; }
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
var currentSlideNumber = getCurrentSlideNumber();
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
switch (e.which) {
|
2011-07-20 22:08:31 +04:00
|
|
|
case 13: // Enter
|
2011-08-09 20:47:32 +04:00
|
|
|
if (isListMode()) {
|
2011-07-20 22:08:31 +04:00
|
|
|
e.preventDefault();
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
history.pushState(null, null, url.pathname + '?full' + getSlideHash(currentSlideNumber));
|
|
|
|
enterSlideMode();
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
updateProgress(currentSlideNumber);
|
2011-07-20 22:08:31 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 27: // Esc
|
2011-08-09 20:47:32 +04:00
|
|
|
if (!isListMode()) {
|
2011-07-20 22:08:31 +04:00
|
|
|
e.preventDefault();
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
history.pushState(null, null, url.pathname + getSlideHash(currentSlideNumber));
|
|
|
|
enterListMode();
|
2011-07-20 22:08:31 +04:00
|
|
|
scrollToCurrentSlide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 33: // PgUp
|
|
|
|
case 38: // Up
|
|
|
|
case 37: // Left
|
2011-07-25 13:13:11 +04:00
|
|
|
case 72: // h
|
2011-07-20 22:08:31 +04:00
|
|
|
case 75: // k
|
|
|
|
e.preventDefault();
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
resetInnerNavigation(currentSlideNumber);
|
|
|
|
currentSlideNumber--;
|
|
|
|
goToSlide(currentSlideNumber);
|
2011-07-20 22:08:31 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 34: // PgDown
|
|
|
|
case 40: // Down
|
|
|
|
case 39: // Right
|
2011-07-25 13:13:11 +04:00
|
|
|
case 76: // l
|
2011-07-20 22:08:31 +04:00
|
|
|
case 74: // j
|
|
|
|
e.preventDefault();
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
// Only go to next slide if current slide have no inner
|
|
|
|
// navigation or inner navigation is fully shown
|
|
|
|
if (
|
|
|
|
!slideList[currentSlideNumber].hasInnerNavigation ||
|
|
|
|
-1 === increaseInnerNavigation(currentSlideNumber)
|
|
|
|
) {
|
|
|
|
resetInnerNavigation(currentSlideNumber);
|
|
|
|
currentSlideNumber++;
|
|
|
|
goToSlide(currentSlideNumber);
|
|
|
|
}
|
2011-07-20 22:08:31 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 36: // Home
|
|
|
|
e.preventDefault();
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
resetInnerNavigation(currentSlideNumber);
|
|
|
|
currentSlideNumber = 0;
|
|
|
|
goToSlide(currentSlideNumber);
|
2011-07-20 22:08:31 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 35: // End
|
|
|
|
e.preventDefault();
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
resetInnerNavigation(currentSlideNumber);
|
|
|
|
currentSlideNumber = slideList.length - 1;
|
|
|
|
goToSlide(currentSlideNumber);
|
2011-07-20 22:08:31 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
case 9: // Tab = +1; Shift + Tab = -1
|
2011-07-20 22:08:31 +04:00
|
|
|
case 32: // Space = +1; Shift + Space = -1
|
|
|
|
e.preventDefault();
|
|
|
|
|
2011-08-09 20:47:32 +04:00
|
|
|
resetInnerNavigation(currentSlideNumber);
|
|
|
|
currentSlideNumber += e.shiftKey ? -1 : 1;
|
|
|
|
goToSlide(currentSlideNumber);
|
2011-07-20 22:08:31 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Behave as usual
|
|
|
|
}
|
2011-07-18 13:39:47 +04:00
|
|
|
}, false);
|
|
|
|
|
2011-08-01 03:39:03 +04:00
|
|
|
document.addEventListener('click', dispatchSingleSlideMode, false);
|
|
|
|
document.addEventListener('touchend', dispatchSingleSlideMode, false);
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-01 03:39:03 +04:00
|
|
|
document.addEventListener('touchstart', function (e) {
|
2011-08-09 20:47:32 +04:00
|
|
|
if (!isListMode()) {
|
|
|
|
var currentSlideNumber = getCurrentSlideNumber(),
|
2011-08-01 03:39:03 +04:00
|
|
|
x = e.touches[0].pageX;
|
2011-08-09 20:47:32 +04:00
|
|
|
if (x > window.innerWidth / 2) {
|
|
|
|
currentSlideNumber++;
|
2011-08-01 03:39:03 +04:00
|
|
|
} else {
|
2011-08-09 20:47:32 +04:00
|
|
|
currentSlideNumber--;
|
2011-08-01 03:39:03 +04:00
|
|
|
}
|
2011-08-09 20:47:32 +04:00
|
|
|
|
|
|
|
goToSlide(currentSlideNumber);
|
2011-08-01 03:39:03 +04:00
|
|
|
}
|
|
|
|
}, false);
|
2011-07-20 22:08:31 +04:00
|
|
|
|
2011-08-01 03:39:03 +04:00
|
|
|
document.addEventListener('touchmove', function (e) {
|
2011-08-09 20:47:32 +04:00
|
|
|
if (!isListMode()) {
|
2011-08-01 03:39:03 +04:00
|
|
|
e.preventDefault();
|
2011-07-20 22:08:31 +04:00
|
|
|
}
|
|
|
|
}, false);
|
2010-11-08 02:26:01 +03:00
|
|
|
|
2011-07-20 22:08:31 +04:00
|
|
|
}());
|