summaryrefslogtreecommitdiff
path: root/framework/Web/UI/JuiControls/TJuiControlAdapter.php
diff options
context:
space:
mode:
authorFabio Bas <ctrlaltca@gmail.com>2014-02-06 16:23:42 +0100
committerFabio Bas <ctrlaltca@gmail.com>2014-02-06 16:23:42 +0100
commit2c1d9d453404ec2e3344ef477bf834a2a3c065af (patch)
treed309eca34de724db2804e0febaab76a68b50b889 /framework/Web/UI/JuiControls/TJuiControlAdapter.php
parent705a311b3bbc223737a51bec1d2f978554da086a (diff)
Implemented basic support for callback events for JUI controls; TJuiDraggable and TJuiDroppable examples
Diffstat (limited to 'framework/Web/UI/JuiControls/TJuiControlAdapter.php')
-rw-r--r--framework/Web/UI/JuiControls/TJuiControlAdapter.php133
1 files changed, 118 insertions, 15 deletions
diff --git a/framework/Web/UI/JuiControls/TJuiControlAdapter.php b/framework/Web/UI/JuiControls/TJuiControlAdapter.php
index 58ca34da..a9722a74 100644
--- a/framework/Web/UI/JuiControls/TJuiControlAdapter.php
+++ b/framework/Web/UI/JuiControls/TJuiControlAdapter.php
@@ -15,7 +15,8 @@ Prado::using('System.Web.UI.ActiveControls.TActiveControlAdapter');
* TJuiControlAdapter class
*
* TJuiControlAdapter is the base adapter class for controls that are
- * derived from a jQuery-ui widget.
+ * derived from a jQuery-ui widget. It exposes convenience methods to
+ * publish jQuery-UI javascript and css assets.
*
* @author Fabio Bas <ctrlaltca@gmail.com>
* @package System.Web.UI.JuiControls
@@ -93,17 +94,20 @@ interface IJuiOptions
{
public function getOptions();
public function getValidOptions();
+ public function getValidEvents();
}
/**
* TJuiControlOptions interface
*
- * TJuiControlOptions is an helper class that can collect a series of options
+ * TJuiControlOptions is an helper class that can collect a list of options
* for a control. The control must implement {@link IJuiOptions}.
- * The options are validated againg an array of valid options provided by the control.
+ * The options are validated againg an array of valid options provided by the control itself.
* Since component properties are case insensitive, the array of valid options is used
* to ensure the option name has the correct case.
* The options array can then get retrieved using {@link toArray} and applied to the jQuery-ui widget.
+ * In addition to the options, this class will render the needed javascript to raise a callback
+ * for any event for which an handler is defined in the control.
*
* @author Fabio Bas <ctrlaltca@gmail.com>
* @package System.Web.UI.JuiControls
@@ -115,7 +119,9 @@ class TJuiControlOptions
* @var TMap map of javascript options.
*/
private $_options;
-
+ /**
+ * @var TControl parent control.
+ */
private $_control;
public function __construct($control)
@@ -128,22 +134,24 @@ class TJuiControlOptions
* Sets a named options with a value. Options are used to store and retrive
* named values for the javascript control.
* @param string option name.
- * @param mixed new value.
- * @param mixed default value.
- * @return mixed options value.
+ * @param mixed option value.
+ * @throws THttpException
*/
public function __set($name,$value)
{
if($this->_options===null)
$this->_options=array();
+
+ $tmpname=strtolower($name);
foreach($this->_control->getValidOptions() as $option)
{
- if($name == strtolower($option))
+ if($tmpname == $option)
{
$this->_options[$option] = $value;
return;
}
}
+
throw new THttpException(500,'juioptions_option_invalid',$control->ID, $name);
}
@@ -151,23 +159,118 @@ class TJuiControlOptions
* Gets an option named value. Options are used to store and retrive
* named values for the base active controls.
* @param string option name.
- * @param mixed default value.
- * @return mixed options value.
+ * @return mixed options value or null if not set.
*/
public function __get($name)
{
if($this->_options===null)
$this->_options=array();
- return isset($this->_options[$name]) ? $this->_options[$name] : null;
+
+ $tmpname=strtolower($name);
+ if(isset($this->_options[$tmpname]))
+ return $this->_options[$tmpname];
+
+ return null;
}
/**
- * @return TMap active control options
+ * @return Array of active control options
*/
public function toArray()
{
- if($this->_options===null)
- $this->_options=array();
- return $this->_options;
+ $ret= ($this->_options===null) ? $this->_options : array();
+
+ foreach($this->_control->getValidEvents() as $event)
+ if($this->_control->hasEventHandler('on'.$event))
+ $ret[$event]=new TJavaScriptLiteral("function( event, ui ) { Prado.JuiCallback(".TJavascript::encode($this->_control->getUniqueID()).", ".TJavascript::encode($event).", event, ui, this); }");
+
+ return $ret;
+ }
+
+ /**
+ * Raise the specific callback event handler of the target control.
+ * @param mixed callback parameters
+ */
+ public function raiseCallbackEvent($param)
+ {
+ $callbackParam=$param->CallbackParameter;
+ if(isset($callbackParam->event))
+ {
+ $eventName = 'On'.ucfirst($callbackParam->event);
+ if($this->_control->hasEventHandler($eventName))
+ {
+ $this->_control->$eventName( new TJuiEventParameter(
+ $this->_control->getResponse(),
+ isset($callbackParam->ui) ? $callbackParam->ui : null)
+ );
+ }
+ }
}
}
+
+/**
+ * TJuiEventParameter class
+ *
+ * TJuiEventParameter encapsulate the parameters for callback
+ * events of TJui* components.
+ * Any parameter representing a control is identified by its
+ * clientside ID.
+ * TJuiEventParameter contains a {@link getControl} helper method
+ * that retrieves an existing PRADO control on che current page from its
+ * clientside ID as returned by the callback.
+ * For example, if the parameter contains a "draggable" item (as returned in
+ * {@link TJuiDroppable}::OnDrop event), the relative PRADO control can be
+ * retrieved using:
+ * <code>
+ * $draggable = $param->getControl($param->getCallbackParameter()->draggable);
+ * </code>
+ *
+ * A shortcut __get() method is implemented, too:
+ * <code>
+ * $draggable = $param->DraggableControl;
+ * </code>
+ *
+ * @author Fabio Bas <ctrlaltca[at]gmail[dot]com>
+ * @license http://www.pradosoft.com/license
+ * @package System.Web.UI.JuiControls
+ */
+class TJuiEventParameter extends TCallbackEventParameter
+{
+ /**
+ * getControl
+ *
+ * Compatibility method to get a control from its clientside id
+ * @return TControl control, or null if not found
+ */
+ public function getControl($id)
+ {
+ $control=null;
+ $service=prado::getApplication()->getService();
+ if ($service instanceof TPageService)
+ {
+ // Find the control
+ // Warning, this will not work if you have a '_' in your control Id !
+ $controlId=str_replace(TControl::CLIENT_ID_SEPARATOR,TControl::ID_SEPARATOR,$id);
+ $control=$service->getRequestedPage()->findControl($controlId);
+ }
+ return $control;
+ }
+
+ /**
+ * Gets a control instance named after a returned control id.
+ * Example: if a $param->draggable control id is returned from clientside,
+ * calling $param->DraggableControl will return the control instance
+ * @return mixed control or null if not set.
+ */
+ public function __get($name)
+ {
+ $pos=strpos($name, 'Control',1);
+ $name=strtolower(substr($name, 0, $pos));
+
+ $cp=$this->getCallbackParameter();
+ if(!isset($cp->$name) || $cp->$name=='')
+ return null;
+
+ return $this->getControl($cp->$name);
+ }
+} \ No newline at end of file