Skip to content

Commit e570265

Browse files
committed
adds new api methods (getCurrentSlide, getPreviousSlide, getIndices) closes hakimel#73
1 parent 1916d2f commit e570265

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ Reveal.initialize({
6565

6666
### API
6767

68-
The Reveal class provides a minimal JavaScript API for controlling its navigation:
68+
The Reveal class provides a minimal JavaScript API for controlling navigation and reading state:
6969

7070
```javascript
71+
// Navigation
7172
Reveal.navigateTo( indexh, indexv );
7273
Reveal.navigateLeft();
7374
Reveal.navigateRight();
@@ -76,6 +77,12 @@ Reveal.navigateDown();
7677
Reveal.navigatePrev();
7778
Reveal.navigateNext();
7879
Reveal.toggleOverview();
80+
81+
// Retrieves the previous and current slide elements
82+
Reveal.getPreviousSlide();
83+
Reveal.getCurrentSlide();
84+
85+
Reveal.getIndices(); // { h: 0, v: 0 } }
7986
```
8087

8188
### States
@@ -172,7 +179,9 @@ You can change the appearance of the speaker notes by editing the file at `plugi
172179
## History
173180

174181
#### 1.5 (master/beta)
175-
- TBD
182+
- New API method ```Reveal.getPreviousSlide()```
183+
- New API method ```Reveal.getCurrentSlide()```
184+
- New API method ```Reveal.getIndices()```
176185

177186
#### 1.4
178187
- Main ```#reveal container``` is now selected via a class instead of ID

css/main.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ body {
10051005
background: rgba( 0, 0, 0, 0.6 );
10061006
}
10071007

1008+
10081009
/*********************************************
10091010
* SPEAKER NOTES
10101011
*********************************************/

js/reveal.js

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* reveal.js 1.5 r1
2+
* reveal.js 1.5 r2
33
* http://lab.hakim.se/reveal-js
44
* MIT licensed
55
*
@@ -12,10 +12,6 @@ var Reveal = (function(){
1212

1313
IS_TOUCH_DEVICE = !!( 'ontouchstart' in window ),
1414

15-
// The horizontal and verical index of the currently active slide
16-
indexh = 0,
17-
indexv = 0,
18-
1915
// Configurations defaults, can be overridden at initialization time
2016
config = {
2117
// Display controls in the bottom right corner
@@ -50,6 +46,14 @@ var Reveal = (function(){
5046
transition: 'default' // default/cube/page/concave/linear(2d)
5147
},
5248

49+
// The horizontal and verical index of the currently active slide
50+
indexh = 0,
51+
indexv = 0,
52+
53+
// The previous and current slide HTML elements
54+
previousSlide,
55+
currentSlide,
56+
5357
// Slides may hold a data-state attribute which we pick up and apply
5458
// as a class to the body. This list contains the combined state of
5559
// all current slides.
@@ -650,6 +654,9 @@ var Reveal = (function(){
650654
* set indices.
651655
*/
652656
function slide( h, v ) {
657+
// Remember where we were at before
658+
previousSlide = currentSlide;
659+
653660
// Remember the state before this slide
654661
var stateBefore = state.concat();
655662

@@ -700,31 +707,30 @@ var Reveal = (function(){
700707
clearTimeout( writeURLTimeout );
701708
writeURLTimeout = setTimeout( writeURL, 1500 );
702709

703-
// Only fire if the slide index is different from before
704-
if( indexh !== indexhBefore || indexv !== indexvBefore ) {
705-
// Query all horizontal slides in the deck
706-
var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
710+
// Query all horizontal slides in the deck
711+
var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
707712

708-
// Find the previous and current horizontal slides
709-
var previousHorizontalSlide = horizontalSlides[ indexhBefore ],
710-
currentHorizontalSlide = horizontalSlides[ indexh ];
713+
// Find the current horizontal slide and any possible vertical slides
714+
// within it
715+
var currentHorizontalSlide = horizontalSlides[ indexh ],
716+
currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
711717

712-
// Query all vertical slides inside of the previous and current horizontal slides
713-
var previousVerticalSlides = previousHorizontalSlide.querySelectorAll( 'section' );
714-
currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
718+
// Store references to the previous and current slides
719+
currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
715720

716-
// Dispatch an event notifying observers of the change in slide
721+
// Dispatch an event if the slide changed
722+
if( indexh !== indexhBefore || indexv !== indexvBefore ) {
717723
dispatchEvent( 'slidechanged', {
718-
// Include the current indices in the event
719724
'indexh': indexh,
720725
'indexv': indexv,
721-
722-
// Passes direct references to the slide HTML elements, attempts to find
723-
// a vertical slide and falls back on the horizontal parent
724-
'previousSlide': previousVerticalSlides[ indexvBefore ] || previousHorizontalSlide,
725-
'currentSlide': currentVerticalSlides[ indexv ] || currentHorizontalSlide
726+
'previousSlide': previousSlide,
727+
'currentSlide': currentSlide
726728
} );
727729
}
730+
else {
731+
// Ensure that the previous slide is never the same as the current
732+
previousSlide = null;
733+
}
728734
}
729735

730736
/**
@@ -980,9 +986,28 @@ var Reveal = (function(){
980986
navigateNext: navigateNext,
981987
toggleOverview: toggleOverview,
982988

989+
// Adds or removes all internal event listeners (such as keyboard)
983990
addEventListeners: addEventListeners,
984991
removeEventListeners: removeEventListeners,
985992

993+
// Returns the indices of the current slide
994+
getIndices: function() {
995+
return {
996+
h: indexh,
997+
v: indexv
998+
};
999+
},
1000+
1001+
// Returns the previous slide element, may be null
1002+
getPreviousSlide: function() {
1003+
return previousSlide
1004+
},
1005+
1006+
// Returns the current slide element
1007+
getCurrentSlide: function() {
1008+
return currentSlide
1009+
},
1010+
9861011
// Forward event binding to the reveal DOM element
9871012
addEventListener: function( type, listener, useCapture ) {
9881013
( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );

0 commit comments

Comments
 (0)