/*
*/
let pbi_width = window.innerWidth;
function pbi_resize(ele, index) {
const elements = document.querySelectorAll(ele);
if (!elements.length || !elements[index]) return;
const el = elements[index];
if (!el.offsetParent) return; // verifica se está visível
let _w_ = el.getAttribute("width");
let _h_ = el.getAttribute("height");
if (_w_ !== null && _h_ !== null && !_h_.includes("%")) {
if (!el.hasAttribute("data-original-w")) {
el.setAttribute("data-original-w", _w_);
el.setAttribute("data-original-h", _h_);
} else {
_w_ = el.getAttribute("data-original-w");
_h_ = el.getAttribute("data-original-h");
}
el.setAttribute("width", "100%");
const newW = el.offsetWidth;
const newH = (_h_ * newW) / _w_;
if (!isNaN(newH)) {
el.setAttribute("height", `${newH}px`);
}
}
}
function pbi_trigger(tipo) {
const iframes = document.querySelectorAll("iframe");
iframes.forEach((iframe, index) => {
const src = iframe.getAttribute("src") || "";
if (src.includes("powerbi")) {
console.log(`iframe ${index}: ${src}`);
pbi_resize("iframe", index);
}
});
}
//Gatilhos
//Init
document.addEventListener("DOMContentLoaded", () => {
pbi_trigger("init");
});
//Resize com debounce
const waitForFinalResizeStreamings = (() => {
const timers = {};
return function (callback, ms, uniqueId) {
if (!uniqueId) uniqueId = "Don't call this twice without a uniqueId";
if (timers[uniqueId]) clearTimeout(timers[uniqueId]);
timers[uniqueId] = setTimeout(callback, ms);
};
})();
window.addEventListener("resize", () => {
waitForFinalResizeStreamings(() => {
if (pbi_width !== window.innerWidth) {
pbi_width = window.innerWidth;
pbi_trigger("resize");
}
}, 400, "unique string");
});