summaryrefslogtreecommitdiff
path: root/framework/Web
diff options
context:
space:
mode:
authorxue <>2006-02-18 07:24:51 +0000
committerxue <>2006-02-18 07:24:51 +0000
commit145c4aef09e278d29bfd8e9858e114ca097e5901 (patch)
treef125683e12badb170f9b5c435a811fd4f8eb3942 /framework/Web
parent64a5d4b6d3adaeab325d0e72d6c6c7624d861807 (diff)
client script reorganization.
Diffstat (limited to 'framework/Web')
-rw-r--r--framework/Web/Javascripts/TJavaScript.php110
-rw-r--r--framework/Web/THttpUtility.php20
-rw-r--r--framework/Web/UI/TClientScriptManager.php164
-rw-r--r--framework/Web/UI/WebControls/TBaseValidator.php5
-rw-r--r--framework/Web/UI/WebControls/TColorPicker.php8
-rw-r--r--framework/Web/UI/WebControls/TDatePicker.php14
-rw-r--r--framework/Web/UI/WebControls/THtmlArea.php3
-rw-r--r--framework/Web/UI/WebControls/TJavascriptLogger.php2
-rw-r--r--framework/Web/UI/WebControls/TRatingList.php5
-rw-r--r--framework/Web/UI/WebControls/TTextHighlighter.php2
-rw-r--r--framework/Web/UI/WebControls/TValidationSummary.php3
11 files changed, 198 insertions, 138 deletions
diff --git a/framework/Web/Javascripts/TJavaScript.php b/framework/Web/Javascripts/TJavaScript.php
new file mode 100644
index 00000000..f1efb5b3
--- /dev/null
+++ b/framework/Web/Javascripts/TJavaScript.php
@@ -0,0 +1,110 @@
+<?php
+
+/**
+ * TJavascript class file. Javascript utilties, converts basic PHP types into
+ * appropriate javascript types.
+ *
+ * Example:
+ * <code>
+ * $options['onLoading'] = "doit";
+ * $options['onComplete'] = "more";
+ * $js = new TJavascriptSerializer($options);
+ * echo $js->toMap();
+ * //expects the following javascript code
+ * // {'onLoading':'doit','onComplete':'more'}
+ * </code>
+ *
+ * For higher complexity data structures use TJSON to serialize and unserialize.
+ *
+ * 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
+{
+ public static function quoteJavaScriptString($js,$forUrl=false)
+ {
+ if($forUrl)
+ return strtr($js,array('%'=>'%25',"\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\'));
+ else
+ return strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\'));
+ }
+
+ public static function trimJavaScriptString($js)
+ {
+ if($js!=='' && $js!==null)
+ {
+ $js=trim($js);
+ if(($pos=strpos($js,'javascript:'))===0)
+ $js=substr($js,11);
+ $js=rtrim($js,';').';';
+ }
+ return $js;
+ }
+
+ public static function encode($value,$toMap=true)
+ {
+ if(is_string($value))
+ {
+ if(($n=strlen($value))>2)
+ {
+ $first=$value[0];
+ $last=$value[$n-1];
+ if(($first==='[' && $last===']') || ($first==='{' && $last==='}'))
+ return $value;
+ }
+ return "'".self::quoteJavaScriptString($value)."'";
+ }
+ else if(is_bool($value))
+ return $value?'true':'false';
+ else if(is_array($value))
+ {
+ $results=array();
+ if($toMap)
+ {
+ foreach($value as $k=>$v)
+ $results[]="'{$k}':".self::encode($v,$toMap);
+ return '{'.implode(',',$results).'}';
+ }
+ else
+ {
+ foreach($value as $k=>$v)
+ $results[]=self::encode($v,$toMap);
+ return '['.implode(',',$results).']';
+ }
+ }
+ else if(is_integer($value))
+ return "$value";
+ else if(is_float($value))
+ {
+ if($value===-INF)
+ return 'Number.NEGATIVE_INFINITY';
+ else if($value===INF)
+ return 'Number.POSITIVE_INFINITY';
+ else
+ return "$value";
+ }
+ else if(is_object($value))
+ return self::encode(get_object_vars($this->data),$toMap);
+ else if($value===null)
+ return 'null';
+ else
+ return '';
+ }
+
+ public static function encodeJSON($value)
+ {
+ Prado::using('System.Web.Javascripts.TJSON');
+ return TJSON::encode($value);
+ }
+
+ public static function decodeJSON($value)
+ {
+ Prado::using('System.Web.Javascripts.TJSON');
+ return TJSON::decode($value);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/framework/Web/THttpUtility.php b/framework/Web/THttpUtility.php
index 6aadb65d..34f1a8a4 100644
--- a/framework/Web/THttpUtility.php
+++ b/framework/Web/THttpUtility.php
@@ -38,26 +38,6 @@ class THttpUtility
{
self::$_entityTable=array_flip(get_html_translation_table(HTML_ENTITIES,ENT_QUOTES));
}
-
- public static function quoteJavaScriptString($js,$forUrl=false)
- {
- if($forUrl)
- return strtr($js,array('%'=>'%25',"\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\'));
- else
- return strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\'));
- }
-
- public static function trimJavaScriptString($js)
- {
- if($js!=='' && $js!==null)
- {
- $js=trim($js);
- if(($pos=strpos($js,'javascript:'))===0)
- $js=substr($js,11);
- $js=rtrim($js,';').';';
- }
- return $js;
- }
}
?> \ No newline at end of file
diff --git a/framework/Web/UI/TClientScriptManager.php b/framework/Web/UI/TClientScriptManager.php
index 39c23b99..c4ac316a 100644
--- a/framework/Web/UI/TClientScriptManager.php
+++ b/framework/Web/UI/TClientScriptManager.php
@@ -22,40 +22,91 @@ Prado::using('System.Web.Javascripts.*');
*/
class TClientScriptManager extends TApplicationComponent
{
- const SCRIPT_DIR='Web/Javascripts/js';
- //const POSTBACK_FUNC='Prado.doPostBack';
+ /**
+ * directory containing Prado javascript files
+ */
+ const SCRIPT_PATH='Web/Javascripts/js';
+ /**
+ * the PHP script for loading Prado javascript files
+ */
+ const SCRIPT_LOADER='clientscripts.php';
+ /**
+ * @var TPage page who owns this manager
+ */
private $_page;
+ /**
+ * @var array registered hidden fields, indexed by hidden field names
+ */
private $_hiddenFields=array();
private $_beginScripts=array();
private $_endScripts=array();
private $_scriptFiles=array();
+ private $_onSubmitStatements=array();
+ private $_arrayDeclares=array();
+ private $_expandoAttributes=array();
- //private $_headScriptFiles=array();
- //private $_headScripts=array();
+ private $_headScriptFiles=array();
+ private $_headScripts=array();
private $_styleSheetFiles=array();
private $_styleSheets=array();
- private $_client;
-
- /*private $_onSubmitStatements=array();
- private $_arrayDeclares=array();
- private $_expandoAttributes=array();
- private $_postBackScriptRegistered=false;
- private $_focusScriptRegistered=false;
- private $_scrollScriptRegistered=false;
- */
-
- private $_publishedScriptFiles=array();
+ private $_registeredPradoScripts=array();
+ private $_registeredPradoFiles=array();
+ /**
+ * Client-side javascript library dependencies
+ * @var array
+ */
+ private static $_pradoScripts=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'),
+ 'colorpicker' => array('prado', 'colorpicker')
+ );
+ /**
+ * Constructor.
+ * @param TPage page that owns this client script manager
+ */
public function __construct(TPage $owner)
{
$this->_page=$owner;
- $this->_client = new TClientScript($this);
}
+ /**
+ * Registers Prado scripts by library name.
+ * The script files will be published.
+ * @param string script library name. Valid names include
+ * 'prado', 'effects', 'ajax', 'validator', 'logger',
+ * 'datepicker', 'rico', 'colorpicker'.
+ */
+ public function registerPradoScript($name)
+ {
+ if(!isset($this->_registeredPradoScripts[$name]))
+ {
+ $this->_registeredPradoScripts[$name]=true;
+ if(!isset(self::$_pradoScripts[$name]))
+ throw new TInvalidOperationException('csmanager_pradoscript_invalid',$name);
+ $basePath=Prado::getFrameworkPath().'/'.self::SCRIPT_PATH;
+ foreach(self::$_pradoScripts[$name] as $script)
+ {
+ $this->publishFilePath($basePath.'/'.$script.'.js');
+ $this->_registeredPradoFiles[$script]=true;
+ }
+ $scriptLoader=$basePath.'/'.self::SCRIPT_LOADER;
+ $url=$this->publishFilePath($scriptLoader);
+ $url.='?js='.implode(',',array_keys($this->_registeredPradoFiles));
+ if($this->getApplication()->getMode()===TApplication::STATE_DEBUG)
+ $url.='&__nocache';
+ $this->registerScriptFile('prado:pradoscripts',$url);
+ }
+ }
public function registerPostBackControl($control,$namespace='Prado.WebUI')
{
@@ -67,7 +118,7 @@ class TClientScriptManager extends TApplicationComponent
$this->registerHiddenField(TPage::FIELD_POSTBACK_TARGET,'');
$this->registerHiddenField(TPage::FIELD_POSTBACK_PARAMETER,'');
- $this->registerClientScript('prado');
+ $this->registerPradoScript('prado');
}
protected function getPostBackOptions($control)
@@ -77,8 +128,7 @@ class TClientScriptManager extends TApplicationComponent
$postback['ID'] = $control->getClientID();
if(!isset($postback['FormID']))
$postback['FormID'] = $this->_page->getForm()->getClientID();
- $options = new TJavascriptSerializer($postback);
- return $options->toJavascript();
+ return TJavaScript::encode($postback);
}
/**
@@ -89,12 +139,9 @@ class TClientScriptManager extends TApplicationComponent
*/
public function registerDefaultButton($panel, $button)
{
- $serializer = new TJavascriptSerializer(
- $this->getDefaultButtonOptions($panel, $button));
- $options = $serializer->toJavascript();
+ $options = TJavaScript::encode($this->getDefaultButtonOptions($panel, $button));
$code = "new Prado.WebUI.DefaultButton($options);";
- $scripts = $this->_page->getClientScript();
- $scripts->registerEndScript("prado:".$panel->getClientID(), $code);
+ $this->registerEndScript("prado:".$panel->getClientID(), $code);
}
/**
@@ -108,63 +155,6 @@ class TClientScriptManager extends TApplicationComponent
return $options;
}
-
- /**
- * Register client scripts.
- */
- public function registerClientScript($script)
- {
- static $scripts = array();
- $scripts = array_unique(array_merge($scripts,
- TClientScript::getScripts($script)));
-
- $this->publishClientScriptAssets($scripts);
-
- //create the client script url
- $url = $this->publishClientScriptCompressorAsset();
- $url .= '?js='.implode(',', $scripts);
- if($this->getApplication()->getMode() == TApplication::STATE_DEBUG)
- $url .= '&__nocache';
- $this->registerScriptFile('prado:gzipscripts', $url);
- }
-
- /**
- * Publish each individual javascript file.
- */
- protected function publishClientScriptAssets($scripts)
- {
- foreach($scripts as $lib)
- {
- if(!isset($this->_publishedScriptFiles[$lib]))
- {
- $base = Prado::getFrameworkPath();
- $clientScripts = self::SCRIPT_DIR;
- $file = "{$base}/{$clientScripts}/{$lib}.js";
- $this->publishFilePath($file);
- $this->_publishedScriptFiles[$lib] = true;
- }
- }
- }
-
- /**
- * @return string URL of the compressor asset script.
- */
- protected function publishClientScriptCompressorAsset()
- {
- $scriptFile = 'clientscripts.php';
- if(isset($this->_publishedScriptFiles[$scriptFile]))
- return $this->_publishedScriptFiles[$scriptFile];
- else
- {
- $base = Prado::getFrameworkPath();
- $clientScripts = self::SCRIPT_DIR;
- $file = "{$base}/{$clientScripts}/{$scriptFile}";
- $url= $this->publishFilePath($file);
- $this->_publishedScriptFiles[$scriptFile] = $url;
- return $url;
- }
- }
-
/* protected function registerPostBackScript()
{
if(!$this->_postBackScriptRegistered)
@@ -449,18 +439,6 @@ class TClientScriptManager extends TApplicationComponent
$this->registerEndScript($key, $script);
}
*/
-
-
- /*
- private void EnsureEventValidationFieldLoaded();
- internal string GetEventValidationFieldValue();
- public string GetWebResourceUrl(Type type, string resourceName);
- public void RegisterClientScriptResource(Type type, string resourceName);
- internal void RegisterDefaultButtonScript(Control button, $writer, bool useAddAttribute);
- public function SaveEventValidationField();
- public void ValidateEvent(string uniqueId, string argument);
- public function getCallbackEventReference()
- */
}
/**
diff --git a/framework/Web/UI/WebControls/TBaseValidator.php b/framework/Web/UI/WebControls/TBaseValidator.php
index 97ba1a8b..46f031ca 100644
--- a/framework/Web/UI/WebControls/TBaseValidator.php
+++ b/framework/Web/UI/WebControls/TBaseValidator.php
@@ -156,7 +156,7 @@ abstract class TBaseValidator extends TLabel implements IValidator
$scriptKey = "TBaseValidator:$formID";
if($this->getEnableClientScript() && !$scripts->isEndScriptRegistered($scriptKey))
{
- $scripts->registerClientScript('validator');
+ $scripts->registerPradoScript('validator');
$scripts->registerEndScript($scriptKey, "Prado.Validation.AddForm('$formID');");
}
if($this->getEnableClientScript())
@@ -194,8 +194,7 @@ abstract class TBaseValidator extends TLabel implements IValidator
$class = get_class($this);
$scriptKey = "prado:".$this->getClientID();
$scripts = $this->getPage()->getClientScript();
- $serializer = new TJavascriptSerializer($this->getClientScriptOptions());
- $options = $serializer->toJavascript();
+ $options = TJavaScript::encode($this->getClientScriptOptions());
$js = "new Prado.Validation(Prado.Validation.{$class}, {$options});";
$scripts->registerEndScript($scriptKey, $js);
}
diff --git a/framework/Web/UI/WebControls/TColorPicker.php b/framework/Web/UI/WebControls/TColorPicker.php
index c0cc3ae7..fda6bced 100644
--- a/framework/Web/UI/WebControls/TColorPicker.php
+++ b/framework/Web/UI/WebControls/TColorPicker.php
@@ -168,8 +168,7 @@ class TColorPicker extends TTextBox
}
$imgs['button.gif'] = $list['button.gif'];
$imgs['background.png'] = $list['background.png'];
- $serializer = new TJavascriptSerializer($imgs);
- $options = $serializer->toJavascript();
+ $options = TJavaScript::encode($imgs);
$code = "Prado.WebUI.TColorPicker.UIImages = {$options};";
$cs->registerEndScript($key, $code);
return $list;
@@ -186,9 +185,8 @@ class TColorPicker extends TTextBox
parent::addAttributesToRender($writer);
$writer->addAttribute('id',$this->getClientID());
$scripts = $this->getPage()->getClientScript();
- $scripts->registerClientScript("colorpicker");
- $serializer = new TJavascriptSerializer($this->getColorPickerOptions());
- $options = $serializer->toJavascript();
+ $scripts->registerPradoScript("colorpicker");
+ $options = TJavaScript::encode($this->getColorPickerOptions());
$id = $this->getClientID();
$code = "Event.OnLoad(function(){ new Prado.WebUI.TColorPicker($options); });";
$scripts->registerEndScript("prado:$id", $code);
diff --git a/framework/Web/UI/WebControls/TDatePicker.php b/framework/Web/UI/WebControls/TDatePicker.php
index fd280938..a70e25b3 100644
--- a/framework/Web/UI/WebControls/TDatePicker.php
+++ b/framework/Web/UI/WebControls/TDatePicker.php
@@ -228,7 +228,8 @@ class TDatePicker extends TTextBox
{
$options['Format'] = $this->getDateFormat();
$options['FirstDayOfWeek'] = $this->getFirstDayOfWeek();
- $options['ClassName'] = $this->getCssClass();
+ if(($cssClass=$this->getCssClass())!=='')
+ $options['ClassName'] = $cssClass;
$options['FromYear'] = $this->getFromYear();
$options['UpToYear'] = $this->getUpToYear();
if($this->getMode()!=='Basic')
@@ -252,10 +253,8 @@ class TDatePicker extends TTextBox
Prado::using('System.I18N.core.DateTimeFormatInfo');
$info = Prado::createComponent('System.I18N.core.CultureInfo', $culture);
$date = $info->getDateTimeFormat();
- $serializer = new TJavascriptSerializer($date->getMonthNames());
- $options['MonthNames'] = $serializer->toList();
- $serializer = new TJavascriptSerializer($date->getAbbreviatedDayNames());
- $options['ShortWeekDayNames'] = $serializer->toList();
+ $options['MonthNames'] = TJavaScript::encode($date->getMonthNames(),false);
+ $options['ShortWeekDayNames'] = TJavaScript::encode($date->getAbbreviatedDayNames(),false);
return $options;
}
@@ -367,9 +366,8 @@ class TDatePicker extends TTextBox
if($this->getShowCalendar())
{
$scripts = $this->getPage()->getClientScript();
- $scripts->registerClientScript("datepicker");
- $serializer = new TJavascriptSerializer($this->getDatePickerOptions());
- $options = $serializer->toJavascript();
+ $scripts->registerPradoScript("datepicker");
+ $options = TJavaScript::encode($this->getDatePickerOptions());
$id = $this->getClientID();
$code = "Event.OnLoad(function(){ new Prado.WebUI.TDatePicker('$id', $options); });";
$scripts->registerEndScript("prado:$id", $code);
diff --git a/framework/Web/UI/WebControls/THtmlArea.php b/framework/Web/UI/WebControls/THtmlArea.php
index 1801e739..70788e2d 100644
--- a/framework/Web/UI/WebControls/THtmlArea.php
+++ b/framework/Web/UI/WebControls/THtmlArea.php
@@ -262,8 +262,7 @@ class THtmlArea extends TTextBox
$scripts = $this->getPage()->getClientScript();
if(!$scripts->isScriptFileRegistered('prado:THtmlArea'))
$scripts->registerScriptFile('prado:THtmlArea', $this->getScriptUrl());
- $serializer = new TJavascriptSerializer($this->getEditorOptions());
- $options = $serializer->toJavascript(true);
+ $options = TJavaScript::encode($this->getEditorOptions());
$script = "if(tinyMCE){ tinyMCE.init($options); }";
$scripts->registerEndScript('THtmlArea'.$this->ClientID,$script);
}
diff --git a/framework/Web/UI/WebControls/TJavascriptLogger.php b/framework/Web/UI/WebControls/TJavascriptLogger.php
index fa28d2b0..c38f7bc4 100644
--- a/framework/Web/UI/WebControls/TJavascriptLogger.php
+++ b/framework/Web/UI/WebControls/TJavascriptLogger.php
@@ -54,7 +54,7 @@ class TJavascriptLogger extends TWebControl
*/
public function renderContents($writer)
{
- $this->Page->ClientScript->registerClientScript('logger');
+ $this->Page->ClientScript->registerPradoScript('logger');
$info = '(<a href="http://gleepglop.com/javascripts/logger/" target="_blank">more info</a>).';
$usage = 'Press ALT-D (Or CTRL-D on OS X) to toggle the javascript log console';
$writer->write("{$usage} {$info}");
diff --git a/framework/Web/UI/WebControls/TRatingList.php b/framework/Web/UI/WebControls/TRatingList.php
index 4302c3b3..006391f7 100644
--- a/framework/Web/UI/WebControls/TRatingList.php
+++ b/framework/Web/UI/WebControls/TRatingList.php
@@ -106,9 +106,8 @@ class TRatingList extends TRadioButtonList
$this->publishRatingListAssets();
$id = $this->getClientID();
$scripts = $this->getPage()->getClientScript();
- $scripts->registerClientScript('prado');
- $serializer = new TJavascriptSerializer($this->getRatingClientOptions());
- $options = $serializer->toJavascript();
+ $scripts->registerPradoScript('prado');
+ $options = TJavaScript::encode($this->getRatingClientOptions());
$code = "new Prado.WebUI.TRatingList($options);";
$scripts->registerEndScript("prado:$id", $code);
}
diff --git a/framework/Web/UI/WebControls/TTextHighlighter.php b/framework/Web/UI/WebControls/TTextHighlighter.php
index 4eec7de1..cedc807f 100644
--- a/framework/Web/UI/WebControls/TTextHighlighter.php
+++ b/framework/Web/UI/WebControls/TTextHighlighter.php
@@ -85,7 +85,7 @@ class TTextHighlighter extends TWebControl
{
parent::onPreRender($writer);
$this->registerHighlightStyleSheet();
- $this->getPage()->getClientScript()->registerClientScript('prado');
+ $this->getPage()->getClientScript()->registerPradoScript('prado');
}
/**
diff --git a/framework/Web/UI/WebControls/TValidationSummary.php b/framework/Web/UI/WebControls/TValidationSummary.php
index 027a2cee..e7115c99 100644
--- a/framework/Web/UI/WebControls/TValidationSummary.php
+++ b/framework/Web/UI/WebControls/TValidationSummary.php
@@ -182,8 +182,7 @@ class TValidationSummary extends TWebControl
{
if(!$this->getEnabled(true) || !$this->getEnableClientScript())
return;
- $serializer = new TJavascriptSerializer($this->getClientScriptOptions());
- $options = $serializer->toJavascript();
+ $options=TJavaScript::encode($this->getClientScriptOptions());
$script = "new Prado.Validation.Summary({$options});";
$this->getPage()->getClientScript()->registerEndScript($this->getClientID(), $script);
}