Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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