From b8e93ed82bee138d1207d31803bf248abcea4d3b Mon Sep 17 00:00:00 2001 From: diosmosis Date: Thu, 6 Feb 2014 03:35:57 +0000 Subject: Move containing
rendering of UIControl to new template, add new custom Twig tag to render Views and allow selected segment of segment selector to be determined by PHP code. --- core/View/RenderTokenParser.php | 82 ++++++++++++++++++++++++++++++++++++ core/View/UIControl.php | 92 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 core/View/RenderTokenParser.php (limited to 'core/View') diff --git a/core/View/RenderTokenParser.php b/core/View/RenderTokenParser.php new file mode 100644 index 0000000000..14f1eb6806 --- /dev/null +++ b/core/View/RenderTokenParser.php @@ -0,0 +1,82 @@ +parser; + $stream = $parser->getStream(); + + $view = $parser->getExpressionParser()->parseExpression(); + + $variablesOverride = null; + if ($stream->nextIf(Twig_Token::NAME_TYPE, 'with')) { + $variablesOverride = $this->parser->getExpressionParser()->parseExpression(); + } + + $stream->expect(Twig_Token::BLOCK_END_TYPE); + + $viewTemplateExpr = new Twig_Node_Expression_MethodCall( + $view, + 'getTemplateFile', + new Twig_Node_Expression_Array(array(), $token->getLine()), + $token->getLine() + ); + + $variablesOverrideExpr = $variablesOverride === null ? array() : array($variablesOverride); + $variablesExpr = new Twig_Node_Expression_MethodCall( + $view, + 'getTemplateVars', + new Twig_Node_Expression_Array($variablesOverrideExpr, $token->getLine()), + $token->getLine() + ); + + return new Twig_Node_Include( + $viewTemplateExpr, + $variablesExpr, + $only = false, + $ignoreMissing = false, + $token->getLine() + ); + } + + /** + * Returns the tag identifier. + * + * @return string + */ + public function getTag() + { + return 'render'; + } +} \ No newline at end of file diff --git a/core/View/UIControl.php b/core/View/UIControl.php index 90d0b0b331..39994aa828 100644 --- a/core/View/UIControl.php +++ b/core/View/UIControl.php @@ -8,6 +8,9 @@ */ namespace Piwik\View; +use Piwik\View; +use Exception; + /** * Base type of UI controls. * @@ -24,13 +27,100 @@ class UIControl extends \Piwik\View */ const TEMPLATE = ''; + /** + * Holds the array of values that are passed to the UIControl JavaScript class. + * + * @var array + */ + public $clientSideProperties = array(); + + /** + * Holds an array of values that are passed to the UIControl JavaScript class. These values + * differ from those in {@link $clientSideProperties} in that they are meant to passed as + * request parameters when the JavaScript code makes an AJAX request. + * + * @var array + */ + public $clientSideParameters = array(); + + /** + * The CSS class that is used to map the root element of this control with the JavaScript class. + * + * This field must be set prior to rendering. + * + * @var string + */ + public $cssIdentifier = null; + + /** + * The name of the JavaScript class that handles the behavior of this control. + * + * The JavaScript class must exist in the **piwik/UI** JavaScript module (so it will exist in + * `window.piwik.UI`). + * + * This field must be set prior to rendering. + * + * @var string + */ + public $jsClass = null; + + /** + * Extra CSS class(es) for the root element. + * + * @var string + */ + public $cssClass = ""; + + /** + * Whether we are currently rendering the containing div or not. + */ + private $renderingContainer = false; + /** * Constructor. */ - public function __construct() { + public function __construct() + { parent::__construct(static::TEMPLATE); $this->clientSideProperties = array(); $this->clientSideParameters = array(); } + + /** + * Renders the control view within a containing
that is used by the UIControl JavaScript + * class. + * + * @return string + */ + public function render() + { + if ($this->cssIdentifier === null) { + throw new Exception("All UIControls must set a cssIdentifier property"); + } + + if ($this->jsClass === null) { + throw new Exception("All UIControls must set a jsClass property"); + } + + if ($this->renderingContainer) { + return parent::render(); + } else { + $this->renderingContainer = true; + + $surroundingDivView = new View("@CoreHome\_uiControl"); + $surroundingDivView->clientSideProperties = $this->clientSideProperties; + $surroundingDivView->clientSideParameters = $this->clientSideParameters; + $surroundingDivView->implView = $this; + $surroundingDivView->cssIdentifier = $this->cssIdentifier; + $surroundingDivView->cssClass = $this->cssClass; + $surroundingDivView->jsClass = $this->jsClass; + + $result = $surroundingDivView->render(); + + $this->renderingContainer = false; + + return $result; + } + } } \ No newline at end of file -- cgit v1.2.3