Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix H5P resizer (stable branch) #458

Open
wants to merge 2 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion amd/build/embed.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions amd/build/embed.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 63 additions & 56 deletions amd/src/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,85 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/* eslint-disable no-undef, no-console, no-unused-vars, max-len */
define(['jquery', 'mod_hvp/communicator'], function($, H5PEmbedCommunicator) {

// Wait for instances to be initialize.
$(document).ready(function() {
$('.h5p-iframe').ready(function() {
var iFrame = document.querySelector('.h5p-iframe');
var H5P = iFrame.contentWindow.H5P;

// Check for H5P instances.
if (!H5P || !H5P.instances || !H5P.instances[0]) {
return;
}

var resizeDelay;
var instance = H5P.instances[0];
var parentIsFriendly = false;

// Handle that the resizer is loaded after the iframe.
H5PEmbedCommunicator.on('ready', function() {
H5PEmbedCommunicator.send('hello');
});
initEmbedCommunicator = function() {
var resizeDelay;
var instance = H5P.instances[0];
var parentIsFriendly = false;

// Handle hello message from our parent window.
H5PEmbedCommunicator.on('hello', function() {
// Initial setup/handshake is done.
parentIsFriendly = true;
// Handle that the resizer is loaded after the iframe.
H5PEmbedCommunicator.on('ready', function() {
H5PEmbedCommunicator.send('hello');
});

// Hide scrollbars for correct size.
iFrame.contentDocument.body.style.overflow = 'hidden';
// Handle hello message from our parent window.
H5PEmbedCommunicator.on('hello', function() {
// Initial setup/handshake is done.
parentIsFriendly = true;

document.body.classList.add('h5p-resizing');
// Hide scrollbars for correct size.
iFrame.contentDocument.body.style.overflow = 'hidden';

// Content need to be resized to fit the new iframe size.
H5P.trigger(instance, 'resize');
});
document.body.classList.add('h5p-resizing');

// When resize has been prepared tell parent window to resize.
H5PEmbedCommunicator.on('resizePrepared', function() {
H5PEmbedCommunicator.send('resize', {
scrollHeight: iFrame.contentDocument.body.scrollHeight
// Content need to be resized to fit the new iframe size.
H5P.trigger(instance, 'resize');
});
});

H5PEmbedCommunicator.on('resize', function() {
H5P.trigger(instance, 'resize');
});
// When resize has been prepared tell parent window to resize.
H5PEmbedCommunicator.on('resizePrepared', function() {
H5PEmbedCommunicator.send('resize', {
scrollHeight: iFrame.contentDocument.body.scrollHeight
});
});

H5P.on(instance, 'resize', function() {
if (H5P.isFullscreen) {
return; // Skip iframe resize.
}
H5PEmbedCommunicator.on('resize', function() {
H5P.trigger(instance, 'resize');
});

// Use a delay to make sure iframe is resized to the correct size.
clearTimeout(resizeDelay);
resizeDelay = setTimeout(function() {
// Only resize if the iframe can be resized.
if (parentIsFriendly) {
H5PEmbedCommunicator.send('prepareResize',
{
scrollHeight: iFrame.contentDocument.body.scrollHeight,
clientHeight: iFrame.contentDocument.body.clientHeight
}
);
} else {
H5PEmbedCommunicator.send('hello');
H5P.on(instance, 'resize', function() {
if (H5P.isFullscreen) {
return; // Skip iframe resize.
}
}, 0);
});

// Trigger initial resize for instance.
H5P.trigger(instance, 'resize');
// Use a delay to make sure iframe is resized to the correct size.
clearTimeout(resizeDelay);
resizeDelay = setTimeout(function() {
// Only resize if the iframe can be resized.
if (parentIsFriendly) {
H5PEmbedCommunicator.send('prepareResize',
{
scrollHeight: iFrame.contentDocument.body.scrollHeight,
clientHeight: iFrame.contentDocument.body.clientHeight
}
);
} else {
H5PEmbedCommunicator.send('hello');
}
}, 0);
});

// Trigger initial resize for instance.
H5P.trigger(instance, 'resize');
};
var iFrame = document.querySelector('.h5p-iframe');
var H5P = iFrame.contentWindow.H5P;
// Check for H5P instances.
if (!H5P || !H5P.instances || !H5P.instances[0]) {
console.warn("H5P embed.js: ACK! Embedded H5P.instances[0] in lowest iframe is not set up yet. Waiting for 'initialized' event");
window.H5P.externalDispatcher.on('initialized', function(event) {
console.log("H5P embed.js: 'initialized' event received");
H5P = iFrame.contentWindow.H5P;
initEmbedCommunicator();
});
} else {
initEmbedCommunicator();
}
});
});

Expand Down