summaryrefslogtreecommitdiff
path: root/framework/Web/UI/TClientScriptManager.php
diff options
context:
space:
mode:
authorwei <>2006-01-18 04:20:26 +0000
committerwei <>2006-01-18 04:20:26 +0000
commit0226f8f5f430d34b3cead40c4eb7b458933d16c6 (patch)
tree68b21c4bec09ed73edc64019b4e20a2e3853d477 /framework/Web/UI/TClientScriptManager.php
parente2219c91b98088289080bfa451d3083851eddf76 (diff)
update javascript library and usage in web controls
Diffstat (limited to 'framework/Web/UI/TClientScriptManager.php')
-rw-r--r--framework/Web/UI/TClientScriptManager.php355
1 files changed, 65 insertions, 290 deletions
diff --git a/framework/Web/UI/TClientScriptManager.php b/framework/Web/UI/TClientScriptManager.php
index c5b828dc..72b4ce21 100644
--- a/framework/Web/UI/TClientScriptManager.php
+++ b/framework/Web/UI/TClientScriptManager.php
@@ -70,127 +70,77 @@ class TPostBackOptions extends TComponent
}
}
+Prado::using('System.Web.Javascripts.*');
+
class TClientScriptManager extends TComponent
{
const SCRIPT_DIR='Web/Javascripts/js';
- const POSTBACK_FUNC='Prado.doPostBack';
+ //const POSTBACK_FUNC='Prado.doPostBack';
+
private $_page;
private $_hiddenFields=array();
private $_beginScripts=array();
private $_endScripts=array();
private $_scriptFiles=array();
- private $_headScriptFiles=array();
- private $_headScripts=array();
+
+ //private $_headScriptFiles=array();
+ //private $_headScripts=array();
+
private $_styleSheetFiles=array();
private $_styleSheets=array();
- private $_onSubmitStatements=array();
+
+ private $_client;
+
+ /*private $_onSubmitStatements=array();
private $_arrayDeclares=array();
private $_expandoAttributes=array();
private $_postBackScriptRegistered=false;
private $_focusScriptRegistered=false;
private $_scrollScriptRegistered=false;
+ */
+
private $_publishedScriptFiles=array();
+
public function __construct(TPage $owner)
{
$this->_page=$owner;
+ $this->_client = new TClientScript($this);
}
-
- public function getPostBackEventReference($control,$parameter='',$options=null,$javascriptPrefix=true)
+
+
+ public function registerPostBackControl($control,$namespace='Prado.WebUI')
{
- if(!$options || (!$options->getPerformValidation() && !$options->getTrackFocus() && $options->getClientSubmit() && $options->getActionUrl()==''))
- {
- $this->registerPostBackScript();
- if(($form=$this->_page->getForm())!==null)
- $formID=$form->getClientID();
- else
- throw new TConfigurationException('clientscriptmanager_form_required');
- $postback=self::POSTBACK_FUNC.'(\''.$formID.'\',\''.$control->getUniqueID().'\',\''.THttpUtility::quoteJavaScriptString($parameter).'\')';
- if($options && $options->getAutoPostBack())
- $postback='setTimeout(\''.THttpUtility::quoteJavaScriptString($postback).'\',0)';
- return $javascriptPrefix?'javascript:'.$postback:$postback;
- }
- $opt='';
- $flag=false;
- if($options->getPerformValidation())
- {
- $flag=true;
- $this->registerValidationScript();
- $opt.=',true,';
- }
- else
- $opt.=',false,';
- if($options->getValidationGroup()!=='')
- {
- $flag=true;
- $opt.='"'.$options->getValidationGroup().'",';
- }
- else
- $opt.='\'\',';
- if($options->getActionUrl()!=='')
- {
- $flag=true;
- $this->_page->setCrossPagePostBack(true);
- $opt.='"'.$options->getActionUrl().'",';
- }
- else
- $opt.='null,';
- if($options->getTrackFocus())
- {
- $flag=true;
- $this->registerFocusScript();
- $opt.='true,';
- }
- else
- $opt.='false,';
- if($options->getClientSubmit())
- {
- $flag=true;
- $opt.='true';
- }
- else
- $opt.='false';
- if(!$flag)
- return '';
- $this->registerPostBackScript();
- if(($form=$this->_page->getForm())!==null)
- $formID=$form->getClientID();
- else
- throw new TConfigurationException('clientscriptmanager_form_required');
- $postback=self::POSTBACK_FUNC.'(\''.$formID.'\',\''.$control->getUniqueID().'\',\''.THttpUtility::quoteJavaScriptString($parameter).'\''.$opt.')';
- if($options && $options->getAutoPostBack())
- $postback='setTimeout(\''.THttpUtility::quoteJavaScriptString($postback).'\',0)';
- return $javascriptPrefix?'javascript:'.$postback:$postback;
+ $options = $this->getPostBackOptions($control);
+ $type = get_class($control);
+ $code = "new {$namespace}.{$type}($options);";
+ $this->registerEndScript(sprintf('%08X', crc32($code)), $code);
+
+ $this->registerHiddenField(TPage::FIELD_POSTBACK_TARGET,'');
+ $this->registerHiddenField(TPage::FIELD_POSTBACK_PARAMETER,'');
+ $this->registerClientScript('prado');
}
- /*public function registerPradoScript($script)
+ protected function getPostBackOptions($control)
{
- foreach(TPradoClientScript::getScripts($script) as $scriptFile)
- {
- if(isset($this->_publishedScriptFiles[$scriptFile]))
- $url=$this->_publishedScriptFiles[$scriptFile];
- else
- {
- $base = Prado::getFrameworkPath();
- $clientScripts = self::SCRIPT_DIR;
- $file = "{$base}/{$clientScripts}/{$scriptFile}.js";
- $assetManager = $this->_page->getService()->getAssetManager();
- $url= $assetManager->publishFilePath($file);
- $this->_publishedScriptFiles[$scriptFile]=$url;
- $this->registerScriptFile('prado:'.$scriptFile,$url);
- }
- }
- //return $url;
- }*/
+ $postback = $control->getPostBackOptions();
+ if(!isset($postback['ID']))
+ $postback['ID'] = $control->getClientID();
+ if(!isset($postback['FormID']))
+ $postback['FormID'] = $this->_page->getForm()->getClientID();
+ $options = new TJavascriptSerializer($postback);
+ return $options->toJavascript();
+ }
+
/**
- * Register Prado client scripts.
+ * Register client scripts.
*/
- public function registerPradoScript($script)
+ public function registerClientScript($script)
{
static $scripts = array();
$scripts = array_unique(array_merge($scripts,
- TPradoClientScript::getScripts($script)));
+ TClientScript::getScripts($script)));
$this->publishClientScriptAssets($scripts);
@@ -205,7 +155,7 @@ class TClientScriptManager extends TComponent
/**
* Publish each individual javascript file.
*/
- private function publishClientScriptAssets($scripts)
+ protected function publishClientScriptAssets($scripts)
{
foreach($scripts as $lib)
{
@@ -224,7 +174,7 @@ class TClientScriptManager extends TComponent
/**
* @return string URL of the compressor asset script.
*/
- private function publishClientScriptCompressorAsset()
+ protected function publishClientScriptCompressorAsset()
{
$scriptFile = 'clientscripts.php';
if(isset($this->_publishedScriptFiles[$scriptFile]))
@@ -241,8 +191,7 @@ class TClientScriptManager extends TComponent
}
}
-
- protected function registerPostBackScript()
+/* protected function registerPostBackScript()
{
if(!$this->_postBackScriptRegistered)
{
@@ -279,12 +228,12 @@ class TClientScriptManager extends TComponent
$this->registerPradoScript('prado');
$button = $target->getClientID();
$panel = $source->getClientID();
- return "Event.observe('{$panel}', 'keypress', Prado.Button.fireButton.bindEvent($('{$panel}'), '$button'));";
+ return "Event.observe('{$panel}', 'keyup', Prado.Button.fireButton.bindEvent($('{$panel}'), '$button'));";
}
public function registerValidationScript()
{
- }
+ }*/
public function isHiddenFieldRegistered($key)
{
@@ -311,7 +260,7 @@ class TClientScriptManager extends TComponent
return isset($this->_endScripts[$key]);
}
- public function isHeadScriptFileRegistered($key)
+/* public function isHeadScriptFileRegistered($key)
{
return isset($this->_headScriptFiles[$key]);
}
@@ -320,6 +269,7 @@ class TClientScriptManager extends TComponent
{
return isset($this->_headScripts[$key]);
}
+*/
public function isStyleSheetFileRegistered($key)
{
@@ -331,7 +281,7 @@ class TClientScriptManager extends TComponent
return isset($this->_styleSheets[$key]);
}
- public function isOnSubmitStatementRegistered($key)
+/* public function isOnSubmitStatementRegistered($key)
{
return isset($this->_onSubmitStatements[$key]);
}
@@ -340,7 +290,7 @@ class TClientScriptManager extends TComponent
{
$this->_arrayDeclares[$name][]=$value;
}
-
+*/
public function registerScriptFile($key,$url)
{
$this->_scriptFiles[$key]=$url;
@@ -353,11 +303,11 @@ class TClientScriptManager extends TComponent
$this->_hiddenFields[$name]=$value;
}
- public function registerOnSubmitStatement($key,$script)
+/* public function registerOnSubmitStatement($key,$script)
{
$this->_onSubmitStatements[$key]=$script;
}
-
+*/
public function registerBeginScript($key,$script)
{
$this->_beginScripts[$key]=$script;
@@ -368,7 +318,7 @@ class TClientScriptManager extends TComponent
$this->_endScripts[$key]=$script;
}
- public function registerHeadScriptFile($key,$url)
+/* public function registerHeadScriptFile($key,$url)
{
$this->_headScriptFiles[$key]=$url;
}
@@ -377,7 +327,7 @@ class TClientScriptManager extends TComponent
{
$this->_headScripts[$key]=$script;
}
-
+*/
public function registerStyleSheetFile($key,$url)
{
$this->_styleSheetFiles[$key]=$url;
@@ -388,7 +338,7 @@ class TClientScriptManager extends TComponent
$this->_styleSheets[$key]=$css;
}
- public function registerExpandoAttribute($controlID,$name,$value)
+/* public function registerExpandoAttribute($controlID,$name,$value)
{
$this->_expandoAttributes[$controlID][$name]=$value;
}
@@ -404,7 +354,7 @@ class TClientScriptManager extends TComponent
$writer->write($str);
}
}
-
+*/
public function renderScriptFiles($writer)
{
$str='';
@@ -413,11 +363,11 @@ class TClientScriptManager extends TComponent
$writer->write($str);
}
- public function renderOnSubmitStatements($writer)
+/* public function renderOnSubmitStatements($writer)
{
// ???
}
-
+*/
public function renderBeginScripts($writer)
{
if(count($this->_beginScripts))
@@ -448,7 +398,7 @@ class TClientScriptManager extends TComponent
$writer->write("<div>\n".$str."</div>\n");
}
- public function renderExpandoAttributes($writer)
+/* public function renderExpandoAttributes($writer)
{
if(count($this->_expandoAttributes))
{
@@ -468,8 +418,9 @@ class TClientScriptManager extends TComponent
$writer->write($str);
}
}
+*/
- public function renderHeadScriptFiles($writer)
+/* public function renderHeadScriptFiles($writer)
{
$str='';
foreach($this->_headScriptFiles as $url)
@@ -482,6 +433,7 @@ class TClientScriptManager extends TComponent
if(count($this->_headScripts))
$writer->write("<script type=\"text/javascript\">\n//<![CDATA[\n".implode("\n",$this->_headScripts)."\n//]]>\n</script>\n");
}
+*/
public function renderStyleSheetFiles($writer)
{
@@ -504,12 +456,12 @@ class TClientScriptManager extends TComponent
return count($this->_hiddenFields)>0;
}
- public function getHasSubmitStatements()
+/* public function getHasSubmitStatements()
{
return count($this->_onSubmitStatements)>0;
}
-
- public function registerClientEvent($control, $event, $code)
+*/
+/* public function registerClientEvent($control, $event, $code)
{
if(empty($code)) return;
$this->registerPradoScript("prado");
@@ -517,7 +469,7 @@ class TClientScriptManager extends TComponent
$key = "prado:{$control->ClientID}:{$event}";
$this->registerEndScript($key, $script);
}
-
+*/
/*
@@ -532,181 +484,4 @@ class TClientScriptManager extends TComponent
*/
}
-/**
- * TJavascript class file. Javascript utilties, converts basic PHP types into
- * appropriate javascript types.
- *
- * Example:
- * <code>
- * $options['onLoading'] = "doit";
- * $options['onComplete'] = "more";
- * $js = TJavascript::toList($options);
- * //expects the following javascript code
- * // {'onLoading':'doit','onComplete':'more'}
- * </code>
- *
- * Namespace: System.Web.UI
- *
- * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
- * @version $Revision: 1.3 $ $Date: 2005/11/10 23:43:26 $
- * @package System.Web.UI
- */
-class TJavascript
-{
- /**
- * Coverts PHP arrays (only the array values) into javascript array.
- * @param array the array data to convert
- * @param string append additional javascript array data
- * @param boolean if true empty string and empty array will be converted
- * @return string javascript array as string.
- */
- public static function toArray($array,$append=null,$strict=false)
- {
- $results = array();
- $converter = new TJavascript();
- foreach($array as $v)
- {
- if($strict || (!$strict && $v !== '' && $v !== array()))
- {
- $type = 'to_'.gettype($v);
- if($type == 'to_array')
- $results[] = $converter->toArray($v, $append, $strict);
- else
- $results[] = $converter->{$type}($v);
- }
- }
- $extra = '';
- if(strlen($append) > 0)
- $extra .= count($results) > 0 ? ','.$append : $append;
- return '['.implode(',', $results).$extra.']';
- }
-
- /**
- * Coverts PHP arrays (both key and value) into javascript objects.
- * @param array the array data to convert
- * @param string append additional javascript object data
- * @param boolean if true empty string and empty array will be converted
- * @return string javascript object as string.
- */
- public static function toList($array,$append=null, $strict=false)
- {
- $results = array();
- $converter = new TJavascript();
- foreach($array as $k => $v)
- {
- if($strict || (!$strict && $v !== '' && $v !== array()))
- {
- $type = 'to_'.gettype($v);
- if($type == 'to_array')
- $results[] = "'{$k}':".$converter->toList($v, $append, $strict);
- else
- $results[] = "'{$k}':".$converter->{$type}($v);
- }
- }
- $extra = '';
- if(strlen($append) > 0)
- $extra .= count($results) > 0 ? ','.$append : $append;
-
- return '{'.implode(',', $results).$extra.'}';
- }
-
- public function to_boolean($v)
- {
- return $v ? 'true' : 'false';
- }
-
- public function to_integer($v)
- {
- return "{$v}";
- }
-
- public function to_double($v)
- {
- return "{$v}";
- }
-
- /**
- * If string begins with [ and ends ], or begins with { and ends }
- * it is assumed to be javascript arrays or objects and no further
- * conversion is applied.
- */
- public function to_string($v)
- {
- if(strlen($v)>1)
- {
- $first = $v{0}; $last = $v{strlen($v)-1};
- if($first == '[' && $last == ']' ||
- ($first == '{' && $last == '}'))
- return $v;
- }
- return "'".addslashes($v)."'";
- }
-
- public function to_array($v)
- {
- return TJavascript::toArray($v);
- }
-
- public function to_null($v)
- {
- return 'null';
- }
-}
-
-/**
- * PradoClientScript class.
- *
- * Resolves Prado client script dependencies. e.g. TPradoClientScript::getScripts("dom");
- *
- * - <b>base</b> basic javascript utilities, e.g. $()
- * - <b>dom</b> DOM and Form functions, e.g. $F(inputID) to retrive form input values.
- * - <b>effects</b> Effects such as fade, shake, move
- * - <b>controls</b> Prado client-side components, e.g. Slider, AJAX components
- * - <b>validator</b> Prado client-side validators.
- * - <b>ajax</b> Prado AJAX library including Prototype's AJAX and JSON.
- *
- * Dependencies for each library are automatically resolved.
- *
- * Namespace: System.Web.UI
- *
- * @author Wei Zhuo<weizhuo[at]gmail[dot]com>
- * @version $Revision: 1.1 $ $Date: 2005/11/06 23:02:33 $
- * @package System.Web.UI
- */
-class TPradoClientScript
-{
- /**
- * Client-side javascript library dependencies
- * @var array
- */
- protected static $dependencies = array(
- 'prado' => array('prado'),
- 'effects' => array('prado', 'effects'),
- 'ajax' => array('prado', 'effects', 'ajax'),
- 'validator' => array('prado', 'validator'),
- 'logger' => array('prado', 'logger'),
- 'datepicker' => array('prado', 'datepicker'),
- 'rico' => array('prado', 'effects', 'ajax', 'rico')
- );
-
- /**
- * Resolve dependencies for the given library.
- * @param array list of libraries to load.
- * @return array list of libraries including its dependencies.
- */
- public static function getScripts($scripts)
- {
- $files = array();
- if(!is_array($scripts)) $scripts = array($scripts);
- foreach($scripts as $script)
- {
- if(isset(self::$dependencies[$script]))
- $files = array_merge($files, self::$dependencies[$script]);
- $files[] = $script;
- }
- $files = array_unique($files);
- return $files;
- }
-}
-
?> \ No newline at end of file