From 52459b6f9561e36b6cf3e67f5a1ebde1ba380bcf Mon Sep 17 00:00:00 2001 From: Torben Dannhauer Date: Sat, 13 Dec 2025 16:16:39 +0100 Subject: [PATCH] Refactor JavaScript handling in form renderer Refactor JavaScript inclusion for form sections to ensure synchronous loading and immediate availability of variables. --- lib/Horde/Form/Renderer.php | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/Horde/Form/Renderer.php b/lib/Horde/Form/Renderer.php index 0e04d8d..ec93f5c 100644 --- a/lib/Horde/Form/Renderer.php +++ b/lib/Horde/Form/Renderer.php @@ -146,15 +146,28 @@ public function _renderSectionTabs($form) /* Add the javascript for the toggling the sections. */ $page = $GLOBALS['injector']->getInstance('Horde_PageOutput'); - $page->addScriptFile('form_sections.js', 'horde'); - $page->addInlineScript( - sprintf( - 'var sections_%1$s = new Horde_Form_Sections(\'%1$s\', \'%2$s\');', - $form->getName(), - rawurlencode($open_section) - ) + $var_name = 'sections_' . $form->getName(); + $var_script = sprintf( + 'var %1$s = new Horde_Form_Sections(\'%2$s\', \'%3$s\');', + $var_name, + $form->getName(), + rawurlencode($open_section) ); - + + /* Output script inline in body to ensure it's loaded synchronously before variable initialization */ + /* Note: Even though scripts in header are loaded synchronously, there can be timing issues + * when the variable is initialized in the body. Inline embedding guarantees the script + * is available immediately when the variable is initialized. */ + $script_path = $GLOBALS['registry']->get('jsfs', 'horde') . 'form_sections.js'; + if (file_exists($script_path)) { + $script_content = file_get_contents($script_path); + echo '' . "\n"; + } + /* Loop through the sections and print out a tab for each. */ echo "

\n"; + + /* Output JavaScript variable directly in body, right after tabs, to ensure it's available immediately */ + /* Script is loaded inline above, so Horde_Form_Sections is guaranteed to be available */ + echo '' . "\n"; } public function _renderSectionBegin($form, $section)