summaryrefslogtreecommitdiff
path: root/framework/Web/UI/JuiControls/TJuiControlAdapter.php
diff options
context:
space:
mode:
authorJens Klaer <kj.landwehr.software@gmail.com>2015-05-20 15:54:33 +0200
committerJens Klaer <kj.landwehr.software@gmail.com>2015-05-20 15:54:33 +0200
commit0fb75d64f8dbcc98b4c06e7556125b94bf612819 (patch)
tree248dcab8fc248ab0b0d521e093c9da3b90ab560a /framework/Web/UI/JuiControls/TJuiControlAdapter.php
parentee25e9fc2a4cc702f1ac9af5645134eaa893cf4c (diff)
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
Diffstat (limited to 'framework/Web/UI/JuiControls/TJuiControlAdapter.php')
-rw-r--r--framework/Web/UI/JuiControls/TJuiControlAdapter.php85
1 files changed, 82 insertions, 3 deletions
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
@@ -29,6 +29,17 @@ class TJuiControlAdapter extends TActiveControlAdapter
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
*/
public function setJuiBaseStyle($value)
@@ -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.
@@ -190,6 +227,14 @@ class TJuiControlOptions
}
/**
+ * 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
*/
public function toArray()
@@ -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