piazza-in-festa-wp/assets/js/resize.js

44 lines
964 B
JavaScript

/**
* Keeps the widget as tall as its content.
*
* The framed page posts its height whenever it changes; we accept the message
* only when it comes from the Piazza in Festa origin and from a window that is
* one of our own iframes, then set the height. Nothing else leaves or enters
* the page.
*/
(function () {
'use strict';
var cfg = window.pinfestaCfg || {};
var origin = cfg.origin || '';
if (!origin) {
return;
}
window.addEventListener('message', function (event) {
if (event.origin !== origin) {
return;
}
var data = event.data;
if (!data || data.tipo !== 'piazzainfesta:altezza') {
return;
}
var altezza = parseInt(data.altezza, 10);
if (!altezza || altezza < 1) {
return;
}
var frames = document.querySelectorAll('iframe.pinfesta-embed');
for (var i = 0; i < frames.length; i++) {
if (frames[i].contentWindow === event.source) {
frames[i].style.height = altezza + 2 + 'px';
}
}
});
})();