bubble all reveal.js events to parent window through postMessage

This commit is contained in:
Hakim El Hattab 2014-04-18 20:03:50 +02:00
parent 11ea0aa3e1
commit a4b09aecda

View File

@ -568,6 +568,7 @@ var Reveal = (function(){
window.addEventListener( 'message', function ( event ) { window.addEventListener( 'message', function ( event ) {
var data = JSON.parse( event.data ); var data = JSON.parse( event.data );
var method = Reveal[data.method]; var method = Reveal[data.method];
if( typeof method === 'function' ) { if( typeof method === 'function' ) {
method.apply( Reveal, data.args ); method.apply( Reveal, data.args );
} }
@ -957,13 +958,22 @@ var Reveal = (function(){
* Dispatches an event of the specified type from the * Dispatches an event of the specified type from the
* reveal DOM element. * reveal DOM element.
*/ */
function dispatchEvent( type, properties ) { function dispatchEvent( type, args ) {
var event = document.createEvent( "HTMLEvents", 1, 2 ); var event = document.createEvent( 'HTMLEvents', 1, 2 );
event.initEvent( type, true, true ); event.initEvent( type, true, true );
extend( event, properties ); extend( event, args );
dom.wrapper.dispatchEvent( event ); dom.wrapper.dispatchEvent( event );
// If we're in an iframe, post each reveal.js event to the
// parent window. Used by the notes plugin
if( window.parent !== window.self ) {
// Remove arguments that can't be stringified (circular structures)
if( args && args.currentSlide ) delete args.currentSlide;
if( args && args.previousSlide ) delete args.previousSlide;
window.parent.postMessage( JSON.stringify({ namespace: 'reveal', eventName: type, eventArgs: args || null }), '*' );
}
} }
/** /**