bmstu-mt-wp/scripts/script.js

127 lines
3.2 KiB
JavaScript
Raw Normal View History

2010-11-15 16:45:40 +03:00
(function() {
2011-07-12 15:08:29 +04:00
var url = window.location,
body = document.body,
slides = document.querySelectorAll('div.slide'),
2010-11-15 16:45:40 +03:00
progress = document.querySelector('div.progress div'),
slideList = [];
2010-10-30 04:16:19 +04:00
for(var i = 0, slidesLength = slides.length; i < slidesLength; i++) {
slides[i].firstChild.addEventListener('click', enterFull, false);
slideList[i] = slides[i].id;
}
2011-07-16 05:13:35 +04:00
function resizeFull(p) {
2011-07-16 05:13:35 +04:00
if(typeof p == 'boolean' && !p) {
var transform = 'none';
} else {
2011-07-12 15:08:29 +04:00
var sx = body.clientWidth / window.innerWidth,
sy = body.clientHeight / window.innerHeight,
transform = 'scale(' + (1/Math.max(sx, sy)) + ')';
}
body.style.MozTransform = transform;
body.style.WebkitTransform = transform;
body.style.OTransform = transform;
body.style.msTransform = transform;
body.style.transform = transform;
}
2011-07-12 15:18:02 +04:00
function turnSlide(e) {
2011-07-18 16:40:01 +04:00
var current = currentSlide(), target;
if(e) {
if(e.type == 'keydown') {
var prevent = true;
switch(e.which) {
case 33 : // PgUp
case 38 : // Up
case 37 : // Left
case 75 : // k
current--;
break;
case 34 : // PgDown
case 40 : // Down
case 39 : // Right
case 74 : // j
current++;
break;
case 36 : // Home
current = 0;
break;
case 35 : // End
current = slideList.length-1;
break;
case 32 : // Space
current += e.shiftKey ? -1 : 1;
break;
default:
prevent = false;
}
if(prevent) e.preventDefault();
}
if(e.type == 'click') {
2011-07-16 20:19:30 +04:00
current = slideList.indexOf(e.target.parentNode.parentNode.id);
}
2011-07-12 15:08:29 +04:00
} else {
current = (current+1) ? current : 0;
2010-11-08 02:26:01 +03:00
}
2011-07-12 15:08:29 +04:00
target = slideList[current];
if(target) url.hash = target;
updateProgress();
2010-11-08 02:26:01 +03:00
}
2010-10-30 04:16:19 +04:00
function enterFull(e) {
body.className = 'full';
2011-07-16 05:13:35 +04:00
resizeFull(true);
turnSlide(e);
updateProgress();
if(!isFull()) history.pushState(null, null, url.pathname + '?full' + url.hash);
2011-07-16 05:13:35 +04:00
window.addEventListener('resize', resizeFull, false);
2011-07-18 12:03:49 +04:00
document.addEventListener('keyup', exitFullKey, false);
document.removeEventListener('keydown', enterFullKey, false);
}
function enterFullKey(e) {
if(e.which != 13) return;
enterFull();
2011-07-12 15:08:29 +04:00
}
function exitFull() {
body.className = 'list';
2011-07-16 05:13:35 +04:00
resizeFull(false);
var hash = url.hash;
history.pushState(null, null, url.pathname.replace('?full', ''));
url.hash = hash;
2011-07-16 05:13:35 +04:00
window.removeEventListener('resize', resizeFull, false);
2011-07-18 12:03:49 +04:00
document.removeEventListener('keyup', exitFullKey, false);
document.addEventListener('keydown', enterFullKey, false);
2011-07-12 15:08:29 +04:00
}
2011-07-16 05:13:35 +04:00
2011-07-18 12:03:49 +04:00
function exitFullKey(e) {
2011-07-12 15:08:29 +04:00
if(e.which != 27) return;
exitFull();
}
2011-07-12 15:08:29 +04:00
function isFull() {
return url.search.substr(1) == 'full';
2010-11-15 16:45:40 +03:00
}
2011-07-18 16:40:01 +04:00
function currentSlide() {
return slideList.indexOf(url.hash.substr(1));
}
2010-10-30 04:16:19 +04:00
function updateProgress() {
if(!progress) return;
2011-07-18 16:40:01 +04:00
progress.style.width = (100/(slideList.length-1) * currentSlide()).toFixed(2) + '%';
}
window.addEventListener('DOMContentLoaded', function() {
if(isFull()) enterFull();
}, false);
2011-07-18 16:40:01 +04:00
window.addEventListener('popstate', function() {
if(-1 === currentSlide()) exitFull();
2011-07-18 13:39:47 +04:00
}, false);
document.addEventListener('keydown', turnSlide, false);
2011-07-18 12:03:49 +04:00
document.addEventListener('keydown', enterFullKey, false);
2010-11-08 02:26:01 +03:00
2011-07-18 16:40:01 +04:00
})();