From 9aa5d258419108c449e9d454a4ba01530e0d830d Mon Sep 17 00:00:00 2001 From: Cristhian Garcia Date: Fri, 30 Aug 2024 14:59:31 -0500 Subject: [PATCH] fix: delay loading of unfocused dashboards --- .../static/js/embed_dashboard.js | 24 +++++++++++++++---- platform_plugin_aspects/static/js/superset.js | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/platform_plugin_aspects/static/js/embed_dashboard.js b/platform_plugin_aspects/static/js/embed_dashboard.js index eafedb6..5555c62 100644 --- a/platform_plugin_aspects/static/js/embed_dashboard.js +++ b/platform_plugin_aspects/static/js/embed_dashboard.js @@ -43,9 +43,7 @@ async function fetchGuestToken() { return data.guestToken; } -function embedDashboard(dashboard_uuid, superset_url, xblock_id) { - xblock_id = xblock_id || ""; - +function _embedDashboard(dashboard_uuid, superset_url, xblock_id){ window.supersetEmbeddedSdk .embedDashboard({ id: dashboard_uuid, // given by the Superset embedding UI @@ -70,10 +68,26 @@ function embedDashboard(dashboard_uuid, superset_url, xblock_id) { when the dashboard is loaded */ }); +} + +function embedDashboard(dashboard, superset_url, xblock_id, index) { + xblock_id = xblock_id || ""; + let radio = document.querySelector(`#tab-${index+1}`) + if (index == 0){ + dashboard.loaded = true; + _embedDashboard(dashboard.uuid, superset_url, xblock_id) + } + radio.addEventListener("change", () => { + if (dashboard.loaded){ + return + } + dashboard.loaded = true; + _embedDashboard(dashboard.uuid, superset_url, xblock_id) + }); }; if (window.superset_dashboards !== undefined) { - window.superset_dashboards.forEach(function(dashboard) { - embedDashboard(dashboard.uuid, window.superset_url, dashboard.uuid); + window.superset_dashboards.forEach(function(dashboard, i) { + embedDashboard(dashboard, window.superset_url, dashboard.uuid, i); }); } diff --git a/platform_plugin_aspects/static/js/superset.js b/platform_plugin_aspects/static/js/superset.js index a29749d..190d4f1 100644 --- a/platform_plugin_aspects/static/js/superset.js +++ b/platform_plugin_aspects/static/js/superset.js @@ -8,7 +8,7 @@ function SupersetXBlock(runtime, element, context) { window.from_xblock = true; function initSuperset(supersetEmbeddedSdk) { - embedDashboard(dashboard_uuid, superset_url, xblock_id); + _embedDashboard(dashboard_uuid, superset_url, xblock_id); } if (typeof require === "function") {