From a4b09aecda8f70837c44a18c846ab892a2c0800c Mon Sep 17 00:00:00 2001 From: Hakim El Hattab Date: Fri, 18 Apr 2014 20:03:50 +0200 Subject: [PATCH] bubble all reveal.js events to parent window through postMessage --- js/reveal.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/js/reveal.js b/js/reveal.js index 57f0872..81cbd16 100644 --- a/js/reveal.js +++ b/js/reveal.js @@ -568,10 +568,11 @@ var Reveal = (function(){ window.addEventListener( 'message', function ( event ) { var data = JSON.parse( event.data ); var method = Reveal[data.method]; + if( typeof method === 'function' ) { method.apply( Reveal, data.args ); } - }, false); + }, false ); } @@ -957,13 +958,22 @@ var Reveal = (function(){ * Dispatches an event of the specified type from the * 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 ); - extend( event, properties ); + extend( event, args ); 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 }), '*' ); + } + } /**