Add UIBlocks to twig (new blocks)

This commit is contained in:
Eric
2021-01-13 18:25:37 +01:00
parent 77808ecd41
commit bffb7b5eab
10 changed files with 242 additions and 25 deletions

View File

@@ -3,9 +3,9 @@
{% apply spaceless %}
{% UIContentBlock "" "ibo-update-core" %}
{% UIHtml %}
<div class="page_header"><h1 class="ibo-title--text title is-size-3">{{ 'iTopUpdate:UI:PageTitle'|dict_s }}</h1></div>
{% EndUIHtml %}
{% UITitle ForPage {'title':'iTopUpdate:UI:PageTitle'|dict_s} %}
{% UIHtml %}
<div class="ibo-v-spacer">&nbsp;</div>
{% EndUIHtml %}
@@ -13,13 +13,9 @@
{% UIContentBlock "" "display_block display-files" %}
{% UIFieldSet {'legend':'iTopUpdate:UI:Status'|dict_s} %}
{% UIHtml %}
<div id="header-requirements" class="ibo-alert ibo-is-information ibo-is-opened">
<div class="ibo-alert--body">
<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>
</div>
</div>
{% EndUIHtml %}
{% UIAlert ForInformation {'title':'', 'id':'header-requirements'} %}
<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>
{% EndUIAlert %}
{% set sVersion = 'UI:iTopVersion:Long'|dict_format(constant('ITOP_APPLICATION'), constant('ITOP_VERSION'), constant('ITOP_REVISION'), constant('ITOP_BUILD_DATE')) %}

View File

