cosmetical tweaks to postmessage plugin

This commit is contained in:
Hakim El Hattab 2012-11-10 17:15:11 -05:00
parent 42766b1bfa
commit 87591d95d1
3 changed files with 65 additions and 58 deletions

View File

@ -0,0 +1,39 @@
<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,34 +0,0 @@
<html>
<body>
<iframe id="reveal" src="reveal.js/index.html" 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,10 +1,11 @@
/* /*
simple postmessage plugin
Useful when a reveal slideshow is inside an iframe. simple postmessage plugin
It allows to call reveal methods from outside.
Example: Useful when a reveal slideshow is inside an iframe.
It allows to call reveal methods from outside.
Example:
var reveal = window.frames[0]; var reveal = window.frames[0];
// Reveal.prev(); // Reveal.prev();
@ -14,26 +15,27 @@ Example:
// Reveal.slide(2, 2); // Reveal.slide(2, 2);
reveal.postMessage(JSON.stringify({method: 'slide', args: [2,2]}), '*'); reveal.postMessage(JSON.stringify({method: 'slide', args: [2,2]}), '*');
Add to the slideshow: Add to the slideshow:
dependencies: [ dependencies: [
... ...
{ src: 'plugin/postmessage/postmessage.js', async: true, condition: function() { return !!document.body.classList; } } { src: 'plugin/postmessage/postmessage.js', async: true, condition: function() { return !!document.body.classList; } }
] ]
*/ */
(function (){ (function (){
window.addEventListener("message", function (event){ window.addEventListener( "message", function ( event ) {
var data = JSON.parse(event.data), var data = JSON.parse( event.data ),
method = data.method, method = data.method,
args = data.args; args = data.args;
if (Reveal[method]){
Reveal[method].apply(Reveal, data.args); if( typeof Reveal[method] === 'function' ) {
Reveal[method].apply( Reveal, data.args );
} }
}, false); }, false);
}()); }());