Proposition that allows to include modules from ar array. Works both on usual page + ajax page

This commit is contained in:
jf-cbd
2025-12-23 14:55:00 +01:00
parent a627f8a471
commit 35d3194e6c
11 changed files with 190 additions and 8 deletions

View File

@@ -65,6 +65,11 @@ abstract class UIBlock implements iUIBlock
public const DEFAULT_JS_FILES_REL_PATH = [
'js/ui-block.js',
];
/**
* @var array
* @see static::$aJsModulesConfig
*/
public const DEFAULT_JS_MODULES_CONFIG = [];
/**
* @var string|null Relative path (from <ITOP>/templates/) to the "on init" JS template
* @see static::$aJsTemplatesRelPath
@@ -138,6 +143,11 @@ abstract class UIBlock implements iUIBlock
* and not in {@see static::DEFAULT_JS_TEMPLATE_REL_PATH} ! Indeed the later is output before external files loading.
*/
protected $aJsFilesRelPath = [];
/**
* @var array Relative paths (from <ITOP>/) to the external JS module files to include in the page.
* @description Used to include JS file that contains modules + the JS code assocciated to these modules. Even if you use only a few modules, the whole JS file will be loaded.
*/
protected $aJsModulesConfig = [];
/**
* @var array
* @see iUIBlock::GetCssFilesRelPaths()
@@ -182,6 +192,9 @@ abstract class UIBlock implements iUIBlock
$this->AddMultipleJsFilesRelPaths(static::DEFAULT_JS_FILES_REL_PATH);
}
$this->AddMultipleJsModulesFilesRelPaths(static::DEFAULT_JS_MODULES_CONFIG);
// Add external CSS files
// 1) From ancestors if they are required
if (static::REQUIRES_ANCESTORS_DEFAULT_CSS_FILES) {
@@ -253,6 +266,15 @@ abstract class UIBlock implements iUIBlock
return $this->aJsFilesRelPath;
}
/**
* @inheritDoc
*/
public function GetJsModuleConfigs(): array
{
return $this->aJsModulesConfig;
}
/**
* @inheritDoc
*/
@@ -316,6 +338,31 @@ abstract class UIBlock implements iUIBlock
return $this->GetFilesUrlRecursively(static::ENUM_BLOCK_FILES_TYPE_JS, $bAbsoluteUrl);
}
/**
* @inheritDoc
* @throws \Exception
*/
public function GetJsModulesRecursively(bool $bAbsoluteUrl = false): array
{
$aJsModulesConfigs = [];
// Files from the block itself
foreach ($this->GetJsModuleConfigs() as $sJsCurrentModuleBlockConfig) {
$aJsModulesConfigs[] = $sJsCurrentModuleBlockConfig;
}
// Files from its sub blocks
foreach ($this->GetSubBlocks() as $sSubBlockName => $oSubBlock) {
/** @noinspection SlowArrayOperationsInLoopInspection */
$aJsModulesConfigs = array_merge(
$aJsModulesConfigs,
$oSubBlock->GetJsModulesRecursively($bAbsoluteUrl)
);
}
return $aJsModulesConfigs;
}
/**
* @inheritDoc
* @throws \Exception
@@ -354,6 +401,16 @@ abstract class UIBlock implements iUIBlock
return $this;
}
/**
* @inheritDoc
*/
public function AddJsModuleConfigs(array $aModuleConfig)
{
$this->aJsModulesConfig[] = $aModuleConfig;
return $this;
}
/**
* @inheritDoc
*/
@@ -366,6 +423,18 @@ abstract class UIBlock implements iUIBlock
return $this;
}
/**
* @inheritDoc
*/
public function AddMultipleJsModulesFilesRelPaths(array $aModulesConfig)
{
foreach ($aModulesConfig as $aModuleConfig) {
$this->AddJsModuleConfigs($aModuleConfig);
}
return $this;
}
/**
* @inheritDoc
*/