aboutsummaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorHakim El Hattab <hakim.elhattab@gmail.com>2015-05-07 10:09:50 +0200
committerHakim El Hattab <hakim.elhattab@gmail.com>2015-05-07 10:09:50 +0200
commitaf270a909cdbf0de6edd25dfac4aeae0a58e8ab7 (patch)
tree4d2b9d20e64be81723a28da350a160b72771b077 /js
parentbf6a426cf29a675fb3d8f6e20376207bee10838f (diff)
downloadfreenode-live-2017-presentation-af270a909cdbf0de6edd25dfac4aeae0a58e8ab7.tar
freenode-live-2017-presentation-af270a909cdbf0de6edd25dfac4aeae0a58e8ab7.tar.gz
iframe postmesssage api works with lazy loaded iframes
Diffstat (limited to 'js')
-rw-r--r--js/reveal.js60
1 files changed, 37 insertions, 23 deletions
diff --git a/js/reveal.js b/js/reveal.js
index 60b8292..2c55833 100644
--- a/js/reveal.js
+++ b/js/reveal.js
@@ -2910,29 +2910,42 @@
}
} );
+ // Normal iframes
+ toArray( slide.querySelectorAll( 'iframe[src]' ) ).forEach( function( el ) {
+ startEmbeddedIframe( { target: el } );
+ } );
+
// Lazy loading iframes
- toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( element ) {
- element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
+ toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
+ if( el.getAttribute( 'src' ) !== el.getAttribute( 'data-src' ) ) {
+ el.removeEventListener( 'load', startEmbeddedIframe ); // remove first to avoid dupes
+ el.addEventListener( 'load', startEmbeddedIframe );
+ el.setAttribute( 'src', el.getAttribute( 'data-src' ) );
+ }
} );
+ }
- // Generic postMessage API for non-lazy loaded iframes
- toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
- el.contentWindow.postMessage( 'slide:start', '*' );
- });
+ }
- // YouTube postMessage API
- toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
- if( el.hasAttribute( 'data-autoplay' ) ) {
- el.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
- }
- });
+ /**
+ * "Starts" the content of an embedded iframe using the
+ * postmessage API.
+ */
+ function startEmbeddedIframe( event ) {
- // Vimeo postMessage API
- toArray( slide.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
- if( el.hasAttribute( 'data-autoplay' ) ) {
- el.contentWindow.postMessage( '{"method":"play"}', '*' );
- }
- });
+ var iframe = event.target;
+
+ // YouTube postMessage API
+ if( /youtube\.com\/embed\//.test( iframe.getAttribute( 'src' ) ) && iframe.hasAttribute( 'data-autoplay' ) ) {
+ iframe.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
+ }
+ // Vimeo postMessage API
+ else if( /player\.vimeo\.com\//.test( iframe.getAttribute( 'src' ) ) && iframe.hasAttribute( 'data-autoplay' ) ) {
+ iframe.contentWindow.postMessage( '{"method":"play"}', '*' );
+ }
+ // Generic postMessage API
+ else {
+ iframe.contentWindow.postMessage( 'slide:start', '*' );
}
}
@@ -2951,14 +2964,10 @@
}
} );
- // Lazy loading iframes
- toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( element ) {
- element.removeAttribute( 'src' );
- } );
-
// Generic postMessage API for non-lazy loaded iframes
toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
el.contentWindow.postMessage( 'slide:stop', '*' );
+ el.removeEventListener( 'load', startEmbeddedIframe );
});
// YouTube postMessage API
@@ -2974,6 +2983,11 @@
el.contentWindow.postMessage( '{"method":"pause"}', '*' );
}
});
+
+ // Lazy loading iframes
+ toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
+ el.removeAttribute( 'src' );
+ } );
}
}