Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: javascript.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * Javascript Helper class file.
5
 *
6
 * PHP versions 4 and 5
7
 *
8
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
9
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
10
 *
11
 * Licensed under The MIT License
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @filesource
15
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
16
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
17
 * @package       cake
18
 * @subpackage    cake.cake.libs.view.helpers
19
 * @since         CakePHP(tm) v 0.10.0.1076
20
 * @version       $Revision: 7945 $
21
 * @modifiedby    $LastChangedBy: gwoo $
22
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
23
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
24
 */
25
/**
26
 * Javascript Helper class for easy use of JavaScript.
27
 *
28
 * JavascriptHelper encloses all methods needed while working with JavaScript.
29
 *
30
 * @package       cake
31
 * @subpackage    cake.cake.libs.view.helpers
32
 */
33
class JavascriptHelper extends AppHelper {
34
/**
35
 * Determines whether native JSON extension is used for encoding.  Set by object constructor.
36
 *
37
 * @var boolean
38
 * @access public
39
 */
40
	var $useNative = false;
41
/**
42
 * If true, automatically writes events to the end of a script or to an external JavaScript file
43
 * at the end of page execution
44
 *
45
 * @var boolean
46
 * @access public
47
 */
48
	var $enabled = true;
49
/**
50
 * Indicates whether <script /> blocks should be written 'safely,' i.e. wrapped in CDATA blocks
51
 *
52
 * @var boolean
53
 * @access public
54
 */
55
	var $safe = false;
56
/**
57
 * HTML tags used by this helper.
58
 *
59
 * @var array
60
 * @access public
61
 */
62
	var $tags = array(
63
		'javascriptblock' => '<script type="text/javascript">%s</script>',
64
		'javascriptstart' => '<script type="text/javascript">',
65
		'javascriptlink' => '<script type="text/javascript" src="%s"></script>',
66
		'javascriptend' => '</script>'
67
	);
68
/**
69
 * Holds options passed to codeBlock(), saved for when block is dumped to output
70
 *
71
 * @var array
72
 * @access protected
73
 * @see JavascriptHelper::codeBlock()
74
 */
75
	var $_blockOptions = array();
76
/**
77
 * Caches events written by event() for output at the end of page execution
78
 *
79
 * @var array
80
 * @access protected
81
 * @see JavascriptHelper::event()
82
 */
83
	var $_cachedEvents = array();
84
/**
85
 * Indicates whether generated events should be cached for later output (can be written at the
86
 * end of the page, in the <head />, or to an external file).
87
 *
88
 * @var boolean
89
 * @access protected
90
 * @see JavascriptHelper::event()
91
 * @see JavascriptHelper::writeEvents()
92
 */
93
	var $_cacheEvents = false;
94
/**
95
 * Indicates whether cached events should be written to an external file
96
 *
97
 * @var boolean
98
 * @access protected
99
 * @see JavascriptHelper::event()
100
 * @see JavascriptHelper::writeEvents()
101
 */
102
	var $_cacheToFile = false;
103
/**
104
 * Indicates whether *all* generated JavaScript should be cached for later output
105
 *
106
 * @var boolean
107
 * @access protected
108
 * @see JavascriptHelper::codeBlock()
109
 * @see JavascriptHelper::blockEnd()
110
 */
111
	var $_cacheAll = false;
112
/**
113
 * Contains event rules attached with CSS selectors.  Used with the event:Selectors JavaScript
114
 * library.
115
 *
116
 * @var array
117
 * @access protected
118
 * @see JavascriptHelper::event()
119
 * @link          http://alternateidea.com/event-selectors/
120
 */
121
	var $_rules = array();
122
/**
123
 * @var string
124
 * @access private
125
 */
126
	var $__scriptBuffer = null;
127
/**
128
 * Constructor. Checks for presence of native PHP JSON extension to use for object encoding
129
 *
130
 * @access public
131
 */
132
	function __construct($options = array()) {
133
		if (!empty($options)) {
134
			foreach ($options as $key => $val) {
135
				if (is_numeric($key)) {
136
					$key = $val;
137
					$val = true;
138
				}
139
				switch ($key) {
140
					case 'cache':
141
 
142
					break;
143
					case 'safe':
144
						$this->safe = $val;
145
					break;
146
				}
147
			}
148
		}
149
		$this->useNative = function_exists('json_encode');
150
		return parent::__construct($options);
151
	}
152
/**
153
 * Returns a JavaScript script tag.
154
 *
155
 * @param string $script The JavaScript to be wrapped in SCRIPT tags.
156
 * @param array $options Set of options:
157
 *             - allowCache: boolean, designates whether this block is cacheable using the
158
 *               current cache settings.
159
 *             - safe: boolean, whether this block should be wrapped in CDATA tags.  Defaults
160
 *               to helper's object configuration.
161
 *             - inline: whether the block should be printed inline, or written
162
 *               to cached for later output (i.e. $scripts_for_layout).
163
 * @return string The full SCRIPT element, with the JavaScript inside it, or null,
164
 *                if 'inline' is set to false.
165
 */
166
	function codeBlock($script = null, $options = array()) {
167
		if (!empty($options) && !is_array($options)) {
168
			$options = array('allowCache' => $options);
169
		} elseif (empty($options)) {
170
			$options = array();
171
		}
172
		$defaultOptions = array('allowCache' => true, 'safe' => true, 'inline' => true);
173
		$options = array_merge($defaultOptions, compact('safe'), $options);
174
 
175
		if ($this->_cacheEvents && $this->_cacheAll && $options['allowCache'] && $script !== null) {
176
			$this->_cachedEvents[] = $script;
177
		} else {
178
			$block = ($script !== null);
179
			$safe = ($options['safe'] || $this->safe);
180
			if ($safe && !($this->_cacheAll && $options['allowCache'])) {
181
				$script  = "\n" . '//<![CDATA[' . "\n" . $script;
182
				if ($block) {
183
					$script .= "\n" . '//]]>' . "\n";
184
				}
185
			}
186
 
187
			if ($script === null) {
188
				$this->__scriptBuffer = @ob_get_contents();
189
				$this->_blockOptions = $options;
190
				$this->inBlock = true;
191
				@ob_end_clean();
192
				ob_start();
193
				return null;
194
			} else if (!$block) {
195
				$this->_blockOptions = $options;
196
			}
197
 
198
			if ($options['inline']) {
199
				if ($block) {
200
					return sprintf($this->tags['javascriptblock'], $script);
201
				} else {
202
					$safe = ($safe ? "\n" . '//<![CDATA[' . "\n" : '');
203
					return $this->tags['javascriptstart'] . $safe;
204
				}
205
			} elseif ($block) {
206
				$view =& ClassRegistry::getObject('view');
207
				$view->addScript(sprintf($this->tags['javascriptblock'], $script));
208
			}
209
		}
210
	}
211
/**
212
 * Ends a block of cached JavaScript code
213
 *
214
 * @return mixed
215
 */
216
	function blockEnd() {
217
		$script = @ob_get_contents();
218
		@ob_end_clean();
219
		ob_start();
220
		echo $this->__scriptBuffer;
221
		$this->__scriptBuffer = null;
222
		$options = $this->_blockOptions;
223
		$safe = ((isset($options['safe']) && $options['safe']) || $this->safe);
224
		$this->_blockOptions = array();
225
		$this->inBlock = false;
226
 
227
		if (isset($options['inline']) && !$options['inline']) {
228
			$view =& ClassRegistry::getObject('view');
229
			$view->addScript(sprintf($this->tags['javascriptblock'], $script));
230
		}
231
 
232
		if (!empty($script) && $this->_cacheAll && $options['allowCache']) {
233
			$this->_cachedEvents[] = $script;
234
			return null;
235
		}
236
		return ife($safe, "\n" . '//]]>' . "\n", '').$this->tags['javascriptend'];
237
	}
238
/**
239
 * Returns a JavaScript include tag (SCRIPT element).  If the filename is prefixed with "/",
240
 * the path will be relative to the base path of your application.  Otherwise, the path will
241
 * be relative to your JavaScript path, usually webroot/js.
242
 *
243
 * @param  mixed  $url String URL to JavaScript file, or an array of URLs.
244
 * @param  boolean $inline If true, the <script /> tag will be printed inline,
245
 *                         otherwise it will be printed in the <head />, using $scripts_for_layout
246
 * @see JS_URL
247
 * @return string
248
 */
249
	function link($url, $inline = true) {
250
		if (is_array($url)) {
251
			$out = '';
252
			foreach ($url as $i) {
253
				$out .= "\n\t" . $this->link($i, $inline);
254
			}
255
			if ($inline)  {
256
				return $out . "\n";
257
			}
258
			return;
259
		}
260
 
261
		if (strpos($url, '://') === false) {
262
			if ($url[0] !== '/') {
263
				$url = JS_URL . $url;
264
			}
265
			if (strpos($url, '?') === false) {
266
				if (strpos($url, '.js') === false) {
267
					$url .= '.js';
268
				}
269
			}
270
 
271
			$url = $this->webroot($url);
272
			$timestampEnabled = (
273
				(Configure::read('Asset.timestamp') === true && Configure::read() > 0) ||
274
				Configure::read('Asset.timestamp') === 'force'
275
			);
276
 
277
			if (strpos($url, '?') === false && $timestampEnabled) {
278
				$url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $url));
279
			}
280
 
281
			if (Configure::read('Asset.filter.js')) {
282
				$url = str_replace(JS_URL, 'cjs/', $url);
283
			}
284
		}
