Duck-typing fix. Less `typeof`, more casting.

This commit is contained in:
Dmitry Baranovskiy 2010-11-08 12:34:56 +11:00
parent fc10c30822
commit 247d715ff0
1 changed files with 65 additions and 65 deletions

View File

@ -1,74 +1,74 @@
var url = document.location, (function () {
linkScreen = document.querySelector('link[title=screen]'), var url = document.location,
linkProjection = document.querySelector('link[title=projection]'), linkScreen = document.querySelector('link[title=screen]'),
shower = document.querySelector('div.shower'), linkProjection = document.querySelector('link[title=projection]'),
slides = document.querySelectorAll('section.slide'), shower = document.querySelector('div.shower'),
slideList = [], hashList = {}, slides = document.querySelectorAll('section.slide'),
fullscreen = false; slideList = [], hashList = {},
fullscreen = false;
for(var i = 0, slidesLength = slides.length; i < slidesLength; i++) { for(var i = 0, slidesLength = slides.length; i < slidesLength; i++) {
var id = slides[i].id; var id = slides[i].id;
slideList[i] = '#' + id; slideList[i] = '#' + id;
hashList['#' + id] = i; hashList['#' + id] = i;
} }
function toggleFull(force) { function toggleFull(force) {
fullscreen = (typeof force == 'boolean') ? force : ( fullscreen = (force != null) ? !!force : (
window.fullScreen || // Firefox 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
); );
updateView(); updateView();
} }
function turnSlide(e) { function turnSlide(e) {
if(!fullscreen) return; if(!fullscreen) return;
var current = hashList[url.hash], var current = hashList[url.hash],
target; target;
if(e.type == 'keyup') { // Key-Based if(e == null) {
switch(e.which) { target = slideList[current++];
case 33 : // PgUp } else if(e.type == 'keyup') { // Key-Based
case 38 : // Up switch(e.which) {
case 37 : // Left case 33 : // PgUp
current--; case 38 : // Up
break; case 37 : // Left
case 34 : // PgDown current--;
case 40 : // Down break;
case 39 : // Right case 34 : // PgDown
current++; case 40 : // Down
break; case 39 : // Right
case 32 : // Space current++;
current += e.shiftKey ? -1 : 1; break;
break; case 32 : // Space
default: current += e.shiftKey ? -1 : 1;
return; break;
default:
return;
}
target = slideList[current];
e.preventDefault();
} else { // Specific
target = slideList[+e || 0];
} }
target = slideList[current]; if(target) url.hash = target;
e.preventDefault();
} }
if(typeof e == 'number') { // Specific
target = slideList[e]; function updateView() {
linkScreen.disabled = fullscreen;
linkProjection.disabled = !fullscreen;
if(!hashList[url.hash]) turnSlide(0);
} }
if(typeof e == 'undefined') { // Next
target = slideList[current++];
}
if(target) url.hash = target;
}
function updateView() { slides[0].addEventListener('click', function(){
linkScreen.disabled = fullscreen; toggleFull(true);
linkProjection.disabled = !fullscreen; }, false);
if(!hashList[url.hash]) turnSlide(0);
}
slides[0].addEventListener('click', function(){ document.addEventListener('keyup', turnSlide, false);
toggleFull(true); document.addEventListener('keyup', function(e) {
}, false); if(e.which == 27) toggleFull(false);
}, false);
document.addEventListener('keyup', turnSlide, false); window.addEventListener('resize', toggleFull, false);
document.addEventListener('keyup', function(e) { window.addEventListener('DOMContentLoaded', toggleFull, false);
if(e.which == 27) toggleFull(false); })();
}, false);
window.addEventListener('resize', toggleFull, false);
window.addEventListener('DOMContentLoaded', toggleFull, false);