3 * http://lab.hakim.se/reveal-js
6 * Copyright (C) 2011-2012 Hakim El Hattab, http://hakim.se
8 var Reveal = (function(){
12 var HORIZONTAL_SLIDES_SELECTOR = '.reveal .slides>section',
13 VERTICAL_SLIDES_SELECTOR = '.reveal .slides>section.present>section',
15 // Configurations defaults, can be overridden at initialization time
17 // Display controls in the bottom right corner
20 // Display a presentation progress bar
23 // Push each slide change to the browser history
26 // Enable keyboard shortcuts for navigation
29 // Enable the slide overview mode
32 // Loop the presentation
35 // Number of milliseconds between automatically proceeding to the
36 // next slide, disabled when set to 0
39 // Enable slide navigation via mouse wheel
42 // Apply a 3D roll to links on hover
45 // Transition style (see /css/theme)
49 transition: 'default', // default/cube/page/concave/linear(2d),
51 // Script dependencies to load
55 // The horizontal and verical index of the currently active slide
59 // The previous and current slide HTML elements
63 // Slides may hold a data-state attribute which we pick up and apply
64 // as a class to the body. This list contains the combined state of
65 // all current slides.
68 // Cached references to DOM elements
71 // Detect support for CSS 3D transforms
72 supports3DTransforms = 'WebkitPerspective' in document.body.style ||
73 'MozPerspective' in document.body.style ||
74 'msPerspective' in document.body.style ||
75 'OPerspective' in document.body.style ||
76 'perspective' in document.body.style,
78 supports2DTransforms = 'WebkitTransform' in document.body.style ||
79 'MozTransform' in document.body.style ||
80 'msTransform' in document.body.style ||
81 'OTransform' in document.body.style ||
82 'transform' in document.body.style,
84 // Throttles mouse wheel navigation
85 mouseWheelTimeout = 0,
87 // An interval used to automatically move on to the next slide
90 // Delays updates to the URL due to a Chrome thumbnailer bug
93 // Holds information about the currently ongoing touch input
105 * Starts up the presentation if the client is capable.
107 function initialize( options ) {
108 if( ( !supports2DTransforms && !supports3DTransforms ) ) {
109 document.body.setAttribute( 'class', 'no-transforms' );
111 // If the browser doesn't support core features we won't be
112 // using JavaScript to control the presentation
116 // Copy options over to our config object
117 extend( config, options );
119 // Make sure we've got all the DOM elements we need
122 // Hide the address bar in mobile browsers
125 // Loads the dependencies and continues to #start() once done
131 * Finds and stores references to DOM elements which are
132 * required by the presentation. If a required element is
133 * not found, it is created.
135 function setupDOM() {
136 // Cache references to key DOM elements
137 dom.theme = document.querySelector( '#theme' );
138 dom.wrapper = document.querySelector( '.reveal' );
141 if( !dom.wrapper.querySelector( '.progress' ) && config.progress ) {
142 var progressElement = document.createElement( 'div' );
143 progressElement.classList.add( 'progress' );
144 progressElement.innerHTML = '<span></span>';
145 dom.wrapper.appendChild( progressElement );
149 if( !dom.wrapper.querySelector( '.controls' ) && config.controls ) {
150 var controlsElement = document.createElement( 'aside' );
151 controlsElement.classList.add( 'controls' );
152 controlsElement.innerHTML = '<a class="left" href="#">◄</a>' +
153 '<a class="right" href="#">►</a>' +
154 '<a class="up" href="#">▲</a>' +
155 '<a class="down" href="#">▼</a>';
156 dom.wrapper.appendChild( controlsElement );
159 // Presentation background element
160 if( !dom.wrapper.querySelector( '.state-background' ) ) {
161 var backgroundElement = document.createElement( 'div' );
162 backgroundElement.classList.add( 'state-background' );
163 dom.wrapper.appendChild( backgroundElement );
166 // Overlay graphic which is displayed during the paused mode
167 if( !dom.wrapper.querySelector( '.pause-overlay' ) ) {
168 var pausedElement = document.createElement( 'div' );
169 pausedElement.classList.add( 'pause-overlay' );
170 dom.wrapper.appendChild( pausedElement );
173 // Cache references to elements
174 dom.progress = document.querySelector( '.reveal .progress' );
175 dom.progressbar = document.querySelector( '.reveal .progress span' );
177 if ( config.controls ) {
178 dom.controls = document.querySelector( '.reveal .controls' );
179 dom.controlsLeft = document.querySelector( '.reveal .controls .left' );
180 dom.controlsRight = document.querySelector( '.reveal .controls .right' );
181 dom.controlsUp = document.querySelector( '.reveal .controls .up' );
182 dom.controlsDown = document.querySelector( '.reveal .controls .down' );
187 * Hides the address bar if we're on a mobile device.
189 function hideAddressBar() {
190 if( navigator.userAgent.match( /(iphone|ipod|android)/i ) ) {
191 // Give the page some scrollable overflow
192 document.documentElement.style.overflow = 'scroll';
193 document.body.style.height = '120%';
195 // Events that should trigger the address bar to hide
196 window.addEventListener( 'load', removeAddressBar, false );
197 window.addEventListener( 'orientationchange', removeAddressBar, false );
202 * Loads the dependencies of reveal.js. Dependencies are
203 * defined via the configuration option 'dependencies'
204 * and will be loaded prior to starting/binding reveal.js.
205 * Some dependencies may have an 'async' flag, if so they
206 * will load after reveal.js has been started up.
212 for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
213 var s = config.dependencies[i];
215 // Load if there's no condition or the condition is truthy
216 if( !s.condition || s.condition() ) {
218 scriptsAsync.push( s.src );
221 scripts.push( s.src );
224 // Extension may contain callback functions
225 if( typeof s.callback === 'function' ) {
226 head.ready( s.src.match( /([\w\d_\-]*)\.?[^\\\/]*$/i )[0], s.callback );
231 // Called once synchronous scritps finish loading
233 // Load asynchronous scripts
234 head.js.apply( null, scriptsAsync );
239 if( scripts.length ) {
240 head.ready( proceed );
242 // Load synchronous scripts
243 head.js.apply( null, scripts );
251 * Starts up reveal.js by binding input events and navigating
252 * to the current URL deeplink if there is one.
255 // Subscribe to input
258 // Updates the presentation to match the current configuration values
261 // Read the initial hash
264 // Start auto-sliding if it's enabled
269 * Applies the configuration settings from the config object.
271 function configure() {
272 if( supports3DTransforms === false ) {
273 config.transition = 'linear';
276 if( config.controls && dom.controls ) {
277 dom.controls.style.display = 'block';
280 if( config.progress && dom.progress ) {
281 dom.progress.style.display = 'block';
284 // Load the theme in the config, if it's not already loaded
285 if( config.theme && dom.theme ) {
286 var themeURL = dom.theme.getAttribute( 'href' );
287 var themeFinder = /[^\/]*?(?=\.css)/;
288 var themeName = themeURL.match(themeFinder)[0];
290 if( config.theme !== themeName ) {
291 themeURL = themeURL.replace(themeFinder, config.theme);
292 dom.theme.setAttribute( 'href', themeURL );
296 if( config.transition !== 'default' ) {
297 dom.wrapper.classList.add( config.transition );
300 if( config.mouseWheel ) {
301 document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
302 document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
305 if( config.rollingLinks ) {
306 // Add some 3D magic to our anchors
311 function addEventListeners() {
312 document.addEventListener( 'touchstart', onDocumentTouchStart, false );
313 document.addEventListener( 'touchmove', onDocumentTouchMove, false );
314 document.addEventListener( 'touchend', onDocumentTouchEnd, false );
315 window.addEventListener( 'hashchange', onWindowHashChange, false );
317 if( config.keyboard ) {
318 document.addEventListener( 'keydown', onDocumentKeyDown, false );
321 if ( config.controls && dom.controls ) {
322 dom.controlsLeft.addEventListener( 'click', preventAndForward( navigateLeft ), false );
323 dom.controlsRight.addEventListener( 'click', preventAndForward( navigateRight ), false );
324 dom.controlsUp.addEventListener( 'click', preventAndForward( navigateUp ), false );
325 dom.controlsDown.addEventListener( 'click', preventAndForward( navigateDown ), false );
329 function removeEventListeners() {
330 document.removeEventListener( 'keydown', onDocumentKeyDown, false );
331 document.removeEventListener( 'touchstart', onDocumentTouchStart, false );
332 document.removeEventListener( 'touchmove', onDocumentTouchMove, false );
333 document.removeEventListener( 'touchend', onDocumentTouchEnd, false );
334 window.removeEventListener( 'hashchange', onWindowHashChange, false );
336 if ( config.controls && dom.controls ) {
337 dom.controlsLeft.removeEventListener( 'click', preventAndForward( navigateLeft ), false );
338 dom.controlsRight.removeEventListener( 'click', preventAndForward( navigateRight ), false );
339 dom.controlsUp.removeEventListener( 'click', preventAndForward( navigateUp ), false );
340 dom.controlsDown.removeEventListener( 'click', preventAndForward( navigateDown ), false );
345 * Extend object a with the properties of object b.
346 * If there's a conflict, object b takes precedence.
348 function extend( a, b ) {
355 * Measures the distance in pixels between point a
358 * @param {Object} a point with x/y properties
359 * @param {Object} b point with x/y properties
361 function distanceBetween( a, b ) {
365 return Math.sqrt( dx*dx + dy*dy );
369 * Prevents an events defaults behavior calls the
370 * specified delegate.
372 * @param {Function} delegate The method to call
373 * after the wrapper has been executed
375 function preventAndForward( delegate ) {
376 return function( event ) {
377 event.preventDefault();
383 * Causes the address bar to hide on mobile devices,
384 * more vertical space ftw.
386 function removeAddressBar() {
387 setTimeout( function() {
388 window.scrollTo( 0, 1 );
393 * Dispatches an event of the specified type from the
394 * reveal DOM element.
396 function dispatchEvent( type, properties ) {
397 var event = document.createEvent( "HTMLEvents", 1, 2 );
398 event.initEvent( type, true, true );
399 extend( event, properties );
400 dom.wrapper.dispatchEvent( event );
404 * Handler for the document level 'keydown' event.
406 * @param {Object} event
408 function onDocumentKeyDown( event ) {
409 // Disregard the event if the target is editable or a
410 // modifier is present
411 if ( document.querySelector( ':focus' ) !== null || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
413 var triggered = true;
415 switch( event.keyCode ) {
417 case 80: case 33: navigatePrev(); break;
419 case 78: case 34: navigateNext(); break;
421 case 72: case 37: navigateLeft(); break;
423 case 76: case 39: navigateRight(); break;
425 case 75: case 38: navigateUp(); break;
427 case 74: case 40: navigateDown(); break;
429 case 36: navigateTo( 0 ); break;
431 case 35: navigateTo( Number.MAX_VALUE ); break;
433 case 32: isOverviewActive() ? deactivateOverview() : navigateNext(); break;
435 case 13: isOverviewActive() ? deactivateOverview() : triggered = false; break;
437 case 66: case 190: togglePause(); break;
442 // If the input resulted in a triggered action we should prevent
443 // the browsers default behavior
445 event.preventDefault();
447 else if ( event.keyCode === 27 && supports3DTransforms ) {
450 event.preventDefault();
453 // If auto-sliding is enabled we need to cue up
460 * Handler for the document level 'touchstart' event,
461 * enables support for swipe and pinch gestures.
463 function onDocumentTouchStart( event ) {
464 touch.startX = event.touches[0].clientX;
465 touch.startY = event.touches[0].clientY;
466 touch.startCount = event.touches.length;
468 // If there's two touches we need to memorize the distance
469 // between those two points to detect pinching
470 if( event.touches.length === 2 ) {
471 touch.startSpan = distanceBetween( {
472 x: event.touches[1].clientX,
473 y: event.touches[1].clientY
482 * Handler for the document level 'touchmove' event.
484 function onDocumentTouchMove( event ) {
485 // Each touch should only trigger one action
486 if( !touch.handled ) {
487 var currentX = event.touches[0].clientX;
488 var currentY = event.touches[0].clientY;
490 // If the touch started off with two points and still has
491 // two active touches; test for the pinch gesture
492 if( event.touches.length === 2 && touch.startCount === 2 ) {
494 // The current distance in pixels between the two touch points
495 var currentSpan = distanceBetween( {
496 x: event.touches[1].clientX,
497 y: event.touches[1].clientY
503 // If the span is larger than the desire amount we've got
505 if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
506 touch.handled = true;
508 if( currentSpan < touch.startSpan ) {
512 deactivateOverview();
517 // There was only one touch point, look for a swipe
518 else if( event.touches.length === 1 ) {
519 var deltaX = currentX - touch.startX,
520 deltaY = currentY - touch.startY;
522 if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
523 touch.handled = true;
526 else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
527 touch.handled = true;
530 else if( deltaY > touch.threshold ) {
531 touch.handled = true;
534 else if( deltaY < -touch.threshold ) {
535 touch.handled = true;
540 event.preventDefault();
542 // There's a bug with swiping on some Android devices unless
543 // the default action is always prevented
544 else if( navigator.userAgent.match( /android/gi ) ) {
545 event.preventDefault();
550 * Handler for the document level 'touchend' event.
552 function onDocumentTouchEnd( event ) {
553 touch.handled = false;
557 * Handles mouse wheel scrolling, throttled to avoid
558 * skipping multiple slides.
560 function onDocumentMouseScroll( event ){
561 clearTimeout( mouseWheelTimeout );
563 mouseWheelTimeout = setTimeout( function() {
564 var delta = event.detail || -event.wheelDelta;
575 * Handler for the window level 'hashchange' event.
577 * @param {Object} event
579 function onWindowHashChange( event ) {
584 * Invoked when a slide is and we're in the overview.
586 function onOverviewSlideClicked( event ) {
587 // TODO There's a bug here where the event listeners are not
588 // removed after deactivating the overview.
589 if( isOverviewActive() ) {
590 event.preventDefault();
592 deactivateOverview();
594 indexh = this.getAttribute( 'data-index-h' );
595 indexv = this.getAttribute( 'data-index-v' );
602 * Wrap all links in 3D goodness.
605 if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
606 var nodes = document.querySelectorAll( '.reveal .slides section a:not(.image)' );
608 for( var i = 0, len = nodes.length; i < len; i++ ) {
611 if( node.textContent && !node.querySelector( 'img' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) {
612 node.classList.add( 'roll' );
613 node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
620 * Displays the overview of slides (quick nav) by
621 * scaling down and arranging all slide elements.
623 * Experimental feature, might be dropped if perf
626 function activateOverview() {
628 // Only proceed if enabled in config
629 if( config.overview ) {
631 dom.wrapper.classList.add( 'overview' );
633 var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
635 for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
636 var hslide = horizontalSlides[i],
637 htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
639 hslide.setAttribute( 'data-index-h', i );
640 hslide.style.display = 'block';
641 hslide.style.WebkitTransform = htransform;
642 hslide.style.MozTransform = htransform;
643 hslide.style.msTransform = htransform;
644 hslide.style.OTransform = htransform;
645 hslide.style.transform = htransform;
647 if( !hslide.classList.contains( 'stack' ) ) {
648 // Navigate to this slide on click
649 hslide.addEventListener( 'click', onOverviewSlideClicked, true );
652 var verticalSlides = hslide.querySelectorAll( 'section' );
654 for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
655 var vslide = verticalSlides[j],
656 vtransform = 'translate(0%, ' + ( ( j - ( i === indexh ? indexv : 0 ) ) * 105 ) + '%)';
658 vslide.setAttribute( 'data-index-h', i );
659 vslide.setAttribute( 'data-index-v', j );
660 vslide.style.display = 'block';
661 vslide.style.WebkitTransform = vtransform;
662 vslide.style.MozTransform = vtransform;
663 vslide.style.msTransform = vtransform;
664 vslide.style.OTransform = vtransform;
665 vslide.style.transform = vtransform;
667 // Navigate to this slide on click
668 vslide.addEventListener( 'click', onOverviewSlideClicked, true );
678 * Exits the slide overview and enters the currently
681 function deactivateOverview() {
683 // Only proceed if enabled in config
684 if( config.overview ) {
686 dom.wrapper.classList.remove( 'overview' );
689 var slides = Array.prototype.slice.call( document.querySelectorAll( '.reveal .slides section' ) );
691 for( var i = 0, len = slides.length; i < len; i++ ) {
692 var element = slides[i];
694 // Resets all transforms to use the external styles
695 element.style.WebkitTransform = '';
696 element.style.MozTransform = '';
697 element.style.msTransform = '';
698 element.style.OTransform = '';
699 element.style.transform = '';
701 element.removeEventListener( 'click', onOverviewSlideClicked );
710 * Toggles the slide overview mode on and off.
712 * @param {Boolean} override Optional flag which overrides the
713 * toggle logic and forcibly sets the desired state. True means
714 * overview is open, false means it's closed.
716 function toggleOverview( override ) {
717 if( typeof override === 'boolean' ) {
718 override ? activateOverview() : deactivateOverview();
721 isOverviewActive() ? deactivateOverview() : activateOverview();
726 * Checks if the overview is currently active.
728 * @return {Boolean} true if the overview is active,
731 function isOverviewActive() {
732 return dom.wrapper.classList.contains( 'overview' );
736 * Enters the paused mode which fades everything on screen to
740 dom.wrapper.classList.add( 'paused' );
744 * Exits from the paused mode.
747 dom.wrapper.classList.remove( 'paused' );
751 * Toggles the paused mode on and off.
753 function togglePause() {
763 * Checks if we are currently in the paused mode.
765 function isPaused() {
766 return dom.wrapper.classList.contains( 'paused' );
770 * Updates one dimension of slides by showing the slide
771 * with the specified index.
773 * @param {String} selector A CSS selector that will fetch
774 * the group of slides we are working with
775 * @param {Number} index The index of the slide that should be
778 * @return {Number} The index of the slide that is now shown,
779 * might differ from the passed in index if it was out of
782 function updateSlides( selector, index ) {
784 // Select all slides and convert the NodeList result to
786 var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
787 slidesLength = slides.length;
791 // Should the index loop?
793 index %= slidesLength;
796 index = slidesLength + index;
800 // Enforce max and minimum index bounds
801 index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
803 for( var i = 0; i < slidesLength; i++ ) {
804 var slide = slides[i];
806 // Optimization; hide all slides that are three or more steps
807 // away from the present slide
808 if( isOverviewActive() === false ) {
809 // The distance loops so that it measures 1 between the first
811 var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
813 slide.style.display = distance > 3 ? 'none' : 'block';
816 slides[i].classList.remove( 'past' );
817 slides[i].classList.remove( 'present' );
818 slides[i].classList.remove( 'future' );
821 // Any element previous to index is given the 'past' class
822 slides[i].classList.add( 'past' );
824 else if( i > index ) {
825 // Any element subsequent to index is given the 'future' class
826 slides[i].classList.add( 'future' );
829 // If this element contains vertical slides
830 if( slide.querySelector( 'section' ) ) {
831 slides[i].classList.add( 'stack' );
835 // Mark the current slide as present
836 slides[index].classList.add( 'present' );
838 // If this slide has a state associated with it, add it
839 // onto the current state of the deck
840 var slideState = slides[index].getAttribute( 'data-state' );
842 state = state.concat( slideState.split( ' ' ) );
846 // Since there are no slides we can't be anywhere beyond the
856 * Steps from the current point in the presentation to the
857 * slide which matches the specified horizontal and vertical
860 * @param {int} h Horizontal index of the target slide
861 * @param {int} v Vertical index of the target slide
863 function slide( h, v ) {
864 // Remember where we were at before
865 previousSlide = currentSlide;
867 // Remember the state before this slide
868 var stateBefore = state.concat();
870 // Reset the state array
873 var indexhBefore = indexh,
874 indexvBefore = indexv;
876 // Activate and transition to the new slide
877 indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
878 indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
880 // Apply the new state
881 stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
882 // Check if this state existed on the previous slide. If it
883 // did, we will avoid adding it repeatedly.
884 for( var j = 0; j < stateBefore.length; j++ ) {
885 if( stateBefore[j] === state[i] ) {
886 stateBefore.splice( j, 1 );
891 document.documentElement.classList.add( state[i] );
893 // Dispatch custom event matching the state's name
894 dispatchEvent( state[i] );
897 // Clean up the remaints of the previous state
898 while( stateBefore.length ) {
899 document.documentElement.classList.remove( stateBefore.pop() );
902 // Update progress if enabled
903 if( config.progress && dom.progress ) {
904 dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
907 // If the overview is active, re-activate it to update positions
908 if( isOverviewActive() ) {
914 clearTimeout( writeURLTimeout );
915 writeURLTimeout = setTimeout( writeURL, 1500 );
917 // Query all horizontal slides in the deck
918 var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
920 // Find the current horizontal slide and any possible vertical slides
922 var currentHorizontalSlide = horizontalSlides[ indexh ],
923 currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
925 // Store references to the previous and current slides
926 currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
928 // Dispatch an event if the slide changed
929 if( indexh !== indexhBefore || indexv !== indexvBefore ) {
930 dispatchEvent( 'slidechanged', {
933 'previousSlide': previousSlide,
934 'currentSlide': currentSlide
938 // Ensure that the previous slide is never the same as the current
939 previousSlide = null;
942 // Solves an edge case where the previous slide maintains the
943 // 'present' class when navigating between adjacent vertical
945 if( previousSlide ) {
946 previousSlide.classList.remove( 'present' );
951 * Updates the state and link pointers of the controls.
953 function updateControls() {
954 if ( !config.controls || !dom.controls ) {
958 var routes = availableRoutes();
960 // Remove the 'enabled' class from all directions
961 [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
962 node.classList.remove( 'enabled' );
965 // Add the 'enabled' class to the available routes
966 if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
967 if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
968 if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
969 if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
973 * Determine what available routes there are for navigation.
975 * @return {Object} containing four booleans: left/right/up/down
977 function availableRoutes() {
978 var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
979 verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
983 right: indexh < horizontalSlides.length - 1,
985 down: indexv < verticalSlides.length - 1
990 * Reads the current URL (hash) and navigates accordingly.
993 var hash = window.location.hash;
995 // Attempt to parse the hash as either an index or name
996 var bits = hash.slice( 2 ).split( '/' ),
997 name = hash.replace( /#|\//gi, '' );
999 // If the first bit is invalid and there is a name we can
1000 // assume that this is a named link
1001 if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
1002 // Find the slide with the specified name
1003 var slide = document.querySelector( '#' + name );
1006 // Find the position of the named slide and navigate to it
1007 var indices = Reveal.getIndices( slide );
1008 navigateTo( indices.h, indices.v );
1010 // If the slide doesn't exist, navigate to the current slide
1012 navigateTo( indexh, indexv );
1016 // Read the index components of the hash
1017 var h = parseInt( bits[0], 10 ) || 0,
1018 v = parseInt( bits[1], 10 ) || 0;
1025 * Updates the page URL (hash) to reflect the current
1028 function writeURL() {
1029 if( config.history ) {
1032 // Only include the minimum possible number of components in
1034 if( indexh > 0 || indexv > 0 ) url += indexh;
1035 if( indexv > 0 ) url += '/' + indexv;
1037 window.location.hash = url;
1042 * Navigate to the next slide fragment.
1044 * @return {Boolean} true if there was a next fragment,
1047 function nextFragment() {
1049 if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
1050 var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
1051 if( verticalFragments.length ) {
1052 verticalFragments[0].classList.add( 'visible' );
1054 // Notify subscribers of the change
1055 dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
1059 // Horizontal slides:
1061 var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
1062 if( horizontalFragments.length ) {
1063 horizontalFragments[0].classList.add( 'visible' );
1065 // Notify subscribers of the change
1066 dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
1075 * Navigate to the previous slide fragment.
1077 * @return {Boolean} true if there was a previous fragment,
1080 function previousFragment() {
1082 if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
1083 var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
1084 if( verticalFragments.length ) {
1085 verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
1087 // Notify subscribers of the change
1088 dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
1092 // Horizontal slides:
1094 var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
1095 if( horizontalFragments.length ) {
1096 horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
1098 // Notify subscribers of the change
1099 dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
1108 * Cues a new automated slide if enabled in the config.
1110 function cueAutoSlide() {
1111 clearTimeout( autoSlideTimeout );
1113 // Cue the next auto-slide if enabled
1114 if( config.autoSlide ) {
1115 autoSlideTimeout = setTimeout( navigateNext, config.autoSlide );
1120 * Triggers a navigation to the specified indices.
1122 * @param {Number} h The horizontal index of the slide to show
1123 * @param {Number} v The vertical index of the slide to show
1125 function navigateTo( h, v ) {
1129 function navigateLeft() {
1130 // Prioritize hiding fragments
1131 if( isOverviewActive() || previousFragment() === false ) {
1132 slide( indexh - 1, 0 );
1136 function navigateRight() {
1137 // Prioritize revealing fragments
1138 if( isOverviewActive() || nextFragment() === false ) {
1139 slide( indexh + 1, 0 );
1143 function navigateUp() {
1144 // Prioritize hiding fragments
1145 if( isOverviewActive() || previousFragment() === false ) {
1146 slide( indexh, indexv - 1 );
1150 function navigateDown() {
1151 // Prioritize revealing fragments
1152 if( isOverviewActive() || nextFragment() === false ) {
1153 slide( indexh, indexv + 1 );
1158 * Navigates backwards, prioritized in the following order:
1159 * 1) Previous fragment
1160 * 2) Previous vertical slide
1161 * 3) Previous horizontal slide
1163 function navigatePrev() {
1164 // Prioritize revealing fragments
1165 if( previousFragment() === false ) {
1166 if( availableRoutes().up ) {
1170 // Fetch the previous horizontal slide, if there is one
1171 var previousSlide = document.querySelector( '.reveal .slides>section.past:nth-child(' + indexh + ')' );
1173 if( previousSlide ) {
1174 indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
1183 * Same as #navigatePrev() but navigates forwards.
1185 function navigateNext() {
1186 // Prioritize revealing fragments
1187 if( nextFragment() === false ) {
1188 availableRoutes().down ? navigateDown() : navigateRight();
1191 // If auto-sliding is enabled we need to cue up
1196 // Expose some methods publicly
1198 initialize: initialize,
1199 navigateTo: navigateTo,
1200 navigateLeft: navigateLeft,
1201 navigateRight: navigateRight,
1202 navigateUp: navigateUp,
1203 navigateDown: navigateDown,
1204 navigatePrev: navigatePrev,
1205 navigateNext: navigateNext,
1206 toggleOverview: toggleOverview,
1208 // Adds or removes all internal event listeners (such as keyboard)
1209 addEventListeners: addEventListeners,
1210 removeEventListeners: removeEventListeners,
1212 // Returns the indices of the current, or specified, slide
1213 getIndices: function( slide ) {
1214 // By default, return the current indices
1218 // If a slide is specified, return the indices of that slide
1220 var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
1221 var slideh = isVertical ? slide.parentNode : slide;
1223 // Select all horizontal slides
1224 var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
1226 // Now that we know which the horizontal slide is, get its index
1227 h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
1229 // If this is a vertical slide, grab the vertical index
1231 v = Math.max( Array.prototype.slice.call( slide.parentNode.children ).indexOf( slide ), 0 );
1235 return { h: h, v: v };
1238 // Returns the previous slide element, may be null
1239 getPreviousSlide: function() {
1240 return previousSlide;
1243 // Returns the current slide element
1244 getCurrentSlide: function() {
1245 return currentSlide;
1248 // Helper method, retrieves query string as a key/value hash
1249 getQueryHash: function() {
1252 location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
1253 query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
1259 // Forward event binding to the reveal DOM element
1260 addEventListener: function( type, listener, useCapture ) {
1261 if( 'addEventListener' in window ) {
1262 ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
1265 removeEventListener: function( type, listener, useCapture ) {
1266 if( 'addEventListener' in window ) {
1267 ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );