From cb61ae8d61e7e38d1c3779ccb2df78e4caefd2e9 Mon Sep 17 00:00:00 2001 From: Stephen Abello Date: Mon, 19 Jan 2026 17:00:31 +0100 Subject: [PATCH] Make dashlet that needs to execute JS be able to do so in the first paint and when refreshed --- js/layouts/dashboard/dashboard-grid.js | 18 +++++++++++++++++- .../Base/Component/Dashlet/DashletWrapper.php | 5 +++++ .../UI/Base/Layout/Dashboard/DashboardGrid.php | 5 +++++ .../Layout/Dashboard/DashboardGridSlot.php | 9 +++++---- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/js/layouts/dashboard/dashboard-grid.js b/js/layouts/dashboard/dashboard-grid.js index edecbee4a..0c3cff157 100644 --- a/js/layouts/dashboard/dashboard-grid.js +++ b/js/layouts/dashboard/dashboard-grid.js @@ -88,7 +88,8 @@ class IboGrid extends HTMLElement { RefreshDashlet (sDashlet, aOptions = {}) { const oParser = new DOMParser(); const oDocument = oParser.parseFromString(sDashlet, 'text/html'); - const oNewDashlet = oDocument.body.firstElementChild; + const oNewDashlet = oDocument.body.querySelector('ibo-dashlet'); + const oNewScript = oDocument.body.querySelectorAll('script'); // Can't use oNewDashet.sDashletId as it's not in the DOM yet and connectedCallback hasn't been called yet const oExistingDashlet = this.GetDashletElement(oNewDashlet.getAttribute('data-dashlet-id') ); @@ -114,6 +115,21 @@ class IboGrid extends HTMLElement { oSlotForExistingDashlet.innerHTML = oNewDashlet.outerHTML; this.oGrid.makeWidget(oSlotForExistingDashlet); } + + // Append scripts to body so they are executed + oNewScript.forEach( oScript => { + if (oScript) { + const oNewScriptElement = document.createElement('script'); + + // copy attributes + [...oScript.attributes].forEach(attr => + oNewScriptElement.setAttribute(attr.name, attr.value) + ); + + oNewScriptElement.text = oScript.textContent; + document.body.appendChild(oNewScriptElement); + } + }); } CloneDashlet(sDashletId) { diff --git a/sources/Application/UI/Base/Component/Dashlet/DashletWrapper.php b/sources/Application/UI/Base/Component/Dashlet/DashletWrapper.php index 5ad174384..f2d315028 100644 --- a/sources/Application/UI/Base/Component/Dashlet/DashletWrapper.php +++ b/sources/Application/UI/Base/Component/Dashlet/DashletWrapper.php @@ -58,4 +58,9 @@ class DashletWrapper extends UIBlock { return $this->aFormViewData; } + + public function GetSubBlocks(): array + { + return [$this->oDashletContainer]; + } } diff --git a/sources/Application/UI/Base/Layout/Dashboard/DashboardGrid.php b/sources/Application/UI/Base/Layout/Dashboard/DashboardGrid.php index 7b985402f..b2f4fd642 100644 --- a/sources/Application/UI/Base/Layout/Dashboard/DashboardGrid.php +++ b/sources/Application/UI/Base/Layout/Dashboard/DashboardGrid.php @@ -61,4 +61,9 @@ class DashboardGrid extends UIBlock return $this; } + + public function GetSubBlocks(): array + { + return $this->aSlots; + } } diff --git a/sources/Application/UI/Base/Layout/Dashboard/DashboardGridSlot.php b/sources/Application/UI/Base/Layout/Dashboard/DashboardGridSlot.php index 0e163c2b7..f19385cc8 100644 --- a/sources/Application/UI/Base/Layout/Dashboard/DashboardGridSlot.php +++ b/sources/Application/UI/Base/Layout/Dashboard/DashboardGridSlot.php @@ -36,10 +36,6 @@ class DashboardGridSlot extends UIBlock $this->iHeight = $iHeight; } - public function GetSubBlocks(): array - { - return [$this->oUIBlock]; - } public function HasPositionX(): bool { @@ -116,4 +112,9 @@ class DashboardGridSlot extends UIBlock return $this; } + + public function GetSubBlocks(): array + { + return [$this->oDashlet]; + } }