@@ -151,6 +151,8 @@ return array(
'Combodo\\iTop\\Application\\TwigBase\\Controller\\PageNotFoundException' => $baseDir . '/sources/application/TwigBase/Controller/Controller.php',
'Combodo\\iTop\\Application\\TwigBase\\Twig\\Extension' => $baseDir . '/sources/application/TwigBase/Twig/Extension.php',
'Combodo\\iTop\\Application\\TwigBase\\Twig\\TwigHelper' => $baseDir . '/sources/application/TwigBase/Twig/TwigHelper.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIAlertNode' => $baseDir . '/sources/application/TwigBase/UI/Component/Alert/UIAlertNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIAlertParser' => $baseDir . '/sources/application/TwigBase/UI/Component/Alert/UIAlertParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIContentBlockNode' => $baseDir . '/sources/application/TwigBase/UI/Layout/UIContentBlockNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIContentBlockParser' => $baseDir . '/sources/application/TwigBase/UI/Layout/UIContentBlockParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIFieldNode' => $baseDir . '/sources/application/TwigBase/UI/Component/Field/UIFieldNode.php',
@@ -159,6 +161,8 @@ return array(
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIFieldSetParser' => $baseDir . '/sources/application/TwigBase/UI/Component/FieldSet/UIFieldSetParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIHtmlNode' => $baseDir . '/sources/application/TwigBase/UI/Component/Html/UIHtmlNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIHtmlParser' => $baseDir . '/sources/application/TwigBase/UI/Component/Html/UIHtmlParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UITitleNode' => $baseDir . '/sources/application/TwigBase/UI/Component/Title/UITitleNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UITitleParser' => $baseDir . '/sources/application/TwigBase/UI/Component/Title/UITitleParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\UIBlockExtension' => $baseDir . '/sources/application/TwigBase/UI/UIBlockExtension.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\UIBlockHelper' => $baseDir . '/sources/application/TwigBase/UI/UIBlockHelper.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Alert\\Alert' => $baseDir . '/sources/application/UI/Base/Component/Alert/Alert.php',

View File

@@ -381,6 +381,8 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
'Combodo\\iTop\\Application\\TwigBase\\Controller\\PageNotFoundException' => __DIR__ . '/../..' . '/sources/application/TwigBase/Controller/Controller.php',
'Combodo\\iTop\\Application\\TwigBase\\Twig\\Extension' => __DIR__ . '/../..' . '/sources/application/TwigBase/Twig/Extension.php',
'Combodo\\iTop\\Application\\TwigBase\\Twig\\TwigHelper' => __DIR__ . '/../..' . '/sources/application/TwigBase/Twig/TwigHelper.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIAlertNode' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/Alert/UIAlertNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIAlertParser' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/Alert/UIAlertParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIContentBlockNode' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Layout/UIContentBlockNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIContentBlockParser' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Layout/UIContentBlockParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIFieldNode' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/Field/UIFieldNode.php',
@@ -389,6 +391,8 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIFieldSetParser' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/FieldSet/UIFieldSetParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIHtmlNode' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/Html/UIHtmlNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UIHtmlParser' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/Html/UIHtmlParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UITitleNode' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/Title/UITitleNode.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\Component\\UITitleParser' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/Component/Title/UITitleParser.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\UIBlockExtension' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/UIBlockExtension.php',
'Combodo\\iTop\\Application\\TwigBase\\UI\\UIBlockHelper' => __DIR__ . '/../..' . '/sources/application/TwigBase/UI/UIBlockHelper.php',
'Combodo\\iTop\\Application\\UI\\Base\\Component\\Alert\\Alert' => __DIR__ . '/../..' . '/sources/application/UI/Base/Component/Alert/Alert.php',

View File

@@ -0,0 +1,61 @@
<?php
/**
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\TwigBase\UI\Component;
use Combodo\iTop\Application\TwigBase\UI\UIBlockHelper;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Node\Node;
class UIAlertNode extends Node
{
public function __construct($sType, $oParams, $oBody, $lineno = 0, $tag = null)
{
parent::__construct(['body' => $oBody], ['type' => $sType, 'params' => $oParams], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$sBlockVar = UIBlockHelper::GetBlockVarName('oFieldSet');
$oParams = $this->getAttribute('params');
$compiler
->addDebugInfo($this)
->write("\$aParams = ")
->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");
$sType = $this->getAttribute('type');
switch ($sType) {
case 'ForInformation':
case 'Neutral':
case 'ForSuccess':
case 'ForWarning':
case 'ForDanger':
case 'ForFailure':
case 'WithBrandingPrimaryColor':
case 'WithBrandingSecondaryColor':
$compiler
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Alert\\AlertFactory::Make{$sType}(\$sTitle, \$sContent, \$sId);\n");
break;
// TODO 3.0 add other Factory methods
default:
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));
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\TwigBase\UI\Component;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
class UIAlertParser extends AbstractTokenParser
{
/**
* @inheritDoc
*/
public function parse(Token $token)
{
$iLineno = $token->getLine();
$oStream = $this->parser->getStream();
$sType = $oStream->expect(Token::NAME_TYPE)->getValue();
$oParams = $this->parser->getExpressionParser()->parseExpression();
$oStream->expect(Token::BLOCK_END_TYPE);
$oBody = $this->parser->subparse([$this, 'decideForEnd'], true);
$oStream->expect(Token::BLOCK_END_TYPE);
return new UIAlertNode($sType, $oParams, $oBody, $iLineno, $this->getTag());
}
/**
* @inheritDoc
*/
public function getTag()
{
return 'UIAlert';
}
public function decideForEnd(Token $token)
{
return $token->test('EndUIAlert');
}
}

View File

@@ -44,7 +44,7 @@ class UIFieldNode extends Node
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Field\\FieldFactory::Make{$sType}(\$sLabel, \$sValue);\n")
->write("\${$sBlockVar}->SetValueId(\$sValueId);\n");
break;
// TODO 3.0 add other FieldFactory methods
// TODO 3.0 add other Factory methods
default:
throw new SyntaxError(sprintf('%s: Bad type "%s" for %s at line %d', $this->getTemplateName(), $sType, $this->tag, $this->lineno), $this->lineno, $this->getSourceContext());

View File

@@ -0,0 +1,50 @@
<?php
/**
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\TwigBase\UI\Component;
use Combodo\iTop\Application\TwigBase\UI\UIBlockHelper;
use Twig\Compiler;
use Twig\Error\SyntaxError;
use Twig\Node\Node;
class UITitleNode extends Node
{
public function __construct($sType, $oParams, $lineno = 0, $tag = null)
{
parent::__construct([], ['type' => $sType, 'params' => $oParams], $lineno, $tag);
}
public function compile(Compiler $compiler)
{
$sBlockVar = UIBlockHelper::GetBlockVarName('oFieldSet');
$oParams = $this->getAttribute('params');
$compiler
->addDebugInfo($this)
->write("\$aParams = ")
->subcompile($oParams)
->raw(";\n");
$sType = $this->getAttribute('type');
switch ($sType) {
case 'ForPage':
$compiler
->write("\$sTitle = \$aParams['title'] ?? '';\n")
->write("\$sId = \$aParams['id'] ?? null;\n")
->write("\${$sBlockVar} = Combodo\\iTop\\Application\\UI\\Base\\Component\\Title\\TitleFactory::Make{$sType}(\$sTitle, \$sId);\n");
break;
// TODO 3.0 add other Factory methods
default:
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));
}
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* @copyright Copyright (C) 2010-2021 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Application\TwigBase\UI\Component;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
class UITitleParser extends AbstractTokenParser
{
/**
* @inheritDoc
*/
public function parse(Token $token)
{
$iLineno = $token->getLine();
$oStream = $this->parser->getStream();
$sType = $oStream->expect(Token::NAME_TYPE)->getValue();
$oParams = $this->parser->getExpressionParser()->parseExpression();
$oStream->expect(Token::BLOCK_END_TYPE);
return new UITitleNode($sType, $oParams, $iLineno, $this->getTag());
}
/**
* @inheritDoc
*/
public function getTag()
{
return 'UITitle';
}
}

