summaryrefslogtreecommitdiff
path: root/framework/Web/Javascripts
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/Javascripts
parent64a5d4b6d3adaeab325d0e72d6c6c7624d861807 (diff)
client script reorganization.
Diffstat (limited to 'framework/Web/Javascripts')
-rw-r--r--framework/Web/Javascripts/TJavaScript.php110
1 files changed, 110 insertions, 0 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