From cde5362db3c9f9c11377bce38c527b4e7d09344f Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Sat, 28 Apr 2012 12:16:56 -0400 Subject: [PATCH] added support for looped presentations --- README.md | 4 ++++ index.html | 3 +++ js/reveal.js | 34 +++++++++++++++++++++++++--------- js/reveal.min.js | 6 +++--- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index d3c22286..b1e73551 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,9 @@ Reveal.initialize({ // If true; each slide will be pushed to the browser history history: true, + // Loops the presentation, defaults to false + loop: false, + // Flags if mouse wheel navigation should be enabled mouseWheel: true, @@ -98,6 +101,7 @@ document.addEventListener( 'someState', function() { #### 1.3 (master) - Revised keyboard shortcuts, including ESC for overview, N for next, P for previous. Thanks [mahemoff](https://github.com/mahemoff) +- Added support for looped presentations via config #### 1.2 diff --git a/index.html b/index.html index 69d598f3..5346e349 100644 --- a/index.html +++ b/index.html @@ -260,6 +260,9 @@ // If true; each slide will be pushed to the browser history history: true, + // Loops the presentation, defaults to false + loop: false, + // Flags if mouse wheel navigation should be enabled mouseWheel: true, diff --git a/js/reveal.js b/js/reveal.js index 237386dc..3360255a 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -19,10 +19,11 @@ var Reveal = (function(){ controls: false, progress: false, history: false, - transition: 'default', - theme: 'default', + loop: false, mouseWheel: true, - rollingLinks: true + rollingLinks: true, + transition: 'default', + theme: 'default' }, // Slides may hold a data-state attribute which we pick up and apply @@ -406,19 +407,34 @@ var Reveal = (function(){ // Select all slides and convert the NodeList result to // an array - var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ); + var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ), + slidesLength = slides.length; - if( slides.length ) { - // Enforce max and minimum index bounds - index = Math.max(Math.min(index, slides.length - 1), 0); + if( slidesLength ) { - for( var i = 0; i < slides.length; i++ ) { + // Should the index loop? + if( config.loop ) { + index %= slidesLength; + + if( index < 0 ) { + index = slidesLength + index; + } + } + + // Enforce max and minimum index bounds + index = Math.max( Math.min( index, slidesLength - 1 ), 0 ); + + for( var i = 0; i < slidesLength; i++ ) { var slide = slides[i]; // Optimization; hide all slides that are three or more steps // away from the present slide if( overviewIsActive() === false ) { - slide.style.display = Math.abs( index - i ) > 3 ? 'none' : 'block'; + // The distance loops so that it measures 1 between the first + // and last slides + var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0; + + slide.style.display = distance > 3 ? 'none' : 'block'; } slides[i].classList.remove( 'past' ); diff --git a/js/reveal.min.js b/js/reveal.min.js index 749bdcdc..2bbb956f 100644 --- a/js/reveal.min.js +++ b/js/reveal.min.js @@ -5,7 +5,7 @@ * * Copyright (C) 2012 Hakim El Hattab, http://hakim.se */ -var Reveal=(function(){var i="#reveal .slides>section",a="#reveal .slides>section.present>section",j=0,c=0,E={controls:false,progress:false,history:false,transition:"default",theme:"default",mouseWheel:true,rollingLinks:true},K=[],d={},F=document.body.style.perspectiveProperty!==undefined||document.body.style.WebkitPerspective!==undefined||document.body.style.MozPerspective!==undefined||document.body.style.msPerspective!==undefined||document.body.style.OPerspective!==undefined,k=document.body.style.transformProperty!==undefined||document.body.style.WebkitTransform!==undefined||document.body.style.MozTransform!==undefined||document.body.style.msTransform!==undefined||document.body.style.OTransform!==undefined,t=0,w=0; +var Reveal=(function(){var i="#reveal .slides>section",a="#reveal .slides>section.present>section",j=0,c=0,E={controls:false,progress:false,history:false,loop:false,mouseWheel:true,rollingLinks:true,transition:"default",theme:"default"},K=[],d={},F=document.body.style.perspectiveProperty!==undefined||document.body.style.WebkitPerspective!==undefined||document.body.style.MozPerspective!==undefined||document.body.style.msPerspective!==undefined||document.body.style.OPerspective!==undefined,k=document.body.style.transformProperty!==undefined||document.body.style.WebkitTransform!==undefined||document.body.style.MozTransform!==undefined||document.body.style.msTransform!==undefined||document.body.style.OTransform!==undefined,t=0,w=0; function g(M){if(!k&&!F){document.body.setAttribute("class","no-transforms");return;}d.wrapper=document.querySelector("#reveal");d.progress=document.querySelector("#reveal .progress"); d.progressbar=document.querySelector("#reveal .progress span");d.controls=document.querySelector("#reveal .controls");d.controlsLeft=document.querySelector("#reveal .controls .left"); d.controlsRight=document.querySelector("#reveal .controls .right");d.controlsUp=document.querySelector("#reveal .controls .up");d.controlsDown=document.querySelector("#reveal .controls .down"); @@ -28,8 +28,8 @@ Q3?"none":"block"; +}function x(M){if(H()){M.preventDefault();I();j=this.getAttribute("data-index-h");c=this.getAttribute("data-index-v");b();}}function L(N,P){var R=Array.prototype.slice.call(document.querySelectorAll(N)),S=R.length; +if(S){if(E.loop){P%=S;if(P<0){P=S+P;}}P=Math.max(Math.min(P,S-1),0);for(var Q=0;Q3?"none":"block"; }R[Q].classList.remove("past");R[Q].classList.remove("present");R[Q].classList.remove("future");if(QP){R[Q].classList.add("future"); }}if(M.querySelector("section")){R[Q].classList.add("stack");}}R[P].classList.add("present");var O=R[P].dataset.state;if(O){K=K.concat(O.split(" "));}}else{P=0; }return P;}function b(){var Q=K.concat();K.length=0;j=L(i,j);c=L(a,c);stateLoop:for(var O=0,M=K.length;O