1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
define(['jquery', 'mod_hvp/communicator'], function($, H5PEmbedCommunicator) {
|
|
|
17 |
|
|
|
18 |
// Wait for instances to be initialize.
|
|
|
19 |
$(document).ready(function() {
|
|
|
20 |
$('.h5p-iframe').ready(function() {
|
|
|
21 |
var iFrame = document.querySelector('.h5p-iframe');
|
|
|
22 |
var H5P = iFrame.contentWindow.H5P;
|
|
|
23 |
|
|
|
24 |
// Check for H5P instances.
|
|
|
25 |
if (!H5P || !H5P.instances || !H5P.instances[0]) {
|
|
|
26 |
return;
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
var resizeDelay;
|
|
|
30 |
var instance = H5P.instances[0];
|
|
|
31 |
var parentIsFriendly = false;
|
|
|
32 |
|
|
|
33 |
// Handle that the resizer is loaded after the iframe.
|
|
|
34 |
H5PEmbedCommunicator.on('ready', function() {
|
|
|
35 |
H5PEmbedCommunicator.send('hello');
|
|
|
36 |
});
|
|
|
37 |
|
|
|
38 |
// Handle hello message from our parent window.
|
|
|
39 |
H5PEmbedCommunicator.on('hello', function() {
|
|
|
40 |
// Initial setup/handshake is done.
|
|
|
41 |
parentIsFriendly = true;
|
|
|
42 |
|
|
|
43 |
// Hide scrollbars for correct size.
|
|
|
44 |
iFrame.contentDocument.body.style.overflow = 'hidden';
|
|
|
45 |
|
|
|
46 |
document.body.classList.add('h5p-resizing');
|
|
|
47 |
|
|
|
48 |
// Content need to be resized to fit the new iframe size.
|
|
|
49 |
H5P.trigger(instance, 'resize');
|
|
|
50 |
});
|
|
|
51 |
|
|
|
52 |
// When resize has been prepared tell parent window to resize.
|
|
|
53 |
H5PEmbedCommunicator.on('resizePrepared', function() {
|
|
|
54 |
H5PEmbedCommunicator.send('resize', {
|
|
|
55 |
scrollHeight: iFrame.contentDocument.body.scrollHeight
|
|
|
56 |
});
|
|
|
57 |
});
|
|
|
58 |
|
|
|
59 |
H5PEmbedCommunicator.on('resize', function() {
|
|
|
60 |
H5P.trigger(instance, 'resize');
|
|
|
61 |
});
|
|
|
62 |
|
|
|
63 |
H5P.on(instance, 'resize', function() {
|
|
|
64 |
if (H5P.isFullscreen) {
|
|
|
65 |
return; // Skip iframe resize.
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
// Use a delay to make sure iframe is resized to the correct size.
|
|
|
69 |
clearTimeout(resizeDelay);
|
|
|
70 |
resizeDelay = setTimeout(function() {
|
|
|
71 |
// Only resize if the iframe can be resized.
|
|
|
72 |
if (parentIsFriendly) {
|
|
|
73 |
H5PEmbedCommunicator.send('prepareResize',
|
|
|
74 |
{
|
|
|
75 |
scrollHeight: iFrame.contentDocument.body.scrollHeight,
|
|
|
76 |
clientHeight: iFrame.contentDocument.body.clientHeight
|
|
|
77 |
}
|
|
|
78 |
);
|
|
|
79 |
} else {
|
|
|
80 |
H5PEmbedCommunicator.send('hello');
|
|
|
81 |
}
|
|
|
82 |
}, 0);
|
|
|
83 |
});
|
|
|
84 |
|
|
|
85 |
// Trigger initial resize for instance.
|
|
|
86 |
H5P.trigger(instance, 'resize');
|
|
|
87 |
});
|
|
|
88 |
});
|
|
|
89 |
|
|
|
90 |
});
|