1 |
efrain |
1 |
// H5P iframe Resizer
|
|
|
2 |
(function () {
|
|
|
3 |
if (!window.postMessage || !window.addEventListener || window.h5pResizerInitialized) {
|
|
|
4 |
return; // Not supported
|
|
|
5 |
}
|
|
|
6 |
window.h5pResizerInitialized = true;
|
|
|
7 |
|
|
|
8 |
// Map actions to handlers
|
|
|
9 |
var actionHandlers = {};
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Prepare iframe resize.
|
|
|
13 |
*
|
|
|
14 |
* @private
|
|
|
15 |
* @param {Object} iframe Element
|
|
|
16 |
* @param {Object} data Payload
|
|
|
17 |
* @param {Function} respond Send a response to the iframe
|
|
|
18 |
*/
|
|
|
19 |
actionHandlers.hello = function (iframe, data, respond) {
|
|
|
20 |
// Make iframe responsive
|
|
|
21 |
iframe.style.width = '100%';
|
|
|
22 |
|
|
|
23 |
// Bugfix for Chrome: Force update of iframe width. If this is not done the
|
|
|
24 |
// document size may not be updated before the content resizes.
|
|
|
25 |
iframe.getBoundingClientRect();
|
|
|
26 |
|
|
|
27 |
// Tell iframe that it needs to resize when our window resizes
|
|
|
28 |
var resize = function () {
|
|
|
29 |
if (iframe.contentWindow) {
|
|
|
30 |
// Limit resize calls to avoid flickering
|
|
|
31 |
respond('resize');
|
|
|
32 |
}
|
|
|
33 |
else {
|
|
|
34 |
// Frame is gone, unregister.
|
|
|
35 |
window.removeEventListener('resize', resize);
|
|
|
36 |
}
|
|
|
37 |
};
|
|
|
38 |
window.addEventListener('resize', resize, false);
|
|
|
39 |
|
|
|
40 |
// Respond to let the iframe know we can resize it
|
|
|
41 |
respond('hello');
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Prepare iframe resize.
|
|
|
46 |
*
|
|
|
47 |
* @private
|
|
|
48 |
* @param {Object} iframe Element
|
|
|
49 |
* @param {Object} data Payload
|
|
|
50 |
* @param {Function} respond Send a response to the iframe
|
|
|
51 |
*/
|
|
|
52 |
actionHandlers.prepareResize = function (iframe, data, respond) {
|
|
|
53 |
// Do not resize unless page and scrolling differs
|
|
|
54 |
if (iframe.clientHeight !== data.scrollHeight ||
|
|
|
55 |
data.scrollHeight !== data.clientHeight) {
|
|
|
56 |
|
|
|
57 |
// Reset iframe height, in case content has shrinked.
|
|
|
58 |
iframe.style.height = data.clientHeight + 'px';
|
|
|
59 |
respond('resizePrepared');
|
|
|
60 |
}
|
|
|
61 |
};
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Resize parent and iframe to desired height.
|
|
|
65 |
*
|
|
|
66 |
* @private
|
|
|
67 |
* @param {Object} iframe Element
|
|
|
68 |
* @param {Object} data Payload
|
|
|
69 |
* @param {Function} respond Send a response to the iframe
|
|
|
70 |
*/
|
|
|
71 |
actionHandlers.resize = function (iframe, data) {
|
|
|
72 |
// Resize iframe so all content is visible. Use scrollHeight to make sure we get everything
|
|
|
73 |
iframe.style.height = data.scrollHeight + 'px';
|
|
|
74 |
};
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Keyup event handler. Exits full screen on escape.
|
|
|
78 |
*
|
|
|
79 |
* @param {Event} event
|
|
|
80 |
*/
|
|
|
81 |
var escape = function (event) {
|
|
|
82 |
if (event.keyCode === 27) {
|
|
|
83 |
exitFullScreen();
|
|
|
84 |
}
|
|
|
85 |
};
|
|
|
86 |
|
|
|
87 |
// Listen for messages from iframes
|
|
|
88 |
window.addEventListener('message', function receiveMessage(event) {
|
|
|
89 |
if (event.data.context !== 'h5p') {
|
|
|
90 |
return; // Only handle h5p requests.
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// Find out who sent the message
|
|
|
94 |
var iframe, iframes = document.getElementsByTagName('iframe');
|
|
|
95 |
for (var i = 0; i < iframes.length; i++) {
|
|
|
96 |
if (iframes[i].contentWindow === event.source) {
|
|
|
97 |
iframe = iframes[i];
|
|
|
98 |
break;
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
if (!iframe) {
|
|
|
103 |
return; // Cannot find sender
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
// Find action handler handler
|
|
|
107 |
if (actionHandlers[event.data.action]) {
|
|
|
108 |
actionHandlers[event.data.action](iframe, event.data, function respond(action, data) {
|
|
|
109 |
if (data === undefined) {
|
|
|
110 |
data = {};
|
|
|
111 |
}
|
|
|
112 |
data.action = action;
|
|
|
113 |
data.context = 'h5p';
|
|
|
114 |
event.source.postMessage(data, event.origin);
|
|
|
115 |
});
|
|
|
116 |
}
|
|
|
117 |
}, false);
|
|
|
118 |
|
|
|
119 |
// Let h5p iframes know we're ready!
|
|
|
120 |
var iframes = document.getElementsByTagName('iframe');
|
|
|
121 |
var ready = {
|
|
|
122 |
context: 'h5p',
|
|
|
123 |
action: 'ready'
|
|
|
124 |
};
|
|
|
125 |
for (var i = 0; i < iframes.length; i++) {
|
|
|
126 |
if (iframes[i].src.indexOf('h5p') !== -1) {
|
|
|
127 |
iframes[i].contentWindow.postMessage(ready, '*');
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
})();
|