285
		$out = $this->output(sprintf($this->tags['javascriptlink'], $url));
286
 
287
		if ($inline) {
288
			return $out;
289
		} else {
290
			$view =& ClassRegistry::getObject('view');
291
			$view->addScript($out);
292
		}
293
	}
294
/**
295
 * Escape carriage returns and single and double quotes for JavaScript segments.
296
 *
297
 * @param string $script string that might have javascript elements
298
 * @return string escaped string
299
 */
300
	function escapeScript($script) {
301
		$script = str_replace(array("\r\n", "\n", "\r"), '\n', $script);
302
		$script = str_replace(array('"', "'"), array('\"', "\\'"), $script);
303
		return $script;
304
	}
305
/**
306
 * Escape a string to be JavaScript friendly.
307
 *
308
 * List of escaped ellements:
309
 *	+ "\r\n" => '\n'
310
 *	+ "\r" => '\n'
311
 *	+ "\n" => '\n'
312
 *	+ '"' => '\"'
313
 *	+ "'" => "\\'"
314
 *
315
 * @param  string $script String that needs to get escaped.
316
 * @return string Escaped string.
317
 */
318
	function escapeString($string) {
319
		$escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'");
320
		return str_replace(array_keys($escape), array_values($escape), $string);
321
	}
322
/**
323
 * Attach an event to an element. Used with the Prototype library.
324
 *
325
 * @param string $object Object to be observed
326
 * @param string $event event to observe
327
 * @param string $observer function to call
328
 * @param array $options Set options: useCapture, allowCache, safe
329
 * @return boolean true on success
330
 */
