"navigation handlers are now added when impress is initialized"

This commit is contained in:
Bartek Szopka
2012-03-12 22:29:45 +00:00
parent 70bc3b737b
commit a055e97a54

View File

@@ -168,10 +168,20 @@
transitionDuration: 1000 transitionDuration: 1000
}; };
var empty = function () { return false; };
var impress = window.impress = function ( rootId ) { var impress = window.impress = function ( rootId ) {
// if impress.js is not supported by the browser return a dummy API
// it may not be a perfect solution but we return early and avoid
// running code that may use features not implemented in the browser
if (!impressSupported) { if (!impressSupported) {
return null; return {
init: empty,
stepTo: empty,
prev: empty,
next: empty
};
} }
rootId = rootId || "impress"; rootId = rootId || "impress";
@@ -191,9 +201,9 @@
// step events // step events
var triggerEvent = function (el, eventName) { var triggerEvent = function (el, eventName, detail) {
var event = document.createEvent("CustomEvent"); var event = document.createEvent("CustomEvent");
event.initCustomEvent(eventName, true, true, {}); event.initCustomEvent(eventName, true, true, detail);
el.dispatchEvent(event); el.dispatchEvent(event);
}; };
@@ -342,7 +352,7 @@
initialized = true; initialized = true;
triggerEvent(root, "impressInit"); triggerEvent(root, "impressInit", { api: roots[ "impress-root-" + rootId ] });
}; };
var stepTo = function ( el, force ) { var stepTo = function ( el, force ) {
@@ -502,13 +512,6 @@
(function ( document, window ) { (function ( document, window ) {
'use strict'; 'use strict';
var impress = window.impress;
// if impress is not supported don't add any handlers
if (!impress.supported) {
return;
}
// throttling function calls, by Remy Sharp // throttling function calls, by Remy Sharp
// http://remysharp.com/2010/07/21/throttling-function-calls/ // http://remysharp.com/2010/07/21/throttling-function-calls/
var throttle = function (fn, delay) { var throttle = function (fn, delay) {
@@ -522,6 +525,9 @@
}; };
}; };
document.addEventListener("impressInit", function (event) {
var api = event.detail.api;
// keyboard navigation handlers // keyboard navigation handlers
// prevent default keydown action when one of supported key is pressed // prevent default keydown action when one of supported key is pressed
@@ -538,14 +544,14 @@
case 33: // pg up case 33: // pg up
case 37: // left case 37: // left
case 38: // up case 38: // up
impress().prev(); api.prev();
break; break;
case 9: // tab case 9: // tab
case 32: // space case 32: // space
case 34: // pg down case 34: // pg down
case 39: // right case 39: // right
case 40: // down case 40: // down
impress().next(); api.next();
break; break;
} }
@@ -572,7 +578,7 @@
} }
} }
if ( impress().stepTo(target) ) { if ( api.stepTo(target) ) {
event.stopImmediatePropagation(); event.stopImmediatePropagation();
event.preventDefault(); event.preventDefault();
} }
@@ -587,7 +593,7 @@
target = target.parentNode; target = target.parentNode;
} }
if ( impress().stepTo(target) ) { if ( api.stepTo(target) ) {
event.preventDefault(); event.preventDefault();
} }
}, false); }, false);
@@ -600,9 +606,9 @@
result = null; result = null;
if ( x < width ) { if ( x < width ) {
result = impress().prev(); result = api.prev();
} else if ( x > window.innerWidth - width ) { } else if ( x > window.innerWidth - width ) {
result = impress().next(); result = api.next();
} }
if (result) { if (result) {
@@ -614,7 +620,10 @@
// rescale presentation when window is resized // rescale presentation when window is resized
window.addEventListener("resize", throttle(function () { window.addEventListener("resize", throttle(function () {
// force going to active step again, to trigger rescaling // force going to active step again, to trigger rescaling
impress().stepTo( document.querySelector(".active"), true ); api.stepTo( document.querySelector(".active"), true );
}, 250), false); }, 250), false);
}, false);
})(document, window); })(document, window);