script refactoring

This commit is contained in:
Vadim Makeev 2010-11-08 02:26:01 +03:00
parent 53f357cbc0
commit fc10c30822
1 changed files with 50 additions and 51 deletions

View File

@ -1,41 +1,31 @@
var url = document.location, var url = document.location,
domSlides = document.querySelectorAll('section.slide'),
slides = [], backhash = {},
linkScreen = document.querySelector('link[title=screen]'), linkScreen = document.querySelector('link[title=screen]'),
linkProjection = document.querySelector('link[title=projection]'), linkProjection = document.querySelector('link[title=projection]'),
shower = document.querySelector('div.shower'),
slides = document.querySelectorAll('section.slide'),
slideList = [], hashList = {},
fullscreen = false; fullscreen = false;
for(var i = 0, iLength = domSlides.length; i < iLength; i++) { for(var i = 0, slidesLength = slides.length; i < slidesLength; i++) {
var id = domSlides[i].id; var id = slides[i].id;
slides[i] = '#' + id; slideList[i] = '#' + id;
backhash['#' + id] = i; hashList['#' + id] = i;
} }
function enterFull() { function toggleFull(force) {
fullscreen = true; fullscreen = (typeof force == 'boolean') ? force : (
updateView(); window.fullScreen || // Firefox
}
function exitFull() {
fullscreen = false;
updateView();
}
function toggleFull() {
if( window.fullScreen || // Firefox
document.webkitFullScreen || // Webkit document.webkitFullScreen || // Webkit
screen.width == window.outerWidth && screen.height == window.outerHeight ) { screen.width == window.outerWidth && screen.height == window.outerHeight
enterFull(); );
} else { updateView();
exitFull();
}
} }
function turnSlide(e) { function turnSlide(e) {
if(!fullscreen) return; if(!fullscreen) return;
var current = backhash[url.hash], var current = hashList[url.hash],
target, target;
command; if(e.type == 'keyup') { // Key-Based
switch(e.which) { switch(e.which) {
case 33 : // PgUp case 33 : // PgUp
case 38 : // Up case 38 : // Up
@ -53,23 +43,32 @@ function turnSlide(e) {
default: default:
return; return;
} }
target = slides[current]; target = slideList[current];
e.preventDefault(); e.preventDefault();
}
if(typeof e == 'number') { // Specific
target = slideList[e];
}
if(typeof e == 'undefined') { // Next
target = slideList[current++];
}
if(target) url.hash = target; if(target) url.hash = target;
} }
function updateView() { function updateView() {
linkScreen.disabled = fullscreen; linkScreen.disabled = fullscreen;
linkProjection.disabled = !fullscreen; linkProjection.disabled = !fullscreen;
if(fullscreen && !backhash[url.hash]) url.hash = slides[0]; if(!hashList[url.hash]) turnSlide(0);
} }
domSlides[0].addEventListener('click', enterFull, false); slides[0].addEventListener('click', function(){
toggleFull(true);
window.addEventListener('DOMContentLoaded', toggleFull, false); }, false);
window.addEventListener('resize', toggleFull, false);
document.addEventListener('keyup', turnSlide, false); document.addEventListener('keyup', turnSlide, false);
document.addEventListener('keyup', function(e) { document.addEventListener('keyup', function(e) {
if(e.which == 27) exitFull(); if(e.which == 27) toggleFull(false);
}, false); }, false);
window.addEventListener('resize', toggleFull, false);
window.addEventListener('DOMContentLoaded', toggleFull, false);