331
	function event($object, $event, $observer = null, $options = array()) {
332
		if (!empty($options) && !is_array($options)) {
333
			$options = array('useCapture' => $options);
334
		} else if (empty($options)) {
335
			$options = array();
336
		}
337
 
338
		$defaultOptions = array('useCapture' => false);
339
		$options = array_merge($defaultOptions, $options);
340
 
341
		if ($options['useCapture'] == true) {
342
			$options['useCapture'] = 'true';
343
		} else {
344
			$options['useCapture'] = 'false';
345
		}
346
		$isObject = (
347
			strpos($object, 'window') !== false || strpos($object, 'document') !== false ||
348
			strpos($object, '$(') !== false || strpos($object, '"') !== false ||
349
			strpos($object, '\'') !== false
350
		);
351
 
352
		if ($isObject) {
353
			$b = "Event.observe({$object}, '{$event}', function(event) { {$observer} }, ";
354
			$b .= "{$options['useCapture']});";
355
		} elseif ($object[0] == '\'') {
356
			$b = "Event.observe(" . substr($object, 1) . ", '{$event}', function(event) { ";
357
			$b .= "{$observer} }, {$options['useCapture']});";
358
		} else {
359
			$chars = array('#', ' ', ', ', '.', ':');
360
			$found = false;
361
			foreach ($chars as $char) {
362
				if (strpos($object, $char) !== false) {
363
					$found = true;
364
					break;
365
				}
366
			}
367
			if ($found) {
368
				$this->_rules[$object] = $event;
369
			} else {
370
				$b = "Event.observe(\$('{$object}'), '{$event}', function(event) { ";
371
				$b .= "{$observer} }, {$options['useCapture']});";
372
			}
373
		}
374
 
375
		if (isset($b) && !empty($b)) {
376
			if ($this->_cacheEvents === true) {
377
				$this->_cachedEvents[] = $b;
378
				return;
379
			} else {
380
				return $this->codeBlock($b, array_diff_key($options, $defaultOptions));
381
			}
382
		}
383
	}
