Browse Source

added support for looped presentations

Hakim El Hattab 13 years ago
parent
commit
cde5362db3
4 changed files with 35 additions and 12 deletions
  1. 4 0
      README.md
  2. 3 0
      index.html
  3. 25 9
      js/reveal.js
  4. 3 3
      js/reveal.min.js

+ 4 - 0
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
 

+ 3 - 0
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,
 

+ 25 - 9
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' );

+ 3 - 3
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 @@ Q<N;Q++){var T=U[Q],S="translate(0%, "+((Q-c)*105)+"%)";T.setAttribute("data-ind
 T.style.MozTransform=S;T.style.msTransform=S;T.style.OTransform=S;T.style.transform=S;T.addEventListener("click",x,true);}}}function I(){d.wrapper.classList.remove("overview");
 var P=Array.prototype.slice.call(document.querySelectorAll("#reveal .slides section"));for(var O=0,M=P.length;O<M;O++){var N=P[O];N.style.WebkitTransform="";
 N.style.MozTransform="";N.style.msTransform="";N.style.OTransform="";N.style.transform="";N.removeEventListener("click",x);}b();}function H(){return d.wrapper.classList.contains("overview");
-}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));
-if(R.length){P=Math.max(Math.min(P,R.length-1),0);for(var Q=0;Q<R.length;Q++){var M=R[Q];if(H()===false){M.style.display=Math.abs(P-Q)>3?"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;Q<S;Q++){var M=R[Q];if(H()===false){var T=Math.abs((P-Q)%(S-3))||0;M.style.display=T>3?"none":"block";
 }R[Q].classList.remove("past");R[Q].classList.remove("present");R[Q].classList.remove("future");if(Q<P){R[Q].classList.add("past");}else{if(Q>P){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<M;O++){for(var N=0;N<Q.length;N++){if(Q[N]===K[O]){Q.splice(N,1);