Add UIBlocks to twig (DataTable, Form, Input)

This commit is contained in:
Eric
2021-01-14 13:14:05 +01:00
parent e51fd028fa
commit d8316a090a
21 changed files with 434 additions and 212 deletions

View File

@@ -0,0 +1,79 @@
<?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 UIInputNode 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('oInput');
$oParams = $this->getAttribute('params');
$compiler
->addDebugInfo($this)
->write("\$aParams = ")
->subcompile($oParams)
->raw(";\n");
$sFactoryType = $this->getAttribute('type');
switch ($sFactoryType) {
case 'ForHidden':
$compiler
->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");
break;
case 'Standard':
$compiler
->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, \$sName, \$sValue, \$sId);\n")
->write("if (\$aParams['checked'] ?? false) {\n")
->indent()
->write("\${$sBlockVar}->SetChecked(true);\n")
->outdent()
->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:
throw new SyntaxError(sprintf('%s: Bad type "%s" for %s at line %d', $this->getTemplateName(), $sFactoryType, $this->tag, $this->lineno), $this->lineno, $this->getSourceContext());
}
$compiler->write(UIBlockHelper::AddToParentBlock($sBlockVar));
}
}

View File

@@ -0,0 +1,43 @@
<?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 UIInputParser 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 UIInputNode($sType, $oParams, $iLineno, $this->getTag());
}
/**
* @inheritDoc
*/
public function getTag()
{
return 'UIInput';
}
}