prevent duplicate slidechange from firing after hash change (closes #54)

This commit is contained in:
Hakim El Hattab 2012-06-06 22:22:13 -04:00
parent 5a782e54f8
commit 2b6c61060b
2 changed files with 26 additions and 29 deletions

View file

@ -610,16 +610,19 @@ var Reveal = (function(){
* Updates the visual slides to represent the currently * Updates the visual slides to represent the currently
* set indices. * set indices.
*/ */
function slide() { function slide( h, v ) {
// Remember the state before this slide // Remember the state before this slide
var stateBefore = state.concat(); var stateBefore = state.concat();
// Reset the state array // Reset the state array
state.length = 0; state.length = 0;
var indexhBefore = indexh,
indexvBefore = indexv;
// Activate and transition to the new slide // Activate and transition to the new slide
indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, indexh ); indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, indexv ); indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
// Apply the new state // Apply the new state
stateLoop: for( var i = 0, len = state.length; i < len; i++ ) { stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
@ -658,11 +661,13 @@ var Reveal = (function(){
clearTimeout( writeURLTimeout ); clearTimeout( writeURLTimeout );
writeURLTimeout = setTimeout( writeURL, 1500 ); writeURLTimeout = setTimeout( writeURL, 1500 );
// Dispatch an event notifying observers of the change in slide if( indexh !== indexhBefore || indexv !== indexvBefore ) {
dispatchEvent( 'slidechanged', { // Dispatch an event notifying observers of the change in slide
'indexh': indexh, dispatchEvent( 'slidechanged', {
'indexv': indexv 'indexh': indexh,
} ); 'indexv': indexv
} );
}
} }
/** /**
@ -814,40 +819,31 @@ var Reveal = (function(){
* @param {Number} v The vertical index of the slide to show * @param {Number} v The vertical index of the slide to show
*/ */
function navigateTo( h, v ) { function navigateTo( h, v ) {
indexh = h === undefined ? indexh : h; slide( h, v );
indexv = v === undefined ? indexv : v;
slide();
} }
function navigateLeft() { function navigateLeft() {
// Prioritize hiding fragments // Prioritize hiding fragments
if( overviewIsActive() || previousFragment() === false ) { if( overviewIsActive() || previousFragment() === false ) {
indexh --; slide( indexh - 1, 0 );
indexv = 0;
slide();
} }
} }
function navigateRight() { function navigateRight() {
// Prioritize revealing fragments // Prioritize revealing fragments
if( overviewIsActive() || nextFragment() === false ) { if( overviewIsActive() || nextFragment() === false ) {
indexh ++; slide( indexh + 1, 0 );
indexv = 0;
slide();
} }
} }
function navigateUp() { function navigateUp() {
// Prioritize hiding fragments // Prioritize hiding fragments
if( overviewIsActive() || previousFragment() === false ) { if( overviewIsActive() || previousFragment() === false ) {
indexv --; slide( indexh, indexv - 1 );
slide();
} }
} }
function navigateDown() { function navigateDown() {
// Prioritize revealing fragments // Prioritize revealing fragments
if( overviewIsActive() || nextFragment() === false ) { if( overviewIsActive() || nextFragment() === false ) {
indexv ++; slide( indexh, indexv + 1 );
slide();
} }
} }

15
js/reveal.min.js vendored
View file

@ -42,9 +42,10 @@ c=this.getAttribute("data-index-v");b();}}function U(W,Y){var aa=Array.prototype
if(Y<0){Y=ab+Y;}}Y=Math.max(Math.min(Y,ab-1),0);for(var Z=0;Z<ab;Z++){var V=aa[Z];if(O()===false){var ac=Math.abs((Y-Z)%(ab-3))||0;V.style.display=ac>3?"none":"block"; if(Y<0){Y=ab+Y;}}Y=Math.max(Math.min(Y,ab-1),0);for(var Z=0;Z<ab;Z++){var V=aa[Z];if(O()===false){var ac=Math.abs((Y-Z)%(ab-3))||0;V.style.display=ac>3?"none":"block";
}aa[Z].classList.remove("past");aa[Z].classList.remove("present");aa[Z].classList.remove("future");if(Z<Y){aa[Z].classList.add("past");}else{if(Z>Y){aa[Z].classList.add("future"); }aa[Z].classList.remove("past");aa[Z].classList.remove("present");aa[Z].classList.remove("future");if(Z<Y){aa[Z].classList.add("past");}else{if(Z>Y){aa[Z].classList.add("future");
}}if(V.querySelector("section")){aa[Z].classList.add("stack");}}aa[Y].classList.add("present");var X=aa[Y].getAttribute("data-state");if(X){T=T.concat(X.split(" ")); }}if(V.querySelector("section")){aa[Z].classList.add("stack");}}aa[Y].classList.add("present");var X=aa[Y].getAttribute("data-state");if(X){T=T.concat(X.split(" "));
}}else{Y=0;}return Y;}function b(){var Y=T.concat();T.length=0;k=U(j,k);c=U(a,c);stateLoop:for(var X=0,V=T.length;X<V;X++){for(var W=0;W<Y.length;W++){if(Y[W]===T[X]){Y.splice(W,1); }}else{Y=0;}return Y;}function b(aa,W){var ac=T.concat();T.length=0;var ab=k,Z=c;k=U(j,aa===undefined?k:aa);c=U(a,W===undefined?c:W);stateLoop:for(var Y=0,V=T.length;
continue stateLoop;}}document.documentElement.classList.add(T[X]);o(T[X]);}while(Y.length){document.documentElement.classList.remove(Y.pop());}if(H.progress){d.progressbar.style.width=(k/(document.querySelectorAll(j).length-1))*window.innerWidth+"px"; Y<V;Y++){for(var X=0;X<ac.length;X++){if(ac[X]===T[Y]){ac.splice(X,1);continue stateLoop;}}document.documentElement.classList.add(T[Y]);o(T[Y]);}while(ac.length){document.documentElement.classList.remove(ac.pop());
}if(O()){B();}p();clearTimeout(writeURLTimeout);writeURLTimeout=setTimeout(g,1500);o("slidechanged",{indexh:k,indexv:c});}function p(){var V=f();[d.controlsLeft,d.controlsRight,d.controlsUp,d.controlsDown].forEach(function(W){W.classList.remove("enabled"); }if(H.progress){d.progressbar.style.width=(k/(document.querySelectorAll(j).length-1))*window.innerWidth+"px";}if(O()){B();}p();clearTimeout(writeURLTimeout);
writeURLTimeout=setTimeout(g,1500);if(k!==ab||c!==Z){o("slidechanged",{indexh:k,indexv:c});}}function p(){var V=f();[d.controlsLeft,d.controlsRight,d.controlsUp,d.controlsDown].forEach(function(W){W.classList.remove("enabled");
});if(V.left){d.controlsLeft.classList.add("enabled");}if(V.right){d.controlsRight.classList.add("enabled");}if(V.up){d.controlsUp.classList.add("enabled"); });if(V.left){d.controlsLeft.classList.add("enabled");}if(V.right){d.controlsRight.classList.add("enabled");}if(V.up){d.controlsUp.classList.add("enabled");
}if(V.down){d.controlsDown.classList.add("enabled");}}function f(){var V=document.querySelectorAll(j);var W=document.querySelectorAll(a);return{left:k>0,right:k<V.length-1,up:c>0,down:c<W.length-1}; }if(V.down){d.controlsDown.classList.add("enabled");}}function f(){var V=document.querySelectorAll(j);var W=document.querySelectorAll(a);return{left:k>0,right:k<V.length-1,up:c>0,down:c<W.length-1};
}function D(){var V=window.location.hash.slice(2).split("/");k=parseInt(V[0])||0;c=parseInt(V[1])||0;F(k,c);}function g(){if(H.history){var V="/";if(k>0||c>0){V+=k; }function D(){var V=window.location.hash.slice(2).split("/");k=parseInt(V[0])||0;c=parseInt(V[1])||0;F(k,c);}function g(){if(H.history){var V="/";if(k>0||c>0){V+=k;
@ -53,8 +54,8 @@ continue stateLoop;}}document.documentElement.classList.add(T[X]);o(T[X]);}while
o("fragmentshown",{fragment:W[0]});return true;}}else{var V=document.querySelectorAll(j+".present .fragment:not(.visible)");if(V.length){V[0].classList.add("visible"); o("fragmentshown",{fragment:W[0]});return true;}}else{var V=document.querySelectorAll(j+".present .fragment:not(.visible)");if(V.length){V[0].classList.add("visible");
o("fragmentshown",{fragment:V[0]});return true;}}return false;}function G(){if(document.querySelector(a+".present")){var W=document.querySelectorAll(a+".present .fragment.visible"); o("fragmentshown",{fragment:V[0]});return true;}}return false;}function G(){if(document.querySelector(a+".present")){var W=document.querySelectorAll(a+".present .fragment.visible");
if(W.length){W[W.length-1].classList.remove("visible");o("fragmenthidden",{fragment:W[0]});return true;}}else{var V=document.querySelectorAll(j+".present .fragment.visible"); if(W.length){W[W.length-1].classList.remove("visible");o("fragmenthidden",{fragment:W[0]});return true;}}else{var V=document.querySelectorAll(j+".present .fragment.visible");
if(V.length){V[V.length-1].classList.remove("visible");o("fragmenthidden",{fragment:V[0]});return true;}}return false;}function F(W,V){k=W===undefined?k:W; if(V.length){V[V.length-1].classList.remove("visible");o("fragmenthidden",{fragment:V[0]});return true;}}return false;}function F(W,V){b(W,V);}function w(){if(O()||G()===false){b(k-1,0);
c=V===undefined?c:V;b();}function w(){if(O()||G()===false){k--;c=0;b();}}function i(){if(O()||s()===false){k++;c=0;b();}}function r(){if(O()||G()===false){c--; }}function i(){if(O()||s()===false){b(k+1,0);}}function r(){if(O()||G()===false){b(k,c-1);}}function A(){if(O()||s()===false){b(k,c+1);}}function N(){if(G()===false){if(f().up){r();
b();}}function A(){if(O()||s()===false){c++;b();}}function N(){if(G()===false){if(f().up){r();}else{var V=document.querySelector(".reveal .slides>section.past:nth-child("+k+")"); }else{var V=document.querySelector(".reveal .slides>section.past:nth-child("+k+")");if(V){c=(V.querySelectorAll("section").length+1)||0;k--;b();}}}}function u(){if(s()===false){f().down?A():i();
if(V){c=(V.querySelectorAll("section").length+1)||0;k--;b();}}}}function u(){if(s()===false){f().down?A():i();}}function M(){if(O()){Q();}else{B();}}return{initialize:h,navigateTo:F,navigateLeft:w,navigateRight:i,navigateUp:r,navigateDown:A,toggleOverview:M,addEventListeners:z,removeEventListeners:K,addEventListener:function(W,X,V){(d.wrapper||document.querySelector(".reveal")).addEventListener(W,X,V); }}function M(){if(O()){Q();}else{B();}}return{initialize:h,navigateTo:F,navigateLeft:w,navigateRight:i,navigateUp:r,navigateDown:A,toggleOverview:M,addEventListeners:z,removeEventListeners:K,addEventListener:function(W,X,V){(d.wrapper||document.querySelector(".reveal")).addEventListener(W,X,V);
},removeEventListener:function(W,X,V){(d.wrapper||document.querySelector(".reveal")).removeEventListener(W,X,V);}};})(); },removeEventListener:function(W,X,V){(d.wrapper||document.querySelector(".reveal")).removeEventListener(W,X,V);}};})();