| 1441 |
ariadna |
1 |
/*jshint multistr: true */
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Converts old script tag embed to iframe
|
|
|
5 |
*/
|
|
|
6 |
var H5POldEmbed = H5POldEmbed || (function () {
|
|
|
7 |
var head = document.getElementsByTagName('head')[0];
|
|
|
8 |
var resizer = false;
|
|
|
9 |
|
|
|
10 |
/**
|
|
|
11 |
* Loads the resizing script
|
|
|
12 |
*/
|
|
|
13 |
var loadResizer = function (url) {
|
|
|
14 |
var data, callback = 'H5POldEmbed';
|
|
|
15 |
resizer = true;
|
|
|
16 |
|
|
|
17 |
// Callback for when content data is loaded.
|
|
|
18 |
window[callback] = function (content) {
|
|
|
19 |
// Add resizing script to head
|
|
|
20 |
var resizer = document.createElement('script');
|
|
|
21 |
resizer.src = content;
|
|
|
22 |
head.appendChild(resizer);
|
|
|
23 |
|
|
|
24 |
// Clean up
|
|
|
25 |
head.removeChild(data);
|
|
|
26 |
delete window[callback];
|
|
|
27 |
};
|
|
|
28 |
|
|
|
29 |
// Create data script
|
|
|
30 |
data = document.createElement('script');
|
|
|
31 |
data.src = url + (url.indexOf('?') === -1 ? '?' : '&') + 'callback=' + callback;
|
|
|
32 |
head.appendChild(data);
|
|
|
33 |
};
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Replaced script tag with iframe
|
|
|
37 |
*/
|
|
|
38 |
var addIframe = function (script) {
|
|
|
39 |
// Add iframe
|
|
|
40 |
var iframe = document.createElement('iframe');
|
|
|
41 |
iframe.src = script.getAttribute('data-h5p');
|
|
|
42 |
iframe.frameBorder = false;
|
|
|
43 |
iframe.allowFullscreen = true;
|
|
|
44 |
var parent = script.parentNode;
|
|
|
45 |
parent.insertBefore(iframe, script);
|
|
|
46 |
parent.removeChild(script);
|
|
|
47 |
};
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Go throught all script tags with the data-h5p attribute and load content.
|
|
|
51 |
*/
|
|
|
52 |
function H5POldEmbed() {
|
|
|
53 |
var scripts = document.getElementsByTagName('script');
|
|
|
54 |
var h5ps = []; // Use seperate array since scripts grow in size.
|
|
|
55 |
for (var i = 0; i < scripts.length; i++) {
|
|
|
56 |
var script = scripts[i];
|
|
|
57 |
if (script.src.indexOf('/h5p-resizer.js') !== -1) {
|
|
|
58 |
resizer = true;
|
|
|
59 |
}
|
|
|
60 |
else if (script.hasAttribute('data-h5p')) {
|
|
|
61 |
h5ps.push(script);
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
for (i = 0; i < h5ps.length; i++) {
|
|
|
65 |
if (!resizer) {
|
|
|
66 |
loadResizer(h5ps[i].getAttribute('data-h5p'));
|
|
|
67 |
}
|
|
|
68 |
addIframe(h5ps[i]);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
return H5POldEmbed;
|
|
|
73 |
})();
|
|
|
74 |
|
|
|
75 |
new H5POldEmbed();
|