From 0fb75d64f8dbcc98b4c06e7556125b94bf612819 Mon Sep 17 00:00:00 2001 From: Jens Klaer Date: Wed, 20 May 2015 15:54:33 +0200 Subject: made TJuiControl options changeable during callback - TJuiControlOptions are now stored in viewstate and tracked by the new TJuiCallbackPageStateTracker class extending the default viewstate tracker class - TJuiCallbackPageStateTracker registers endscripts automatically for each changed option for the clientside changes, added some normalized getter for that --- .../Web/UI/JuiControls/TJuiControlAdapter.php | 85 +++++++++++++++++++++- 1 file changed, 82 insertions(+), 3 deletions(-) (limited to 'framework/Web/UI/JuiControls/TJuiControlAdapter.php') diff --git a/framework/Web/UI/JuiControls/TJuiControlAdapter.php b/framework/Web/UI/JuiControls/TJuiControlAdapter.php index 3668a8b4..638f60f1 100644 --- a/framework/Web/UI/JuiControls/TJuiControlAdapter.php +++ b/framework/Web/UI/JuiControls/TJuiControlAdapter.php @@ -28,6 +28,17 @@ class TJuiControlAdapter extends TActiveControlAdapter const CSS_PATH = 'css'; const BASE_CSS_FILENAME ='jquery-ui.css'; + /** + * Replace default StateTracker with {@link TJuiCallbackPageStateTracker} for + * options tracking in ViewState. + * @param TEventParameter event parameter to be passed to the event handlers + */ + public function onInit($param) + { + parent::onInit($param); + $this->setStateTracker('TJuiCallbackPageStateTracker'); + } + /** * @param string set the jquery-ui style */ @@ -78,6 +89,15 @@ class TJuiControlAdapter extends TActiveControlAdapter return $url; } + /** + * Calls the parent implementation first and sets the parent control for the + * {@link TJuiControlOptions} again afterwards since it was not serialized in viewstate. + */ + public function loadState() { + parent::loadState(); + $this->getControl()->getOptions()->setControl($this->getControl()); + } + } /** @@ -92,6 +112,8 @@ class TJuiControlAdapter extends TActiveControlAdapter */ interface IJuiOptions { + public function getWidget(); + public function getWidgetID(); public function getOptions(); public function getValidOptions(); public function getValidEvents(); @@ -124,12 +146,27 @@ class TJuiControlOptions */ private $_control; + /** + * Constructor. Set the parent control owning these options. + * @param TControl parent control + */ public function __construct($control) { - if(!$control instanceof IJuiOptions) - throw new THttpException(500,'juioptions_control_invalid',$control->ID); - $this->_control=$control; + $this->setControl($control); } + + /** + * Sets the parent control. + * @param TControl $control + * @throws THttpException + */ + public function setControl($control) + { + if(!$control instanceof IJuiOptions) + throw new THttpException(500,'juioptions_control_invalid',$control->ID); + $this->_control=$control; + } + /** * Sets a named options with a value. Options are used to store and retrive * named values for the javascript control. @@ -189,6 +226,14 @@ class TJuiControlOptions return null; } + /** + * Only serialize the options itself, not the corresponding parent control. + * @return mixed array with the names of all variables of that object that should be serialized. + */ + public function __sleep() { + return array('_options'); + } + /** * @return Array of active control options */ @@ -289,4 +334,38 @@ class TJuiEventParameter extends TCallbackEventParameter return $this->getControl($cp->$name); } +} + +/** + * TJuiCallbackPageStateTracker class. + * + * Tracking changes to the page state during callback, including {@link TJuiControlOptions}. + * + * @author LANDWEHR Computer und Software GmbH + * @package System.Web.UI.JuiControls + * @since 3.3 + */ +class TJuiCallbackPageStateTracker extends TCallbackPageStateTracker { + + /** + * Add the {@link TJuiControlOptions} to the states to track. + */ + protected function addStatesToTrack() + { + parent::addStatesToTrack(); + $states = $this->getStatesToTrack(); + $states['JuiOptions'] = array('TMapCollectionDiff', array($this, 'updateJuiOptions')); + } + + /** + * Updates the options of the jQueryUI widget. + * @param array list of widget options to change. + */ + protected function updateJuiOptions($options) + { + foreach ($options as $key => $value) $options[$key] = $key . ': ' . (is_string($value) ? "'{$value}'" : TPropertyValue::ensureString($value)); + $code = "jQuery('#{$this->_control->getWidgetID()}').{$this->_control->getWidget()}('option', { " . implode(', ', $options) . " });"; + $this->_control->getPage()->getClientScript()->registerEndScript(sprintf('%08X', crc32($code)), $code); + } + } \ No newline at end of file -- cgit v1.2.3