Add UIBlocks to twig (Use sub-blocks for alerts and fields)

This commit is contained in:
Eric
2021-01-14 13:56:51 +01:00
parent d8316a090a
commit 2aae6cd744
7 changed files with 62 additions and 59 deletions

View File

@@ -13,26 +13,42 @@
{% UIContentBlock {'container_class':'display_block display-files'} %}
{% UIFieldSet {'legend':'iTopUpdate:UI:Status'|dict_s} %}
{% UIAlert ForInformation {'title':'', 'id':'header-requirements'} %}
{% UIAlert ForInformation {'title':'', 'id':'header-requirements', 'is_collapsible':false, 'is_closable':false} %}
{% UIHtml %}
<span id="can-core-update">{{ 'iTopUpdate:UI:CanCoreUpdate:Loading'|dict_s }} <i class="ajax-spin fas fa-spinner fa-spin" aria-hidden="true"></i></span>
{% EndUIHtml %}
{% EndUIAlert %}
{% set sVersion = 'UI:iTopVersion:Long'|dict_format(constant('ITOP_APPLICATION'), constant('ITOP_VERSION'), constant('ITOP_REVISION'), constant('ITOP_BUILD_DATE')) %}
{% UIField Small {'label':'iTopUpdate:UI:CurrentVersion'|dict_s} %}
{% UIHtml %}
{{ sVersion }}
{% EndUIHtml %}
{% EndUIField %}
{% UIField Small {'label':'iTopUpdate:UI:DiskFreeSpace'|dict_s} %}
{% UIHtml %}
{{ sDiskFreeSpace }}
{% EndUIHtml %}
{% EndUIField %}
{% UIField Small {'label':'iTopUpdate:UI:ItopDiskSpace'|dict_s, 'value_id':'itop-disk-space'} %}
{% UIHtml %}
<i class="ajax-spin fas fa-spinner fa-spin" aria-hidden="true"></i>
{% EndUIHtml %}
{% EndUIField %}
{% UIField Small {'label':'iTopUpdate:UI:DBDiskSpace'|dict_s, 'value_id':'db-disk-space'} %}
{% UIHtml %}
<i class="ajax-spin fas fa-spinner fa-spin" aria-hidden="true"></i>
{% EndUIHtml %}
{% EndUIField %}
{% UIField Small {'label':'iTopUpdate:UI:FileUploadMaxSize'|dict_s} %}
{% UIHtml %}
{{ sFileUploadMaxSize }}
{% EndUIHtml %}
{% EndUIField %}
{% EndUIFieldSet %}
@@ -57,7 +73,7 @@
{% EndUIHtml %}
{% UIContentBlock {'container_class':'upload_container'} %}
{% UIInput WithLabel {'type':'file', 'id':'file', 'name':'file'} %}
{% UIInput Standard {'type':'file', 'id':'file', 'name':'file'} %}
{% EndUIContentBlock %}
{% UIHtml %}
@@ -72,9 +88,10 @@
{% UIInput Standard {'type':'checkbox', 'id':'doFilesArchive', 'name':'doFilesArchive', 'value':'1', 'checked':true} %}
{% EndUIField %}
{# </div> #}
{# <p><input id="check-update" type="submit" value="{{ 'iTopUpdate:UI:CheckUpdate'|dict_s }}" disabled="disabled"/> <i id="submit-wait" style="display: none" class="fas fa-spinner fa-spin" aria-hidden="true"></i></p> #}
{# </form> #}
{% UIHtml %}
<p><input id="check-update" type="submit" value="{{ 'iTopUpdate:UI:CheckUpdate'|dict_s }}" disabled="disabled"/> <i id="submit-wait" style="display: none" class="fas fa-spinner fa-spin" aria-hidden="true"></i></p>
{% EndUIHtml %}
{% EndUIForm %}
{% EndUIFieldSet %}

View File

@@ -30,11 +30,8 @@ class UIAlertNode extends Node
->subcompile($oParams)
->raw(";\n")
->write("\$sTitle = \$aParams['title'] ?? '';\n")
->write("\$sId = \$aParams['id'] ?? null;\n")
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->write("\$sContent = ob_get_contents();\n")
->write("ob_end_clean();\n");
->write("\$sContent = \$aParams['content'] ?? '';\n")
->write("\$sId = \$aParams['id'] ?? null;\n");
$sType = $this->getAttribute('type');
switch ($sType) {
@@ -47,7 +44,17 @@ class UIAlertNode extends Node
case 'WithBrandingPrimaryColor':
case 'WithBrandingSecondaryColor':
$compiler
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Alert\\AlertFactory::Make{$sType}(\$sTitle, \$sContent, \$sId);\n");
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Alert\\AlertFactory::Make{$sType}(\$sTitle, \$sContent, \$sId);\n")
->write("if (!\$aParams['is_collapsible'] ?? true) {\n")
->indent()
->write("\${$sBlockVar}->SetIsCollapsible(false);\n")
->outdent()
->write("}\n")
->write("if (!\$aParams['is_closable'] ?? true) {\n")
->indent()
->write("\${$sBlockVar}->SetIsClosable(false);\n")
->outdent()
->write("}\n");
break;
// TODO 3.0 add other Factory methods
@@ -55,7 +62,11 @@ class UIAlertNode extends Node
throw new SyntaxError(sprintf('%s: Bad type "%s" for %s at line %d', $this->getTemplateName(), $sType, $this->tag, $this->lineno), $this->lineno, $this->getSourceContext());
}
$compiler->write(UIBlockHelper::AddToParentBlock($sBlockVar));
$compiler
->write(UIBlockHelper::AddToParentBlock($sBlockVar))
->write(UIBlockHelper::PushParentBlock($sBlockVar))
->subcompile($this->getNode('body'))
->write(UIBlockHelper::PopParentBlock());
}
}

View File

@@ -36,11 +36,8 @@ class UIFieldNode extends Node
case 'Large':
$compiler
->write("\$sLabel = \$aParams['label'] ?? '';\n")
->write("\$sValue = \$aParams['value'] ?? '';\n")
->write("\$sValueId = \$aParams['value_id'] ?? null;\n")
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->write("\$sValue = ob_get_contents();\n")
->write("ob_end_clean();\n")
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Field\\FieldFactory::Make{$sType}(\$sLabel, \$sValue);\n")
->write("\${$sBlockVar}->SetValueId(\$sValueId);\n");
break;
@@ -50,7 +47,11 @@ class UIFieldNode extends Node
throw new SyntaxError(sprintf('%s: Bad type "%s" for %s at line %d', $this->getTemplateName(), $sType, $this->tag, $this->lineno), $this->lineno, $this->getSourceContext());
}
$compiler->write(UIBlockHelper::AddToParentBlock($sBlockVar));
$compiler
->write(UIBlockHelper::AddToParentBlock($sBlockVar))
->write(UIBlockHelper::PushParentBlock($sBlockVar))
->subcompile($this->getNode('body'))
->write(UIBlockHelper::PopParentBlock());
}
}