384
/**
385
 * Cache JavaScript events created with event()
386
 *
387
 * @param boolean $file If true, code will be written to a file
388
 * @param boolean $all If true, all code written with JavascriptHelper will be sent to a file
389
 * @return null
390
 */
391
	function cacheEvents($file = false, $all = false) {
392
		$this->_cacheEvents = true;
393
		$this->_cacheToFile = $file;
394
		$this->_cacheAll = $all;
395
	}
396
/**
397
 * Gets (and clears) the current JavaScript event cache
398
 *
399
 * @param boolean $clear
400
 * @return string
401
 */
402
	function getCache($clear = true) {
403
		$out = '';
404
		$rules = array();
405
 
406
		if (!empty($this->_rules)) {
407
			foreach ($this->_rules as $sel => $event) {
408
				$rules[] = "\t'{$sel}': function(element, event) {\n\t\t{$event}\n\t}";
409
			}
410
		}
411
		$data = implode("\n", $this->_cachedEvents);
412
 
413
		if (!empty($rules)) {
414
			$data .= "\nvar Rules = {\n" . implode(",\n\n", $rules) . "\n}";
415
			$data .= "\nEventSelectors.start(Rules);\n";
416
		}
417
		if ($clear) {
418
			$this->_rules = array();
419
			$this->_cacheEvents = false;
420
			$this->_cachedEvents = array();
421
		}
422
		return $data;
423
	}
424
/**
425
 * Write cached JavaScript events
426
 *
427
 * @param boolean $inline If true, returns JavaScript event code.  Otherwise it is added to the
428
 *                        output of $scripts_for_layout in the layout.
429
 * @param array $options Set options for codeBlock
430
 * @return string
431
 */
432
	function writeEvents($inline = true, $options = array()) {
433
		$out = '';
434
		$rules = array();
435
 
436
		if (!$this->_cacheEvents) {
437
			return;
438
		}
439
		$data = $this->getCache();
440
 
441
		if (empty($data)) {
442
			return;
443
		}
444
 
445
		if ($this->_cacheToFile) {
446
			$filename = md5($data);
447
			if (!file_exists(JS . $filename . '.js')) {
448
				cache(str_replace(WWW_ROOT, '', JS) . $filename . '.js', $data, '+999 days', 'public');
449
			}
450
			$out = $this->link($filename);
451
		} else {
452
			$out = $this->codeBlock("\n" . $data . "\n", $options);
453
		}
454
 
455
		if ($inline) {
456
			return $out;
457
		} else {
458
			$view =& ClassRegistry::getObject('view');
459
			$view->addScript($out);
460
		}
461
	}
462
/**
463
 * Includes the Prototype Javascript library (and anything else) inside a single script tag.
464
 *
465
 * Note: The recommended approach is to copy the contents of
466
 * javascripts into your application's
467
 * public/javascripts/ directory, and use @see javascriptIncludeTag() to
468
 * create remote script links.
469
 *
470
 * @param string $script Script file to include
471
 * @param array $options Set options for codeBlock
472
 * @return string script with all javascript in/javascripts folder
473
 */
474
	function includeScript($script = "", $options = array()) {
475
		if ($script == "") {
476
			$files = scandir(JS);
477
			$javascript = '';
478
 
479
			foreach ($files as $file) {
480
				if (substr($file, -3) == '.js') {
481
					$javascript .= file_get_contents(JS . "{$file}") . "\n\n";
482
				}
483
			}
484
		} else {
485
			$javascript = file_get_contents(JS . "$script.js") . "\n\n";
486
		}
487
		return $this->codeBlock("\n\n" . $javascript, $options);
488
	}
