Skip to content

Commit

Permalink
slide method now accepts fragment index argument hakimel#228
Browse files Browse the repository at this point in the history
  • Loading branch information
hakimel committed Nov 22, 2012
2 parents 9d0cb0f + 6ffa60f commit 34b3675
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ The Reveal class provides a minimal JavaScript API for controlling navigation an

```javascript
// Navigation
Reveal.slide( indexh, indexv );
Reveal.slide( indexh, indexv, indexf );
Reveal.left();
Reveal.right();
Reveal.up();
Expand Down
3 changes: 2 additions & 1 deletion grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ module.exports = function(grunt) {
},
globals: {
head: false,
module: false
module: false,
console: false
}
},

Expand Down
62 changes: 48 additions & 14 deletions js/reveal.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,10 @@ 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
* target slide to activate
*/
function slide( h, v ) {
function slide( h, v, f ) {
// Remember where we were at before
previousSlide = currentSlide;

Expand Down Expand Up @@ -774,12 +776,18 @@ var Reveal = (function(){
indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );

// No need to proceed if we're navigating to the same slide as
// we're already on, unless a fragment index is specified
if( indexh === indexhBefore && indexv === indexvBefore && !f ) {
return;
}

layout();

// Apply the new state
stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
// Check if this state existed on the previous slide. If it
// did, we will avoid adding it repeatedly.
// did, we will avoid adding it repeatedly
for( var j = 0; j < stateBefore.length; j++ ) {
if( stateBefore[j] === state[i] ) {
stateBefore.splice( j, 1 );
Expand All @@ -805,8 +813,7 @@ var Reveal = (function(){

// Update the URL hash after a delay since updating it mid-transition
// is likely to cause visual lag
clearTimeout( writeURLTimeout );
writeURLTimeout = setTimeout( writeURL, 1500 );
writeURL( 1500 );

// Find the current horizontal slide and any possible vertical slides
// within it
Expand All @@ -816,6 +823,20 @@ var Reveal = (function(){
// Store references to the previous and current slides
currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;

// Show fragment, if specified
if ( typeof f !== undefined ) {
var fragments = currentSlide.querySelectorAll( '.fragment' );

toArray( fragments ).forEach( function( fragment, indexf ) {
if( indexf < f ) {
fragment.classList.add( 'visible' );
}
else {
fragment.classList.remove( 'visible' );
}
} );
}

// Dispatch an event if the slide changed
if( indexh !== indexhBefore || indexv !== indexvBefore ) {
dispatchEvent( 'slidechanged', {
Expand Down Expand Up @@ -1068,22 +1089,35 @@ var Reveal = (function(){
/**
* Updates the page URL (hash) to reflect the current
* state.
*
* @param {Number} delay The time in ms to wait before
* writing the hash
*/
function writeURL() {
function writeURL( delay ) {
if( config.history ) {
var url = '/';

// If the current slide has an ID, use that as a named link
if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
url = '/' + currentSlide.getAttribute( 'id' );
// Make sure there's never more than one timeout running
clearTimeout( writeURLTimeout );

// If a delay is specified, timeout this call
if( typeof delay === 'number' ) {
writeURLTimeout = setTimeout( writeURL, delay );
}
// Otherwise use the /h/v index
else {
if( indexh > 0 || indexv > 0 ) url += indexh;
if( indexv > 0 ) url += '/' + indexv;
}
var url = '/';

window.location.hash = url;
// If the current slide has an ID, use that as a named link
if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
url = '/' + currentSlide.getAttribute( 'id' );
}
// Otherwise use the /h/v index
else {
if( indexh > 0 || indexv > 0 ) url += indexh;
if( indexv > 0 ) url += '/' + indexv;
}

window.location.hash = url;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions js/reveal.min.js

Large diffs are not rendered by default.

0 comments on commit 34b3675

Please sign in to comment.