This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathiframe_resizer.js
50 lines (46 loc) · 1.66 KB
/
iframe_resizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Listen for a window post message to resize an embedded iframe
* Needs to be an json stringified object that identifies the id of
* the element to resize like this:
parent.postMessage(JSON.stringify({
subject: "lti.frameResize",
height: default_height,
iframe_resize_id: "lumen_assessment_1"
}), "*");
* The element_id needed is passed as a query parameter `iframe_resize_id`
*/
if (self == top) {
window.addEventListener('message', function (e) {
try {
var message = JSON.parse(e.data);
switch (message.subject) {
case 'lti.frameResize':
var $iframe = jQuery('#' + message.iframe_resize_id);
if ($iframe.length == 1 && $iframe.hasClass('resizable')) {
var height = message.height;
if (height >= 5000) height = 5000;
if (height <= 0) height = 1;
$iframe.css('height', height + 'px');
}
break;
}
} catch (err) {
(console.error || console.log)('invalid message received from ', e.origin);
}
});
}
/**
* Sends a Window.postMessage to resize the iframe
* (Only works in Canvas for now)
*/
if(self != top) {
// get rid of double iframe scrollbars
var default_height = Math.max(
document.body.scrollHeight, document.body.offsetHeight,
document.documentElement.clientHeight, document.documentElement.scrollHeight,
document.documentElement.offsetHeight);
parent.postMessage(JSON.stringify({
subject: "lti.frameResize",
height: default_height
}), "*");
}