From 10b65d6d03ee0afc1ec1a50f320af42a79f5492b Mon Sep 17 00:00:00 2001 From: wei <> Date: Sun, 30 Apr 2006 00:15:23 +0000 Subject: Adding Callback foundations. --- framework/Web/UI/ActiveControls/TActiveControl.php | 23 + .../UI/ActiveControls/TActiveControlAdapter.php | 25 ++ .../Web/UI/ActiveControls/TActivePageAdapter.php | 219 ++++++++++ .../UI/ActiveControls/TCallbackClientScript.php | 485 +++++++++++++++++++++ .../Web/UI/ActiveControls/TCallbackResponse.php | 20 + 5 files changed, 772 insertions(+) create mode 100644 framework/Web/UI/ActiveControls/TActiveControl.php create mode 100644 framework/Web/UI/ActiveControls/TActiveControlAdapter.php create mode 100644 framework/Web/UI/ActiveControls/TActivePageAdapter.php create mode 100644 framework/Web/UI/ActiveControls/TCallbackClientScript.php create mode 100644 framework/Web/UI/ActiveControls/TCallbackResponse.php (limited to 'framework/Web/UI/ActiveControls') diff --git a/framework/Web/UI/ActiveControls/TActiveControl.php b/framework/Web/UI/ActiveControls/TActiveControl.php new file mode 100644 index 00000000..d289bab9 --- /dev/null +++ b/framework/Web/UI/ActiveControls/TActiveControl.php @@ -0,0 +1,23 @@ +setAdapter(new TActiveControlAdapter($this)); + } + + public function raiseCallbackEvent($param) + { + var_dump($param); + $client = $this->getPage()->getCallbackClient(); + $client->hide($this); + } +} + +?> diff --git a/framework/Web/UI/ActiveControls/TActiveControlAdapter.php b/framework/Web/UI/ActiveControls/TActiveControlAdapter.php new file mode 100644 index 00000000..187b2cac --- /dev/null +++ b/framework/Web/UI/ActiveControls/TActiveControlAdapter.php @@ -0,0 +1,25 @@ +getPage()->getPostDataLoaders(),false); + $script = "Prado.Callback.PostDataLoaders.concat({$options});"; + $this->getPage()->getClientScript()->registerEndScript(get_class($this), $script); + self::$_renderedPosts = true; + } + parent::render($writer); + } +} +?> diff --git a/framework/Web/UI/ActiveControls/TActivePageAdapter.php b/framework/Web/UI/ActiveControls/TActivePageAdapter.php new file mode 100644 index 00000000..ab042d54 --- /dev/null +++ b/framework/Web/UI/ActiveControls/TActivePageAdapter.php @@ -0,0 +1,219 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.Web.UI.ActiveControls + */ + +/** + * TActivePageAdapter class. + * + * Callback request page handler. + * + * @author Wei Zhuo + * @version $Revision: $ $Date: $ + * @package System.Web.UI.ActiveControls + * @since 3.0 + */ +class TActivePageAdapter extends TControlAdapter +{ + /** + * @var ICallbackEventHandler callback event handler. + */ + private $_callbackEventTarget; + /** + * @var mixed callback event parameter. + */ + private $_callbackEventParameter; + /** + * @var TCallbackClientScript callback client script handler + */ + private $_callbackClient; + /** + * @var TCallbackResponse callback response handler. + */ + private $_callbackResponse; + + private $_callbackEventResult; + + /** + * Constructor, trap errors and exception to let the callback response + * handle them. + */ + public function __construct(TPage $control) + { + parent::__construct($control); + $this->trapCallbackErrorsExceptions(); + } + + /** + * Process the callback request. + */ + public function processCallbackEvent($writer) + { + Prado::trace("ActivePage raiseCallbackEvent()",'System.Web.UI.ActiveControls.TActivePageAdapter'); + $this->raiseCallbackEvent(); + } + + protected function trapCallbackErrorsExceptions() + { + //TODO: How to trap the errors and exceptions and return them + // as part of the response. + } + + public function renderCallbackResponse($writer) + { + Prado::trace("ActivePage renderCallbackResponse()",'System.Web.UI.ActiveControls.TActivePageAdapter'); + $this->renderResponse($writer); + } + + protected function renderResponse($writer) + { + //var_dump(getallheaders()); + //TODO: How to render the response, it will contain 3 pieces of data + // 1) The arbituary data returned to the client-side callback handler + // 2) client-side function call statements + // 3) Content body, which may need to be partitioned + + /* + $response = $this->getCallbackResponseHandler(); + $response->writeClientScriptResponse($this->getCallbackClientHandler()); + $response->writeResponseData($this->getCallbackEventResult()); + $response->flush(); + */ + } + + /** + * Trys to find the callback event handler and raise its callback event. + * @throws TInvalidCallbackRequestException if call back target is not + * found. + * @throws TInvalidCallbackHandlerException if the requested target does not + * implement ICallbackEventHandler. + */ + private function raiseCallbackEvent() + { + if(($callbackHandler=$this->getCallbackEventTarget())!==null) + { + if($callbackHandler instanceof ICallbackEventHandler) + $callbackHandler->raiseCallbackEvent($this->getCallbackEventParameter()); + else + throw new TInvalidCallbackHandlerException($callbackHandler->getUniqueID()); + } + else + { + $target = $this->getRequest()->itemAt(TPage::FIELD_CALLBACK_TARGET); + throw new TInvalidCallbackRequestException($target); + } + } + + /** + * @return mixed callback event result. + */ + public function getCallbackEventResult() + { + return $this->_callbackEventResult->getResult(); + } + + /** + * @return TControl the control responsible for the current callback event, + * null if nonexistent + */ + public function getCallbackEventTarget() + { + if($this->_callbackEventTarget===null) + { + $eventTarget=$this->getRequest()->itemAt(TPage::FIELD_CALLBACK_TARGET); + if(!empty($eventTarget)) + $this->_callbackEventTarget=$this->getPage()->findControl($eventTarget); + } + return $this->_callbackEventTarget; + } + + /** + * Registers a control to raise callback event in the current request. + * @param TControl control registered to raise callback event. + */ + public function setCallbackEventTarget(TControl $control) + { + $this->_callbackEventTarget=$control; + } + + /** + * Callback parameter is decoded assuming JSON encoding. + * @return string postback event parameter + */ + public function getCallbackEventParameter() + { + if($this->_callbackEventParameter===null) + { + $param = $this->getRequest()->itemAt(TPage::FIELD_CALLBACK_PARAMETER); + if(strlen($param) > 0) + $this->_callbackEventParameter=TJavascript::jsonDecode((string)$param); + var_dump($param); + } + return $this->_callbackEventParameter; + } + + /** + * @param mixed postback event parameter + */ + public function setCallbackEventParameter($value) + { + $this->_callbackEventParameter=$value; + } + + /** + * Gets the callback client script handler that allows javascript functions + * to be executed during the callback response. + * @return TCallbackClientScript callback client handler. + */ + public function getCallbackClientHandler() + { + if(is_null($this->_callbackClient)) + $this->_callbackClient = new TCallbackClientScript; + return $this->_callbackClient; + } + + /** + * @param TCallbackClientScript new callback client handler. + */ + public function setCallbackClientHandler($handler) + { + $this->_callbackClient = $handler; + } + + /** + * Gets the callback response handler. + * @return TCallbackResponse callback response + */ + public function getCallbackResponseHandler() + { + if(is_null($this->_callbackResponse)) + $this->_callbackResponse = new TCallbackResponse; + return $this->_callbackResponse; + } + + /** + * @param TCallbackResponse new callback response handler. + */ + public function setCallbackResponseHandler($handler) + { + $this->_callbackResponse = $handler; + } +} + +class TInvalidCallbackHandlerException extends TException +{ + +} + +class TInvalidCallbackRequestException extends TException +{ +} + +?> diff --git a/framework/Web/UI/ActiveControls/TCallbackClientScript.php b/framework/Web/UI/ActiveControls/TCallbackClientScript.php new file mode 100644 index 00000000..550c88b5 --- /dev/null +++ b/framework/Web/UI/ActiveControls/TCallbackClientScript.php @@ -0,0 +1,485 @@ + + * @link http://www.pradosoft.com/ + * @copyright Copyright © 2005 PradoSoft + * @license http://www.pradosoft.com/license/ + * @version $Revision: $ $Date: $ + * @package System.Web.UI.ActiveControls + */ + +/** + * TCallbackClientScript class. + * + * The TCallbackClientScript class provides corresponding methods that can be + * executed on the client-side (i.e. the browser client that is viewing + * the page) during a callback response. + * + * The avaiable methods includes setting/clicking input elements, changing Css + * styles, hiding/showing elements, and adding visual effects to elements on the + * page. The client-side methods can be access through the CallbackClient + * property available in TPage. + * + * For example, to hide "$myTextBox" element during callback response, do + * + * $this->getPage()->getCallbackClient()->hide($myTextBox); + * + * + * @author Wei Zhuo + * @version $Revision: $ $Date: $ + * @package System.Web.UI.ActiveControls + * @since 3.0 + */ +class TCallbackClientScript +{ + /** + * @var TList list of client functions to execute. + */ + private $_actions; + + /** + * Constructor. + */ + public function __construct() + { + $this->_actions = new TList; + } + + /** + * @return array list of client function to be executed during callback + * response. + */ + public function getClientFunctionsToExecute() + { + return $this->_actions; + } + + /** + * Executes a client-side statement. + * @param string javascript function name + * @param array list of arguments for the function + */ + public function callClientFunction($function, $params=null) + { + if(!is_array($params) && $params !== null) + $params = array($params); + else + $params = array(); + + if(count($params) > 0) + { + if($params[0] instanceof TControl) + $params[0] = $params[0]->getID(); + } + $this->_actions->add(array($function => $params)); + } + + /** + * Client script to set the value of a particular input element. + * @param TControl|string control element to set the new value + * @param string new value + */ + public function setValue($input, $text) + { + $this->callClientFunction('Prado.Element.setValue', array($input, $text)); + } + + /** + * Client script to select/clear/check a drop down list, check box list, + * or radio button list. + * The second parameter determines the selection method. Valid methods are + * - Value, select or check by value + * - Index, select or check by list index (zero based index) + * - All, selects or checks all in the list + * - Clear, clears or selections or checks in the list + * - Invert, inverts the current selection or checks. + * @param TControl|string list control + * @param string selection method + * @param string|int the value or index to select/check. + */ + public function select($listControl, $method="Value", $valueOrIndex=null) + { + $this->callClientFunction('Prado.Element.select', array($listControl, $method, $valueOrIndex)); + } + + /** + * Client script to click on an element. This client-side function + * is unpredictable. + * @param TControl|string control element or element id + */ + public function click($control) + { + $this->callClientFunction('Prado.Element.click', $control); + } + + /** + * Client script to check or uncheck a checkbox or radio button. + * @param TControl|string control element or element id + * @param boolean check or uncheck the checkbox or radio button. + */ + public function check($checkbox, $checked=true) + { + $this->select($checkbox, "Value", $checked); + } + + /** + * Sets the attribute of a particular control. + * @param TControl|string control element or element id + * @param string attribute name + * @param string attribute value + */ + public function setAttribute($control, $name, $value) + { + $this->callClientFunction('Prado.Element.setAttribute',array($control, $name, $value)); + } + + /** + * Sets the options of a select input element. + * @param TControl|string control element or element id + * @param TCollection a list of new options + */ + public function setOptions($control, $items) + { + $options = array(); + foreach($items as $item) + $options[] = array($item->getText(),$item->getValue()); + $this->callClientFunction('Prado.Element.setOptions', array($control, $options)); + } + + /** + * Shows an element by changing its CSS display style as empty. + * @param TControl|string control element or element id + */ + public function show($element) + { + $this->callClientFunction('Element.show', $element); + } + + /** + * Hides an element by changing its CSS display style to "none". + * @param TControl|string control element or element id + */ + public function hide($element) + { + $this->callClientFunction('Element.hide', $element); + } + + /** + * Toggles the visibility of the element. + * @param TControl|string control element or element id + */ + public function toggle($element) + { + $this->callClientFunction('Element.toggle', $element); + } + + /** + * Removes an element from the HTML page. + * @param TControl|string control element or element id + */ + public function remove($element) + { + $this->callClientFunction('Element.remove', $element); + } + + /** + * Update the element's innerHTML with new content. + * @param TControl|string control element or element id + * @param TControl|string new HTML content, if content is of a TControl, the + * controls render method is called. + */ + public function update($element, $innerHTML) + { + if($innerHTML instanceof TControl) + $innerHTML = $innerHTML->render(); + $this->callClientFunction('Element.update', array($element, $innerHTML)); + } + + /** + * Replace the innerHTML of a content with fragements of the response body. + * @param TControl|string control element or element id + */ + public function replaceContent($element) + { + $this->callClientFunction('Prado.Element.replaceContent', $element); + } + + /** + * Add a Css class name to the element. + * @param TControl|string control element or element id + * @param string CssClass name to add. + */ + public function addCssClass($element, $cssClass) + { + $this->callClientFunction('Element.addClassName', array($element, $cssClass)); + } + + /** + * Remove a Css class name from the element. + * @param TControl|string control element or element id + * @param string CssClass name to remove. + */ + public function removeCssClass($element, $cssClass) + { + $this->callClientFunction('Element.removeClassName', array($element, $cssClass)); + } + + /** + * Sets the CssClass of an element. + * @param TControl|string control element or element id + * @param string new CssClass name for the element. + */ + public function setCssClass($element, $cssClass) + { + $this->callClientFunction('Prado.Element.CssClass.set', array($element, $cssClass)); + } + + /** + * Scroll the top of the browser viewing area to the location of the + * element. + * @param TControl|string control element or element id + */ + public function scrollTo($element) + { + $this->callClientFunction('Element.scrollTo', $element); + } + + /** + * Sets the style of element. The style must be a key-value array where the + * key is the style property and the value is the style value. + * @param TControl|string control element or element id + * @param array list of key-value pairs as style property and style value. + */ + public function setStyle($element, $styles) + { + $this->callClientFunction('Element.setStyle', array($element, $styles)); + } + + /** + * Insert a HTML fragement after the element. + * @param TControl|string control element or element id + * @param TControl|string HTML fragement, otherwise if TControl, its render + * method will be called. + */ + public function insertAfter($element, $innerHTML) + { + if($innerHTML instanceof TControl) + $innerHTML = $innerHTML->render(); + $this->callClientFunction('Prado.Element.Insert.After', array($element, $innerHTML)); + } + + /** + * Insert a HTML fragement before the element. + * @param TControl|string control element or element id + * @param TControl|string HTML fragement, otherwise if TControl, its render + * method will be called. + */ + public function insertBefore($element, $innerHTML) + { + if($innerHTML instanceof TControl) + $innerHTML = $innerHTML->render(); + $this->callClientFunction('Prado.Element.Insert.Before', array($element, $innerHTML)); + } + + /** + * Insert a HTML fragement below the element. + * @param TControl|string control element or element id + * @param TControl|string HTML fragement, otherwise if TControl, its render + * method will be called. + */ + public function insertBelow($element, $innerHTML) + { + if($innerHTML instanceof TControl) + $innerHTML = $innerHTML->render(); + $this->callClientFunction('Prado.Element.Insert.Below', array($element, $innerHTML)); + } + + /** + * Insert a HTML fragement above the element. + * @param TControl|string control element or element id + * @param TControl|string HTML fragement, otherwise if TControl, its render + * method will be called. + */ + public function insertAbove($element, $innerHTML) + { + if($innerHTML instanceof TControl) + $innerHTML = $innerHTML->render(); + $this->callClientFunction('Prado.Element.Insert.Above', array($element, $innerHTML)); + } + + /** + * Add a visual effect the element. + * @param string visual effect function name. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function visualEffect($type, $element, $options=null) + { + $this->callClientFunction($type, is_array($options) ? array($element, $options) : $element); + } + + /** + * Visual Effect: Gradually make the element appear. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function appear($element, $options=null) + { + $this->visualEffect('Effect.Appear', $element, $options); + } + + /** + * Visual Effect: Blind down. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function blindDown($element, $options=null) + { + $this->visualEffect('Effect.BlindDown', $element, $options); + } + + /** + * Visual Effect: Blind up. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function blindUp($element, $options=null) + { + $this->visualEffect('Effect.BlindUp', $element, $options); + + } + + /** + * Visual Effect: Drop out. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function dropOut($element, $options=null) + { + $this->visualEffect('Effect.DropOut', $element, $options); + } + + /** + * Visual Effect: Gradually fade the element. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function fade($element, $options=null) + { + $this->visualEffect('Effect.Fade', $element, $options); + } + + /** + * Visual Effect: Fold. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function fold($element, $options = null) + { + $this->visualEffect('Effect.Fold', $element, $options); + } + + /** + * Visual Effect: Gradually make an element grow to a predetermined size. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function grow($element, $options=null) + { + $this->visualEffect('Effect.Grow', $element, $options); + } + + /** + * Visual Effect: Gradually grow and fade the element. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function puff($element, $options=null) + { + $this->visualEffect('Effect.Puff', $element, $options); + } + + /** + * Visual Effect: Pulsate. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function pulsate($element, $options=null) + { + $this->visualEffect('Effect.Pulsate', $element, $options); + } + + /** + * Visual Effect: Shake the element. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function shake($element, $options=null) + { + $this->visualEffect('Effect.Shake', $element, $options); + } + + /** + * Visual Effect: Shrink the element. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function shrink($element, $options=null) + { + $this->visualEffect('Effect.Shrink', $element, $options); + } + + /** + * Visual Effect: Slide down. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function slideDown($element, $options=null) + { + $this->visualEffect('Effect.SlideDown', $element, $options); + } + + /** + * Visual Effect: Side up. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function slideUp($element, $options=null) + { + $this->visualEffect('Effect.SlideUp', $element, $options); + } + + /** + * Visual Effect: Squish the element. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function squish($element, $options=null) + { + $this->visualEffect('Effect.Squish', $element, $options); + } + + /** + * Visual Effect: Switch Off effect. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function switchOff($element, $options=null) + { + $this->visualEffect('Effect.SwitchOff', $element, $options); + } + + /** + * Visual Effect: High light the element for about 2 seconds. + * @param TControl|string control element or element id + * @param array visual effect key-value pair options. + */ + public function highlight($element, $options=null) + { + $this->visualEffect('Effect.Highlight', $element, $options); + } +} + +?> diff --git a/framework/Web/UI/ActiveControls/TCallbackResponse.php b/framework/Web/UI/ActiveControls/TCallbackResponse.php new file mode 100644 index 00000000..bda4e916 --- /dev/null +++ b/framework/Web/UI/ActiveControls/TCallbackResponse.php @@ -0,0 +1,20 @@ + -- cgit v1.2.3