From 75a0a250cc6735d13b3b782daf0127298b37c2b9 Mon Sep 17 00:00:00 2001 From: wei <> Date: Thu, 4 May 2006 02:02:43 +0000 Subject: Adding TCallback component. --- framework/Web/UI/ActiveControls/TActiveControl.php | 125 ++++++++++++++-- .../Web/UI/ActiveControls/TActivePageAdapter.php | 33 +++-- framework/Web/UI/ActiveControls/TCallback.php | 165 +++++++++++++++++++++ .../UI/ActiveControls/TCallbackClientScript.php | 109 ++++++++++---- .../ActiveControls/TCallbackClientSideOptions.php | 68 +++++---- .../Web/UI/ActiveControls/TCallbackResponse.php | 45 +++++- 6 files changed, 465 insertions(+), 80 deletions(-) create mode 100644 framework/Web/UI/ActiveControls/TCallback.php (limited to 'framework/Web/UI/ActiveControls') diff --git a/framework/Web/UI/ActiveControls/TActiveControl.php b/framework/Web/UI/ActiveControls/TActiveControl.php index e61682d3..62470a18 100644 --- a/framework/Web/UI/ActiveControls/TActiveControl.php +++ b/framework/Web/UI/ActiveControls/TActiveControl.php @@ -6,14 +6,65 @@ class TActiveControl extends TControl implements ICallbackEventHandler, IActiveControl { + /** + * @var TCallbackClientSideOptions client-side options. + */ private $_clientSide; + /** + * Creates a new callback control, sets the adapter to + * TActiveControlAdapter. If you override this class, be sure to set the + * adapter appropriately by, for example, call this constructor. + */ public function __construct() { parent::__construct(); $this->setAdapter(new TActiveControlAdapter($this)); } + /** + * @return boolean whether callback event trigger by this button will cause + * input validation, default is true + */ + public function getCausesValidation() + { + return $this->getViewState('CausesValidation',true); + } + + /** + * @param boolean whether callback event trigger by this button will cause + * input validation + */ + public function setCausesValidation($value) + { + $this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true); + } + + /** + * @return string the group of validators which the button causes validation + * upon callback + */ + public function getValidationGroup() + { + return $this->getViewState('ValidationGroup',''); + } + + /** + * @param string the group of validators which the button causes validation + * upon callback + */ + public function setValidationGroup($value) + { + $this->setViewState('ValidationGroup',$value,''); + } + + /** + * Callback client-side options can be set by setting the properties of + * the ClientSide property. E.g. + * See {@link TCallbackClientSideOptions} for details on the properties of + * ClientSide. + * @return TCallbackClientSideOptions client-side callback options. + */ public function getClientSide() { if(is_null($this->_clientSide)) @@ -21,28 +72,78 @@ class TActiveControl extends TControl implements ICallbackEventHandler, IActiveC return $this->_clientSide; } + /** + * @return TCallbackClientSideOptions callback client-side options. + */ protected function createClientSideOptions() { - $client = new TCallbackClientSideOptions; - return $client; + return new TCallbackClientSideOptions; } + + /** + * @return boolean whether to perform validation if the callback is + * requested. + */ + protected function canCauseValidation() + { + if($this->getCausesValidation()) + { + $group=$this->getValidationGroup(); + return $this->getPage()->getValidators($group)->getCount()>0; + } + else + return false; + } + /** + * Raises the callback event. This method is required by {@link + * ICallbackEventHandler} interface. If {@link getCausesValidation + * CausesValidation} is true, it will invoke the page's {@link TPage:: + * validate validate} method first. It will raise {@link onCallback + * OnCallback} event. This method is mainly used by framework and control + * developers. + * @param TCallbackEventParameter the event parameter + */ public function raiseCallbackEvent($param) { - var_dump($param->getParameter()); - $param->Output->write($param->Parameter); - $client = $this->getPage()->getCallbackClient(); - $client->hide($this); - $client->toggle($this); - $client->update($this, 1); - $param->setData(array("asdasdad",1)); + if($this->getCausesValidation()) + $this->getPage()->validate($this->getValidationGroup()); + $this->onCallback($param); + } + + /** + * This method is invoked when a callback is requested. The method raises + * 'OnCallback' event to fire up the event handlers. If you override this + * method, be sure to call the parent implementation so that the event + * handler can be invoked. + * @param TCallbackEventParameter event parameter to be passed to the event handlers + */ + public function onCallback($param) + { + $this->raiseEvent('OnCallback', $this, $param); } + /** + * @return array list of callback javascript options. + */ + protected function getCallbackOptions() + { + $validate = $this->getCausesValidation(); + $options['CausesValidation']= $validate ? '' : false; + $options['ValidationGroup']=$this->getValidationGroup(); + return $options; + } + + /** + * Returns the javascript statement to invoke a callback request for this + * control. Additional options for callback can be set via subproperties of + * {@link getClientSide ClientSide} property. E.g. ClientSide.OnSuccess="..." + * @return string javascript statement to invoke a callback. + */ public function getCallbackReference() { - // $formID = $this->getPage()->getForm()->getClientID(); - // $this->getClientSide()->setValidationForm($formID); - return $this->getPage()->getClientScript()->getCallbackReference($this); + $client = $this->getPage()->getClientScript(); + return $client->getCallbackReference($this, $this->getCallbackOptions()); } } diff --git a/framework/Web/UI/ActiveControls/TActivePageAdapter.php b/framework/Web/UI/ActiveControls/TActivePageAdapter.php index 2607fec2..4cb785dd 100644 --- a/framework/Web/UI/ActiveControls/TActivePageAdapter.php +++ b/framework/Web/UI/ActiveControls/TActivePageAdapter.php @@ -50,7 +50,7 @@ class TActivePageAdapter extends TControlAdapter public function __construct(TPage $control) { parent::__construct($control); - //$this->getApplication()->setResponse($this->getCallbackResponseHandler()); + $this->getApplication()->setResponse($this->createCallbackResponseHandler()); $this->trapCallbackErrorsExceptions(); } @@ -75,6 +75,7 @@ class TActivePageAdapter extends TControlAdapter { Prado::trace("ActivePage renderCallbackResponse()",'System.Web.UI.ActiveControls.TActivePageAdapter'); $this->renderResponse($writer); + //$this->getResponse()->flush(); } /** @@ -87,9 +88,11 @@ class TActivePageAdapter extends TControlAdapter $executeJavascript = $this->getCallbackClientHandler()->getClientFunctionsToExecute()->toArray(); $actions = TJavascript::jsonEncode($executeJavascript); $response->appendHeader(self::CALLBACK_ACTION_HEADER.': '.$actions); - $data = TJavascript::jsonEncode($this->_result->getData()); - $response->appendHeader(self::CALLBACK_DATA_HEADER.': '.$data); - $response->flush(); + if($this->_result) + { + $data = TJavascript::jsonEncode($this->_result->getData()); + $response->appendHeader(self::CALLBACK_DATA_HEADER.': '.$data); + } } /** @@ -104,12 +107,14 @@ class TActivePageAdapter extends TControlAdapter { if($callbackHandler instanceof ICallbackEventHandler) { - $writer = $this->getResponse()->createHtmlWriter(); - $this->_result = new TCallbackEventParameter($writer, $this->getCallbackEventParameter()); + $param = $this->getCallbackEventParameter(); + $this->_result = new TCallbackEventParameter($this->getResponse(), $param); $callbackHandler->raiseCallbackEvent($this->_result); } else + { throw new TInvalidCallbackHandlerException($callbackHandler->getUniqueID()); + } } else { @@ -193,6 +198,11 @@ class TActivePageAdapter extends TControlAdapter $this->_callbackClient = $handler; } + protected function createCallbackResponseHandler() + { + return new TCallbackResponse(); + } + } /** @@ -213,9 +223,9 @@ class TActivePageAdapter extends TControlAdapter class TCallbackEventParameter extends TEventParameter { /** - * @var THtmlWriter output content. + * @var TCallbackResponse output content. */ - private $_output; + private $_response; /** * @var mixed callback request parameter. */ @@ -228,9 +238,9 @@ class TCallbackEventParameter extends TEventParameter /** * Creates a new TCallbackEventParameter. */ - public function __construct($writer, $parameter) + public function __construct($response, $parameter) { - $this->_output = $writer; + $this->_response = $response; $this->_parameter = $parameter; } @@ -239,7 +249,7 @@ class TCallbackEventParameter extends TEventParameter */ public function getOutput() { - return $this->_output; + return $this->_response->createHtmlWriter(); } /** @@ -283,6 +293,7 @@ class TCallbackErrorHandler extends TErrorHandler error_log("Error happened while processing an existing error:\n".$exception->__toString()); header('HTTP/1.0 500 Internal Error'); } + $this->getApplication()->getResponse()->flush(); } private function getExceptionData($exception) diff --git a/framework/Web/UI/ActiveControls/TCallback.php b/framework/Web/UI/ActiveControls/TCallback.php new file mode 100644 index 00000000..d3b1f54d --- /dev/null +++ b/framework/Web/UI/ActiveControls/TCallback.php @@ -0,0 +1,165 @@ +setAdapter(new TActiveControlAdapter($this)); + } + + /** + * @return string tag name of the panel + */ + protected function getTagName() + { + return 'div'; + } + + /** + * @return boolean whether callback event trigger by this button will cause + * input validation, default is true + */ + public function getCausesValidation() + { + return $this->getViewState('CausesValidation',true); + } + + /** + * @param boolean whether callback event trigger by this button will cause + * input validation + */ + public function setCausesValidation($value) + { + $this->setViewState('CausesValidation',TPropertyValue::ensureBoolean($value),true); + } + + /** + * @return string the group of validators which the button causes validation + * upon callback + */ + public function getValidationGroup() + { + return $this->getViewState('ValidationGroup',''); + } + + /** + * @param string the group of validators which the button causes validation + * upon callback + */ + public function setValidationGroup($value) + { + $this->setViewState('ValidationGroup',$value,''); + } + + /** + * Callback client-side options can be set by setting the properties of + * the ClientSide property. E.g. + * See {@link TCallbackClientSideOptions} for details on the properties of + * ClientSide. + * @return TCallbackClientSideOptions client-side callback options. + */ + public function getClientSide() + { + if(is_null($this->_clientSide)) + $this->_clientSide = $this->createClientSideOptions(); + return $this->_clientSide; + } + + /** + * @return TCallbackClientSideOptions callback client-side options. + */ + protected function createClientSideOptions() + { + return new TCallbackClientSideOptions; + } + + /** + * @return boolean whether to perform validation if the callback is + * requested. + */ + protected function canCauseValidation() + { + if($this->getCausesValidation()) + { + $group=$this->getValidationGroup(); + return $this->getPage()->getValidators($group)->getCount()>0; + } + else + return false; + } + + /** + * Raises the callback event. This method is required by {@link + * ICallbackEventHandler} interface. If {@link getCausesValidation + * CausesValidation} is true, it will invoke the page's {@link TPage:: + * validate validate} method first. It will raise {@link onCallback + * OnCallback} event. This method is mainly used by framework and control + * developers. + * @param TCallbackEventParameter the event parameter + */ + public function raiseCallbackEvent($param) + { + if($this->getCausesValidation()) + $this->getPage()->validate($this->getValidationGroup()); + $this->onCallback($param); + } + + /** + * This method is invoked when a callback is requested. The method raises + * 'OnCallback' event to fire up the event handlers. If you override this + * method, be sure to call the parent implementation so that the event + * handler can be invoked. + * @param TCallbackEventParameter event parameter to be passed to the event handlers + */ + public function onCallback($param) + { + $this->raiseEvent('OnCallback', $this, $param); + } + + /** + * @return array list of callback javascript options. + */ + protected function getCallbackOptions() + { + $validate = $this->getCausesValidation(); + $options['CausesValidation']= $validate ? '' : false; + $options['ValidationGroup']=$this->getValidationGroup(); + return $options; + } + + /** + * Returns the javascript statement to invoke a callback request for this + * control. Additional options for callback can be set via subproperties of + * {@link getClientSide ClientSide} property. E.g. ClientSide.OnSuccess="..." + * @return string javascript statement to invoke a callback. + */ + public function getCallbackReference() + { + $client = $this->getPage()->getClientScript(); + return $client->getCallbackReference($this, $this->getCallbackOptions()); + } + + public function render($writer) + { + parent::render($writer); + if($this->getPage()->getIsCallback()) + $this->getPage()->getCallbackClient()->replace($this, $writer); + } +} + +?> diff --git a/framework/Web/UI/ActiveControls/TCallbackClientScript.php b/framework/Web/UI/ActiveControls/TCallbackClientScript.php index 10c0e638..aaf81380 100644 --- a/framework/Web/UI/ActiveControls/TCallbackClientScript.php +++ b/framework/Web/UI/ActiveControls/TCallbackClientScript.php @@ -32,7 +32,7 @@ * @package System.Web.UI.ActiveControls * @since 3.0 */ -class TCallbackClientScript +class TCallbackClientScript extends TApplicationComponent { /** * @var TList list of client functions to execute. @@ -69,7 +69,7 @@ class TCallbackClientScript if(count($params) > 0) { if($params[0] instanceof TControl) - $params[0] = $params[0]->getID(); + $params[0] = $params[0]->getClientID(); } $this->_actions->add(array($function => $params)); } @@ -188,22 +188,20 @@ class TCallbackClientScript * @param TControl|string new HTML content, if content is of a TControl, the * controls render method is called. */ - public function update($element, $innerHTML) + public function update($element, $content) { - if($innerHTML instanceof TControl) - $innerHTML = $innerHTML->render(); - $this->callClientFunction('Element.update', array($element, $innerHTML)); + $this->replace($element, $content, 'Element.update'); } /** * Replace the innerHTML of a content with fragements of the response body. * @param TControl|string control element or element id */ - public function replaceContent($element) +/* 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 @@ -229,10 +227,10 @@ class TCallbackClientScript * @param TControl|string control element or element id * @param string new CssClass name for the element. */ - public function setCssClass($element, $cssClass) + /*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 @@ -261,11 +259,9 @@ class TCallbackClientScript * @param TControl|string HTML fragement, otherwise if TControl, its render * method will be called. */ - public function insertAfter($element, $innerHTML) + public function insertAfter($element, $content) { - if($innerHTML instanceof TControl) - $innerHTML = $innerHTML->render(); - $this->callClientFunction('Prado.Element.Insert.After', array($element, $innerHTML)); + $this->replace($element, $content, 'Element.Insert.After'); } /** @@ -274,11 +270,9 @@ class TCallbackClientScript * @param TControl|string HTML fragement, otherwise if TControl, its render * method will be called. */ - public function insertBefore($element, $innerHTML) + public function insertBefore($element, $content) { - if($innerHTML instanceof TControl) - $innerHTML = $innerHTML->render(); - $this->callClientFunction('Prado.Element.Insert.Before', array($element, $innerHTML)); + $this->replace($element, $content, 'Element.Insert.Before'); } /** @@ -287,11 +281,9 @@ class TCallbackClientScript * @param TControl|string HTML fragement, otherwise if TControl, its render * method will be called. */ - public function insertBelow($element, $innerHTML) + public function insertBelow($element, $content) { - if($innerHTML instanceof TControl) - $innerHTML = $innerHTML->render(); - $this->callClientFunction('Prado.Element.Insert.Below', array($element, $innerHTML)); + $this->replace($element, $content, 'Element.Insert.Below'); } /** @@ -300,12 +292,75 @@ class TCallbackClientScript * @param TControl|string HTML fragement, otherwise if TControl, its render * method will be called. */ - public function insertAbove($element, $innerHTML) + public function insertAbove($element, $content) { - if($innerHTML instanceof TControl) - $innerHTML = $innerHTML->render(); - $this->callClientFunction('Prado.Element.Insert.Above', array($element, $innerHTML)); - } + $this->replace($element, $content, 'Element.Insert.Above'); + } + + /** + * Replace the content of an element with new content. The new content can + * be a string or a TControl component. If the content parameter is + * a TControl component, its rendered method will be called and its contents + * will be used for replacement. + * @param TControl|string control element or HTML element id. + * @param TControl|string HTML fragement, otherwise it will TControl's + * rendered content. + * @param string replacement method, default is to replace the outter + * html content. + * @param string provide a custom boundary. + * @see insertAbout + * @see insertBelow + * @see insertBefore + * @see insertAfter + */ + public function replace($element, $content, $method="Element.replace", $boundary=null) + { + if($content instanceof TControl) + { + $boundary = $this->getRenderedContentBoundary($content); + $this->callClientFunction('Prado.Element.replaceContent', + array($element, $method, null, $boundary)); + } + else if($content instanceof THtmlWriter) + { + $boundary = $this->getResponseContentBoundary($content); + $this->callClientFunction('Prado.Element.replaceContent', + array($element, $method, null, $boundary)); + } + else + { + $this->callClientFunction('Prado.Element.replaceContent', + array($element, $method, $content, $boundary)); + } + } + + /** + * Renders the control and return the content boundary from + * TCallbackResponseWriter. This method should only be used by framework + * component developers. + * @param TControl control to be rendered on callback response. + * @return string the boundary for which the rendered content is wrapped. + */ + private function getRenderedContentBoundary($control) + { + $writer = $this->getResponse()->createHtmlWriter(); + $control->render($writer); + return $writer->getWriter()->getBoundary(); + } + + /** + * @param THtmlWriter the writer responsible for rendering html content. + * @return string content boundary. + */ + private function getResponseContentBoundary($html) + { + if($html instanceof THtmlWriter) + { + if($html->getWriter() instanceof TCallbackResponseWriter) + return $html->getWriter()->getBoundary(); + } + return null; + } /** * Add a visual effect the element. diff --git a/framework/Web/UI/ActiveControls/TCallbackClientSideOptions.php b/framework/Web/UI/ActiveControls/TCallbackClientSideOptions.php index dea1b4e1..1e1bd52f 100644 --- a/framework/Web/UI/ActiveControls/TCallbackClientSideOptions.php +++ b/framework/Web/UI/ActiveControls/TCallbackClientSideOptions.php @@ -34,27 +34,35 @@ class TClientSideOptions extends TComponent } /** + * TCallbackClientSideOptions class. + * * The following client side events are executing in order if the callback * request and response are send and received successfuly. * - * - onUninitialized executed when AJAX request is uninitialized. - * - onLoading executed when AJAX request is initiated - * - onLoaded executed when AJAX request begins. - * - onInteractive executed when AJAX request is in progress. - * - onCompleteexecuted when AJAX response returns. + * - onUninitialized executed when callback request is uninitialized. + * - onLoading executed when callback request is initiated + * - onLoaded executed when callback request begins. + * - onInteractive executed when callback request is in progress. + * - onCompleteexecuted when callback response returns. * * The OnSuccess and OnFailure events are raised when the * response is returned. A successful request/response will raise * OnSuccess event otherwise OnFailure will be raised. * - * - onSuccess executed when AJAX request returns and is successful. - * - onFailure executed when AJAX request returns and fails. + * - onSuccess executed when callback request returns and is successful. + * - onFailure executed when callback request returns and fails. + * - onException raised when callback request fails due to + * request/response errors. * - * - CausesValidation true to perform before callback request. - * - ValidationGroup validation grouping name. */ class TCallbackClientSideOptions extends TClientSideOptions { + /** + * Returns javascript statement enclosed within a javascript function. + * @param string javascript statement, if string begins within + * "javascript:" the whole string is assumed to be a function. + * @return string javascript statement wrapped in a javascript function + */ protected function ensureFunction($javascript) { if(TJavascript::isFunction($javascript)) @@ -175,34 +183,38 @@ class TCallbackClientSideOptions extends TClientSideOptions $this->setFunction('onFailure', $javascript); } - public function getCausesValidation() - { - return $this->getOption('CausesValidation'); - } - - public function setCausesValidation($value) - { - $this->getOptions()->add('CausesValidation', TPropertyValue::ensureBoolean($value)); - } - - public function getValidationGroup() + /** + * @return string javascript code for client-side onException event + */ + public function getOnException() { - return $this->getOption('ValidationGroup'); + return $this->getOption('onException'); } - public function setValidationGroup($value) + /** + * @param string javascript code for client-side onException event. + */ + public function setOnException($javascript) { - $this->getOptions()->add('ValidationGroup', $value); - } + $this->setFunction('onException', $javascript); + } - public function getValidationForm() + /** + * @return boolean true to post the state on callback, default is post the + * state on callback. + */ + public function getPostState() { - return $this->getOption('Form'); + return $this->getOption('PostState'); } - public function setValidationForm($value) + /** + * @param boolean true to post the state of the form with callback requests. + * Default is to post the state. + */ + public function setPostState($value) { - $this->getOptions()->add('Form', $value); + $this->getOptions()->add('PostState', TPropertyValue::ensureBoolean($value)); } } diff --git a/framework/Web/UI/ActiveControls/TCallbackResponse.php b/framework/Web/UI/ActiveControls/TCallbackResponse.php index a05c20c7..4a893b9a 100644 --- a/framework/Web/UI/ActiveControls/TCallbackResponse.php +++ b/framework/Web/UI/ActiveControls/TCallbackResponse.php @@ -11,9 +11,50 @@ class TCallbackResponse extends THttpResponse { + private $_writers=array(); + public function createHtmlWriter($type=null) + { + $writer = new TCallbackResponseWriter(); + $this->_writers[] = $writer; + if($type===null) + $type=$this->getHtmlWriterType(); + return Prado::createComponent($type,$writer); + } + + public function flush() + { + foreach($this->_writers as $writer) + echo $writer->flush(); + parent::flush(); + } } - - +class TCallbackResponseWriter extends TTextWriter +{ + private $_boundary; + + public function __construct() + { + $this->_boundary = sprintf('%x',crc32((string)$this)); + } + + public function getBoundary() + { + return $this->_boundary; + } + + public function setBoundary($value) + { + $this->_boundary = $value; + } + + public function flush() + { + $content = ''; + $content .= parent::flush(); + $content .= ''; + return $content; + } +} ?> -- cgit v1.2.3