mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-26 12:08:47 +02:00
UIBlock: Add external JS/CSS files (optional) inheritance
This way we ensure that a block always have the external resources from its ancestors as this will be necessary most of the time. From now on, the JS widget of the blocks will inherit from a common ancestor to factorize some mechanisms that are duplicates at many levels. This can be disabled in a particular block by overloading the INCLUDE_ANCESTORS_DEFAULT_XXX_FILES constants in which case, only the external files of the block itself will be included.
This commit is contained in:
@@ -38,6 +38,16 @@ abstract class UIBlock implements iUIBlock
|
||||
* should be "ibo-my-custom-clock")
|
||||
*/
|
||||
public const BLOCK_CODE = 'ibo-block';
|
||||
/**
|
||||
* @var bool Set to true so the block automatically includes its ancestors' external JS files. If set to false, only the files from the block itself will be included
|
||||
* @see static::DEFAULT_JS_FILES_REL_PATH
|
||||
*/
|
||||
public const INCLUDE_ANCESTORS_DEFAULT_JS_FILES = true;
|
||||
/**
|
||||
* @var bool Set to true so the block automatically includes its ancestors' external CSS files. If set to false, only the files from the block itself will be included
|
||||
* @see static::DEFAULT_CSS_FILES_REL_PATH
|
||||
*/
|
||||
public const INCLUDE_ANCESTORS_DEFAULT_CSS_FILES = true;
|
||||
|
||||
/** @var string|null */
|
||||
public const DEFAULT_GLOBAL_TEMPLATE_REL_PATH = null;
|
||||
@@ -48,7 +58,9 @@ abstract class UIBlock implements iUIBlock
|
||||
* **Warning** : if you need to call a JS var defined in one of this file, then this calling code MUST be in {@see DEFAULT_JS_ON_READY_TEMPLATE_REL_PATH}
|
||||
* and not in {@see DEFAULT_JS_TEMPLATE_REL_PATH} ! Indeed the later is output before external files loading.
|
||||
*/
|
||||
public const DEFAULT_JS_FILES_REL_PATH = [];
|
||||
public const DEFAULT_JS_FILES_REL_PATH = [
|
||||
'js/ui-block.js',
|
||||
];
|
||||
/** @var string|null */
|
||||
public const DEFAULT_JS_TEMPLATE_REL_PATH = null;
|
||||
/** @var string|null Relative path (from <ITOP>/templates/) to the JS template not deferred */
|
||||
@@ -88,9 +100,9 @@ abstract class UIBlock implements iUIBlock
|
||||
/** @var string Relative path (from <ITOP>/templates/) to the CSS template */
|
||||
protected $sCssTemplateRelPath;
|
||||
/** @var array Relative paths (from <ITOP>/) to the JS files */
|
||||
protected $aJsFilesRelPath;
|
||||
protected $aJsFilesRelPath = [];
|
||||
/** @var array Relative paths (from <ITOP>/) to the CSS files */
|
||||
protected $aCssFilesRelPath;
|
||||
protected $aCssFilesRelPath = [];
|
||||
/** @var array Array <KEY> => <VALUE> which will be output as HTML data-xxx attributes (eg. data-<KEY>="<VALUE>") */
|
||||
protected $aDataAttributes = [];
|
||||
/** @var bool Whether the current block is shown or hidden */
|
||||
@@ -106,8 +118,23 @@ abstract class UIBlock implements iUIBlock
|
||||
public function __construct(?string $sId = null)
|
||||
{
|
||||
$this->sId = $sId ?? $this->GenerateId();
|
||||
$this->aJsFilesRelPath = static::DEFAULT_JS_FILES_REL_PATH;
|
||||
$this->aCssFilesRelPath = static::DEFAULT_CSS_FILES_REL_PATH;
|
||||
|
||||
// Add external JS files
|
||||
if(static::INCLUDE_ANCESTORS_DEFAULT_JS_FILES){
|
||||
foreach(array_reverse(class_parents(static::class)) as $sParentClass){
|
||||
$this->AddMultipleJsFilesRelPaths($sParentClass::DEFAULT_JS_FILES_REL_PATH);
|
||||
}
|
||||
}
|
||||
$this->AddMultipleJsFilesRelPaths(static::DEFAULT_JS_FILES_REL_PATH);
|
||||
|
||||
// Add external CSS files
|
||||
if(static::INCLUDE_ANCESTORS_DEFAULT_CSS_FILES){
|
||||
foreach(array_reverse(class_parents(static::class)) as $sParentClass){
|
||||
$this->AddMultipleCssFilesRelPaths($sParentClass::DEFAULT_CSS_FILES_REL_PATH);
|
||||
}
|
||||
}
|
||||
$this->AddMultipleCssFilesRelPaths(static::DEFAULT_CSS_FILES_REL_PATH);
|
||||
|
||||
$this->sHtmlTemplateRelPath = static::DEFAULT_HTML_TEMPLATE_REL_PATH;
|
||||
$this->aJsTemplatesRelPath[self::ENUM_JS_TYPE_LIVE] = static::DEFAULT_JS_LIVE_TEMPLATE_REL_PATH;
|
||||
$this->aJsTemplatesRelPath[self::ENUM_JS_TYPE_ON_INIT] = static::DEFAULT_JS_TEMPLATE_REL_PATH;
|
||||
@@ -238,7 +265,21 @@ abstract class UIBlock implements iUIBlock
|
||||
*/
|
||||
public function AddJsFileRelPath(string $sPath)
|
||||
{
|
||||
$this->aJsFilesRelPath[] = $sPath;
|
||||
if(!in_array($sPath, $this->aJsFilesRelPath)) {
|
||||
$this->aJsFilesRelPath[] = $sPath;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function AddMultipleJsFilesRelPaths(array $aPaths)
|
||||
{
|
||||
foreach($aPaths as $sPath){
|
||||
$this->AddJsFileRelPath($sPath);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -248,7 +289,21 @@ abstract class UIBlock implements iUIBlock
|
||||
*/
|
||||
public function AddCssFileRelPath(string $sPath)
|
||||
{
|
||||
$this->aCssFilesRelPath[] = $sPath;
|
||||
if(!in_array($sPath, $this->aCssFilesRelPath)) {
|
||||
$this->aCssFilesRelPath[] = $sPath;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function AddMultipleCssFilesRelPaths(array $aPaths)
|
||||
{
|
||||
foreach($aPaths as $sPath){
|
||||
$this->AddCssFileRelPath($sPath);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user