-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathiframe-embed.js
101 lines (84 loc) · 2.89 KB
/
iframe-embed.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var H5P = H5P || {};
H5P.IFrameEmbed = function (options, contentId) {
var $ = H5P.jQuery;
var $iframe = null;
this.$ = $(this);
options = H5P.jQuery.extend({
width: "500px",
minWidth: "300px",
height: "500px",
source: "",
resizeSupported: true
}, options);
if (!this instanceof H5P.IFrameEmbed){
return new H5P.IFrameEmbed(options, contentId);
}
this.attach = function ($wrapper) {
// Set up an iframe with the correct source, and append
// it to '$wrapper'.
var iFrameSource = '';
if (options.source !== undefined) {
if (options.source.trim().toLowerCase().substring(0, 4) === 'http') {
iFrameSource = options.source;
}
else {
iFrameSource = H5P.getContentPath(contentId) + '/' + options.source;
}
}
// Remove HTML encoding from source
iFrameSource = new DOMParser().parseFromString(iFrameSource, 'text/html').documentElement.textContent;
// Take care of special characters
iFrameSource = encodeURI(iFrameSource);
$iframe = $('<iframe/>', {
src: iFrameSource,
scrolling: 'no',
frameBorder: 0,
'class': 'h5p-iframe-content h5p-iframe-wrapper',
css: {
width: options.width,
height: options.height,
display: 'block'
}
});
$wrapper.html('');
$wrapper.append($iframe);
if(options.resizeSupported === false) {
/* Unfortunately fullscreen-button is not in DOM yet.
* Therefore we need to remove it using a timer */
setTimeout(function () {
$('.h5p-enable-fullscreen').hide();
}, 1);
}
this.$.trigger('resize');
};
this.resize = function () {
// Set size of 'iframe' on startup, and when the browser
// is resized, or enters fullscreen.
if(options.resizeSupported && $iframe) {
$iframe.css(
(H5P.isFullscreen) ? {width: '100%', height: '100%'} : getElementSize($iframe)
);
}
};
if (options.resizeSupported && this.on !== undefined) {
this.on('resize', this.resize);
}
var getElementSize = function ($element) {
// Get width of 'element' parent. Return width and height
// so that 'element' scales (with the proper ratio) to fit
// the parent. Make sure 'element' doesn't scale below
// 'options.minWidth'.
var elementMinWidth = parseInt(options.minWidth ,10);
var elementSizeRatio = parseInt(options.height, 10) / parseInt(options.width, 10);
var parentWidth = $element.parent().width();
var elementWidth = (parentWidth > elementMinWidth) ? parentWidth : elementMinWidth;
return {
width: elementWidth + 'px',
height: elementWidth * elementSizeRatio + 'px'
};
};
// This is a fix/hack to make touch work in iframe on mobile safari,
// like if the iframe is listening to touch events on the iframe's
// window object. (like in PHET simulations)
window.addEventListener("touchstart", function () {});
};