Skip to content

Commit

Permalink
Merge pull request #23 from golenkovm/h5p-resizer
Browse files Browse the repository at this point in the history
Second attempt to fix H5p resizer
  • Loading branch information
cameron1729 authored Jul 15, 2022
2 parents 9264f99 + 3c356e6 commit ed68c42
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 66 deletions.
5 changes: 3 additions & 2 deletions 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.

128 changes: 64 additions & 64 deletions amd/src/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,86 +12,86 @@
//
// 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;
initEmbedCommunicator = function() {
var resizeDelay;
var instance = H5P.instances[0];
var parentIsFriendly = false;

// Check for H5P instances.
if (!H5P || !H5P.instances || !H5P.instances[0]) {
H5P.externalDispatcher.on('initialized', function() {
setupEmbedCommunicator(iFrame, H5P);
// Handle that the resizer is loaded after the iframe.
H5PEmbedCommunicator.on('ready', function() {
H5PEmbedCommunicator.send('hello');
});
return;
}
setupEmbedCommunicator(iFrame, H5P);
});
});

var setupEmbedCommunicator = function (iFrame, H5P) {
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');
});
// Handle hello message from our parent window.
H5PEmbedCommunicator.on('hello', function() {
// Initial setup/handshake is done.
parentIsFriendly = true;

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

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

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

// 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.
H5PEmbedCommunicator.on('resizePrepared', function() {
H5PEmbedCommunicator.send('resize', {
scrollHeight: iFrame.contentDocument.body.scrollHeight
});
});

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

H5PEmbedCommunicator.on('resize', function() {
H5P.trigger(instance, 'resize');
});
H5P.on(instance, 'resize', function() {
if (H5P.isFullscreen) {
return; // Skip iframe 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);
});

H5P.on(instance, 'resize', function() {
if (H5P.isFullscreen) {
return; // Skip iframe resize.
// 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();
}

// 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');
};
});

});

0 comments on commit ed68c42

Please sign in to comment.