From e961f614a427d19597d2928411d8d69e24c9e3ea Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Mon, 2 Mar 2015 15:53:29 +0100 Subject: [PATCH] Fixed so that iframes can be loaded before resizer script. --- js/h5p-embed.js | 1 - js/h5p-resizer.js | 13 +++++++++++++ js/h5p.js | 12 +++++++++--- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/js/h5p-embed.js b/js/h5p-embed.js index 3196fa1..c141456 100644 --- a/js/h5p-embed.js +++ b/js/h5p-embed.js @@ -66,7 +66,6 @@ var H5POldEmbed = H5POldEmbed || (function () { loadResizer(h5ps[i].getAttribute('data-h5p')); } addIframe(h5ps[i]); - } } diff --git a/js/h5p-resizer.js b/js/h5p-resizer.js index d1a1905..6318fc8 100644 --- a/js/h5p-resizer.js +++ b/js/h5p-resizer.js @@ -108,4 +108,17 @@ }); } }, false); + + // Let h5p iframes know we're ready! + var iframes = document.getElementsByTagName('iframe'); + var ready = { + context: 'h5p', + action: 'ready' + }; + for (var i = 0; i < iframes.length; i++) { + if (iframes[i].src.indexOf('h5p') !== -1) { + iframes[i].contentWindow.postMessage(ready, '*'); + } + } + })(); diff --git a/js/h5p.js b/js/h5p.js index 8ade0e6..1937f43 100644 --- a/js/h5p.js +++ b/js/h5p.js @@ -145,6 +145,11 @@ H5P.init = function () { // External embed var parentIsFriendly = false; + // Handle that the resizer is loaded after the iframe + H5P.communicator.on('ready', function () { + H5P.communicator.send('hello'); + }); + // Handle hello message from our parent window H5P.communicator.on('hello', function () { // Initial setup/handshake is done @@ -153,7 +158,8 @@ H5P.init = function () { // Hide scrollbars for correct size document.body.style.overflow = 'hidden'; - H5P.communicator.send('prepareResize'); + // Content need to be resized to fit the new iframe size + H5P.trigger(instance, 'resize'); }); // When resize has been prepared tell parent window to resize @@ -192,14 +198,14 @@ H5P.init = function () { H5P.jQuery(window.top).resize(function () { if (window.parent.H5P.isFullscreen) { // Use timeout to avoid bug in certain browsers when exiting fullscreen. Some browser will trigger resize before the fullscreenchange event. - H5P.trigger(instance, 'resize'); + H5P.trigger(instance, 'resize'); } else { H5P.trigger(instance, 'resize'); } }); } - + H5P.instances.push(instance); // Resize content.