postmessage plugin is now part of reveal.js core

edit
Hakim El Hattab 2014-04-18 13:56:51 +02:00
parent 38af788a65
commit 11ea0aa3e1
3 changed files with 25 additions and 82 deletions

View File

@ -239,7 +239,6 @@ var Reveal = (function(){
}
/**
* Loads the dependencies of reveal.js. Dependencies are
* defined via the configuration option 'dependencies'
@ -313,6 +312,9 @@ var Reveal = (function(){
// Make sure we've got all the DOM elements we need
setupDOM();
// Listen to messages posted to this window
setupPostMessage();
// Resets all vertical slides so that only the first is visible
resetVerticalSlides();
@ -551,6 +553,28 @@ var Reveal = (function(){
}
/**
* Registers a listener to postMessage events, this makes it
* possible to call all reveal.js API methods from another
* window. For example:
*
* revealWindow.postMessage( JSON.stringify({
* method: 'slide',
* args: [ 2 ]
* }), '*' );
*/
function setupPostMessage() {
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);
}
/**
* Applies the configuration settings from the config
* object. May be called multiple times.

View File

@ -1,39 +0,0 @@
<html>
<body>
<iframe id="reveal" src="../../index.html" style="border: 0;" width="500" height="500"></iframe>
<div>
<input id="back" type="button" value="go back"/>
<input id="ahead" type="button" value="go ahead"/>
<input id="slideto" type="button" value="slideto 2-2"/>
</div>
<script>
(function (){
var back = document.getElementById( 'back' ),
ahead = document.getElementById( 'ahead' ),
slideto = document.getElementById( 'slideto' ),
reveal = window.frames[0];
back.addEventListener( 'click', function () {
reveal.postMessage( JSON.stringify({method: 'prev', args: []}), '*' );
}, false );
ahead.addEventListener( 'click', function (){
reveal.postMessage( JSON.stringify({method: 'next', args: []}), '*' );
}, false );
slideto.addEventListener( 'click', function (){
reveal.postMessage( JSON.stringify({method: 'slide', args: [2,2]}), '*' );
}, false );
}());
</script>
</body>
</html>

View File

@ -1,42 +0,0 @@
/*
simple postmessage plugin
Useful when a reveal slideshow is inside an iframe.
It allows to call reveal methods from outside.
Example:
var reveal = window.frames[0];
// Reveal.prev();
reveal.postMessage(JSON.stringify({method: 'prev', args: []}), '*');
// Reveal.next();
reveal.postMessage(JSON.stringify({method: 'next', args: []}), '*');
// Reveal.slide(2, 2);
reveal.postMessage(JSON.stringify({method: 'slide', args: [2,2]}), '*');
Add to the slideshow:
dependencies: [
...
{ src: 'plugin/postmessage/postmessage.js', async: true, condition: function() { return !!document.body.classList; } }
]
*/
(function (){
window.addEventListener( "message", function ( event ) {
var data = JSON.parse( event.data ),
method = data.method,
args = data.args;
if( typeof Reveal[method] === 'function' ) {
Reveal[method].apply( Reveal, data.args );
}
}, false);
}());