3 * TJuiControlOptions class file.
5 * @author Fabio Bas <ctrlaltca@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2013-2015 PradoSoft
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Web.UI.JuiControls
13 * IJuiOptions interface
15 * IJuiOptions is the interface that must be implemented by controls using
16 * {@link TJuiControlOptions}.
18 * @author Fabio Bas <ctrlaltca@gmail.com>
19 * @package System.Web.UI.JuiControls
24 public function getWidget();
25 public function getWidgetID();
26 public function getOptions();
27 public function getValidOptions();
28 public function getValidEvents();
32 * TJuiControlOptions interface
34 * TJuiControlOptions is an helper class that can collect a list of options
35 * for a control. The control must implement {@link IJuiOptions}.
36 * The options are validated againg an array of valid options provided by the control itself.
37 * Since component properties are case insensitive, the array of valid options is used
38 * to ensure the option name has the correct case.
39 * The options array can then get retrieved using {@link toArray} and applied to the jQuery-ui widget.
40 * In addition to the options, this class will render the needed javascript to raise a callback
41 * for any event for which an handler is defined in the control.
43 * @author Fabio Bas <ctrlaltca@gmail.com>
44 * @package System.Web.UI.JuiControls
47 class TJuiControlOptions
50 * @var TMap map of javascript options.
54 * @var TControl parent control.
59 * Constructor. Set the parent control owning these options.
60 * @param TControl parent control
62 public function __construct($control)
64 $this->setControl($control);
68 * Sets the parent control.
69 * @param TControl $control
70 * @throws THttpException
72 public function setControl($control)
74 if(!$control instanceof IJuiOptions)
75 throw new THttpException(500,'juioptions_control_invalid',$control->ID);
76 $this->_control=$control;
80 * Sets a named options with a value. Options are used to store and retrive
81 * named values for the javascript control.
82 * @param string option name.
83 * @param mixed option value.
84 * @throws THttpException
86 public function __set($name,$value)
88 if($this->_options===null)
89 $this->_options=array();
91 foreach($this->_control->getValidOptions() as $option)
93 if(0 == strcasecmp($name, $option))
95 $low = strtolower($value);
98 $this->_options[$option] = null;
99 } elseif($low === 'true') {
100 $this->_options[$option] = true;
101 } elseif($low === 'false') {
102 $this->_options[$option] = false;
103 } elseif(is_numeric($value)) {
104 // trick to get float or integer automatically when needed
105 $this->_options[$option] = $value + 0;
106 } elseif(substr($low,0,8)=='function') {
107 $this->_options[$option] = new TJavaScriptLiteral($value);
109 $this->_options[$option] = $value;
115 throw new TConfigurationException('juioptions_option_invalid',$this->_control->ID, $name);
119 * Gets an option named value. Options are used to store and retrive
120 * named values for the base active controls.
121 * @param string option name.
122 * @return mixed options value or null if not set.
124 public function __get($name)
126 if($this->_options===null)
127 $this->_options=array();
129 foreach($this->_control->getValidOptions() as $option)
131 if(0 == strcasecmp($name, $option) && isset($this->_options[$option]))
133 return $this->_options[$option];
141 * Only serialize the options itself, not the corresponding parent control.
142 * @return mixed array with the names of all variables of that object that should be serialized.
144 public function __sleep() {
145 return array('_options');
149 * @return Array of active control options
151 public function toArray()
153 $ret= ($this->_options===null) ? array() : $this->_options;
155 foreach($this->_control->getValidEvents() as $event)
156 if($this->_control->hasEventHandler('on'.$event))
157 $ret[$event]=new TJavaScriptLiteral("function( event, ui ) { Prado.JuiCallback(".TJavascript::encode($this->_control->getUniqueID()).", ".TJavascript::encode($event).", event, ui, this); }");
163 * Raise the specific callback event handler of the target control.
164 * @param mixed callback parameters
166 public function raiseCallbackEvent($param)
168 $callbackParam=$param->CallbackParameter;
169 if(isset($callbackParam->event))
171 $eventName = 'On'.ucfirst($callbackParam->event);
172 if($this->_control->hasEventHandler($eventName))
174 $this->_control->$eventName( new TJuiEventParameter(
175 $this->_control->getResponse(),
176 isset($callbackParam->ui) ? $callbackParam->ui : null)