simplify client side notes plugin

This commit is contained in:
Hakim El Hattab 2013-07-26 09:48:21 -04:00
parent 8bf19ab61f
commit 03385d7245
2 changed files with 29 additions and 59 deletions

View file

@ -173,6 +173,7 @@
window.addEventListener( 'message', function( event ) { window.addEventListener( 'message', function( event ) {
var data = JSON.parse( event.data ); var data = JSON.parse( event.data );
// No need for updating the notes in case of fragment changes // No need for updating the notes in case of fragment changes
if ( data.notes !== undefined) { if ( data.notes !== undefined) {
if( data.markdown ) { if( data.markdown ) {
@ -183,18 +184,9 @@
} }
} }
// Showing and hiding fragments // Update the note slides
if( data.fragment === 'next' ) { currentSlide.contentWindow.Reveal.slide( data.indexh, data.indexv, data.indexf );
currentSlide.contentWindow.Reveal.nextFragment(); nextSlide.contentWindow.Reveal.slide( data.nextindexh, data.nextindexv );
}
else if( data.fragment === 'prev' ) {
currentSlide.contentWindow.Reveal.prevFragment();
}
else {
// Update the note slides
currentSlide.contentWindow.Reveal.slide( data.indexh, data.indexv );
nextSlide.contentWindow.Reveal.slide( data.nextindexh, data.nextindexv );
}
}, false ); }, false );

View file

@ -10,72 +10,50 @@ var RevealNotes = (function() {
var notesPopup = window.open( jsFileLocation + 'notes.html', 'reveal.js - Notes', 'width=1120,height=850' ); var notesPopup = window.open( jsFileLocation + 'notes.html', 'reveal.js - Notes', 'width=1120,height=850' );
// Fires when slide is changed // Fires when slide is changed
Reveal.addEventListener( 'slidechanged', function( event ) { Reveal.addEventListener( 'slidechanged', post );
post('slidechanged');
} );
// Fires when a fragment is shown // Fires when a fragment is shown
Reveal.addEventListener( 'fragmentshown', function( event ) { Reveal.addEventListener( 'fragmentshown', post );
post('fragmentshown');
} );
// Fires when a fragment is hidden // Fires when a fragment is hidden
Reveal.addEventListener( 'fragmenthidden', function( event ) { Reveal.addEventListener( 'fragmenthidden', post );
post('fragmenthidden');
} );
/** /**
* Posts the current slide data to the notes window * Posts the current slide data to the notes window
*
* @param {String} eventType Expecting 'slidechanged', 'fragmentshown'
* or 'fragmenthidden' set in the events above to define the needed
* slideDate.
*/ */
function post( eventType ) { function post() {
var slideElement = Reveal.getCurrentSlide(), var slideElement = Reveal.getCurrentSlide(),
slideIndices = Reveal.getIndices(),
messageData; messageData;
if( eventType === 'slidechanged' ) { var notes = slideElement.querySelector( 'aside.notes' ),
var notes = slideElement.querySelector( 'aside.notes' ), nextindexh,
indexh = Reveal.getIndices().h, nextindexv;
indexv = Reveal.getIndices().v,
nextindexh,
nextindexv;
if( slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION' ) { if( slideElement.nextElementSibling && slideElement.parentNode.nodeName == 'SECTION' ) {
nextindexh = indexh; nextindexh = slideIndices.h;
nextindexv = indexv + 1; nextindexv = slideIndices.v + 1;
} else { } else {
nextindexh = indexh + 1; nextindexh = slideIndices.h + 1;
nextindexv = 0; nextindexv = 0;
} }
messageData = { messageData = {
notes : notes ? notes.innerHTML : '', notes : notes ? notes.innerHTML : '',
indexh : indexh, indexh : slideIndices.h,
indexv : indexv, indexv : slideIndices.v,
nextindexh : nextindexh, indexf : slideIndices.f,
nextindexv : nextindexv, nextindexh : nextindexh,
markdown : notes ? typeof notes.getAttribute( 'data-markdown' ) === 'string' : false nextindexv : nextindexv,
}; markdown : notes ? typeof notes.getAttribute( 'data-markdown' ) === 'string' : false
} };
else if( eventType === 'fragmentshown' ) {
messageData = {
fragment : 'next'
};
}
else if( eventType === 'fragmenthidden' ) {
messageData = {
fragment : 'prev'
};
}
notesPopup.postMessage( JSON.stringify( messageData ), '*' ); notesPopup.postMessage( JSON.stringify( messageData ), '*' );
} }
// Navigate to the current slide when the notes are loaded // Navigate to the current slide when the notes are loaded
notesPopup.addEventListener( 'load', function( event ) { notesPopup.addEventListener( 'load', function( event ) {
post('slidechanged'); post();
}, false ); }, false );
} }