View File

@@ -37,7 +37,7 @@ class UIInputNode extends Node
->write("\$sName = \$aParams['name'] ?? '';\n")
->write("\$sValue = \$aParams['value'] ?? '';\n")
->write("\$sId = \$aParams['id'] ?? null;\n")
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::Make{$sFactoryType}(\$sName, \$sValue, \$sId);\n");
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::MakeForHidden(\$sName, \$sValue, \$sId);\n");
break;
case 'Standard':
@@ -46,7 +46,7 @@ class UIInputNode extends Node
->write("\$sName = \$aParams['name'] ?? '';\n")
->write("\$sValue = \$aParams['value'] ?? '';\n")
->write("\$sId = \$aParams['id'] ?? null;\n")
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::Make{$sFactoryType}(\$sType, \$sName, \$sValue, \$sId);\n")
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::MakeStandard(\$sType, \$sName, \$sValue, \$sId);\n")
->write("if (\$aParams['checked'] ?? false) {\n")
->indent()
->write("\${$sBlockVar}->SetChecked(true);\n")
@@ -54,20 +54,6 @@ class UIInputNode extends Node
->write("}\n");
break;
case 'WithLabel':
$compiler
->write("\$sLabel = \$aParams['label'] ?? '';\n")
->write("\$sType = \$aParams['type'] ?? '';\n")
->write("\$sName = \$aParams['name'] ?? '';\n")
->write("\$sValue = \$aParams['value'] ?? '';\n")
->write("\$sId = \$aParams['id'] ?? null;\n")
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Input\\InputFactory::Make{$sFactoryType}(\$sType, \$sLabel, \$sName, \$sValue, \$sId);\n")
->write("if (\$aParams['checked'] ?? false) {\n")
->indent()
->write("\${$sBlockVar}->GetInput()->SetChecked(true);\n")
->outdent()
->write("}\n");
break;
// TODO 3.0 add other Factory methods
default:

View File

@@ -20,7 +20,8 @@
namespace Combodo\iTop\Application\UI\Base\Component\Alert;
use Combodo\iTop\Application\UI\Base\UIBlock;
use Combodo\iTop\Application\UI\Base\Component\Html\Html;
use Combodo\iTop\Application\UI\Base\Layout\UIContentBlock;
/**
* Class Alert
@@ -29,7 +30,7 @@ use Combodo\iTop\Application\UI\Base\UIBlock;
* @package Combodo\iTop\Application\UI\Base\Component\Alert
* @since 3.0.0
*/
class Alert extends UIBlock
class Alert extends UIContentBlock
{
// Overloaded constants
public const BLOCK_CODE = 'ibo-alert';
@@ -86,8 +87,6 @@ class Alert extends UIBlock
/** @var string $sTitle */
protected $sTitle;
/** @var string $sContent The raw HTML content, must be already sanitized */
protected $sContent;
/** @var string $sColor */
protected $sColor;
/** @var bool Whether the alert can be closed or not */
@@ -111,13 +110,15 @@ class Alert extends UIBlock
*/
public function __construct(string $sTitle = '', string $sContent = '', string $sColor = self::DEFAULT_COLOR, ?string $sId = null)
{
parent::__construct($sId);
$this->sTitle = $sTitle;
$this->sContent = $sContent;
$this->sColor = $sColor;
$this->bIsClosable = static::DEFAULT_IS_CLOSABLE;
$this->bIsCollapsible = static::DEFAULT_IS_COLLAPSIBLE;
$this->bIsOpenedByDefault = static::DEFAULT_IS_OPENED_BY_DEFAULT;
parent::__construct($sId);
if (!empty($sContent)) {
$this->AddSubBlock(new Html($sContent));
}
}
/**

View File

@@ -37,21 +37,6 @@ class InputFactory
return $oInput;
}
public static function MakeWithLabel(string $sType, string $sLabel, string $sName, string $sValue, ?string $sId = null): InputWithLabel
{
$oInput = new Input($sId);
$oInput->SetType($sType)
->SetName($sName)
->SetValue($sValue);
if (is_null($sId)) {
$sId = $oInput->GetId();
}
return new InputWithLabel($sLabel, $oInput, $sId);
}
/**
* @see Field component that is better adapter when dealing with a standard iTop form
*

View File

@@ -14,11 +14,13 @@
</div>
{% endif %}
{% if oUIBlock.GetTitle() is not empty %}
<div class="ibo-alert--title"
{% if oUIBlock.IsCollapsible() %}data-role="ibo-alert--collapse-toggler"{% endif %}
>{{ oUIBlock.GetTitle() }}</div>
<div class="ibo-alert--title" {% if oUIBlock.IsCollapsible() %}data-role="ibo-alert--collapse-toggler"{% endif %}>{{ oUIBlock.GetTitle() }}</div>
{% endif %}
{% if oUIBlock.GetContent() is not empty %}
<div class="ibo-alert--body">{{ oUIBlock.GetContent()|raw }}</div>
{% if oUIBlock.GetSubBlocks() is not empty %}
<div class="ibo-alert--body">
{% for oSubBlock in oUIBlock.GetSubBlocks() %}
{{ render_block(oSubBlock, {aPage: aPage}) }}
{% endfor %}
</div>
{% endif %}
</div>