3 * http://lab.hakim.se/reveal-js
6 * Copyright (C) 2015 Hakim El Hattab, http://hakim.se
8 (function( root, factory ) {
9 if( typeof define === 'function' && define.amd ) {
10 // AMD. Register as an anonymous module.
12 root.Reveal = factory();
15 } else if( typeof exports === 'object' ) {
16 // Node. Does not work with strict CommonJS.
17 module.exports = factory();
20 root.Reveal = factory();
28 var SLIDES_SELECTOR = '.slides section',
29 HORIZONTAL_SLIDES_SELECTOR = '.slides>section',
30 VERTICAL_SLIDES_SELECTOR = '.slides>section.present>section',
31 HOME_SLIDE_SELECTOR = '.slides>section:first-of-type',
33 // Configuration defaults, can be overridden at initialization time
36 // The "normal" size of the presentation, aspect ratio will be preserved
37 // when the presentation is scaled to fit different resolutions
41 // Factor of the display size that should remain empty around the content
44 // Bounds for smallest/largest possible scale to apply to content
48 // Display controls in the bottom right corner
51 // Display a presentation progress bar
54 // Display the page number of the current slide
57 // Push each slide change to the browser history
60 // Enable keyboard shortcuts for navigation
63 // Optional function that blocks keyboard events when retuning false
64 keyboardCondition: null,
66 // Enable the slide overview mode
69 // Vertical centering of slides
72 // Enables touch navigation on devices with touch input
75 // Loop the presentation
78 // Change the presentation direction to be RTL
81 // Turns fragments on and off globally
84 // Flags if the presentation is running in an embedded mode,
85 // i.e. contained within a limited portion of the screen
88 // Flags if we should show a help overlay when the questionmark
92 // Flags if it should be possible to pause the presentation (blackout)
95 // Flags if speaker notes should be visible to all viewers
98 // Number of milliseconds between automatically proceeding to the
99 // next slide, disabled when set to 0, this value can be overwritten
100 // by using a data-autoslide attribute on your slides
103 // Stop auto-sliding after user input
104 autoSlideStoppable: true,
106 // Enable slide navigation via mouse wheel
109 // Apply a 3D roll to links on hover
112 // Hides the address bar on mobile devices
113 hideAddressBar: true,
115 // Opens links in an iframe preview overlay
118 // Exposes the reveal.js API through window.postMessage
121 // Dispatches all reveal.js events to the parent window through postMessage
122 postMessageEvents: false,
124 // Focuses body when page changes visiblity to ensure keyboard shortcuts work
125 focusBodyOnPageVisibilityChange: true,
128 transition: 'slide', // none/fade/slide/convex/concave/zoom
131 transitionSpeed: 'default', // default/fast/slow
133 // Transition style for full page slide backgrounds
134 backgroundTransition: 'fade', // none/fade/slide/convex/concave/zoom
136 // Parallax background image
137 parallaxBackgroundImage: '', // CSS syntax, e.g. "a.jpg"
139 // Parallax background size
140 parallaxBackgroundSize: '', // CSS syntax, e.g. "3000px 2000px"
142 // Amount of pixels to move the parallax background per slide step
143 parallaxBackgroundHorizontal: null,
144 parallaxBackgroundVertical: null,
146 // Number of slides away from the current that are visible
149 // Script dependencies to load
154 // Flags if reveal.js is loaded (has dispatched the 'ready' event)
157 // Flags if the overview mode is currently active
160 // The horizontal and vertical index of the currently active slide
164 // The previous and current slide HTML elements
170 // Slides may hold a data-state attribute which we pick up and apply
171 // as a class to the body. This list contains the combined state of
172 // all current slides.
175 // The current scale of the presentation (see width/height config)
178 // CSS transform that is currently applied to the slides container,
179 // split into two groups
180 slidesTransform = { layout: '', overview: '' },
182 // Cached references to DOM elements
185 // Features supported by the browser, see #checkCapabilities()
188 // Client is a mobile device, see #checkCapabilities()
191 // Throttles mouse wheel navigation
192 lastMouseWheelStep = 0,
194 // Delays updates to the URL due to a Chrome thumbnailer bug
197 // Flags if the interaction event listeners are bound
198 eventsAreBound = false,
200 // The current auto-slide duration
203 // Auto slide properties
205 autoSlideTimeout = 0,
206 autoSlideStartTime = -1,
207 autoSlidePaused = false,
209 // Holds information about the currently ongoing touch input
219 // Holds information about the keyboard shortcuts
220 keyboardShortcuts = {
221 'N , SPACE': 'Next slide',
222 'P': 'Previous slide',
223 '← , H': 'Navigate left',
224 '→ , L': 'Navigate right',
225 '↑ , K': 'Navigate up',
226 '↓ , J': 'Navigate down',
227 'Home': 'First slide',
231 'ESC, O': 'Slide overview'
235 * Starts up the presentation if the client is capable.
237 function initialize( options ) {
241 if( !features.transforms2d && !features.transforms3d ) {
242 document.body.setAttribute( 'class', 'no-transforms' );
244 // Since JS won't be running any further, we load all lazy
245 // loading elements upfront
246 var images = toArray( document.getElementsByTagName( 'img' ) ),
247 iframes = toArray( document.getElementsByTagName( 'iframe' ) );
249 var lazyLoadable = images.concat( iframes );
251 for( var i = 0, len = lazyLoadable.length; i < len; i++ ) {
252 var element = lazyLoadable[i];
253 if( element.getAttribute( 'data-src' ) ) {
254 element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
255 element.removeAttribute( 'data-src' );
259 // If the browser doesn't support core features we won't be
260 // using JavaScript to control the presentation
264 // Cache references to key DOM elements
265 dom.wrapper = document.querySelector( '.reveal' );
266 dom.slides = document.querySelector( '.reveal .slides' );
268 // Force a layout when the whole page, incl fonts, has loaded
269 window.addEventListener( 'load', layout, false );
271 var query = Reveal.getQueryHash();
273 // Do not accept new dependencies via query config to avoid
274 // the potential of malicious script injection
275 if( typeof query['dependencies'] !== 'undefined' ) delete query['dependencies'];
277 // Copy options over to our config object
278 extend( config, options );
279 extend( config, query );
281 // Hide the address bar in mobile browsers
284 // Loads the dependencies and continues to #start() once done
290 * Inspect the client to see what it's capable of, this
291 * should only happens once per runtime.
293 function checkCapabilities() {
295 features.transforms3d = 'WebkitPerspective' in document.body.style ||
296 'MozPerspective' in document.body.style ||
297 'msPerspective' in document.body.style ||
298 'OPerspective' in document.body.style ||
299 'perspective' in document.body.style;
301 features.transforms2d = 'WebkitTransform' in document.body.style ||
302 'MozTransform' in document.body.style ||
303 'msTransform' in document.body.style ||
304 'OTransform' in document.body.style ||
305 'transform' in document.body.style;
307 features.requestAnimationFrameMethod = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
308 features.requestAnimationFrame = typeof features.requestAnimationFrameMethod === 'function';
310 features.canvas = !!document.createElement( 'canvas' ).getContext;
312 features.touch = !!( 'ontouchstart' in window );
314 // Transitions in the overview are disabled in desktop and
315 // mobile Safari due to lag
316 features.overviewTransitions = !/Version\/[\d\.]+.*Safari/.test( navigator.userAgent );
318 isMobileDevice = /(iphone|ipod|ipad|android)/gi.test( navigator.userAgent );
323 * Loads the dependencies of reveal.js. Dependencies are
324 * defined via the configuration option 'dependencies'
325 * and will be loaded prior to starting/binding reveal.js.
326 * Some dependencies may have an 'async' flag, if so they
327 * will load after reveal.js has been started up.
333 scriptsToPreload = 0;
335 // Called once synchronous scripts finish loading
337 if( scriptsAsync.length ) {
338 // Load asynchronous scripts
339 head.js.apply( null, scriptsAsync );
345 function loadScript( s ) {
346 head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], function() {
347 // Extension may contain callback functions
348 if( typeof s.callback === 'function' ) {
349 s.callback.apply( this );
352 if( --scriptsToPreload === 0 ) {
358 for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
359 var s = config.dependencies[i];
361 // Load if there's no condition or the condition is truthy
362 if( !s.condition || s.condition() ) {
364 scriptsAsync.push( s.src );
367 scripts.push( s.src );
374 if( scripts.length ) {
375 scriptsToPreload = scripts.length;
377 // Load synchronous scripts
378 head.js.apply( null, scripts );
387 * Starts up reveal.js by binding input events and navigating
388 * to the current URL deeplink if there is one.
392 // Make sure we've got all the DOM elements we need
395 // Listen to messages posted to this window
398 // Prevent iframes from scrolling the slides out of view
399 setupIframeScrollPrevention();
401 // Resets all vertical slides so that only the first is visible
402 resetVerticalSlides();
404 // Updates the presentation to match the current configuration values
407 // Read the initial hash
410 // Update all backgrounds
411 updateBackground( true );
413 // Notify listeners that the presentation is ready but use a 1ms
414 // timeout to ensure it's not fired synchronously after #initialize()
415 setTimeout( function() {
416 // Enable transitions now that we're loaded
417 dom.slides.classList.remove( 'no-transition' );
421 dispatchEvent( 'ready', {
424 'currentSlide': currentSlide
428 // Special setup and config is required when printing to PDF
429 if( isPrintingPDF() ) {
430 removeEventListeners();
432 // The document needs to have loaded for the PDF layout
433 // measurements to be accurate
434 if( document.readyState === 'complete' ) {
438 window.addEventListener( 'load', setupPDF );
445 * Finds and stores references to DOM elements which are
446 * required by the presentation. If a required element is
447 * not found, it is created.
449 function setupDOM() {
451 // Prevent transitions while we're loading
452 dom.slides.classList.add( 'no-transition' );
454 // Background element
455 dom.background = createSingletonNode( dom.wrapper, 'div', 'backgrounds', null );
458 dom.progress = createSingletonNode( dom.wrapper, 'div', 'progress', '<span></span>' );
459 dom.progressbar = dom.progress.querySelector( 'span' );
462 createSingletonNode( dom.wrapper, 'aside', 'controls',
463 '<button class="navigate-left" aria-label="previous slide"></button>' +
464 '<button class="navigate-right" aria-label="next slide"></button>' +
465 '<button class="navigate-up" aria-label="above slide"></button>' +
466 '<button class="navigate-down" aria-label="below slide"></button>' );
469 dom.slideNumber = createSingletonNode( dom.wrapper, 'div', 'slide-number', '' );
471 // Element containing notes that are visible to the audience
472 dom.speakerNotes = createSingletonNode( dom.wrapper, 'div', 'speaker-notes', null );
473 dom.speakerNotes.setAttribute( 'data-prevent-swipe', '' );
475 // Overlay graphic which is displayed during the paused mode
476 createSingletonNode( dom.wrapper, 'div', 'pause-overlay', null );
478 // Cache references to elements
479 dom.controls = document.querySelector( '.reveal .controls' );
480 dom.theme = document.querySelector( '#theme' );
482 dom.wrapper.setAttribute( 'role', 'application' );
484 // There can be multiple instances of controls throughout the page
485 dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
486 dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
487 dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
488 dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
489 dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
490 dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
492 dom.statusDiv = createStatusDiv();
496 * Creates a hidden div with role aria-live to announce the
497 * current slide content. Hide the div off-screen to make it
498 * available only to Assistive Technologies.
500 function createStatusDiv() {
502 var statusDiv = document.getElementById( 'aria-status-div' );
504 statusDiv = document.createElement( 'div' );
505 statusDiv.style.position = 'absolute';
506 statusDiv.style.height = '1px';
507 statusDiv.style.width = '1px';
508 statusDiv.style.overflow ='hidden';
509 statusDiv.style.clip = 'rect( 1px, 1px, 1px, 1px )';
510 statusDiv.setAttribute( 'id', 'aria-status-div' );
511 statusDiv.setAttribute( 'aria-live', 'polite' );
512 statusDiv.setAttribute( 'aria-atomic','true' );
513 dom.wrapper.appendChild( statusDiv );
520 * Configures the presentation for printing to a static
523 function setupPDF() {
525 var slideSize = getComputedSlideSize( window.innerWidth, window.innerHeight );
527 // Dimensions of the PDF pages
528 var pageWidth = Math.floor( slideSize.width * ( 1 + config.margin ) ),
529 pageHeight = Math.floor( slideSize.height * ( 1 + config.margin ) );
531 // Dimensions of slides within the pages
532 var slideWidth = slideSize.width,
533 slideHeight = slideSize.height;
535 // Let the browser know what page size we want to print
536 injectStyleSheet( '@page{size:'+ pageWidth +'px '+ pageHeight +'px; margin: 0;}' );
538 // Limit the size of certain elements to the dimensions of the slide
539 injectStyleSheet( '.reveal section>img, .reveal section>video, .reveal section>iframe{max-width: '+ slideWidth +'px; max-height:'+ slideHeight +'px}' );
541 document.body.classList.add( 'print-pdf' );
542 document.body.style.width = pageWidth + 'px';
543 document.body.style.height = pageHeight + 'px';
545 // Slide and slide background layout
546 toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
548 // Vertical stacks are not centred since their section
550 if( slide.classList.contains( 'stack' ) === false ) {
551 // Center the slide inside of the page, giving the slide some margin
552 var left = ( pageWidth - slideWidth ) / 2,
553 top = ( pageHeight - slideHeight ) / 2;
555 var contentHeight = getAbsoluteHeight( slide );
556 var numberOfPages = Math.max( Math.ceil( contentHeight / pageHeight ), 1 );
558 // Center slides vertically
559 if( numberOfPages === 1 && config.center || slide.classList.contains( 'center' ) ) {
560 top = Math.max( ( pageHeight - contentHeight ) / 2, 0 );
563 // Position the slide inside of the page
564 slide.style.left = left + 'px';
565 slide.style.top = top + 'px';
566 slide.style.width = slideWidth + 'px';
568 // TODO Backgrounds need to be multiplied when the slide
569 // stretches over multiple pages
570 var background = slide.querySelector( '.slide-background' );
572 background.style.width = pageWidth + 'px';
573 background.style.height = ( pageHeight * numberOfPages ) + 'px';
574 background.style.top = -top + 'px';
575 background.style.left = -left + 'px';
578 // If we're configured to `showNotes`, inject them into each slide
579 if( config.showNotes ) {
580 var notes = getSlideNotes( slide );
582 var notesElement = document.createElement( 'div' );
583 notesElement.classList.add( 'speaker-notes' );
584 notesElement.classList.add( 'speaker-notes-pdf' );
585 notesElement.innerHTML = notes;
586 notesElement.style.bottom = ( 40 - top ) + 'px';
587 slide.appendChild( notesElement );
594 // Show all fragments
595 toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' .fragment' ) ).forEach( function( fragment ) {
596 fragment.classList.add( 'visible' );
602 * This is an unfortunate necessity. Iframes can trigger the
603 * parent window to scroll, for example by focusing an input.
604 * This scrolling can not be prevented by hiding overflow in
605 * CSS so we have to resort to repeatedly checking if the
606 * browser has decided to offset our slides :(
608 function setupIframeScrollPrevention() {
610 if( dom.slides.querySelector( 'iframe' ) ) {
611 setInterval( function() {
612 if( dom.wrapper.scrollTop !== 0 || dom.wrapper.scrollLeft !== 0 ) {
613 dom.wrapper.scrollTop = 0;
614 dom.wrapper.scrollLeft = 0;
622 * Creates an HTML element and returns a reference to it.
623 * If the element already exists the existing instance will
626 function createSingletonNode( container, tagname, classname, innerHTML ) {
628 // Find all nodes matching the description
629 var nodes = container.querySelectorAll( '.' + classname );
631 // Check all matches to find one which is a direct child of
632 // the specified container
633 for( var i = 0; i < nodes.length; i++ ) {
634 var testNode = nodes[i];
635 if( testNode.parentNode === container ) {
640 // If no node was found, create it now
641 var node = document.createElement( tagname );
642 node.classList.add( classname );
643 if( typeof innerHTML === 'string' ) {
644 node.innerHTML = innerHTML;
646 container.appendChild( node );
653 * Creates the slide background elements and appends them
654 * to the background container. One element is created per
655 * slide no matter if the given slide has visible background.
657 function createBackgrounds() {
659 var printMode = isPrintingPDF();
661 // Clear prior backgrounds
662 dom.background.innerHTML = '';
663 dom.background.classList.add( 'no-transition' );
665 // Iterate over all horizontal slides
666 toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
671 backgroundStack = createBackground( slideh, slideh );
674 backgroundStack = createBackground( slideh, dom.background );
677 // Iterate over all vertical slides
678 toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
681 createBackground( slidev, slidev );
684 createBackground( slidev, backgroundStack );
687 backgroundStack.classList.add( 'stack' );
693 // Add parallax background if specified
694 if( config.parallaxBackgroundImage ) {
696 dom.background.style.backgroundImage = 'url("' + config.parallaxBackgroundImage + '")';
697 dom.background.style.backgroundSize = config.parallaxBackgroundSize;
699 // Make sure the below properties are set on the element - these properties are
700 // needed for proper transitions to be set on the element via CSS. To remove
701 // annoying background slide-in effect when the presentation starts, apply
702 // these properties after short time delay
703 setTimeout( function() {
704 dom.wrapper.classList.add( 'has-parallax-background' );
710 dom.background.style.backgroundImage = '';
711 dom.wrapper.classList.remove( 'has-parallax-background' );
718 * Creates a background for the given slide.
720 * @param {HTMLElement} slide
721 * @param {HTMLElement} container The element that the background
722 * should be appended to
724 function createBackground( slide, container ) {
727 background: slide.getAttribute( 'data-background' ),
728 backgroundSize: slide.getAttribute( 'data-background-size' ),
729 backgroundImage: slide.getAttribute( 'data-background-image' ),
730 backgroundVideo: slide.getAttribute( 'data-background-video' ),
731 backgroundIframe: slide.getAttribute( 'data-background-iframe' ),
732 backgroundColor: slide.getAttribute( 'data-background-color' ),
733 backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
734 backgroundPosition: slide.getAttribute( 'data-background-position' ),
735 backgroundTransition: slide.getAttribute( 'data-background-transition' )
738 var element = document.createElement( 'div' );
740 // Carry over custom classes from the slide to the background
741 element.className = 'slide-background ' + slide.className.replace( /present|past|future/, '' );
743 if( data.background ) {
744 // Auto-wrap image urls in url(...)
745 if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)$/gi.test( data.background ) ) {
746 slide.setAttribute( 'data-background-image', data.background );
749 element.style.background = data.background;
753 // Create a hash for this combination of background settings.
754 // This is used to determine when two slide backgrounds are
756 if( data.background || data.backgroundColor || data.backgroundImage || data.backgroundVideo || data.backgroundIframe ) {
757 element.setAttribute( 'data-background-hash', data.background +
758 data.backgroundSize +
759 data.backgroundImage +
760 data.backgroundVideo +
761 data.backgroundIframe +
762 data.backgroundColor +
763 data.backgroundRepeat +
764 data.backgroundPosition +
765 data.backgroundTransition );
768 // Additional and optional background properties
769 if( data.backgroundSize ) element.style.backgroundSize = data.backgroundSize;
770 if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
771 if( data.backgroundRepeat ) element.style.backgroundRepeat = data.backgroundRepeat;
772 if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition;
773 if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
775 container.appendChild( element );
777 // If backgrounds are being recreated, clear old classes
778 slide.classList.remove( 'has-dark-background' );
779 slide.classList.remove( 'has-light-background' );
781 // If this slide has a background color, add a class that
782 // signals if it is light or dark. If the slide has no background
783 // color, no class will be set
784 var computedBackgroundColor = window.getComputedStyle( element ).backgroundColor;
785 if( computedBackgroundColor ) {
786 var rgb = colorToRgb( computedBackgroundColor );
788 // Ignore fully transparent backgrounds. Some browsers return
789 // rgba(0,0,0,0) when reading the computed background color of
790 // an element with no background
791 if( rgb && rgb.a !== 0 ) {
792 if( colorBrightness( computedBackgroundColor ) < 128 ) {
793 slide.classList.add( 'has-dark-background' );
796 slide.classList.add( 'has-light-background' );
806 * Registers a listener to postMessage events, this makes it
807 * possible to call all reveal.js API methods from another
808 * window. For example:
810 * revealWindow.postMessage( JSON.stringify({
815 function setupPostMessage() {
817 if( config.postMessage ) {
818 window.addEventListener( 'message', function ( event ) {
819 var data = event.data;
821 // Make sure we're dealing with JSON
822 if( typeof data === 'string' && data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
823 data = JSON.parse( data );
825 // Check if the requested method can be found
826 if( data.method && typeof Reveal[data.method] === 'function' ) {
827 Reveal[data.method].apply( Reveal, data.args );
836 * Applies the configuration settings from the config
837 * object. May be called multiple times.
839 function configure( options ) {
841 var numberOfSlides = dom.wrapper.querySelectorAll( SLIDES_SELECTOR ).length;
843 dom.wrapper.classList.remove( config.transition );
845 // New config options may be passed when this method
846 // is invoked through the API after initialization
847 if( typeof options === 'object' ) extend( config, options );
849 // Force linear transition based on browser capabilities
850 if( features.transforms3d === false ) config.transition = 'linear';
852 dom.wrapper.classList.add( config.transition );
854 dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
855 dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
857 dom.controls.style.display = config.controls ? 'block' : 'none';
858 dom.progress.style.display = config.progress ? 'block' : 'none';
861 dom.wrapper.classList.add( 'rtl' );
864 dom.wrapper.classList.remove( 'rtl' );
867 if( config.center ) {
868 dom.wrapper.classList.add( 'center' );
871 dom.wrapper.classList.remove( 'center' );
874 // Exit the paused mode if it was configured off
875 if( config.pause === false ) {
879 if( config.showNotes ) {
880 dom.speakerNotes.classList.add( 'visible' );
883 dom.speakerNotes.classList.remove( 'visible' );
886 if( config.mouseWheel ) {
887 document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
888 document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
891 document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
892 document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
896 if( config.rollingLinks ) {
897 enableRollingLinks();
900 disableRollingLinks();
903 // Iframe link previews
904 if( config.previewLinks ) {
905 enablePreviewLinks();
908 disablePreviewLinks();
909 enablePreviewLinks( '[data-preview-link]' );
912 // Remove existing auto-slide controls
913 if( autoSlidePlayer ) {
914 autoSlidePlayer.destroy();
915 autoSlidePlayer = null;
918 // Generate auto-slide controls if needed
919 if( numberOfSlides > 1 && config.autoSlide && config.autoSlideStoppable && features.canvas && features.requestAnimationFrame ) {
920 autoSlidePlayer = new Playback( dom.wrapper, function() {
921 return Math.min( Math.max( ( Date.now() - autoSlideStartTime ) / autoSlide, 0 ), 1 );
924 autoSlidePlayer.on( 'click', onAutoSlidePlayerClick );
925 autoSlidePaused = false;
928 // When fragments are turned off they should be visible
929 if( config.fragments === false ) {
930 toArray( dom.slides.querySelectorAll( '.fragment' ) ).forEach( function( element ) {
931 element.classList.add( 'visible' );
932 element.classList.remove( 'current-fragment' );
941 * Binds all event listeners.
943 function addEventListeners() {
945 eventsAreBound = true;
947 window.addEventListener( 'hashchange', onWindowHashChange, false );
948 window.addEventListener( 'resize', onWindowResize, false );
951 dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
952 dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
953 dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
955 // Support pointer-style touch interaction as well
956 if( window.navigator.pointerEnabled ) {
957 // IE 11 uses un-prefixed version of pointer events
958 dom.wrapper.addEventListener( 'pointerdown', onPointerDown, false );
959 dom.wrapper.addEventListener( 'pointermove', onPointerMove, false );
960 dom.wrapper.addEventListener( 'pointerup', onPointerUp, false );
962 else if( window.navigator.msPointerEnabled ) {
963 // IE 10 uses prefixed version of pointer events
964 dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
965 dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
966 dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
970 if( config.keyboard ) {
971 document.addEventListener( 'keydown', onDocumentKeyDown, false );
972 document.addEventListener( 'keypress', onDocumentKeyPress, false );
975 if( config.progress && dom.progress ) {
976 dom.progress.addEventListener( 'click', onProgressClicked, false );
979 if( config.focusBodyOnPageVisibilityChange ) {
980 var visibilityChange;
982 if( 'hidden' in document ) {
983 visibilityChange = 'visibilitychange';
985 else if( 'msHidden' in document ) {
986 visibilityChange = 'msvisibilitychange';
988 else if( 'webkitHidden' in document ) {
989 visibilityChange = 'webkitvisibilitychange';
992 if( visibilityChange ) {
993 document.addEventListener( visibilityChange, onPageVisibilityChange, false );
997 // Listen to both touch and click events, in case the device
999 var pointerEvents = [ 'touchstart', 'click' ];
1001 // Only support touch for Android, fixes double navigations in
1003 if( navigator.userAgent.match( /android/gi ) ) {
1004 pointerEvents = [ 'touchstart' ];
1007 pointerEvents.forEach( function( eventName ) {
1008 dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
1009 dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
1010 dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
1011 dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
1012 dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
1013 dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
1019 * Unbinds all event listeners.
1021 function removeEventListeners() {
1023 eventsAreBound = false;
1025 document.removeEventListener( 'keydown', onDocumentKeyDown, false );
1026 document.removeEventListener( 'keypress', onDocumentKeyPress, false );
1027 window.removeEventListener( 'hashchange', onWindowHashChange, false );
1028 window.removeEventListener( 'resize', onWindowResize, false );
1030 dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
1031 dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
1032 dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
1035 if( window.navigator.pointerEnabled ) {
1036 dom.wrapper.removeEventListener( 'pointerdown', onPointerDown, false );
1037 dom.wrapper.removeEventListener( 'pointermove', onPointerMove, false );
1038 dom.wrapper.removeEventListener( 'pointerup', onPointerUp, false );
1041 else if( window.navigator.msPointerEnabled ) {
1042 dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
1043 dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
1044 dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
1047 if ( config.progress && dom.progress ) {
1048 dom.progress.removeEventListener( 'click', onProgressClicked, false );
1051 [ 'touchstart', 'click' ].forEach( function( eventName ) {
1052 dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
1053 dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
1054 dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
1055 dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
1056 dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
1057 dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
1063 * Extend object a with the properties of object b.
1064 * If there's a conflict, object b takes precedence.
1066 function extend( a, b ) {
1075 * Converts the target object to an array.
1077 function toArray( o ) {
1079 return Array.prototype.slice.call( o );
1084 * Utility for deserializing a value.
1086 function deserialize( value ) {
1088 if( typeof value === 'string' ) {
1089 if( value === 'null' ) return null;
1090 else if( value === 'true' ) return true;
1091 else if( value === 'false' ) return false;
1092 else if( value.match( /^\d+$/ ) ) return parseFloat( value );
1100 * Measures the distance in pixels between point a
1103 * @param {Object} a point with x/y properties
1104 * @param {Object} b point with x/y properties
1106 function distanceBetween( a, b ) {
1111 return Math.sqrt( dx*dx + dy*dy );
1116 * Applies a CSS transform to the target element.
1118 function transformElement( element, transform ) {
1120 element.style.WebkitTransform = transform;
1121 element.style.MozTransform = transform;
1122 element.style.msTransform = transform;
1123 element.style.transform = transform;
1128 * Applies CSS transforms to the slides container. The container
1129 * is transformed from two separate sources: layout and the overview
1132 function transformSlides( transforms ) {
1134 // Pick up new transforms from arguments
1135 if( typeof transforms.layout === 'string' ) slidesTransform.layout = transforms.layout;
1136 if( typeof transforms.overview === 'string' ) slidesTransform.overview = transforms.overview;
1138 // Apply the transforms to the slides container
1139 if( slidesTransform.layout ) {
1140 transformElement( dom.slides, slidesTransform.layout + ' ' + slidesTransform.overview );
1143 transformElement( dom.slides, slidesTransform.overview );
1149 * Injects the given CSS styles into the DOM.
1151 function injectStyleSheet( value ) {
1153 var tag = document.createElement( 'style' );
1154 tag.type = 'text/css';
1155 if( tag.styleSheet ) {
1156 tag.styleSheet.cssText = value;
1159 tag.appendChild( document.createTextNode( value ) );
1161 document.getElementsByTagName( 'head' )[0].appendChild( tag );
1166 * Converts various color input formats to an {r:0,g:0,b:0} object.
1168 * @param {String} color The string representation of a color,
1169 * the following formats are supported:
1174 function colorToRgb( color ) {
1176 var hex3 = color.match( /^#([0-9a-f]{3})$/i );
1177 if( hex3 && hex3[1] ) {
1180 r: parseInt( hex3.charAt( 0 ), 16 ) * 0x11,
1181 g: parseInt( hex3.charAt( 1 ), 16 ) * 0x11,
1182 b: parseInt( hex3.charAt( 2 ), 16 ) * 0x11
1186 var hex6 = color.match( /^#([0-9a-f]{6})$/i );
1187 if( hex6 && hex6[1] ) {
1190 r: parseInt( hex6.substr( 0, 2 ), 16 ),
1191 g: parseInt( hex6.substr( 2, 2 ), 16 ),
1192 b: parseInt( hex6.substr( 4, 2 ), 16 )
1196 var rgb = color.match( /^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i );
1199 r: parseInt( rgb[1], 10 ),
1200 g: parseInt( rgb[2], 10 ),
1201 b: parseInt( rgb[3], 10 )
1205 var rgba = color.match( /^rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\,\s*([\d]+|[\d]*.[\d]+)\s*\)$/i );
1208 r: parseInt( rgba[1], 10 ),
1209 g: parseInt( rgba[2], 10 ),
1210 b: parseInt( rgba[3], 10 ),
1211 a: parseFloat( rgba[4] )
1220 * Calculates brightness on a scale of 0-255.
1222 * @param color See colorStringToRgb for supported formats.
1224 function colorBrightness( color ) {
1226 if( typeof color === 'string' ) color = colorToRgb( color );
1229 return ( color.r * 299 + color.g * 587 + color.b * 114 ) / 1000;
1237 * Retrieves the height of the given element by looking
1238 * at the position and height of its immediate children.
1240 function getAbsoluteHeight( element ) {
1245 var absoluteChildren = 0;
1247 toArray( element.childNodes ).forEach( function( child ) {
1249 if( typeof child.offsetTop === 'number' && child.style ) {
1250 // Count # of abs children
1251 if( window.getComputedStyle( child ).position === 'absolute' ) {
1252 absoluteChildren += 1;
1255 height = Math.max( height, child.offsetTop + child.offsetHeight );
1260 // If there are no absolute children, use offsetHeight
1261 if( absoluteChildren === 0 ) {
1262 height = element.offsetHeight;
1272 * Returns the remaining height within the parent of the
1275 * remaining height = [ configured parent height ] - [ current parent height ]
1277 function getRemainingHeight( element, height ) {
1279 height = height || 0;
1282 var newHeight, oldHeight = element.style.height;
1284 // Change the .stretch element height to 0 in order find the height of all
1285 // the other elements
1286 element.style.height = '0px';
1287 newHeight = height - element.parentNode.offsetHeight;
1289 // Restore the old height, just in case
1290 element.style.height = oldHeight + 'px';
1300 * Checks if this instance is being used to print a PDF.
1302 function isPrintingPDF() {
1304 return ( /print-pdf/gi ).test( window.location.search );
1309 * Hides the address bar if we're on a mobile device.
1311 function hideAddressBar() {
1313 if( config.hideAddressBar && isMobileDevice ) {
1314 // Events that should trigger the address bar to hide
1315 window.addEventListener( 'load', removeAddressBar, false );
1316 window.addEventListener( 'orientationchange', removeAddressBar, false );
1322 * Causes the address bar to hide on mobile devices,
1323 * more vertical space ftw.
1325 function removeAddressBar() {
1327 setTimeout( function() {
1328 window.scrollTo( 0, 1 );
1334 * Dispatches an event of the specified type from the
1335 * reveal DOM element.
1337 function dispatchEvent( type, args ) {
1339 var event = document.createEvent( 'HTMLEvents', 1, 2 );
1340 event.initEvent( type, true, true );
1341 extend( event, args );
1342 dom.wrapper.dispatchEvent( event );
1344 // If we're in an iframe, post each reveal.js event to the
1345 // parent window. Used by the notes plugin
1346 if( config.postMessageEvents && window.parent !== window.self ) {
1347 window.parent.postMessage( JSON.stringify({ namespace: 'reveal', eventName: type, state: getState() }), '*' );
1353 * Wrap all links in 3D goodness.
1355 function enableRollingLinks() {
1357 if( features.transforms3d && !( 'msPerspective' in document.body.style ) ) {
1358 var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a' );
1360 for( var i = 0, len = anchors.length; i < len; i++ ) {
1361 var anchor = anchors[i];
1363 if( anchor.textContent && !anchor.querySelector( '*' ) && ( !anchor.className || !anchor.classList.contains( anchor, 'roll' ) ) ) {
1364 var span = document.createElement('span');
1365 span.setAttribute('data-title', anchor.text);
1366 span.innerHTML = anchor.innerHTML;
1368 anchor.classList.add( 'roll' );
1369 anchor.innerHTML = '';
1370 anchor.appendChild(span);
1378 * Unwrap all 3D links.
1380 function disableRollingLinks() {
1382 var anchors = dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ' a.roll' );
1384 for( var i = 0, len = anchors.length; i < len; i++ ) {
1385 var anchor = anchors[i];
1386 var span = anchor.querySelector( 'span' );
1389 anchor.classList.remove( 'roll' );
1390 anchor.innerHTML = span.innerHTML;
1397 * Bind preview frame links.
1399 function enablePreviewLinks( selector ) {
1401 var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
1403 anchors.forEach( function( element ) {
1404 if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
1405 element.addEventListener( 'click', onPreviewLinkClicked, false );
1412 * Unbind preview frame links.
1414 function disablePreviewLinks() {
1416 var anchors = toArray( document.querySelectorAll( 'a' ) );
1418 anchors.forEach( function( element ) {
1419 if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
1420 element.removeEventListener( 'click', onPreviewLinkClicked, false );
1427 * Opens a preview window for the target URL.
1429 function showPreview( url ) {
1433 dom.overlay = document.createElement( 'div' );
1434 dom.overlay.classList.add( 'overlay' );
1435 dom.overlay.classList.add( 'overlay-preview' );
1436 dom.wrapper.appendChild( dom.overlay );
1438 dom.overlay.innerHTML = [
1440 '<a class="close" href="#"><span class="icon"></span></a>',
1441 '<a class="external" href="'+ url +'" target="_blank"><span class="icon"></span></a>',
1443 '<div class="spinner"></div>',
1444 '<div class="viewport">',
1445 '<iframe src="'+ url +'"></iframe>',
1449 dom.overlay.querySelector( 'iframe' ).addEventListener( 'load', function( event ) {
1450 dom.overlay.classList.add( 'loaded' );
1453 dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
1455 event.preventDefault();
1458 dom.overlay.querySelector( '.external' ).addEventListener( 'click', function( event ) {
1462 setTimeout( function() {
1463 dom.overlay.classList.add( 'visible' );
1469 * Opens a overlay window with help material.
1471 function showHelp() {
1477 dom.overlay = document.createElement( 'div' );
1478 dom.overlay.classList.add( 'overlay' );
1479 dom.overlay.classList.add( 'overlay-help' );
1480 dom.wrapper.appendChild( dom.overlay );
1482 var html = '<p class="title">Keyboard Shortcuts</p><br/>';
1484 html += '<table><th>KEY</th><th>ACTION</th>';
1485 for( var key in keyboardShortcuts ) {
1486 html += '<tr><td>' + key + '</td><td>' + keyboardShortcuts[ key ] + '</td></tr>';
1491 dom.overlay.innerHTML = [
1493 '<a class="close" href="#"><span class="icon"></span></a>',
1495 '<div class="viewport">',
1496 '<div class="viewport-inner">'+ html +'</div>',
1500 dom.overlay.querySelector( '.close' ).addEventListener( 'click', function( event ) {
1502 event.preventDefault();
1505 setTimeout( function() {
1506 dom.overlay.classList.add( 'visible' );
1514 * Closes any currently open overlay.
1516 function closeOverlay() {
1519 dom.overlay.parentNode.removeChild( dom.overlay );
1526 * Applies JavaScript-controlled layout rules to the
1531 if( dom.wrapper && !isPrintingPDF() ) {
1533 var size = getComputedSlideSize();
1535 var slidePadding = 20; // TODO Dig this out of DOM
1537 // Layout the contents of the slides
1538 layoutSlideContents( config.width, config.height, slidePadding );
1540 dom.slides.style.width = size.width + 'px';
1541 dom.slides.style.height = size.height + 'px';
1543 // Determine scale of content to fit within available space
1544 scale = Math.min( size.presentationWidth / size.width, size.presentationHeight / size.height );
1546 // Respect max/min scale settings
1547 scale = Math.max( scale, config.minScale );
1548 scale = Math.min( scale, config.maxScale );
1550 // Don't apply any scaling styles if scale is 1
1552 dom.slides.style.zoom = '';
1553 dom.slides.style.left = '';
1554 dom.slides.style.top = '';
1555 dom.slides.style.bottom = '';
1556 dom.slides.style.right = '';
1557 transformSlides( { layout: '' } );
1560 // Use zoom to scale up in desktop Chrome so that content
1561 // remains crisp. We don't use zoom to scale down since that
1562 // can lead to shifts in text layout/line breaks.
1563 if( scale > 1 && !isMobileDevice && /chrome/i.test( navigator.userAgent ) && typeof dom.slides.style.zoom !== 'undefined' ) {
1564 dom.slides.style.zoom = scale;
1565 dom.slides.style.left = '';
1566 dom.slides.style.top = '';
1567 dom.slides.style.bottom = '';
1568 dom.slides.style.right = '';
1569 transformSlides( { layout: '' } );
1571 // Apply scale transform as a fallback
1573 dom.slides.style.zoom = '';
1574 dom.slides.style.left = '50%';
1575 dom.slides.style.top = '50%';
1576 dom.slides.style.bottom = 'auto';
1577 dom.slides.style.right = 'auto';
1578 transformSlides( { layout: 'translate(-50%, -50%) scale('+ scale +')' } );
1582 // Select all slides, vertical and horizontal
1583 var slides = toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) );
1585 for( var i = 0, len = slides.length; i < len; i++ ) {
1586 var slide = slides[ i ];
1588 // Don't bother updating invisible slides
1589 if( slide.style.display === 'none' ) {
1593 if( config.center || slide.classList.contains( 'center' ) ) {
1594 // Vertical stacks are not centred since their section
1596 if( slide.classList.contains( 'stack' ) ) {
1597 slide.style.top = 0;
1600 slide.style.top = Math.max( ( ( size.height - getAbsoluteHeight( slide ) ) / 2 ) - slidePadding, 0 ) + 'px';
1604 slide.style.top = '';
1617 * Applies layout logic to the contents of all slides in
1620 function layoutSlideContents( width, height, padding ) {
1622 // Handle sizing of elements with the 'stretch' class
1623 toArray( dom.slides.querySelectorAll( 'section > .stretch' ) ).forEach( function( element ) {
1625 // Determine how much vertical space we can use
1626 var remainingHeight = getRemainingHeight( element, height );
1628 // Consider the aspect ratio of media elements
1629 if( /(img|video)/gi.test( element.nodeName ) ) {
1630 var nw = element.naturalWidth || element.videoWidth,
1631 nh = element.naturalHeight || element.videoHeight;
1633 var es = Math.min( width / nw, remainingHeight / nh );
1635 element.style.width = ( nw * es ) + 'px';
1636 element.style.height = ( nh * es ) + 'px';
1640 element.style.width = width + 'px';
1641 element.style.height = remainingHeight + 'px';
1649 * Calculates the computed pixel size of our slides. These
1650 * values are based on the width and height configuration
1653 function getComputedSlideSize( presentationWidth, presentationHeight ) {
1657 width: config.width,
1658 height: config.height,
1660 // Presentation size
1661 presentationWidth: presentationWidth || dom.wrapper.offsetWidth,
1662 presentationHeight: presentationHeight || dom.wrapper.offsetHeight
1665 // Reduce available space by margin
1666 size.presentationWidth -= ( size.presentationWidth * config.margin );
1667 size.presentationHeight -= ( size.presentationHeight * config.margin );
1669 // Slide width may be a percentage of available width
1670 if( typeof size.width === 'string' && /%$/.test( size.width ) ) {
1671 size.width = parseInt( size.width, 10 ) / 100 * size.presentationWidth;
1674 // Slide height may be a percentage of available height
1675 if( typeof size.height === 'string' && /%$/.test( size.height ) ) {
1676 size.height = parseInt( size.height, 10 ) / 100 * size.presentationHeight;
1684 * Stores the vertical index of a stack so that the same
1685 * vertical slide can be selected when navigating to and
1688 * @param {HTMLElement} stack The vertical stack element
1689 * @param {int} v Index to memorize
1691 function setPreviousVerticalIndex( stack, v ) {
1693 if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
1694 stack.setAttribute( 'data-previous-indexv', v || 0 );
1700 * Retrieves the vertical index which was stored using
1701 * #setPreviousVerticalIndex() or 0 if no previous index
1704 * @param {HTMLElement} stack The vertical stack element
1706 function getPreviousVerticalIndex( stack ) {
1708 if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
1709 // Prefer manually defined start-indexv
1710 var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
1712 return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
1720 * Displays the overview of slides (quick nav) by scaling
1721 * down and arranging all slide elements.
1723 function activateOverview() {
1725 // Only proceed if enabled in config
1726 if( config.overview && !isOverview() ) {
1730 dom.wrapper.classList.add( 'overview' );
1731 dom.wrapper.classList.remove( 'overview-deactivating' );
1733 if( features.overviewTransitions ) {
1734 setTimeout( function() {
1735 dom.wrapper.classList.add( 'overview-animated' );
1739 // Don't auto-slide while in overview mode
1742 // Move the backgrounds element into the slide container to
1743 // that the same scaling is applied
1744 dom.slides.appendChild( dom.background );
1746 // Clicking on an overview slide navigates to it
1747 toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
1748 if( !slide.classList.contains( 'stack' ) ) {
1749 slide.addEventListener( 'click', onOverviewSlideClicked, true );
1753 updateSlidesVisibility();
1759 // Notify observers of the overview showing
1760 dispatchEvent( 'overviewshown', {
1763 'currentSlide': currentSlide
1771 * Uses CSS transforms to position all slides in a grid for
1772 * display inside of the overview mode.
1774 function layoutOverview() {
1777 var slideWidth = config.width + margin,
1778 slideHeight = config.height + margin;
1780 // Reverse in RTL mode
1782 slideWidth = -slideWidth;
1786 toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( hslide, h ) {
1787 hslide.setAttribute( 'data-index-h', h );
1788 transformElement( hslide, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
1790 if( hslide.classList.contains( 'stack' ) ) {
1792 toArray( hslide.querySelectorAll( 'section' ) ).forEach( function( vslide, v ) {
1793 vslide.setAttribute( 'data-index-h', h );
1794 vslide.setAttribute( 'data-index-v', v );
1796 transformElement( vslide, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
1802 // Layout slide backgrounds
1803 toArray( dom.background.childNodes ).forEach( function( hbackground, h ) {
1804 transformElement( hbackground, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
1806 toArray( hbackground.querySelectorAll( '.slide-background' ) ).forEach( function( vbackground, v ) {
1807 transformElement( vbackground, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
1814 * Moves the overview viewport to the current slides.
1815 * Called each time the current slide changes.
1817 function updateOverview() {
1820 var slideWidth = config.width + margin,
1821 slideHeight = config.height + margin;
1823 // Reverse in RTL mode
1825 slideWidth = -slideWidth;
1830 'translateX('+ ( -indexh * slideWidth ) +'px)',
1831 'translateY('+ ( -indexv * slideHeight ) +'px)',
1832 'translateZ('+ ( window.innerWidth < 400 ? -1000 : -2500 ) +'px)'
1839 * Exits the slide overview and enters the currently
1842 function deactivateOverview() {
1844 // Only proceed if enabled in config
1845 if( config.overview ) {
1849 dom.wrapper.classList.remove( 'overview' );
1850 dom.wrapper.classList.remove( 'overview-animated' );
1852 // Temporarily add a class so that transitions can do different things
1853 // depending on whether they are exiting/entering overview, or just
1854 // moving from slide to slide
1855 dom.wrapper.classList.add( 'overview-deactivating' );
1857 setTimeout( function () {
1858 dom.wrapper.classList.remove( 'overview-deactivating' );
1861 // Move the background element back out
1862 dom.wrapper.appendChild( dom.background );
1864 // Clean up changes made to slides
1865 toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
1866 transformElement( slide, '' );
1868 slide.removeEventListener( 'click', onOverviewSlideClicked, true );
1871 // Clean up changes made to backgrounds
1872 toArray( dom.background.querySelectorAll( '.slide-background' ) ).forEach( function( background ) {
1873 transformElement( background, '' );
1876 transformSlides( { overview: '' } );
1878 slide( indexh, indexv );
1884 // Notify observers of the overview hiding
1885 dispatchEvent( 'overviewhidden', {
1888 'currentSlide': currentSlide
1895 * Toggles the slide overview mode on and off.
1897 * @param {Boolean} override Optional flag which overrides the
1898 * toggle logic and forcibly sets the desired state. True means
1899 * overview is open, false means it's closed.
1901 function toggleOverview( override ) {
1903 if( typeof override === 'boolean' ) {
1904 override ? activateOverview() : deactivateOverview();
1907 isOverview() ? deactivateOverview() : activateOverview();
1913 * Checks if the overview is currently active.
1915 * @return {Boolean} true if the overview is active,
1918 function isOverview() {
1925 * Checks if the current or specified slide is vertical
1926 * (nested within another slide).
1928 * @param {HTMLElement} slide [optional] The slide to check
1931 function isVerticalSlide( slide ) {
1933 // Prefer slide argument, otherwise use current slide
1934 slide = slide ? slide : currentSlide;
1936 return slide && slide.parentNode && !!slide.parentNode.nodeName.match( /section/i );
1941 * Handling the fullscreen functionality via the fullscreen API
1943 * @see http://fullscreen.spec.whatwg.org/
1944 * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
1946 function enterFullscreen() {
1948 var element = document.body;
1950 // Check which implementation is available
1951 var requestMethod = element.requestFullScreen ||
1952 element.webkitRequestFullscreen ||
1953 element.webkitRequestFullScreen ||
1954 element.mozRequestFullScreen ||
1955 element.msRequestFullscreen;
1957 if( requestMethod ) {
1958 requestMethod.apply( element );
1964 * Enters the paused mode which fades everything on screen to
1969 if( config.pause ) {
1970 var wasPaused = dom.wrapper.classList.contains( 'paused' );
1973 dom.wrapper.classList.add( 'paused' );
1975 if( wasPaused === false ) {
1976 dispatchEvent( 'paused' );
1983 * Exits from the paused mode.
1987 var wasPaused = dom.wrapper.classList.contains( 'paused' );
1988 dom.wrapper.classList.remove( 'paused' );
1993 dispatchEvent( 'resumed' );
1999 * Toggles the paused mode on and off.
2001 function togglePause( override ) {
2003 if( typeof override === 'boolean' ) {
2004 override ? pause() : resume();
2007 isPaused() ? resume() : pause();
2013 * Checks if we are currently in the paused mode.
2015 function isPaused() {
2017 return dom.wrapper.classList.contains( 'paused' );
2022 * Toggles the auto slide mode on and off.
2024 * @param {Boolean} override Optional flag which sets the desired state.
2025 * True means autoplay starts, false means it stops.
2028 function toggleAutoSlide( override ) {
2030 if( typeof override === 'boolean' ) {
2031 override ? resumeAutoSlide() : pauseAutoSlide();
2035 autoSlidePaused ? resumeAutoSlide() : pauseAutoSlide();
2041 * Checks if the auto slide mode is currently on.
2043 function isAutoSliding() {
2045 return !!( autoSlide && !autoSlidePaused );
2050 * Steps from the current point in the presentation to the
2051 * slide which matches the specified horizontal and vertical
2054 * @param {int} h Horizontal index of the target slide
2055 * @param {int} v Vertical index of the target slide
2056 * @param {int} f Optional index of a fragment within the
2057 * target slide to activate
2058 * @param {int} o Optional origin for use in multimaster environments
2060 function slide( h, v, f, o ) {
2062 // Remember where we were at before
2063 previousSlide = currentSlide;
2065 // Query all horizontal slides in the deck
2066 var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
2068 // If no vertical index is specified and the upcoming slide is a
2069 // stack, resume at its previous vertical index
2070 if( v === undefined && !isOverview() ) {
2071 v = getPreviousVerticalIndex( horizontalSlides[ h ] );
2074 // If we were on a vertical stack, remember what vertical index
2075 // it was on so we can resume at the same position when returning
2076 if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
2077 setPreviousVerticalIndex( previousSlide.parentNode, indexv );
2080 // Remember the state before this slide
2081 var stateBefore = state.concat();
2083 // Reset the state array
2086 var indexhBefore = indexh || 0,
2087 indexvBefore = indexv || 0;
2089 // Activate and transition to the new slide
2090 indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
2091 indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
2093 // Update the visibility of slides now that the indices have changed
2094 updateSlidesVisibility();
2098 // Apply the new state
2099 stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
2100 // Check if this state existed on the previous slide. If it
2101 // did, we will avoid adding it repeatedly
2102 for( var j = 0; j < stateBefore.length; j++ ) {
2103 if( stateBefore[j] === state[i] ) {
2104 stateBefore.splice( j, 1 );
2109 document.documentElement.classList.add( state[i] );
2111 // Dispatch custom event matching the state's name
2112 dispatchEvent( state[i] );
2115 // Clean up the remains of the previous state
2116 while( stateBefore.length ) {
2117 document.documentElement.classList.remove( stateBefore.pop() );
2120 // Update the overview if it's currently active
2121 if( isOverview() ) {
2125 // Find the current horizontal slide and any possible vertical slides
2127 var currentHorizontalSlide = horizontalSlides[ indexh ],
2128 currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
2130 // Store references to the previous and current slides
2131 currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
2133 // Show fragment, if specified
2134 if( typeof f !== 'undefined' ) {
2135 navigateFragment( f );
2138 // Dispatch an event if the slide changed
2139 var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
2140 if( slideChanged ) {
2141 dispatchEvent( 'slidechanged', {
2144 'previousSlide': previousSlide,
2145 'currentSlide': currentSlide,
2150 // Ensure that the previous slide is never the same as the current
2151 previousSlide = null;
2154 // Solves an edge case where the previous slide maintains the
2155 // 'present' class when navigating between adjacent vertical
2157 if( previousSlide ) {
2158 previousSlide.classList.remove( 'present' );
2159 previousSlide.setAttribute( 'aria-hidden', 'true' );
2161 // Reset all slides upon navigate to home
2163 if ( dom.wrapper.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
2164 // Launch async task
2165 setTimeout( function () {
2166 var slides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
2167 for( i in slides ) {
2170 setPreviousVerticalIndex( slides[i], 0 );
2177 // Handle embedded content
2178 if( slideChanged || !previousSlide ) {
2179 stopEmbeddedContent( previousSlide );
2180 startEmbeddedContent( currentSlide );
2183 // Announce the current slide contents, for screen readers
2184 dom.statusDiv.textContent = currentSlide.textContent;
2190 updateSlideNumber();
2193 // Update the URL hash
2201 * Syncs the presentation with the current DOM. Useful
2202 * when new slides or control elements are added or when
2203 * the configuration has changed.
2207 // Subscribe to input
2208 removeEventListeners();
2209 addEventListeners();
2211 // Force a layout to make sure the current config is accounted for
2214 // Reflect the current autoSlide value
2215 autoSlide = config.autoSlide;
2217 // Start auto-sliding if it's enabled
2220 // Re-create the slide backgrounds
2221 createBackgrounds();
2223 // Write the current hash to the URL
2230 updateBackground( true );
2231 updateSlideNumber();
2232 updateSlidesVisibility();
2235 formatEmbeddedContent();
2236 startEmbeddedContent( currentSlide );
2238 if( isOverview() ) {
2245 * Resets all vertical slides so that only the first
2248 function resetVerticalSlides() {
2250 var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
2251 horizontalSlides.forEach( function( horizontalSlide ) {
2253 var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
2254 verticalSlides.forEach( function( verticalSlide, y ) {
2257 verticalSlide.classList.remove( 'present' );
2258 verticalSlide.classList.remove( 'past' );
2259 verticalSlide.classList.add( 'future' );
2260 verticalSlide.setAttribute( 'aria-hidden', 'true' );
2270 * Sorts and formats all of fragments in the
2273 function sortAllFragments() {
2275 var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
2276 horizontalSlides.forEach( function( horizontalSlide ) {
2278 var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
2279 verticalSlides.forEach( function( verticalSlide, y ) {
2281 sortFragments( verticalSlide.querySelectorAll( '.fragment' ) );
2285 if( verticalSlides.length === 0 ) sortFragments( horizontalSlide.querySelectorAll( '.fragment' ) );
2292 * Updates one dimension of slides by showing the slide
2293 * with the specified index.
2295 * @param {String} selector A CSS selector that will fetch
2296 * the group of slides we are working with
2297 * @param {Number} index The index of the slide that should be
2300 * @return {Number} The index of the slide that is now shown,
2301 * might differ from the passed in index if it was out of
2304 function updateSlides( selector, index ) {
2306 // Select all slides and convert the NodeList result to
2308 var slides = toArray( dom.wrapper.querySelectorAll( selector ) ),
2309 slidesLength = slides.length;
2311 var printMode = isPrintingPDF();
2313 if( slidesLength ) {
2315 // Should the index loop?
2317 index %= slidesLength;
2320 index = slidesLength + index;
2324 // Enforce max and minimum index bounds
2325 index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
2327 for( var i = 0; i < slidesLength; i++ ) {
2328 var element = slides[i];
2330 var reverse = config.rtl && !isVerticalSlide( element );
2332 element.classList.remove( 'past' );
2333 element.classList.remove( 'present' );
2334 element.classList.remove( 'future' );
2336 // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
2337 element.setAttribute( 'hidden', '' );
2338 element.setAttribute( 'aria-hidden', 'true' );
2340 // If this element contains vertical slides
2341 if( element.querySelector( 'section' ) ) {
2342 element.classList.add( 'stack' );
2345 // If we're printing static slides, all slides are "present"
2347 element.classList.add( 'present' );
2352 // Any element previous to index is given the 'past' class
2353 element.classList.add( reverse ? 'future' : 'past' );
2355 if( config.fragments ) {
2356 var pastFragments = toArray( element.querySelectorAll( '.fragment' ) );
2358 // Show all fragments on prior slides
2359 while( pastFragments.length ) {
2360 var pastFragment = pastFragments.pop();
2361 pastFragment.classList.add( 'visible' );
2362 pastFragment.classList.remove( 'current-fragment' );
2366 else if( i > index ) {
2367 // Any element subsequent to index is given the 'future' class
2368 element.classList.add( reverse ? 'past' : 'future' );
2370 if( config.fragments ) {
2371 var futureFragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
2373 // No fragments in future slides should be visible ahead of time
2374 while( futureFragments.length ) {
2375 var futureFragment = futureFragments.pop();
2376 futureFragment.classList.remove( 'visible' );
2377 futureFragment.classList.remove( 'current-fragment' );
2383 // Mark the current slide as present
2384 slides[index].classList.add( 'present' );
2385 slides[index].removeAttribute( 'hidden' );
2386 slides[index].removeAttribute( 'aria-hidden' );
2388 // If this slide has a state associated with it, add it
2389 // onto the current state of the deck
2390 var slideState = slides[index].getAttribute( 'data-state' );
2392 state = state.concat( slideState.split( ' ' ) );
2397 // Since there are no slides we can't be anywhere beyond the
2407 * Optimization method; hide all slides that are far away
2408 * from the present slide.
2410 function updateSlidesVisibility() {
2412 // Select all slides and convert the NodeList result to
2414 var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ),
2415 horizontalSlidesLength = horizontalSlides.length,
2419 if( horizontalSlidesLength && typeof indexh !== 'undefined' ) {
2421 // The number of steps away from the present slide that will
2423 var viewDistance = isOverview() ? 10 : config.viewDistance;
2425 // Limit view distance on weaker devices
2426 if( isMobileDevice ) {
2427 viewDistance = isOverview() ? 6 : 2;
2430 // All slides need to be visible when exporting to PDF
2431 if( isPrintingPDF() ) {
2432 viewDistance = Number.MAX_VALUE;
2435 for( var x = 0; x < horizontalSlidesLength; x++ ) {
2436 var horizontalSlide = horizontalSlides[x];
2438 var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) ),
2439 verticalSlidesLength = verticalSlides.length;
2441 // Determine how far away this slide is from the present
2442 distanceX = Math.abs( ( indexh || 0 ) - x ) || 0;
2444 // If the presentation is looped, distance should measure
2445 // 1 between the first and last slides
2447 distanceX = Math.abs( ( ( indexh || 0 ) - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
2450 // Show the horizontal slide if it's within the view distance
2451 if( distanceX < viewDistance ) {
2452 showSlide( horizontalSlide );
2455 hideSlide( horizontalSlide );
2458 if( verticalSlidesLength ) {
2460 var oy = getPreviousVerticalIndex( horizontalSlide );
2462 for( var y = 0; y < verticalSlidesLength; y++ ) {
2463 var verticalSlide = verticalSlides[y];
2465 distanceY = x === ( indexh || 0 ) ? Math.abs( ( indexv || 0 ) - y ) : Math.abs( y - oy );
2467 if( distanceX + distanceY < viewDistance ) {
2468 showSlide( verticalSlide );
2471 hideSlide( verticalSlide );
2483 * Pick up notes from the current slide and display tham
2486 * @see `showNotes` config value
2488 function updateNotes() {
2490 if( config.showNotes && dom.speakerNotes && currentSlide && !isPrintingPDF() ) {
2492 dom.speakerNotes.innerHTML = getSlideNotes() || '';
2499 * Updates the progress bar to reflect the current slide.
2501 function updateProgress() {
2503 // Update progress if enabled
2504 if( config.progress && dom.progressbar ) {
2506 dom.progressbar.style.width = getProgress() * dom.wrapper.offsetWidth + 'px';
2513 * Updates the slide number div to reflect the current slide.
2515 * Slide number format can be defined as a string using the
2516 * following variables:
2517 * h: current slide's horizontal index
2518 * v: current slide's vertical index
2519 * c: current slide index (flattened)
2520 * t: total number of slides (flattened)
2522 function updateSlideNumber() {
2524 // Update slide number if enabled
2525 if( config.slideNumber && dom.slideNumber) {
2527 // Default to only showing the current slide number
2530 // Check if a custom slide number format is available
2531 if( typeof config.slideNumber === 'string' ) {
2532 format = config.slideNumber;
2535 dom.slideNumber.innerHTML = format.replace( /h/g, indexh )
2536 .replace( /v/g, indexv )
2537 .replace( /c/g, getSlidePastCount() + 1 )
2538 .replace( /t/g, getTotalSlides() );
2544 * Updates the state of all control/navigation arrows.
2546 function updateControls() {
2548 var routes = availableRoutes();
2549 var fragments = availableFragments();
2551 // Remove the 'enabled' class from all directions
2552 dom.controlsLeft.concat( dom.controlsRight )
2553 .concat( dom.controlsUp )
2554 .concat( dom.controlsDown )
2555 .concat( dom.controlsPrev )
2556 .concat( dom.controlsNext ).forEach( function( node ) {
2557 node.classList.remove( 'enabled' );
2558 node.classList.remove( 'fragmented' );
2561 // Add the 'enabled' class to the available routes
2562 if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
2563 if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
2564 if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
2565 if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
2567 // Prev/next buttons
2568 if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
2569 if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
2571 // Highlight fragment directions
2572 if( currentSlide ) {
2574 // Always apply fragment decorator to prev/next buttons
2575 if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
2576 if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
2578 // Apply fragment decorators to directional buttons based on
2579 // what slide axis they are in
2580 if( isVerticalSlide( currentSlide ) ) {
2581 if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
2582 if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
2585 if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
2586 if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
2594 * Updates the background elements to reflect the current
2597 * @param {Boolean} includeAll If true, the backgrounds of
2598 * all vertical slides (not just the present) will be updated.
2600 function updateBackground( includeAll ) {
2602 var currentBackground = null;
2604 // Reverse past/future classes when in RTL mode
2605 var horizontalPast = config.rtl ? 'future' : 'past',
2606 horizontalFuture = config.rtl ? 'past' : 'future';
2608 // Update the classes of all backgrounds to match the
2609 // states of their slides (past/present/future)
2610 toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
2612 backgroundh.classList.remove( 'past' );
2613 backgroundh.classList.remove( 'present' );
2614 backgroundh.classList.remove( 'future' );
2617 backgroundh.classList.add( horizontalPast );
2619 else if ( h > indexh ) {
2620 backgroundh.classList.add( horizontalFuture );
2623 backgroundh.classList.add( 'present' );
2625 // Store a reference to the current background element
2626 currentBackground = backgroundh;
2629 if( includeAll || h === indexh ) {
2630 toArray( backgroundh.querySelectorAll( '.slide-background' ) ).forEach( function( backgroundv, v ) {
2632 backgroundv.classList.remove( 'past' );
2633 backgroundv.classList.remove( 'present' );
2634 backgroundv.classList.remove( 'future' );
2637 backgroundv.classList.add( 'past' );
2639 else if ( v > indexv ) {
2640 backgroundv.classList.add( 'future' );
2643 backgroundv.classList.add( 'present' );
2645 // Only if this is the present horizontal and vertical slide
2646 if( h === indexh ) currentBackground = backgroundv;
2654 // Stop any currently playing video background
2655 if( previousBackground ) {
2657 var previousVideo = previousBackground.querySelector( 'video' );
2658 if( previousVideo ) previousVideo.pause();
2662 if( currentBackground ) {
2664 // Start video playback
2665 var currentVideo = currentBackground.querySelector( 'video' );
2666 if( currentVideo ) {
2667 if( currentVideo.currentTime > 0 ) currentVideo.currentTime = 0;
2668 currentVideo.play();
2671 var backgroundImageURL = currentBackground.style.backgroundImage || '';
2673 // Restart GIFs (doesn't work in Firefox)
2674 if( /\.gif/i.test( backgroundImageURL ) ) {
2675 currentBackground.style.backgroundImage = '';
2676 window.getComputedStyle( currentBackground ).opacity;
2677 currentBackground.style.backgroundImage = backgroundImageURL;
2680 // Don't transition between identical backgrounds. This
2681 // prevents unwanted flicker.
2682 var previousBackgroundHash = previousBackground ? previousBackground.getAttribute( 'data-background-hash' ) : null;
2683 var currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );
2684 if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== previousBackground ) {
2685 dom.background.classList.add( 'no-transition' );
2688 previousBackground = currentBackground;
2692 // If there's a background brightness flag for this slide,
2693 // bubble it to the .reveal container
2694 if( currentSlide ) {
2695 [ 'has-light-background', 'has-dark-background' ].forEach( function( classToBubble ) {
2696 if( currentSlide.classList.contains( classToBubble ) ) {
2697 dom.wrapper.classList.add( classToBubble );
2700 dom.wrapper.classList.remove( classToBubble );
2705 // Allow the first background to apply without transition
2706 setTimeout( function() {
2707 dom.background.classList.remove( 'no-transition' );
2713 * Updates the position of the parallax background based
2714 * on the current slide index.
2716 function updateParallax() {
2718 if( config.parallaxBackgroundImage ) {
2720 var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
2721 verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
2723 var backgroundSize = dom.background.style.backgroundSize.split( ' ' ),
2724 backgroundWidth, backgroundHeight;
2726 if( backgroundSize.length === 1 ) {
2727 backgroundWidth = backgroundHeight = parseInt( backgroundSize[0], 10 );
2730 backgroundWidth = parseInt( backgroundSize[0], 10 );
2731 backgroundHeight = parseInt( backgroundSize[1], 10 );
2734 var slideWidth = dom.background.offsetWidth,
2735 horizontalSlideCount = horizontalSlides.length,
2736 horizontalOffsetMultiplier,
2739 if( typeof config.parallaxBackgroundHorizontal === 'number' ) {
2740 horizontalOffsetMultiplier = config.parallaxBackgroundHorizontal;
2743 horizontalOffsetMultiplier = ( backgroundWidth - slideWidth ) / ( horizontalSlideCount-1 );
2746 horizontalOffset = horizontalOffsetMultiplier * indexh * -1;
2748 var slideHeight = dom.background.offsetHeight,
2749 verticalSlideCount = verticalSlides.length,
2750 verticalOffsetMultiplier,
2753 if( typeof config.parallaxBackgroundVertical === 'number' ) {
2754 verticalOffsetMultiplier = config.parallaxBackgroundVertical;
2757 verticalOffsetMultiplier = ( backgroundHeight - slideHeight ) / ( verticalSlideCount-1 );
2760 verticalOffset = verticalSlideCount > 0 ? verticalOffsetMultiplier * indexv * 1 : 0;
2762 dom.background.style.backgroundPosition = horizontalOffset + 'px ' + -verticalOffset + 'px';
2769 * Called when the given slide is within the configured view
2770 * distance. Shows the slide element and loads any content
2771 * that is set to load lazily (data-src).
2773 function showSlide( slide ) {
2775 // Show the slide element
2776 slide.style.display = 'block';
2778 // Media elements with data-src attributes
2779 toArray( slide.querySelectorAll( 'img[data-src], video[data-src], audio[data-src]' ) ).forEach( function( element ) {
2780 element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
2781 element.removeAttribute( 'data-src' );
2784 // Media elements with <source> children
2785 toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( media ) {
2788 toArray( media.querySelectorAll( 'source[data-src]' ) ).forEach( function( source ) {
2789 source.setAttribute( 'src', source.getAttribute( 'data-src' ) );
2790 source.removeAttribute( 'data-src' );
2794 // If we rewrote sources for this video/audio element, we need
2795 // to manually tell it to load from its new origin
2802 // Show the corresponding background element
2803 var indices = getIndices( slide );
2804 var background = getSlideBackground( indices.h, indices.v );
2806 background.style.display = 'block';
2808 // If the background contains media, load it
2809 if( background.hasAttribute( 'data-loaded' ) === false ) {
2810 background.setAttribute( 'data-loaded', 'true' );
2812 var backgroundImage = slide.getAttribute( 'data-background-image' ),
2813 backgroundVideo = slide.getAttribute( 'data-background-video' ),
2814 backgroundVideoLoop = slide.hasAttribute( 'data-background-video-loop' ),
2815 backgroundIframe = slide.getAttribute( 'data-background-iframe' );
2818 if( backgroundImage ) {
2819 background.style.backgroundImage = 'url('+ backgroundImage +')';
2822 else if ( backgroundVideo && !isSpeakerNotes() ) {
2823 var video = document.createElement( 'video' );
2825 if( backgroundVideoLoop ) {
2826 video.setAttribute( 'loop', '' );
2829 // Support comma separated lists of video sources
2830 backgroundVideo.split( ',' ).forEach( function( source ) {
2831 video.innerHTML += '<source src="'+ source +'">';
2834 background.appendChild( video );
2837 else if( backgroundIframe ) {
2838 var iframe = document.createElement( 'iframe' );
2839 iframe.setAttribute( 'src', backgroundIframe );
2840 iframe.style.width = '100%';
2841 iframe.style.height = '100%';
2842 iframe.style.maxHeight = '100%';
2843 iframe.style.maxWidth = '100%';
2845 background.appendChild( iframe );
2853 * Called when the given slide is moved outside of the
2854 * configured view distance.
2856 function hideSlide( slide ) {
2858 // Hide the slide element
2859 slide.style.display = 'none';
2861 // Hide the corresponding background element
2862 var indices = getIndices( slide );
2863 var background = getSlideBackground( indices.h, indices.v );
2865 background.style.display = 'none';
2871 * Determine what available routes there are for navigation.
2873 * @return {Object} containing four booleans: left/right/up/down
2875 function availableRoutes() {
2877 var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
2878 verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
2881 left: indexh > 0 || config.loop,
2882 right: indexh < horizontalSlides.length - 1 || config.loop,
2884 down: indexv < verticalSlides.length - 1
2887 // reverse horizontal controls for rtl
2889 var left = routes.left;
2890 routes.left = routes.right;
2891 routes.right = left;
2899 * Returns an object describing the available fragment
2902 * @return {Object} two boolean properties: prev/next
2904 function availableFragments() {
2906 if( currentSlide && config.fragments ) {
2907 var fragments = currentSlide.querySelectorAll( '.fragment' );
2908 var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
2911 prev: fragments.length - hiddenFragments.length > 0,
2912 next: !!hiddenFragments.length
2916 return { prev: false, next: false };
2922 * Enforces origin-specific format rules for embedded media.
2924 function formatEmbeddedContent() {
2926 var _appendParamToIframeSource = function( sourceAttribute, sourceURL, param ) {
2927 toArray( dom.slides.querySelectorAll( 'iframe['+ sourceAttribute +'*="'+ sourceURL +'"]' ) ).forEach( function( el ) {
2928 var src = el.getAttribute( sourceAttribute );
2929 if( src && src.indexOf( param ) === -1 ) {
2930 el.setAttribute( sourceAttribute, src + ( !/\?/.test( src ) ? '?' : '&' ) + param );
2935 // YouTube frames must include "?enablejsapi=1"
2936 _appendParamToIframeSource( 'src', 'youtube.com/embed/', 'enablejsapi=1' );
2937 _appendParamToIframeSource( 'data-src', 'youtube.com/embed/', 'enablejsapi=1' );
2939 // Vimeo frames must include "?api=1"
2940 _appendParamToIframeSource( 'src', 'player.vimeo.com/', 'api=1' );
2941 _appendParamToIframeSource( 'data-src', 'player.vimeo.com/', 'api=1' );
2946 * Start playback of any embedded content inside of
2947 * the targeted slide.
2949 function startEmbeddedContent( slide ) {
2951 if( slide && !isSpeakerNotes() ) {
2953 toArray( slide.querySelectorAll( 'img[src$=".gif"]' ) ).forEach( function( el ) {
2954 // Setting the same unchanged source like this was confirmed
2955 // to work in Chrome, FF & Safari
2956 el.setAttribute( 'src', el.getAttribute( 'src' ) );
2959 // HTML5 media elements
2960 toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
2961 if( el.hasAttribute( 'data-autoplay' ) && typeof el.play === 'function' ) {
2967 toArray( slide.querySelectorAll( 'iframe[src]' ) ).forEach( function( el ) {
2968 startEmbeddedIframe( { target: el } );
2971 // Lazy loading iframes
2972 toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
2973 if( el.getAttribute( 'src' ) !== el.getAttribute( 'data-src' ) ) {
2974 el.removeEventListener( 'load', startEmbeddedIframe ); // remove first to avoid dupes
2975 el.addEventListener( 'load', startEmbeddedIframe );
2976 el.setAttribute( 'src', el.getAttribute( 'data-src' ) );
2984 * "Starts" the content of an embedded iframe using the
2987 function startEmbeddedIframe( event ) {
2989 var iframe = event.target;
2991 // YouTube postMessage API
2992 if( /youtube\.com\/embed\//.test( iframe.getAttribute( 'src' ) ) && iframe.hasAttribute( 'data-autoplay' ) ) {
2993 iframe.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
2995 // Vimeo postMessage API
2996 else if( /player\.vimeo\.com\//.test( iframe.getAttribute( 'src' ) ) && iframe.hasAttribute( 'data-autoplay' ) ) {
2997 iframe.contentWindow.postMessage( '{"method":"play"}', '*' );
2999 // Generic postMessage API
3001 iframe.contentWindow.postMessage( 'slide:start', '*' );
3007 * Stop playback of any embedded content inside of
3008 * the targeted slide.
3010 function stopEmbeddedContent( slide ) {
3012 if( slide && slide.parentNode ) {
3013 // HTML5 media elements
3014 toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
3015 if( !el.hasAttribute( 'data-ignore' ) && typeof el.pause === 'function' ) {
3020 // Generic postMessage API for non-lazy loaded iframes
3021 toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
3022 el.contentWindow.postMessage( 'slide:stop', '*' );
3023 el.removeEventListener( 'load', startEmbeddedIframe );
3026 // YouTube postMessage API
3027 toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
3028 if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
3029 el.contentWindow.postMessage( '{"event":"command","func":"pauseVideo","args":""}', '*' );
3033 // Vimeo postMessage API
3034 toArray( slide.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
3035 if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
3036 el.contentWindow.postMessage( '{"method":"pause"}', '*' );
3040 // Lazy loading iframes
3041 toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
3042 // Only removing the src doesn't actually unload the frame
3043 // in all browsers (Firefox) so we set it to blank first
3044 el.setAttribute( 'src', 'about:blank' );
3045 el.removeAttribute( 'src' );
3052 * Returns the number of past slides. This can be used as a global
3053 * flattened index for slides.
3055 function getSlidePastCount() {
3057 var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
3059 // The number of past slides
3062 // Step through all slides and count the past ones
3063 mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
3065 var horizontalSlide = horizontalSlides[i];
3066 var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
3068 for( var j = 0; j < verticalSlides.length; j++ ) {
3070 // Stop as soon as we arrive at the present
3071 if( verticalSlides[j].classList.contains( 'present' ) ) {
3079 // Stop as soon as we arrive at the present
3080 if( horizontalSlide.classList.contains( 'present' ) ) {
3084 // Don't count the wrapping section for vertical slides
3085 if( horizontalSlide.classList.contains( 'stack' ) === false ) {
3096 * Returns a value ranging from 0-1 that represents
3097 * how far into the presentation we have navigated.
3099 function getProgress() {
3101 // The number of past and total slides
3102 var totalCount = getTotalSlides();
3103 var pastCount = getSlidePastCount();
3105 if( currentSlide ) {
3107 var allFragments = currentSlide.querySelectorAll( '.fragment' );
3109 // If there are fragments in the current slide those should be
3110 // accounted for in the progress.
3111 if( allFragments.length > 0 ) {
3112 var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
3114 // This value represents how big a portion of the slide progress
3115 // that is made up by its fragments (0-1)
3116 var fragmentWeight = 0.9;
3118 // Add fragment progress to the past slide count
3119 pastCount += ( visibleFragments.length / allFragments.length ) * fragmentWeight;
3124 return pastCount / ( totalCount - 1 );
3129 * Checks if this presentation is running inside of the
3130 * speaker notes window.
3132 function isSpeakerNotes() {
3134 return !!window.location.search.match( /receiver/gi );
3139 * Reads the current URL (hash) and navigates accordingly.
3141 function readURL() {
3143 var hash = window.location.hash;
3145 // Attempt to parse the hash as either an index or name
3146 var bits = hash.slice( 2 ).split( '/' ),
3147 name = hash.replace( /#|\//gi, '' );
3149 // If the first bit is invalid and there is a name we can
3150 // assume that this is a named link
3151 if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
3154 // Ensure the named link is a valid HTML ID attribute
3155 if( /^[a-zA-Z][\w:.-]*$/.test( name ) ) {
3156 // Find the slide with the specified ID
3157 element = document.getElementById( name );
3161 // Find the position of the named slide and navigate to it
3162 var indices = Reveal.getIndices( element );
3163 slide( indices.h, indices.v );
3165 // If the slide doesn't exist, navigate to the current slide
3167 slide( indexh || 0, indexv || 0 );
3171 // Read the index components of the hash
3172 var h = parseInt( bits[0], 10 ) || 0,
3173 v = parseInt( bits[1], 10 ) || 0;
3175 if( h !== indexh || v !== indexv ) {
3183 * Updates the page URL (hash) to reflect the current
3186 * @param {Number} delay The time in ms to wait before
3189 function writeURL( delay ) {
3191 if( config.history ) {
3193 // Make sure there's never more than one timeout running
3194 clearTimeout( writeURLTimeout );
3196 // If a delay is specified, timeout this call
3197 if( typeof delay === 'number' ) {
3198 writeURLTimeout = setTimeout( writeURL, delay );
3200 else if( currentSlide ) {
3203 // Attempt to create a named link based on the slide's ID
3204 var id = currentSlide.getAttribute( 'id' );
3206 id = id.replace( /[^a-zA-Z0-9\-\_\:\.]/g, '' );
3209 // If the current slide has an ID, use that as a named link
3210 if( typeof id === 'string' && id.length ) {
3213 // Otherwise use the /h/v index
3215 if( indexh > 0 || indexv > 0 ) url += indexh;
3216 if( indexv > 0 ) url += '/' + indexv;
3219 window.location.hash = url;
3226 * Retrieves the h/v location of the current, or specified,
3229 * @param {HTMLElement} slide If specified, the returned
3230 * index will be for this slide rather than the currently
3233 * @return {Object} { h: <int>, v: <int>, f: <int> }
3235 function getIndices( slide ) {
3237 // By default, return the current indices
3242 // If a slide is specified, return the indices of that slide
3244 var isVertical = isVerticalSlide( slide );
3245 var slideh = isVertical ? slide.parentNode : slide;
3247 // Select all horizontal slides
3248 var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
3250 // Now that we know which the horizontal slide is, get its index
3251 h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
3253 // Assume we're not vertical
3256 // If this is a vertical slide, grab the vertical index
3258 v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
3262 if( !slide && currentSlide ) {
3263 var hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
3264 if( hasFragments ) {
3265 var currentFragment = currentSlide.querySelector( '.current-fragment' );
3266 if( currentFragment && currentFragment.hasAttribute( 'data-fragment-index' ) ) {
3267 f = parseInt( currentFragment.getAttribute( 'data-fragment-index' ), 10 );
3270 f = currentSlide.querySelectorAll( '.fragment.visible' ).length - 1;
3275 return { h: h, v: v, f: f };
3280 * Retrieves the total number of slides in this presentation.
3282 function getTotalSlides() {
3284 return dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
3289 * Returns the slide element matching the specified index.
3291 function getSlide( x, y ) {
3293 var horizontalSlide = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
3294 var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
3296 if( verticalSlides && verticalSlides.length && typeof y === 'number' ) {
3297 return verticalSlides ? verticalSlides[ y ] : undefined;
3300 return horizontalSlide;
3305 * Returns the background element for the given slide.
3306 * All slides, even the ones with no background properties
3307 * defined, have a background element so as long as the
3308 * index is valid an element will be returned.
3310 function getSlideBackground( x, y ) {
3312 // When printing to PDF the slide backgrounds are nested
3313 // inside of the slides
3314 if( isPrintingPDF() ) {
3315 var slide = getSlide( x, y );
3317 var background = slide.querySelector( '.slide-background' );
3318 if( background && background.parentNode === slide ) {
3326 var horizontalBackground = dom.wrapper.querySelectorAll( '.backgrounds>.slide-background' )[ x ];
3327 var verticalBackgrounds = horizontalBackground && horizontalBackground.querySelectorAll( '.slide-background' );
3329 if( verticalBackgrounds && verticalBackgrounds.length && typeof y === 'number' ) {
3330 return verticalBackgrounds ? verticalBackgrounds[ y ] : undefined;
3333 return horizontalBackground;
3338 * Retrieves the speaker notes from a slide. Notes can be
3339 * defined in two ways:
3340 * 1. As a data-notes attribute on the slide <section>
3341 * 2. As an <aside class="notes"> inside of the slide
3343 function getSlideNotes( slide ) {
3345 // Default to the current slide
3346 slide = slide || currentSlide;
3348 // Notes can be specified via the data-notes attribute...
3349 if( slide.hasAttribute( 'data-notes' ) ) {
3350 return slide.getAttribute( 'data-notes' );
3353 // ... or using an <aside class="notes"> element
3354 var notesElement = slide.querySelector( 'aside.notes' );
3355 if( notesElement ) {
3356 return notesElement.innerHTML;
3364 * Retrieves the current state of the presentation as
3365 * an object. This state can then be restored at any
3368 function getState() {
3370 var indices = getIndices();
3377 overview: isOverview()
3383 * Restores the presentation to the given state.
3385 * @param {Object} state As generated by getState()
3387 function setState( state ) {
3389 if( typeof state === 'object' ) {
3390 slide( deserialize( state.indexh ), deserialize( state.indexv ), deserialize( state.indexf ) );
3392 var pausedFlag = deserialize( state.paused ),
3393 overviewFlag = deserialize( state.overview );
3395 if( typeof pausedFlag === 'boolean' && pausedFlag !== isPaused() ) {
3396 togglePause( pausedFlag );
3399 if( typeof overviewFlag === 'boolean' && overviewFlag !== isOverview() ) {
3400 toggleOverview( overviewFlag );
3407 * Return a sorted fragments list, ordered by an increasing
3408 * "data-fragment-index" attribute.
3410 * Fragments will be revealed in the order that they are returned by
3411 * this function, so you can use the index attributes to control the
3412 * order of fragment appearance.
3414 * To maintain a sensible default fragment order, fragments are presumed
3415 * to be passed in document order. This function adds a "fragment-index"
3416 * attribute to each node if such an attribute is not already present,
3417 * and sets that attribute to an integer value which is the position of
3418 * the fragment within the fragments list.
3420 function sortFragments( fragments ) {
3422 fragments = toArray( fragments );
3428 // Group ordered and unordered elements
3429 fragments.forEach( function( fragment, i ) {
3430 if( fragment.hasAttribute( 'data-fragment-index' ) ) {
3431 var index = parseInt( fragment.getAttribute( 'data-fragment-index' ), 10 );
3433 if( !ordered[index] ) {
3434 ordered[index] = [];
3437 ordered[index].push( fragment );
3440 unordered.push( [ fragment ] );
3444 // Append fragments without explicit indices in their
3446 ordered = ordered.concat( unordered );
3448 // Manually count the index up per group to ensure there
3452 // Push all fragments in their sorted order to an array,
3453 // this flattens the groups
3454 ordered.forEach( function( group ) {
3455 group.forEach( function( fragment ) {
3456 sorted.push( fragment );
3457 fragment.setAttribute( 'data-fragment-index', index );
3468 * Navigate to the specified slide fragment.
3470 * @param {Number} index The index of the fragment that
3471 * should be shown, -1 means all are invisible
3472 * @param {Number} offset Integer offset to apply to the
3475 * @return {Boolean} true if a change was made in any
3476 * fragments visibility as part of this call
3478 function navigateFragment( index, offset ) {
3480 if( currentSlide && config.fragments ) {
3482 var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
3483 if( fragments.length ) {
3485 // If no index is specified, find the current
3486 if( typeof index !== 'number' ) {
3487 var lastVisibleFragment = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) ).pop();
3489 if( lastVisibleFragment ) {
3490 index = parseInt( lastVisibleFragment.getAttribute( 'data-fragment-index' ) || 0, 10 );
3497 // If an offset is specified, apply it to the index
3498 if( typeof offset === 'number' ) {
3502 var fragmentsShown = [],
3503 fragmentsHidden = [];
3505 toArray( fragments ).forEach( function( element, i ) {
3507 if( element.hasAttribute( 'data-fragment-index' ) ) {
3508 i = parseInt( element.getAttribute( 'data-fragment-index' ), 10 );
3511 // Visible fragments
3513 if( !element.classList.contains( 'visible' ) ) fragmentsShown.push( element );
3514 element.classList.add( 'visible' );
3515 element.classList.remove( 'current-fragment' );
3517 // Announce the fragments one by one to the Screen Reader
3518 dom.statusDiv.textContent = element.textContent;
3521 element.classList.add( 'current-fragment' );
3526 if( element.classList.contains( 'visible' ) ) fragmentsHidden.push( element );
3527 element.classList.remove( 'visible' );
3528 element.classList.remove( 'current-fragment' );
3534 if( fragmentsHidden.length ) {
3535 dispatchEvent( 'fragmenthidden', { fragment: fragmentsHidden[0], fragments: fragmentsHidden } );
3538 if( fragmentsShown.length ) {
3539 dispatchEvent( 'fragmentshown', { fragment: fragmentsShown[0], fragments: fragmentsShown } );
3545 return !!( fragmentsShown.length || fragmentsHidden.length );
3556 * Navigate to the next slide fragment.
3558 * @return {Boolean} true if there was a next fragment,
3561 function nextFragment() {
3563 return navigateFragment( null, 1 );
3568 * Navigate to the previous slide fragment.
3570 * @return {Boolean} true if there was a previous fragment,
3573 function previousFragment() {
3575 return navigateFragment( null, -1 );
3580 * Cues a new automated slide if enabled in the config.
3582 function cueAutoSlide() {
3586 if( currentSlide ) {
3588 var currentFragment = currentSlide.querySelector( '.current-fragment' );
3590 var fragmentAutoSlide = currentFragment ? currentFragment.getAttribute( 'data-autoslide' ) : null;
3591 var parentAutoSlide = currentSlide.parentNode ? currentSlide.parentNode.getAttribute( 'data-autoslide' ) : null;
3592 var slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
3594 // Pick value in the following priority order:
3595 // 1. Current fragment's data-autoslide
3596 // 2. Current slide's data-autoslide
3597 // 3. Parent slide's data-autoslide
3598 // 4. Global autoSlide setting
3599 if( fragmentAutoSlide ) {
3600 autoSlide = parseInt( fragmentAutoSlide, 10 );
3602 else if( slideAutoSlide ) {
3603 autoSlide = parseInt( slideAutoSlide, 10 );
3605 else if( parentAutoSlide ) {
3606 autoSlide = parseInt( parentAutoSlide, 10 );
3609 autoSlide = config.autoSlide;
3612 // If there are media elements with data-autoplay,
3613 // automatically set the autoSlide duration to the
3614 // length of that media. Not applicable if the slide
3615 // is divided up into fragments.
3616 if( currentSlide.querySelectorAll( '.fragment' ).length === 0 ) {
3617 toArray( currentSlide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
3618 if( el.hasAttribute( 'data-autoplay' ) ) {
3619 if( autoSlide && el.duration * 1000 > autoSlide ) {
3620 autoSlide = ( el.duration * 1000 ) + 1000;
3626 // Cue the next auto-slide if:
3627 // - There is an autoSlide value
3628 // - Auto-sliding isn't paused by the user
3629 // - The presentation isn't paused
3630 // - The overview isn't active
3631 // - The presentation isn't over
3632 if( autoSlide && !autoSlidePaused && !isPaused() && !isOverview() && ( !Reveal.isLastSlide() || availableFragments().next || config.loop === true ) ) {
3633 autoSlideTimeout = setTimeout( navigateNext, autoSlide );
3634 autoSlideStartTime = Date.now();
3637 if( autoSlidePlayer ) {
3638 autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
3646 * Cancels any ongoing request to auto-slide.
3648 function cancelAutoSlide() {
3650 clearTimeout( autoSlideTimeout );
3651 autoSlideTimeout = -1;
3655 function pauseAutoSlide() {
3657 if( autoSlide && !autoSlidePaused ) {
3658 autoSlidePaused = true;
3659 dispatchEvent( 'autoslidepaused' );
3660 clearTimeout( autoSlideTimeout );
3662 if( autoSlidePlayer ) {
3663 autoSlidePlayer.setPlaying( false );
3669 function resumeAutoSlide() {
3671 if( autoSlide && autoSlidePaused ) {
3672 autoSlidePaused = false;
3673 dispatchEvent( 'autoslideresumed' );
3679 function navigateLeft() {
3683 if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
3684 slide( indexh + 1 );
3687 // Normal navigation
3688 else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
3689 slide( indexh - 1 );
3694 function navigateRight() {
3698 if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
3699 slide( indexh - 1 );
3702 // Normal navigation
3703 else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
3704 slide( indexh + 1 );
3709 function navigateUp() {
3711 // Prioritize hiding fragments
3712 if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
3713 slide( indexh, indexv - 1 );
3718 function navigateDown() {
3720 // Prioritize revealing fragments
3721 if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
3722 slide( indexh, indexv + 1 );
3728 * Navigates backwards, prioritized in the following order:
3729 * 1) Previous fragment
3730 * 2) Previous vertical slide
3731 * 3) Previous horizontal slide
3733 function navigatePrev() {
3735 // Prioritize revealing fragments
3736 if( previousFragment() === false ) {
3737 if( availableRoutes().up ) {
3741 // Fetch the previous horizontal slide, if there is one
3745 previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.future' ) ).pop();
3748 previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.past' ) ).pop();
3751 if( previousSlide ) {
3752 var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
3762 * The reverse of #navigatePrev().
3764 function navigateNext() {
3766 // Prioritize revealing fragments
3767 if( nextFragment() === false ) {
3768 if( availableRoutes().down ) {
3771 else if( config.rtl ) {
3779 // If auto-sliding is enabled we need to cue up
3786 * Checks if the target element prevents the triggering of
3789 function isSwipePrevented( target ) {
3791 while( target && typeof target.hasAttribute === 'function' ) {
3792 if( target.hasAttribute( 'data-prevent-swipe' ) ) return true;
3793 target = target.parentNode;
3801 // --------------------------------------------------------------------//
3802 // ----------------------------- EVENTS -------------------------------//
3803 // --------------------------------------------------------------------//
3806 * Called by all event handlers that are based on user
3809 function onUserInput( event ) {
3811 if( config.autoSlideStoppable ) {
3818 * Handler for the document level 'keypress' event.
3820 function onDocumentKeyPress( event ) {
3822 // Check if the pressed key is question mark
3823 if( event.shiftKey && event.charCode === 63 ) {
3835 * Handler for the document level 'keydown' event.
3837 function onDocumentKeyDown( event ) {
3839 // If there's a condition specified and it returns false,
3840 // ignore this event
3841 if( typeof config.keyboardCondition === 'function' && config.keyboardCondition() === false ) {
3845 // Remember if auto-sliding was paused so we can toggle it
3846 var autoSlideWasPaused = autoSlidePaused;
3848 onUserInput( event );
3850 // Check if there's a focused element that could be using
3852 var activeElementIsCE = document.activeElement && document.activeElement.contentEditable !== 'inherit';
3853 var activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName );
3855 // Disregard the event if there's a focused element or a
3856 // keyboard modifier key is present
3857 if( activeElementIsCE || activeElementIsInput || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
3859 // While paused only allow resume keyboard events; 'b', '.''
3860 var resumeKeyCodes = [66,190,191];
3863 // Custom key bindings for togglePause should be able to resume
3864 if( typeof config.keyboard === 'object' ) {
3865 for( key in config.keyboard ) {
3866 if( config.keyboard[key] === 'togglePause' ) {
3867 resumeKeyCodes.push( parseInt( key, 10 ) );
3872 if( isPaused() && resumeKeyCodes.indexOf( event.keyCode ) === -1 ) {
3876 var triggered = false;
3878 // 1. User defined key bindings
3879 if( typeof config.keyboard === 'object' ) {
3881 for( key in config.keyboard ) {
3883 // Check if this binding matches the pressed key
3884 if( parseInt( key, 10 ) === event.keyCode ) {
3886 var value = config.keyboard[ key ];
3888 // Callback function
3889 if( typeof value === 'function' ) {
3890 value.apply( null, [ event ] );
3892 // String shortcuts to reveal.js API
3893 else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
3894 Reveal[ value ].call();
3905 // 2. System defined key bindings
3906 if( triggered === false ) {
3908 // Assume true and try to prove false
3911 switch( event.keyCode ) {
3913 case 80: case 33: navigatePrev(); break;
3915 case 78: case 34: navigateNext(); break;
3917 case 72: case 37: navigateLeft(); break;
3919 case 76: case 39: navigateRight(); break;
3921 case 75: case 38: navigateUp(); break;
3923 case 74: case 40: navigateDown(); break;
3925 case 36: slide( 0 ); break;
3927 case 35: slide( Number.MAX_VALUE ); break;
3929 case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
3931 case 13: isOverview() ? deactivateOverview() : triggered = false; break;
3932 // two-spot, semicolon, b, period, Logitech presenter tools "black screen" button
3933 case 58: case 59: case 66: case 190: case 191: togglePause(); break;
3935 case 70: enterFullscreen(); break;
3937 case 65: if ( config.autoSlideStoppable ) toggleAutoSlide( autoSlideWasPaused ); break;
3944 // If the input resulted in a triggered action we should prevent
3945 // the browsers default behavior
3947 event.preventDefault && event.preventDefault();
3950 else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && features.transforms3d ) {
3958 event.preventDefault && event.preventDefault();
3961 // If auto-sliding is enabled we need to cue up
3968 * Handler for the 'touchstart' event, enables support for
3969 * swipe and pinch gestures.
3971 function onTouchStart( event ) {
3973 if( isSwipePrevented( event.target ) ) return true;
3975 touch.startX = event.touches[0].clientX;
3976 touch.startY = event.touches[0].clientY;
3977 touch.startCount = event.touches.length;
3979 // If there's two touches we need to memorize the distance
3980 // between those two points to detect pinching
3981 if( event.touches.length === 2 && config.overview ) {
3982 touch.startSpan = distanceBetween( {
3983 x: event.touches[1].clientX,
3984 y: event.touches[1].clientY
3994 * Handler for the 'touchmove' event.
3996 function onTouchMove( event ) {
3998 if( isSwipePrevented( event.target ) ) return true;
4000 // Each touch should only trigger one action
4001 if( !touch.captured ) {
4002 onUserInput( event );
4004 var currentX = event.touches[0].clientX;
4005 var currentY = event.touches[0].clientY;
4007 // If the touch started with two points and still has
4008 // two active touches; test for the pinch gesture
4009 if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
4011 // The current distance in pixels between the two touch points
4012 var currentSpan = distanceBetween( {
4013 x: event.touches[1].clientX,
4014 y: event.touches[1].clientY
4020 // If the span is larger than the desire amount we've got
4021 // ourselves a pinch
4022 if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
4023 touch.captured = true;
4025 if( currentSpan < touch.startSpan ) {
4029 deactivateOverview();
4033 event.preventDefault();
4036 // There was only one touch point, look for a swipe
4037 else if( event.touches.length === 1 && touch.startCount !== 2 ) {
4039 var deltaX = currentX - touch.startX,
4040 deltaY = currentY - touch.startY;
4042 if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
4043 touch.captured = true;
4046 else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
4047 touch.captured = true;
4050 else if( deltaY > touch.threshold ) {
4051 touch.captured = true;
4054 else if( deltaY < -touch.threshold ) {
4055 touch.captured = true;
4059 // If we're embedded, only block touch events if they have
4060 // triggered an action
4061 if( config.embedded ) {
4062 if( touch.captured || isVerticalSlide( currentSlide ) ) {
4063 event.preventDefault();
4066 // Not embedded? Block them all to avoid needless tossing
4067 // around of the viewport in iOS
4069 event.preventDefault();
4074 // There's a bug with swiping on some Android devices unless
4075 // the default action is always prevented
4076 else if( navigator.userAgent.match( /android/gi ) ) {
4077 event.preventDefault();
4083 * Handler for the 'touchend' event.
4085 function onTouchEnd( event ) {
4087 touch.captured = false;
4092 * Convert pointer down to touch start.
4094 function onPointerDown( event ) {
4096 if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
4097 event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
4098 onTouchStart( event );
4104 * Convert pointer move to touch move.
4106 function onPointerMove( event ) {
4108 if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
4109 event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
4110 onTouchMove( event );
4116 * Convert pointer up to touch end.
4118 function onPointerUp( event ) {
4120 if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
4121 event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
4122 onTouchEnd( event );
4128 * Handles mouse wheel scrolling, throttled to avoid skipping
4131 function onDocumentMouseScroll( event ) {
4133 if( Date.now() - lastMouseWheelStep > 600 ) {
4135 lastMouseWheelStep = Date.now();
4137 var delta = event.detail || -event.wheelDelta;
4150 * Clicking on the progress bar results in a navigation to the
4151 * closest approximate horizontal slide using this equation:
4153 * ( clickX / presentationWidth ) * numberOfSlides
4155 function onProgressClicked( event ) {
4157 onUserInput( event );
4159 event.preventDefault();
4161 var slidesTotal = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
4162 var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
4165 slideIndex = slidesTotal - slideIndex;
4168 slide( slideIndex );
4173 * Event handler for navigation control buttons.
4175 function onNavigateLeftClicked( event ) { event.preventDefault(); onUserInput(); navigateLeft(); }
4176 function onNavigateRightClicked( event ) { event.preventDefault(); onUserInput(); navigateRight(); }
4177 function onNavigateUpClicked( event ) { event.preventDefault(); onUserInput(); navigateUp(); }
4178 function onNavigateDownClicked( event ) { event.preventDefault(); onUserInput(); navigateDown(); }
4179 function onNavigatePrevClicked( event ) { event.preventDefault(); onUserInput(); navigatePrev(); }
4180 function onNavigateNextClicked( event ) { event.preventDefault(); onUserInput(); navigateNext(); }
4183 * Handler for the window level 'hashchange' event.
4185 function onWindowHashChange( event ) {
4192 * Handler for the window level 'resize' event.
4194 function onWindowResize( event ) {
4201 * Handle for the window level 'visibilitychange' event.
4203 function onPageVisibilityChange( event ) {
4205 var isHidden = document.webkitHidden ||
4206 document.msHidden ||
4209 // If, after clicking a link or similar and we're coming back,
4210 // focus the document.body to ensure we can use keyboard shortcuts
4211 if( isHidden === false && document.activeElement !== document.body ) {
4212 // Not all elements support .blur() - SVGs among them.
4213 if( typeof document.activeElement.blur === 'function' ) {
4214 document.activeElement.blur();
4216 document.body.focus();
4222 * Invoked when a slide is and we're in the overview.
4224 function onOverviewSlideClicked( event ) {
4226 // TODO There's a bug here where the event listeners are not
4227 // removed after deactivating the overview.
4228 if( eventsAreBound && isOverview() ) {
4229 event.preventDefault();
4231 var element = event.target;
4233 while( element && !element.nodeName.match( /section/gi ) ) {
4234 element = element.parentNode;
4237 if( element && !element.classList.contains( 'disabled' ) ) {
4239 deactivateOverview();
4241 if( element.nodeName.match( /section/gi ) ) {
4242 var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
4243 v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
4254 * Handles clicks on links that are set to preview in the
4257 function onPreviewLinkClicked( event ) {
4259 if( event.currentTarget && event.currentTarget.hasAttribute( 'href' ) ) {
4260 var url = event.currentTarget.getAttribute( 'href' );
4263 event.preventDefault();
4270 * Handles click on the auto-sliding controls element.
4272 function onAutoSlidePlayerClick( event ) {
4275 if( Reveal.isLastSlide() && config.loop === false ) {
4280 else if( autoSlidePaused ) {
4291 // --------------------------------------------------------------------//
4292 // ------------------------ PLAYBACK COMPONENT ------------------------//
4293 // --------------------------------------------------------------------//
4297 * Constructor for the playback component, which displays
4298 * play/pause/progress controls.
4300 * @param {HTMLElement} container The component will append
4302 * @param {Function} progressCheck A method which will be
4303 * called frequently to get the current progress on a range
4306 function Playback( container, progressCheck ) {
4312 // Flags if we are currently playing
4313 this.playing = false;
4315 // Current progress on a 0-1 range
4318 // Used to loop the animation smoothly
4319 this.progressOffset = 1;
4321 this.container = container;
4322 this.progressCheck = progressCheck;
4324 this.canvas = document.createElement( 'canvas' );
4325 this.canvas.className = 'playback';
4326 this.canvas.width = this.diameter;
4327 this.canvas.height = this.diameter;
4328 this.context = this.canvas.getContext( '2d' );
4330 this.container.appendChild( this.canvas );
4336 Playback.prototype.setPlaying = function( value ) {
4338 var wasPlaying = this.playing;
4340 this.playing = value;
4342 // Start repainting if we weren't already
4343 if( !wasPlaying && this.playing ) {
4352 Playback.prototype.animate = function() {
4354 var progressBefore = this.progress;
4356 this.progress = this.progressCheck();
4358 // When we loop, offset the progress so that it eases
4359 // smoothly rather than immediately resetting
4360 if( progressBefore > 0.8 && this.progress < 0.2 ) {
4361 this.progressOffset = this.progress;
4366 if( this.playing ) {
4367 features.requestAnimationFrameMethod.call( window, this.animate.bind( this ) );
4373 * Renders the current progress and playback state.
4375 Playback.prototype.render = function() {
4377 var progress = this.playing ? this.progress : 0,
4378 radius = ( this.diameter / 2 ) - this.thickness,
4379 x = this.diameter / 2,
4380 y = this.diameter / 2,
4384 this.progressOffset += ( 1 - this.progressOffset ) * 0.1;
4386 var endAngle = ( - Math.PI / 2 ) + ( progress * ( Math.PI * 2 ) );
4387 var startAngle = ( - Math.PI / 2 ) + ( this.progressOffset * ( Math.PI * 2 ) );
4389 this.context.save();
4390 this.context.clearRect( 0, 0, this.diameter, this.diameter );
4392 // Solid background color
4393 this.context.beginPath();
4394 this.context.arc( x, y, radius + 2, 0, Math.PI * 2, false );
4395 this.context.fillStyle = 'rgba( 0, 0, 0, 0.4 )';
4396 this.context.fill();
4398 // Draw progress track
4399 this.context.beginPath();
4400 this.context.arc( x, y, radius, 0, Math.PI * 2, false );
4401 this.context.lineWidth = this.thickness;
4402 this.context.strokeStyle = '#666';
4403 this.context.stroke();
4405 if( this.playing ) {
4406 // Draw progress on top of track
4407 this.context.beginPath();
4408 this.context.arc( x, y, radius, startAngle, endAngle, false );
4409 this.context.lineWidth = this.thickness;
4410 this.context.strokeStyle = '#fff';
4411 this.context.stroke();
4414 this.context.translate( x - ( iconSize / 2 ), y - ( iconSize / 2 ) );
4416 // Draw play/pause icons
4417 if( this.playing ) {
4418 this.context.fillStyle = '#fff';
4419 this.context.fillRect( 0, 0, iconSize / 2 - 2, iconSize );
4420 this.context.fillRect( iconSize / 2 + 2, 0, iconSize / 2 - 2, iconSize );
4423 this.context.beginPath();
4424 this.context.translate( 2, 0 );
4425 this.context.moveTo( 0, 0 );
4426 this.context.lineTo( iconSize - 2, iconSize / 2 );
4427 this.context.lineTo( 0, iconSize );
4428 this.context.fillStyle = '#fff';
4429 this.context.fill();
4432 this.context.restore();
4436 Playback.prototype.on = function( type, listener ) {
4437 this.canvas.addEventListener( type, listener, false );
4440 Playback.prototype.off = function( type, listener ) {
4441 this.canvas.removeEventListener( type, listener, false );
4444 Playback.prototype.destroy = function() {
4446 this.playing = false;
4448 if( this.canvas.parentNode ) {
4449 this.container.removeChild( this.canvas );
4455 // --------------------------------------------------------------------//
4456 // ------------------------------- API --------------------------------//
4457 // --------------------------------------------------------------------//
4461 initialize: initialize,
4462 configure: configure,
4465 // Navigation methods
4468 right: navigateRight,
4475 navigateFragment: navigateFragment,
4476 prevFragment: previousFragment,
4477 nextFragment: nextFragment,
4479 // Deprecated aliases
4481 navigateLeft: navigateLeft,
4482 navigateRight: navigateRight,
4483 navigateUp: navigateUp,
4484 navigateDown: navigateDown,
4485 navigatePrev: navigatePrev,
4486 navigateNext: navigateNext,
4488 // Forces an update in slide layout
4491 // Returns an object with the available routes as booleans (left/right/top/bottom)
4492 availableRoutes: availableRoutes,
4494 // Returns an object with the available fragments as booleans (prev/next)
4495 availableFragments: availableFragments,
4497 // Toggles the overview mode on/off
4498 toggleOverview: toggleOverview,
4500 // Toggles the "black screen" mode on/off
4501 togglePause: togglePause,
4503 // Toggles the auto slide mode on/off
4504 toggleAutoSlide: toggleAutoSlide,
4507 isOverview: isOverview,
4509 isAutoSliding: isAutoSliding,
4511 // Adds or removes all internal event listeners (such as keyboard)
4512 addEventListeners: addEventListeners,
4513 removeEventListeners: removeEventListeners,
4515 // Facility for persisting and restoring the presentation state
4519 // Presentation progress on range of 0-1
4520 getProgress: getProgress,
4522 // Returns the indices of the current, or specified, slide
4523 getIndices: getIndices,
4525 getTotalSlides: getTotalSlides,
4527 // Returns the slide element at the specified index
4530 // Returns the slide background element at the specified index
4531 getSlideBackground: getSlideBackground,
4533 // Returns the speaker notes string for a slide, or null
4534 getSlideNotes: getSlideNotes,
4536 // Returns the previous slide element, may be null
4537 getPreviousSlide: function() {
4538 return previousSlide;
4541 // Returns the current slide element
4542 getCurrentSlide: function() {
4543 return currentSlide;
4546 // Returns the current scale of the presentation content
4547 getScale: function() {
4551 // Returns the current configuration object
4552 getConfig: function() {
4556 // Helper method, retrieves query string as a key/value hash
4557 getQueryHash: function() {
4560 location.search.replace( /[A-Z0-9]+?=([\w\.%-]*)/gi, function(a) {
4561 query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
4564 // Basic deserialization
4565 for( var i in query ) {
4566 var value = query[ i ];
4568 query[ i ] = deserialize( unescape( value ) );
4574 // Returns true if we're currently on the first slide
4575 isFirstSlide: function() {
4576 return ( indexh === 0 && indexv === 0 );
4579 // Returns true if we're currently on the last slide
4580 isLastSlide: function() {
4581 if( currentSlide ) {
4582 // Does this slide has next a sibling?
4583 if( currentSlide.nextElementSibling ) return false;
4585 // If it's vertical, does its parent have a next sibling?
4586 if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
4594 // Checks if reveal.js has been loaded and is ready for use
4595 isReady: function() {
4599 // Forward event binding to the reveal DOM element
4600 addEventListener: function( type, listener, useCapture ) {
4601 if( 'addEventListener' in window ) {
4602 ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
4605 removeEventListener: function( type, listener, useCapture ) {
4606 if( 'addEventListener' in window ) {
4607 ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
4611 // Programatically triggers a keyboard event
4612 triggerKey: function( keyCode ) {
4613 onDocumentKeyDown( { keyCode: keyCode } );