adds new api methods (getCurrentSlide, getPreviousSlide, getIndices) closes #73

edit
Hakim El Hattab 2012-07-26 22:54:36 -04:00
parent 1916d2f64a
commit e570265a67
3 changed files with 61 additions and 26 deletions

View File

@ -65,9 +65,10 @@ Reveal.initialize({
### API ### API
The Reveal class provides a minimal JavaScript API for controlling its navigation: The Reveal class provides a minimal JavaScript API for controlling navigation and reading state:
```javascript ```javascript
// Navigation
Reveal.navigateTo( indexh, indexv ); Reveal.navigateTo( indexh, indexv );
Reveal.navigateLeft(); Reveal.navigateLeft();
Reveal.navigateRight(); Reveal.navigateRight();
@ -76,6 +77,12 @@ Reveal.navigateDown();
Reveal.navigatePrev(); Reveal.navigatePrev();
Reveal.navigateNext(); Reveal.navigateNext();
Reveal.toggleOverview(); Reveal.toggleOverview();
// Retrieves the previous and current slide elements
Reveal.getPreviousSlide();
Reveal.getCurrentSlide();
Reveal.getIndices(); // { h: 0, v: 0 } }
``` ```
### States ### States
@ -172,7 +179,9 @@ You can change the appearance of the speaker notes by editing the file at `plugi
## History ## History
#### 1.5 (master/beta) #### 1.5 (master/beta)
- TBD - New API method ```Reveal.getPreviousSlide()```
- New API method ```Reveal.getCurrentSlide()```
- New API method ```Reveal.getIndices()```
#### 1.4 #### 1.4
- Main ```#reveal container``` is now selected via a class instead of ID - Main ```#reveal container``` is now selected via a class instead of ID

View File

@ -1005,6 +1005,7 @@ body {
background: rgba( 0, 0, 0, 0.6 ); background: rgba( 0, 0, 0, 0.6 );
} }
/********************************************* /*********************************************
* SPEAKER NOTES * SPEAKER NOTES
*********************************************/ *********************************************/

View File

@ -1,5 +1,5 @@
/*! /*!
* reveal.js 1.5 r1 * reveal.js 1.5 r2
* http://lab.hakim.se/reveal-js * http://lab.hakim.se/reveal-js
* MIT licensed * MIT licensed
* *
@ -12,10 +12,6 @@ var Reveal = (function(){
IS_TOUCH_DEVICE = !!( 'ontouchstart' in window ), IS_TOUCH_DEVICE = !!( 'ontouchstart' in window ),
// The horizontal and verical index of the currently active slide
indexh = 0,
indexv = 0,
// Configurations defaults, can be overridden at initialization time // Configurations defaults, can be overridden at initialization time
config = { config = {
// Display controls in the bottom right corner // Display controls in the bottom right corner
@ -50,6 +46,14 @@ var Reveal = (function(){
transition: 'default' // default/cube/page/concave/linear(2d) transition: 'default' // default/cube/page/concave/linear(2d)
}, },
// The horizontal and verical index of the currently active slide
indexh = 0,
indexv = 0,
// The previous and current slide HTML elements
previousSlide,
currentSlide,
// Slides may hold a data-state attribute which we pick up and apply // Slides may hold a data-state attribute which we pick up and apply
// as a class to the body. This list contains the combined state of // as a class to the body. This list contains the combined state of
// all current slides. // all current slides.
@ -650,6 +654,9 @@ var Reveal = (function(){
* set indices. * set indices.
*/ */
function slide( h, v ) { function slide( h, v ) {
// Remember where we were at before
previousSlide = currentSlide;
// Remember the state before this slide // Remember the state before this slide
var stateBefore = state.concat(); var stateBefore = state.concat();
@ -700,31 +707,30 @@ var Reveal = (function(){
clearTimeout( writeURLTimeout ); clearTimeout( writeURLTimeout );
writeURLTimeout = setTimeout( writeURL, 1500 ); writeURLTimeout = setTimeout( writeURL, 1500 );
// Only fire if the slide index is different from before
if( indexh !== indexhBefore || indexv !== indexvBefore ) {
// Query all horizontal slides in the deck // Query all horizontal slides in the deck
var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ); var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
// Find the previous and current horizontal slides // Find the current horizontal slide and any possible vertical slides
var previousHorizontalSlide = horizontalSlides[ indexhBefore ], // within it
currentHorizontalSlide = horizontalSlides[ indexh ]; var currentHorizontalSlide = horizontalSlides[ indexh ],
// Query all vertical slides inside of the previous and current horizontal slides
var previousVerticalSlides = previousHorizontalSlide.querySelectorAll( 'section' );
currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' ); currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
// Dispatch an event notifying observers of the change in slide // Store references to the previous and current slides
currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
// Dispatch an event if the slide changed
if( indexh !== indexhBefore || indexv !== indexvBefore ) {
dispatchEvent( 'slidechanged', { dispatchEvent( 'slidechanged', {
// Include the current indices in the event
'indexh': indexh, 'indexh': indexh,
'indexv': indexv, 'indexv': indexv,
'previousSlide': previousSlide,
// Passes direct references to the slide HTML elements, attempts to find 'currentSlide': currentSlide
// a vertical slide and falls back on the horizontal parent
'previousSlide': previousVerticalSlides[ indexvBefore ] || previousHorizontalSlide,
'currentSlide': currentVerticalSlides[ indexv ] || currentHorizontalSlide
} ); } );
} }
else {
// Ensure that the previous slide is never the same as the current
previousSlide = null;
}
} }
/** /**
@ -980,9 +986,28 @@ var Reveal = (function(){
navigateNext: navigateNext, navigateNext: navigateNext,
toggleOverview: toggleOverview, toggleOverview: toggleOverview,
// Adds or removes all internal event listeners (such as keyboard)
addEventListeners: addEventListeners, addEventListeners: addEventListeners,
removeEventListeners: removeEventListeners, removeEventListeners: removeEventListeners,
// Returns the indices of the current slide
getIndices: function() {
return {
h: indexh,
v: indexv
};
},
// Returns the previous slide element, may be null
getPreviousSlide: function() {
return previousSlide
},
// Returns the current slide element
getCurrentSlide: function() {
return currentSlide
},
// Forward event binding to the reveal DOM element // Forward event binding to the reveal DOM element
addEventListener: function( type, listener, useCapture ) { addEventListener: function( type, listener, useCapture ) {
( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture ); ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );