2015-02-09 10:22:14 +01:00
|
|
|
// H5P iframe Resizer
|
|
|
|
(function () {
|
2015-04-21 15:08:59 +02:00
|
|
|
if (!window.postMessage || !window.addEventListener || window.h5pResizerInitialized) {
|
2015-02-09 10:22:14 +01:00
|
|
|
return; // Not supported
|
|
|
|
}
|
2015-04-21 15:08:59 +02:00
|
|
|
window.h5pResizerInitialized = true;
|
2015-02-09 10:22:14 +01:00
|
|
|
|
|
|
|
// Map actions to handlers
|
|
|
|
var actionHandlers = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare iframe resize.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} iframe Element
|
|
|
|
* @param {Object} data Payload
|
|
|
|
* @param {Function} respond Send a response to the iframe
|
|
|
|
*/
|
|
|
|
actionHandlers.hello = function (iframe, data, respond) {
|
|
|
|
// Make iframe responsive
|
|
|
|
iframe.style.width = '100%';
|
|
|
|
|
2018-11-16 12:39:57 +01:00
|
|
|
// Bugfix for Chrome: Force update of iframe width. If this is not done the
|
|
|
|
// document size may not be updated before the content resizes.
|
|
|
|
iframe.getBoundingClientRect();
|
|
|
|
|
2015-02-09 10:22:14 +01:00
|
|
|
// Tell iframe that it needs to resize when our window resizes
|
2018-10-23 11:25:46 +02:00
|
|
|
var resize = function () {
|
2015-02-09 10:22:14 +01:00
|
|
|
if (iframe.contentWindow) {
|
|
|
|
// Limit resize calls to avoid flickering
|
|
|
|
respond('resize');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Frame is gone, unregister.
|
|
|
|
window.removeEventListener('resize', resize);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.addEventListener('resize', resize, false);
|
|
|
|
|
|
|
|
// Respond to let the iframe know we can resize it
|
|
|
|
respond('hello');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare iframe resize.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} iframe Element
|
|
|
|
* @param {Object} data Payload
|
|
|
|
* @param {Function} respond Send a response to the iframe
|
|
|
|
*/
|
|
|
|
actionHandlers.prepareResize = function (iframe, data, respond) {
|
2016-01-05 13:57:16 +01:00
|
|
|
// Do not resize unless page and scrolling differs
|
|
|
|
if (iframe.clientHeight !== data.scrollHeight ||
|
|
|
|
data.scrollHeight !== data.clientHeight) {
|
|
|
|
|
|
|
|
// Reset iframe height, in case content has shrinked.
|
|
|
|
iframe.style.height = data.clientHeight + 'px';
|
|
|
|
respond('resizePrepared');
|
|
|
|
}
|
2015-02-09 10:22:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize parent and iframe to desired height.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Object} iframe Element
|
|
|
|
* @param {Object} data Payload
|
|
|
|
* @param {Function} respond Send a response to the iframe
|
|
|
|
*/
|
2018-10-23 11:25:46 +02:00
|
|
|
actionHandlers.resize = function (iframe, data) {
|
2015-10-26 14:13:21 +01:00
|
|
|
// Resize iframe so all content is visible. Use scrollHeight to make sure we get everything
|
2016-01-05 13:57:16 +01:00
|
|
|
iframe.style.height = data.scrollHeight + 'px';
|
2015-02-09 10:22:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-09-02 15:44:38 +02:00
|
|
|
* Keyup event handler. Exits full screen on escape.
|
2015-10-26 14:13:21 +01:00
|
|
|
*
|
2015-02-09 10:22:14 +01:00
|
|
|
* @param {Event} event
|
|
|
|
*/
|
2015-08-26 15:58:49 +02:00
|
|
|
var escape = function (event) {
|
2015-02-09 10:22:14 +01:00
|
|
|
if (event.keyCode === 27) {
|
|
|
|
exitFullScreen();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Listen for messages from iframes
|
|
|
|
window.addEventListener('message', function receiveMessage(event) {
|
|
|
|
if (event.data.context !== 'h5p') {
|
|
|
|
return; // Only handle h5p requests.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out who sent the message
|
|
|
|
var iframe, iframes = document.getElementsByTagName('iframe');
|
|
|
|
for (var i = 0; i < iframes.length; i++) {
|
|
|
|
if (iframes[i].contentWindow === event.source) {
|
|
|
|
iframe = iframes[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iframe) {
|
|
|
|
return; // Cannot find sender
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find action handler handler
|
|
|
|
if (actionHandlers[event.data.action]) {
|
2015-02-27 10:29:43 +01:00
|
|
|
actionHandlers[event.data.action](iframe, event.data, function respond(action, data) {
|
2015-02-09 10:22:14 +01:00
|
|
|
if (data === undefined) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
data.action = action;
|
|
|
|
data.context = 'h5p';
|
|
|
|
event.source.postMessage(data, event.origin);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, false);
|
2015-03-02 15:53:29 +01:00
|
|
|
|
|
|
|
// 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, '*');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-09 10:22:14 +01:00
|
|
|
})();
|