View File

@@ -8,10 +8,12 @@
namespace Combodo\iTop\Application\TwigBase\UI;
use Combodo\iTop\Application\TwigBase\UI\Component\UIAlertParser;
use Combodo\iTop\Application\TwigBase\UI\Component\UIContentBlockParser;
use Combodo\iTop\Application\TwigBase\UI\Component\UIFieldParser;
use Combodo\iTop\Application\TwigBase\UI\Component\UIFieldSetParser;
use Combodo\iTop\Application\TwigBase\UI\Component\UIHtmlParser;
use Combodo\iTop\Application\TwigBase\UI\Component\UITitleParser;
use Twig\Extension\AbstractExtension;
class UIBlockExtension extends AbstractExtension
@@ -23,6 +25,8 @@ class UIBlockExtension extends AbstractExtension
new UIContentBlockParser(),
new UIFieldSetParser(),
new UIFieldParser(),
new UIAlertParser(),
new UITitleParser(),
];
}
}

View File

@@ -35,12 +35,13 @@ class AlertFactory
*
* @param string $sTitle
* @param string $sContent The raw HTML content, must be already sanitized
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Alert\Alert
*/
public static function MakeNeutral(string $sTitle, string $sContent)
public static function MakeNeutral(string $sTitle, string $sContent, ?string $sId = null)
{
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_NEUTRAL);
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_NEUTRAL, $sId);
}
/**
@@ -63,12 +64,13 @@ class AlertFactory
*
* @param string $sTitle
* @param string $sContent The raw HTML content, must be already sanitized
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Alert\Alert
*/
public static function MakeForSuccess(string $sTitle, string $sContent)
public static function MakeForSuccess(string $sTitle, string $sContent, ?string $sId = null)
{
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_SUCCESS);
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_SUCCESS, $sId);
}
/**
@@ -76,12 +78,13 @@ class AlertFactory
*
* @param string $sTitle
* @param string $sContent The raw HTML content, must be already sanitized
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Alert\Alert
*/
public static function MakeForWarning(string $sTitle, string $sContent)
public static function MakeForWarning(string $sTitle, string $sContent, ?string $sId = null)
{
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_WARNING);
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_WARNING, $sId);
}
/**
@@ -89,12 +92,13 @@ class AlertFactory
*
* @param string $sTitle
* @param string $sContent The raw HTML content, must be already sanitized
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Alert\Alert
*/
public static function MakeForDanger(string $sTitle, string $sContent)
public static function MakeForDanger(string $sTitle, string $sContent, ?string $sId = null)
{
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_DANGER);
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_DANGER, $sId);
}
/**
@@ -102,12 +106,13 @@ class AlertFactory
*
* @param string $sTitle
* @param string $sContent The raw HTML content, must be already sanitized
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Alert\Alert
*/
public static function MakeForFailure(string $sTitle, string $sContent)
public static function MakeForFailure(string $sTitle, string $sContent, ?string $sId = null)
{
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_FAILURE);
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_FAILURE, $sId);
}
/**
@@ -115,12 +120,13 @@ class AlertFactory
*
* @param string $sTitle
* @param string $sContent The raw HTML content, must be already sanitized
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Alert\Alert
*/
public static function MakeWithBrandingPrimaryColor(string $sTitle, string $sContent)
public static function MakeWithBrandingPrimaryColor(string $sTitle, string $sContent, ?string $sId = null)
{
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_PRIMARY);
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_PRIMARY, $sId);
}
/**
@@ -128,11 +134,12 @@ class AlertFactory
*
* @param string $sTitle
* @param string $sContent The raw HTML content, must be already sanitized
* @param string|null $sId
*
* @return \Combodo\iTop\Application\UI\Base\Component\Alert\Alert
*/
public static function MakeWithBrandingSecondaryColor(string $sTitle, string $sContent)
public static function MakeWithBrandingSecondaryColor(string $sTitle, string $sContent, ?string $sId = null)
{
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_SECONDARY);
return new Alert($sTitle, $sContent, Alert::ENUM_COLOR_SECONDARY, $sId);
}
}