From 145c4aef09e278d29bfd8e9858e114ca097e5901 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 18 Feb 2006 07:24:51 +0000 Subject: client script reorganization. --- framework/Web/Javascripts/TJavaScript.php | 110 ++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 framework/Web/Javascripts/TJavaScript.php (limited to 'framework/Web/Javascripts/TJavaScript.php') 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 @@ + + * $options['onLoading'] = "doit"; + * $options['onComplete'] = "more"; + * $js = new TJavascriptSerializer($options); + * echo $js->toMap(); + * //expects the following javascript code + * // {'onLoading':'doit','onComplete':'more'} + * + * + * For higher complexity data structures use TJSON to serialize and unserialize. + * + * Namespace: System.Web.UI + * + * @author Wei Zhuo + * @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 -- cgit v1.2.3