| [ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * TJavaScript class file 4 * 5 * @author Wei Zhuo<weizhuo[at]gmail[dot]com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TJavaScript.php 1397 2006-09-07 07:55:53Z wei $ 10 * @package System.Web.Javascripts 11 */ 12 13 /** 14 * TJavaScript class. 15 * 16 * TJavaScript is a utility class containing commonly-used javascript-related 17 * functions. 18 * 19 * @author Wei Zhuo<weizhuo[at]gmail[dot]com> 20 * @version $Id: TJavaScript.php 1397 2006-09-07 07:55:53Z wei $ 21 * @package System.Web.Javascripts 22 * @since 3.0 23 */ 24 class TJavaScript 25 { 26 /** 27 * Renders a list of javascript files 28 * @param array URLs to the javascript files 29 * @return string rendering result 30 */ 31 public static function renderScriptFiles($files) 32 { 33 $str=''; 34 foreach($files as $file) 35 $str.= self::renderScriptFile($file); 36 return $str; 37 } 38 39 /** 40 * Renders a javascript file 41 * @param string URL to the javascript file 42 * @return string rendering result 43 */ 44 public static function renderScriptFile($file) 45 { 46 return '<script type="text/javascript" src="'.THttpUtility::htmlEncode($file)."\"></script>\n"; 47 } 48 49 /** 50 * Renders a list of javascript blocks 51 * @param array javascript blocks 52 * @return string rendering result 53 */ 54 public static function renderScriptBlocks($scripts) 55 { 56 if(count($scripts)) 57 return "<script type=\"text/javascript\">\n/*<![CDATA[*/\n".implode("\n",$scripts)."\n/*]]>*/\n</script>\n"; 58 else 59 return ''; 60 } 61 62 /** 63 * Renders javascript block 64 * @param string javascript block 65 * @return string rendering result 66 */ 67 public static function renderScriptBlock($script) 68 { 69 return "<script type=\"text/javascript\">\n/*<![CDATA[*/\n{$script}\n/*]]>*/\n</script>\n"; 70 } 71 72 /** 73 * Quotes a javascript string. 74 * After processing, the string can be safely enclosed within a pair of 75 * quotation marks and serve as a javascript string. 76 * @param string string to be quoted 77 * @param boolean whether this string is used as a URL 78 * @return string the quoted string 79 */ 80 public static function quoteString($js,$forUrl=false) 81 { 82 if($forUrl) 83 return strtr($js,array('%'=>'%25',"\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\')); 84 else 85 return strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\')); 86 } 87 88 /** 89 * @return string considers the string as raw javascript function code 90 */ 91 public static function quoteFunction($js) 92 { 93 if(self::isFunction($js)) 94 return $js; 95 else 96 return 'javascript:'.$js; 97 } 98 99 /** 100 * @return boolean true if string is raw javascript function code, i.e., if 101 * the string begins with <tt>javascript:</tt> 102 */ 103 public static function isFunction($js) 104 { 105 return preg_match('/^\s*javascript:/i', $js); 106 } 107 108 /** 109 * Encodes a PHP variable into javascript representation. 110 * 111 * Example: 112 * <code> 113 * $options['onLoading'] = "doit"; 114 * $options['onComplete'] = "more"; 115 * echo TJavaScript::encode($options); 116 * //expects the following javascript code 117 * // {'onLoading':'doit','onComplete':'more'} 118 * </code> 119 * 120 * For higher complexity data structures use {@link jsonEncode} and {@link jsonDecode} 121 * to serialize and unserialize. 122 * 123 * Note: strings begining with <tt>javascript:</tt> will be considered as 124 * raw javascript code and no encoding of that string will be enforced. 125 * 126 * @param mixed PHP variable to be encoded 127 * @param boolean whether the output is a map or a list. 128 * @return string the encoded string 129 */ 130 public static function encode($value,$toMap=true) 131 { 132 if(is_string($value)) 133 { 134 if(($n=strlen($value))>2) 135 { 136 $first=$value[0]; 137 $last=$value[$n-1]; 138 if(($first==='[' && $last===']') || ($first==='{' && $last==='}')) 139 return $value; 140 } 141 // if string begins with javascript: return the raw string minus the prefix 142 if(self::isFunction($value)) 143 return preg_replace('/^\s*javascript:/', '', $value); 144 else 145 return "'".self::quoteString($value)."'"; 146 } 147 else if(is_bool($value)) 148 return $value?'true':'false'; 149 else if(is_array($value)) 150 { 151 $results=array(); 152 if($toMap) 153 { 154 foreach($value as $k=>$v) 155 if($v!=='') 156 $results[]="'{$k}':".self::encode($v,$toMap); 157 return '{'.implode(',',$results).'}'; 158 } 159 else 160 { 161 foreach($value as $k=>$v) 162 if($v!=='') 163 $results[]=self::encode($v,$toMap); 164 return '['.implode(',',$results).']'; 165 } 166 } 167 else if(is_integer($value)) 168 return "$value"; 169 else if(is_float($value)) 170 { 171 if($value===-INF) 172 return 'Number.NEGATIVE_INFINITY'; 173 else if($value===INF) 174 return 'Number.POSITIVE_INFINITY'; 175 else 176 return "$value"; 177 } 178 else if(is_object($value)) 179 return self::encode(get_object_vars($this->data),$toMap); 180 else if($value===null) 181 return 'null'; 182 else 183 return ''; 184 } 185 186 /** 187 * Encodes a PHP variable into javascript string. 188 * This method invokes {@TJSON} utility class to perform the encoding. 189 * @param mixed variable to be encoded 190 * @return string encoded string 191 */ 192 public static function jsonEncode($value) 193 { 194 Prado::using('System.Web.Javascripts.TJSON'); 195 return TJSON::encode($value); 196 } 197 198 /** 199 * Decodes a javascript string into PHP variable. 200 * This method invokes {@TJSON} utility class to perform the decoding. 201 * @param string string to be decoded 202 * @return mixed decoded variable 203 */ 204 public static function jsonDecode($value) 205 { 206 Prado::using('System.Web.Javascripts.TJSON'); 207 return TJSON::decode($value); 208 } 209 } 210 211 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |