diff options
Diffstat (limited to 'js/reveal.js')
-rw-r--r-- | js/reveal.js | 234 |
1 files changed, 198 insertions, 36 deletions
diff --git a/js/reveal.js b/js/reveal.js index 70e2539..542edc2 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -12,6 +12,7 @@ var Reveal = (function(){ var SLIDES_SELECTOR = '.reveal .slides section', HORIZONTAL_SLIDES_SELECTOR = '.reveal .slides>section', VERTICAL_SLIDES_SELECTOR = '.reveal .slides>section.present>section', + HOME_SLIDE_SELECTOR = '.reveal .slides>section:first-child', // Configurations defaults, can be overridden at initialization time config = { @@ -33,6 +34,9 @@ var Reveal = (function(){ // Vertical centering of slides center: true, + // Enables touch navigation on devices with touch input + touch: true, + // Loop the presentation loop: false, @@ -50,11 +54,11 @@ var Reveal = (function(){ // Apply a 3D roll to links on hover rollingLinks: true, - // Transition style (see /css/theme) + // Theme (see /css/theme) theme: null, // Transition style - transition: 'default', // default/cube/page/concave/zoom/linear/none + transition: 'default', // default/cube/page/concave/zoom/linear/fade/none // Script dependencies to load dependencies: [] @@ -87,6 +91,7 @@ var Reveal = (function(){ 'OPerspective' in document.body.style || 'perspective' in document.body.style, + // Detect support for CSS 2D transforms supports2DTransforms = 'WebkitTransform' in document.body.style || 'MozTransform' in document.body.style || 'msTransform' in document.body.style || @@ -102,9 +107,12 @@ var Reveal = (function(){ // Delays updates to the URL due to a Chrome thumbnailer bug writeURLTimeout = 0, - // A delay used to ativate the overview mode + // A delay used to activate the overview mode activateOverviewTimeout = 0, + // A delay used to deactivate the overview mode + deactivateOverviewTimeout = 0, + // Holds information about the currently ongoing touch input touch = { startX: 0, @@ -119,6 +127,7 @@ var Reveal = (function(){ * Starts up the presentation if the client is capable. */ function initialize( options ) { + if( ( !supports2DTransforms && !supports3DTransforms ) ) { document.body.setAttribute( 'class', 'no-transforms' ); @@ -147,6 +156,7 @@ var Reveal = (function(){ * not found, it is created. */ function setupDOM() { + // Cache references to key DOM elements dom.theme = document.querySelector( '#theme' ); dom.wrapper = document.querySelector( '.reveal' ); @@ -200,12 +210,14 @@ var Reveal = (function(){ dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) ); dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) ); } + } /** * Hides the address bar if we're on a mobile device. */ function hideAddressBar() { + if( navigator.userAgent.match( /(iphone|ipod)/i ) ) { // Give the page some scrollable overflow document.documentElement.style.overflow = 'scroll'; @@ -215,6 +227,7 @@ var Reveal = (function(){ window.addEventListener( 'load', removeAddressBar, false ); window.addEventListener( 'orientationchange', removeAddressBar, false ); } + } /** @@ -225,6 +238,7 @@ var Reveal = (function(){ * will load after reveal.js has been started up. */ function load() { + var scripts = [], scriptsAsync = []; @@ -266,6 +280,7 @@ var Reveal = (function(){ else { proceed(); } + } /** @@ -273,6 +288,7 @@ var Reveal = (function(){ * to the current URL deeplink if there is one. */ function start() { + // Make sure we've got all the DOM elements we need setupDOM(); @@ -301,12 +317,14 @@ var Reveal = (function(){ 'currentSlide': currentSlide } ); }, 1 ); + } /** * Applies the configuration settings from the config object. */ function configure() { + if( supports3DTransforms === false ) { config.transition = 'linear'; } @@ -352,18 +370,23 @@ var Reveal = (function(){ dom.theme.setAttribute( 'href', themeURL ); } } + } /** * Binds all event listeners. */ function addEventListeners() { - document.addEventListener( 'touchstart', onDocumentTouchStart, false ); - document.addEventListener( 'touchmove', onDocumentTouchMove, false ); - document.addEventListener( 'touchend', onDocumentTouchEnd, false ); + window.addEventListener( 'hashchange', onWindowHashChange, false ); window.addEventListener( 'resize', onWindowResize, false ); + if( config.touch ) { + document.addEventListener( 'touchstart', onDocumentTouchStart, false ); + document.addEventListener( 'touchmove', onDocumentTouchMove, false ); + document.addEventListener( 'touchend', onDocumentTouchEnd, false ); + } + if( config.keyboard ) { document.addEventListener( 'keydown', onDocumentKeyDown, false ); } @@ -381,19 +404,24 @@ var Reveal = (function(){ dom.controlsPrev.forEach( function( el ) { el.addEventListener( actionEvent, preventAndForward( navigatePrev ), false ); } ); dom.controlsNext.forEach( function( el ) { el.addEventListener( actionEvent, preventAndForward( navigateNext ), false ); } ); } + } /** * Unbinds all event listeners. */ function removeEventListeners() { + document.removeEventListener( 'keydown', onDocumentKeyDown, false ); - document.removeEventListener( 'touchstart', onDocumentTouchStart, false ); - document.removeEventListener( 'touchmove', onDocumentTouchMove, false ); - document.removeEventListener( 'touchend', onDocumentTouchEnd, false ); window.removeEventListener( 'hashchange', onWindowHashChange, false ); window.removeEventListener( 'resize', onWindowResize, false ); + if( config.touch ) { + document.removeEventListener( 'touchstart', onDocumentTouchStart, false ); + document.removeEventListener( 'touchmove', onDocumentTouchMove, false ); + document.removeEventListener( 'touchend', onDocumentTouchEnd, false ); + } + if ( config.progress && dom.progress ) { dom.progress.removeEventListener( 'click', preventAndForward( onProgressClick ), false ); } @@ -407,6 +435,7 @@ var Reveal = (function(){ dom.controlsPrev.forEach( function( el ) { el.removeEventListener( actionEvent, preventAndForward( navigatePrev ), false ); } ); dom.controlsNext.forEach( function( el ) { el.removeEventListener( actionEvent, preventAndForward( navigateNext ), false ); } ); } + } /** @@ -414,22 +443,20 @@ var Reveal = (function(){ * If there's a conflict, object b takes precedence. */ function extend( a, b ) { + for( var i in b ) { a[ i ] = b[ i ]; } + } /** * Converts the target object to an array. */ function toArray( o ) { + return Array.prototype.slice.call( o ); - } - function each( targets, method, args ) { - targets.forEach( function( el ) { - el[method].apply( el, args ); - } ); } /** @@ -440,10 +467,12 @@ var Reveal = (function(){ * @param {Object} b point with x/y properties */ function distanceBetween( a, b ) { + var dx = a.x - b.x, dy = a.y - b.y; return Math.sqrt( dx*dx + dy*dy ); + } /** @@ -454,10 +483,12 @@ var Reveal = (function(){ * after the wrapper has been executed */ function preventAndForward( delegate ) { + return function( event ) { event.preventDefault(); delegate.call( null, event ); }; + } /** @@ -465,9 +496,11 @@ var Reveal = (function(){ * more vertical space ftw. */ function removeAddressBar() { + setTimeout( function() { window.scrollTo( 0, 1 ); }, 0 ); + } /** @@ -475,28 +508,37 @@ var Reveal = (function(){ * reveal DOM element. */ function dispatchEvent( type, properties ) { + var event = document.createEvent( "HTMLEvents", 1, 2 ); event.initEvent( type, true, true ); extend( event, properties ); dom.wrapper.dispatchEvent( event ); + } /** * Wrap all links in 3D goodness. */ function linkify() { + if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) { var nodes = document.querySelectorAll( SLIDES_SELECTOR + ' a:not(.image)' ); for( var i = 0, len = nodes.length; i < len; i++ ) { var node = nodes[i]; - if( node.textContent && !node.querySelector( 'img' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) { + if( node.textContent && !node.querySelector( '*' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) { + var span = document.createElement('span'); + span.setAttribute('data-title', node.text); + span.innerHTML = node.innerHTML; + node.classList.add( 'roll' ); - node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>'; + node.innerHTML = ''; + node.appendChild(span); } } } + } /** @@ -516,7 +558,7 @@ var Reveal = (function(){ for( var i = 0, len = slides.length; i < len; i++ ) { var slide = slides[ i ]; - // Don't bother update invisible slides + // Don't bother updating invisible slides if( slide.style.display === 'none' ) { continue; } @@ -536,17 +578,19 @@ var Reveal = (function(){ } /** - * Stores the vertical index of a stack so that the same - * vertical slide can be selected when navigating to and + * Stores the vertical index of a stack so that the same + * vertical slide can be selected when navigating to and * from the stack. - * + * * @param {HTMLElement} stack The vertical stack element * @param {int} v Index to memorize */ function setPreviousVerticalIndex( stack, v ) { + if( stack ) { stack.setAttribute( 'data-previous-indexv', v || 0 ); } + } /** @@ -557,11 +601,13 @@ var Reveal = (function(){ * @param {HTMLElement} stack The vertical stack element */ function getPreviousVerticalIndex( stack ) { + if( stack && stack.classList.contains( 'stack' ) ) { return parseInt( stack.getAttribute( 'data-previous-indexv' ) || 0, 10 ); } return 0; + } /** @@ -576,13 +622,17 @@ var Reveal = (function(){ // Only proceed if enabled in config if( config.overview ) { + var wasActive = dom.wrapper.classList.contains( 'overview' ); + dom.wrapper.classList.add( 'overview' ); + dom.wrapper.classList.remove( 'exit-overview' ); clearTimeout( activateOverviewTimeout ); + clearTimeout( deactivateOverviewTimeout ); - // Not the pretties solution, but need to let the overview - // class apply first so that slides are measured accurately - // before we can positon them + // Not the pretties solution, but need to let the overview + // class apply first so that slides are measured accurately + // before we can position them activateOverviewTimeout = setTimeout( function(){ var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ); @@ -633,6 +683,15 @@ var Reveal = (function(){ layout(); + if( !wasActive ) { + // Notify observers of the overview showing + dispatchEvent( 'overviewshown', { + 'indexh': indexh, + 'indexv': indexv, + 'currentSlide': currentSlide + } ); + } + }, 10 ); } @@ -649,9 +708,19 @@ var Reveal = (function(){ if( config.overview ) { clearTimeout( activateOverviewTimeout ); + clearTimeout( deactivateOverviewTimeout ); dom.wrapper.classList.remove( 'overview' ); + // Temporarily add a class so that transitions can do different things + // depending on whether they are exiting/entering overview, or just + // moving from slide to slide + dom.wrapper.classList.add( 'exit-overview' ); + + deactivateOverviewTimeout = setTimeout( function () { + dom.wrapper.classList.remove( 'exit-overview' ); + }, 10); + // Select all slides var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) ); @@ -672,6 +741,13 @@ var Reveal = (function(){ slide( indexh, indexv ); + // Notify observers of the overview hiding + dispatchEvent( 'overviewhidden', { + 'indexh': indexh, + 'indexv': indexv, + 'currentSlide': currentSlide + } ); + } } @@ -683,12 +759,14 @@ var Reveal = (function(){ * overview is open, false means it's closed. */ function toggleOverview( override ) { + if( typeof override === 'boolean' ) { override ? activateOverview() : deactivateOverview(); } else { isOverviewActive() ? deactivateOverview() : activateOverview(); } + } /** @@ -698,7 +776,9 @@ var Reveal = (function(){ * false otherwise */ function isOverviewActive() { + return dom.wrapper.classList.contains( 'overview' ); + } /** @@ -708,6 +788,7 @@ var Reveal = (function(){ * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode */ function enterFullscreen() { + var element = document.body; // Check which implementation is available @@ -719,6 +800,7 @@ var Reveal = (function(){ if( requestMethod ) { requestMethod.apply( element ); } + } /** @@ -726,33 +808,41 @@ var Reveal = (function(){ * black. */ function pause() { + dom.wrapper.classList.add( 'paused' ); + } /** * Exits from the paused mode. */ function resume() { + dom.wrapper.classList.remove( 'paused' ); + } /** * Toggles the paused mode on and off. */ function togglePause() { + if( isPaused() ) { resume(); } else { pause(); } + } /** * Checks if we are currently in the paused mode. */ function isPaused() { + return dom.wrapper.classList.contains( 'paused' ); + } /** @@ -762,23 +852,24 @@ var Reveal = (function(){ * * @param {int} h Horizontal index of the target slide * @param {int} v Vertical index of the target slide - * @param {int} f Optional index of a fragment within the + * @param {int} f Optional index of a fragment within the * target slide to activate */ function slide( h, v, f ) { + // Remember where we were at before previousSlide = currentSlide; // Query all horizontal slides in the deck var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ); - // If no vertical index is specified and the upcoming slide is a + // If no vertical index is specified and the upcoming slide is a // stack, resume at its previous vertical index if( v === undefined ) { v = getPreviousVerticalIndex( horizontalSlides[ h ] ); } - // If we were on a vertical stack, remember what vertical index + // If we were on a vertical stack, remember what vertical index // it was on so we can resume at the same position when returning if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) { setPreviousVerticalIndex( previousSlide.parentNode, indexv ); @@ -816,7 +907,7 @@ var Reveal = (function(){ dispatchEvent( state[i] ); } - // Clean up the remaints of the previous state + // Clean up the remains of the previous state while( stateBefore.length ) { document.documentElement.classList.remove( stateBefore.pop() ); } @@ -840,7 +931,7 @@ var Reveal = (function(){ // Show fragment, if specified - if( ( indexh !== indexhBefore || indexv !== indexvBefore ) && f ) { + if( typeof f !== 'undefined' ) { var fragments = currentSlide.querySelectorAll( '.fragment' ); toArray( fragments ).forEach( function( fragment, indexf ) { @@ -872,10 +963,26 @@ var Reveal = (function(){ // stacks if( previousSlide ) { previousSlide.classList.remove( 'present' ); + + // Reset all slides upon navigate to home + // Issue: #285 + if ( document.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) { + // Launch async task + setTimeout( function () { + var slides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i; + for( i in slides ) { + if( slides[i] ) { + // Reset stack + setPreviousVerticalIndex( slides[i], 0 ); + } + } + }, 0 ); + } } updateControls(); updateProgress(); + } /** @@ -892,6 +999,7 @@ var Reveal = (function(){ * bounds. */ function updateSlides( selector, index ) { + // Select all slides and convert the NodeList result to // an array var slides = toArray( document.querySelectorAll( selector ) ), @@ -958,7 +1066,8 @@ var Reveal = (function(){ var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' ); if( slideAutoSlide ) { autoSlide = parseInt( slideAutoSlide, 10 ); - } else { + } + else { autoSlide = config.autoSlide; } @@ -977,6 +1086,7 @@ var Reveal = (function(){ * Updates the progress bar to reflect the current slide. */ function updateProgress() { + // Update progress if enabled if( config.progress && dom.progress ) { @@ -1018,12 +1128,14 @@ var Reveal = (function(){ dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px'; } + } /** * Updates the state of all control/navigation arrows. */ function updateControls() { + if ( config.controls && dom.controls ) { var routes = availableRoutes(); @@ -1048,6 +1160,7 @@ var Reveal = (function(){ if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } ); } + } /** @@ -1056,6 +1169,7 @@ var Reveal = (function(){ * @return {Object} containing four booleans: left/right/up/down */ function availableRoutes() { + var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ), verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR ); @@ -1065,12 +1179,14 @@ var Reveal = (function(){ up: indexv > 0, down: indexv < verticalSlides.length - 1 }; + } /** * Reads the current URL (hash) and navigates accordingly. */ function readURL() { + var hash = window.location.hash; // Attempt to parse the hash as either an index or name @@ -1100,16 +1216,18 @@ var Reveal = (function(){ slide( h, v ); } + } /** * Updates the page URL (hash) to reflect the current * state. * - * @param {Number} delay The time in ms to wait before + * @param {Number} delay The time in ms to wait before * writing the hash */ function writeURL( delay ) { + if( config.history ) { // Make sure there's never more than one timeout running @@ -1135,6 +1253,7 @@ var Reveal = (function(){ window.location.hash = url; } } + } /** @@ -1148,6 +1267,7 @@ var Reveal = (function(){ * @return {Object} { h: <int>, v: <int> } */ function getIndices( slide ) { + // By default, return the current indices var h = indexh, v = indexv; @@ -1165,11 +1285,12 @@ var Reveal = (function(){ // If this is a vertical slide, grab the vertical index if( isVertical ) { - v = Math.max( toArray( slide.parentNode.children ).indexOf( slide ), 0 ); + v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 ); } } return { h: h, v: v }; + } /** @@ -1179,6 +1300,7 @@ var Reveal = (function(){ * false otherwise */ function nextFragment() { + // Vertical slides: if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) { var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' ); @@ -1203,6 +1325,7 @@ var Reveal = (function(){ } return false; + } /** @@ -1212,6 +1335,7 @@ var Reveal = (function(){ * false otherwise */ function previousFragment() { + // Vertical slides: if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) { var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' ); @@ -1236,46 +1360,57 @@ var Reveal = (function(){ } return false; + } /** * Cues a new automated slide if enabled in the config. */ function cueAutoSlide() { + clearTimeout( autoSlideTimeout ); // Cue the next auto-slide if enabled if( autoSlide ) { autoSlideTimeout = setTimeout( navigateNext, autoSlide ); } + } function navigateLeft() { + // Prioritize hiding fragments if( availableRoutes().left && isOverviewActive() || previousFragment() === false ) { slide( indexh - 1 ); } + } function navigateRight() { + // Prioritize revealing fragments if( availableRoutes().right && isOverviewActive() || nextFragment() === false ) { slide( indexh + 1 ); } + } function navigateUp() { + // Prioritize hiding fragments if( availableRoutes().up && isOverviewActive() || previousFragment() === false ) { slide( indexh, indexv - 1 ); } + } function navigateDown() { + // Prioritize revealing fragments if( availableRoutes().down && isOverviewActive() || nextFragment() === false ) { slide( indexh, indexv + 1 ); } + } /** @@ -1285,6 +1420,7 @@ var Reveal = (function(){ * 3) Previous horizontal slide */ function navigatePrev() { + // Prioritize revealing fragments if( previousFragment() === false ) { if( availableRoutes().up ) { @@ -1301,12 +1437,14 @@ var Reveal = (function(){ } } } + } /** * Same as #navigatePrev() but navigates forwards. */ function navigateNext() { + // Prioritize revealing fragments if( nextFragment() === false ) { availableRoutes().down ? navigateDown() : navigateRight(); @@ -1315,6 +1453,7 @@ var Reveal = (function(){ // If auto-sliding is enabled we need to cue up // another timeout cueAutoSlide(); + } @@ -1329,12 +1468,13 @@ var Reveal = (function(){ * @param {Object} event */ function onDocumentKeyDown( event ) { - // Check if there's a focused element that could be using + + // Check if there's a focused element that could be using // the keyboard var activeElement = document.activeElement; var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) ); - // Disregard the event if there's a focused element or a + // Disregard the event if there's a focused element or a // keyboard modifier key is present if ( hasFocus || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return; @@ -1361,8 +1501,8 @@ var Reveal = (function(){ case 32: isOverviewActive() ? deactivateOverview() : navigateNext(); break; // return case 13: isOverviewActive() ? deactivateOverview() : triggered = false; break; - // b, period - case 66: case 190: togglePause(); break; + // b, period, Logitech presenter tools "black screen" button + case 66: case 190: case 191: togglePause(); break; // f case 70: enterFullscreen(); break; default: @@ -1391,6 +1531,7 @@ var Reveal = (function(){ * enables support for swipe and pinch gestures. */ function onDocumentTouchStart( event ) { + touch.startX = event.touches[0].clientX; touch.startY = event.touches[0].clientY; touch.startCount = event.touches.length; @@ -1406,12 +1547,14 @@ var Reveal = (function(){ y: touch.startY } ); } + } /** * Handler for the document level 'touchmove' event. */ function onDocumentTouchMove( event ) { + // Each touch should only trigger one action if( !touch.handled ) { var currentX = event.touches[0].clientX; @@ -1478,20 +1621,24 @@ var Reveal = (function(){ else if( navigator.userAgent.match( /android/gi ) ) { event.preventDefault(); } + } /** * Handler for the document level 'touchend' event. */ function onDocumentTouchEnd( event ) { + touch.handled = false; + } /** * Handles mouse wheel scrolling, throttled to avoid skipping * multiple slides. */ - function onDocumentMouseScroll( event ){ + function onDocumentMouseScroll( event ) { + clearTimeout( mouseWheelTimeout ); mouseWheelTimeout = setTimeout( function() { @@ -1503,6 +1650,7 @@ var Reveal = (function(){ navigatePrev(); } }, 100 ); + } /** @@ -1512,30 +1660,37 @@ var Reveal = (function(){ * ( clickX / presentationWidth ) * numberOfSlides */ function onProgressClick( event ) { + var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length; var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal ); slide( slideIndex ); + } /** * Handler for the window level 'hashchange' event. */ function onWindowHashChange( event ) { + readURL(); + } /** * Handler for the window level 'resize' event. */ function onWindowResize( event ) { + layout(); + } /** * Invoked when a slide is and we're in the overview. */ function onOverviewSlideClicked( event ) { + // TODO There's a bug here where the event listeners are not // removed after deactivating the overview. if( isOverviewActive() ) { @@ -1556,6 +1711,7 @@ var Reveal = (function(){ slide( h, v ); } } + } @@ -1587,9 +1743,15 @@ var Reveal = (function(){ navigatePrev: navigatePrev, navigateNext: navigateNext, + // Forces an update in slide layout + layout: layout, + // Toggles the overview mode on/off toggleOverview: toggleOverview, + // Toggles the "black screen" mode on/off + togglePause: togglePause, + // Adds or removes all internal event listeners (such as keyboard) addEventListeners: addEventListeners, removeEventListeners: removeEventListeners, |