diff options
Diffstat (limited to 'core/View/UIControl.php')
-rw-r--r-- | core/View/UIControl.php | 92 |
1 files changed, 91 insertions, 1 deletions
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. * @@ -25,12 +28,99 @@ 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 <div> 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 |