h5p-php-library/js/h5p-embed.js

76 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

/*jshint multistr: true */
2014-03-26 08:43:29 +01:00
/**
2015-02-27 14:53:26 +01:00
* Converts old script tag embed to iframe
2014-03-26 08:43:29 +01:00
*/
2015-02-27 14:53:26 +01:00
var H5POldEmbed = H5POldEmbed || (function () {
2014-03-26 08:43:29 +01:00
var head = document.getElementsByTagName('head')[0];
2015-02-27 14:53:26 +01:00
var resizer = false;
2014-03-26 08:43:29 +01:00
/**
2015-02-27 14:53:26 +01:00
* Loads the resizing script
2014-03-26 08:43:29 +01:00
*/
2015-02-27 14:53:26 +01:00
var loadResizer = function (url) {
var data, callback = 'H5POldEmbed';
resizer = true;
2014-03-26 08:43:29 +01:00
// Callback for when content data is loaded.
window[callback] = function (content) {
2015-02-27 14:53:26 +01:00
// Add resizing script to head
var resizer = document.createElement('script');
resizer.src = content;
head.appendChild(resizer);
2014-03-26 08:43:29 +01:00
// Clean up
head.removeChild(data);
delete window[callback];
};
2014-03-26 08:43:29 +01:00
// Create data script
data = document.createElement('script');
data.src = url + (url.indexOf('?') === -1 ? '?' : '&') + 'callback=' + callback;
head.appendChild(data);
};
2015-02-27 14:53:26 +01:00
/**
* Replaced script tag with iframe
*/
var addIframe = function (script) {
// Add iframe
var iframe = document.createElement('iframe');
iframe.src = script.getAttribute('data-h5p');
iframe.frameBorder = false;
iframe.allowFullscreen = true;
var parent = script.parentNode;
parent.insertBefore(iframe, script);
parent.removeChild(script);
};
2014-03-26 08:43:29 +01:00
/**
* Go throught all script tags with the data-h5p attribute and load content.
*/
2015-02-27 14:53:26 +01:00
function H5POldEmbed() {
2014-03-26 08:43:29 +01:00
var scripts = document.getElementsByTagName('script');
var h5ps = []; // Use seperate array since scripts grow in size.
for (var i = 0; i < scripts.length; i++) {
var script = scripts[i];
2015-02-27 14:53:26 +01:00
if (script.src.indexOf('/h5p-resizer.js') !== -1) {
resizer = true;
}
else if (script.hasAttribute('data-h5p')) {
2014-03-26 08:43:29 +01:00
h5ps.push(script);
}
}
for (i = 0; i < h5ps.length; i++) {
2015-02-27 14:53:26 +01:00
if (!resizer) {
loadResizer(h5ps[i].getAttribute('data-h5p'));
2014-04-13 15:52:12 +02:00
}
2015-02-27 14:53:26 +01:00
addIframe(h5ps[i]);
}
2014-03-26 08:43:29 +01:00
}
2015-02-27 14:53:26 +01:00
return H5POldEmbed;
2014-03-26 08:43:29 +01:00
})();
2015-02-27 14:53:26 +01:00
new H5POldEmbed();