489
/**
490
 * Generates a JavaScript object in JavaScript Object Notation (JSON)
491
 * from an array
492
 *
493
 * @param array $data Data to be converted
494
 * @param array $options Set of options: block, prefix, postfix, stringKeys, quoteKeys, q
495
 * @param string $prefix DEPRECATED, use $options['prefix'] instead. Prepends the string to the returned data
496
 * @param string $postfix DEPRECATED, use $options['postfix'] instead. Appends the string to the returned data
497
 * @param array $stringKeys DEPRECATED, use $options['stringKeys'] instead. A list of array keys to be treated as a string
498
 * @param boolean $quoteKeys DEPRECATED, use $options['quoteKeys'] instead. If false, treats $stringKey as a list of keys *not* to be quoted
499
 * @param string $q DEPRECATED, use $options['q'] instead. The type of quote to use
500
 * @return string A JSON code block
501
 */
502
	function object($data = array(), $options = array(), $prefix = null, $postfix = null, $stringKeys = null, $quoteKeys = null, $q = null) {
503
		if (!empty($options) && !is_array($options)) {
504
			$options = array('block' => $options);
505
		} else if (empty($options)) {
506
			$options = array();
507
		}
508
 
509
		$defaultOptions = array(
510
			'block' => false, 'prefix' => '', 'postfix' => '',
511
			'stringKeys' => array(), 'quoteKeys' => true, 'q' => '"'
512
		);
513
		$options = array_merge($defaultOptions, $options, array_filter(compact(array_keys($defaultOptions))));
514
 
515
		if (is_object($data)) {
516
			$data = get_object_vars($data);
517
		}
518
 
519
		$out = $keys = array();
520
		$numeric = true;
521
 
522
		if ($this->useNative) {
523
			$rt = json_encode($data);
524
		} else {
525
			if (is_array($data)) {
526
				$keys = array_keys($data);
527
			}
528
 
529
			if (!empty($keys)) {
530
				$numeric = (array_values($keys) === array_keys(array_values($keys)));
531
			}
532
 
533
			foreach ($data as $key => $val) {
534
				if (is_array($val) || is_object($val)) {
535
					$val = $this->object($val, array_merge($options, array('block' => false)));
536
				} else {
537
					$quoteStrings = (
538
						!count($options['stringKeys']) ||
539
						($options['quoteKeys'] && in_array($key, $options['stringKeys'], true)) ||
540
						(!$options['quoteKeys'] && !in_array($key, $options['stringKeys'], true))
541
					);
542
					$val = $this->value($val, $quoteStrings);
543
				}
544
				if (!$numeric) {
545
					$val = $options['q'] . $this->value($key, false) . $options['q'] . ':' . $val;
546
				}
547
				$out[] = $val;
548
			}
549
 
550
			if (!$numeric) {
551
				$rt = '{' . join(',', $out) . '}';
552
			} else {
553
				$rt = '[' . join(',', $out) . ']';
554
			}
555
		}
556
		$rt = $options['prefix'] . $rt . $options['postfix'];
557
 
558
		if ($options['block']) {
559
			$rt = $this->codeBlock($rt, array_diff_key($options, $defaultOptions));
560
		}
561
 
562
		return $rt;
563
	}
564
/**
565
 * Converts a PHP-native variable of any type to a JSON-equivalent representation
566
 *
567
 * @param mixed $val A PHP variable to be converted to JSON
568
 * @param boolean $quoteStrings If false, leaves string values unquoted
569
 * @return string a JavaScript-safe/JSON representation of $val
570
 */
571
	function value($val, $quoteStrings = true) {
572
		switch (true) {
573
			case (is_array($val) || is_object($val)):
574
				$val = $this->object($val);
575
			break;
576
			case ($val === null):
577
				$val = 'null';
578
			break;
579
			case (is_bool($val)):
580
				$val = ife($val, 'true', 'false');
581
			break;
582
			case (is_int($val)):
583
				$val = $val;
584
			break;
585
			case (is_float($val)):
586
				$val = sprintf("%.11f", $val);
587
			break;
588
			default:
589
				$val = $this->escapeString($val);
590
				if ($quoteStrings) {
591
					$val = '"' . $val . '"';
592
				}
593
			break;
594
		}
595
		return $val;
596
	}
597
/**
598
 * AfterRender callback.  Writes any cached events to the view, or to a temp file.
599
 *
600
 * @return null
601
 */
602
	function afterRender() {
603
		if (!$this->enabled) {
604
			return;
605
		}
606
		echo $this->writeEvents(true);
607
	}
608
}
609
 
610
?>