diff options
author | Hakim El Hattab | 2013-11-18 09:01:33 -0500 |
---|---|---|
committer | Hakim El Hattab | 2013-11-18 09:01:33 -0500 |
commit | b4e2c97d0c05395fa7ce46ca57dad029f878d088 (patch) | |
tree | 6196ef5dd5ae043b910d2212da54897425c32579 /js/reveal.js | |
parent | e0dccd85bedb75cff41627909ed5282150f22323 (diff) |
initial fragment rewrite
Diffstat (limited to 'js/reveal.js')
-rw-r--r-- | js/reveal.js | 102 |
1 files changed, 66 insertions, 36 deletions
diff --git a/js/reveal.js b/js/reveal.js index 7b81e5c..ed41ce5 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -1047,7 +1047,7 @@ var Reveal = (function(){ a.forEach( function( el, idx ) { if( !el.hasAttribute( 'data-fragment-index' ) ) { - el.setAttribute( 'data-fragment-index', idx ); + el.setAttribute( 'data-fragment-index', idx + 1 ); } } ); @@ -2279,33 +2279,72 @@ var Reveal = (function(){ } /** - * Navigate to the next slide fragment. + * Navigate to the specified slide fragment. * * @return {Boolean} true if there was a next fragment, * false otherwise */ - function nextFragment() { + function navigateFragment( index, offset ) { if( currentSlide && config.fragments ) { - var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment:not(.visible)' ) ); + var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) ); if( fragments.length ) { - // Find the index of the next fragment - var index = fragments[0].getAttribute( 'data-fragment-index' ); - // Find all fragments with the same index - fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' ); + if( typeof index !== 'number' ) { + var lastVisibleFragment = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) ).pop(); + + if( lastVisibleFragment ) { + index = parseInt( lastVisibleFragment.getAttribute( 'data-fragment-index' ) || 1, 10 ); + } + else { + index = 0; + } + } + + if( typeof offset === 'number' ) { + index += offset; + } + + var fragmentsShown = [], + fragmentsHidden = []; + + toArray( fragments ).forEach( function( element, i ) { + + if( i < index ) { + if( !element.classList.contains( 'visible' ) ) fragmentsShown.push( element ); + element.classList.add( 'visible' ); + element.classList.remove( 'current-fragment' ); + + if( i === index ) { + element.classList.add( 'current-fragment' ); + } + } + else { + if( element.classList.contains( 'visible' ) ) fragmentsHidden.push( element ); + element.classList.remove( 'visible' ); + element.classList.remove( 'current-fragment' ); + } + - toArray( fragments ).forEach( function( element ) { - element.classList.add( 'visible' ); } ); - // Notify subscribers of the change - dispatchEvent( 'fragmentshown', { fragment: fragments[0], fragments: fragments } ); + if( offset < 0 && fragmentsHidden.length ) { + console.log('hidden'); + dispatchEvent( 'fragmenthidden', { fragment: fragmentsHidden[0], fragments: fragmentsHidden } ); + } + + if( offset > 0 && fragmentsShown.length ) { + console.log('hidden'); + dispatchEvent( 'fragmentshown', { fragment: fragmentsShown[0], fragments: fragmentsShown } ); + } updateControls(); - return true; + + return !!( fragmentsShown.length || fragmentsHidden.length ); + } + } return false; @@ -2313,36 +2352,26 @@ var Reveal = (function(){ } /** - * Navigate to the previous slide fragment. + * Navigate to the next slide fragment. * - * @return {Boolean} true if there was a previous fragment, + * @return {Boolean} true if there was a next fragment, * false otherwise */ - function previousFragment() { - - if( currentSlide && config.fragments ) { - var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) ); - - if( fragments.length ) { - // Find the index of the previous fragment - var index = fragments[ fragments.length - 1 ].getAttribute( 'data-fragment-index' ); - - // Find all fragments with the same index - fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' ); + function nextFragment() { - toArray( fragments ).forEach( function( f ) { - f.classList.remove( 'visible' ); - } ); + return navigateFragment( null, 1 ); - // Notify subscribers of the change - dispatchEvent( 'fragmenthidden', { fragment: fragments[0], fragments: fragments } ); + } - updateControls(); - return true; - } - } + /** + * Navigate to the previous slide fragment. + * + * @return {Boolean} true if there was a previous fragment, + * false otherwise + */ + function previousFragment() { - return false; + return navigateFragment( null, -1 ); } @@ -3132,6 +3161,7 @@ var Reveal = (function(){ next: navigateNext, prevFragment: previousFragment, nextFragment: nextFragment, + navigateFragment: navigateFragment, // Deprecated aliases navigateTo: slide, |