2005-08-21 06:49:02 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Javascript Helper class file.
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* CakePHP : Rapid Development Framework <http://www.cakephp.org/>
|
2006-01-20 07:46:14 +00:00
|
|
|
* Copyright (c) 2006, Cake Software Foundation, Inc.
|
2005-12-23 21:57:26 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-12-23 21:57:26 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2006-01-12 16:55:46 +00:00
|
|
|
* @filesource
|
2006-01-20 07:46:14 +00:00
|
|
|
* @copyright Copyright (c) 2006, Cake Software Foundation, Inc.
|
2005-12-23 21:57:26 +00:00
|
|
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
|
2005-08-21 06:49:02 +00:00
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
* @since CakePHP v 0.10.0.1076
|
2005-08-21 06:49:02 +00:00
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
|
*/
|
|
|
|
|
2005-08-21 20:01:32 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* Javascript Helper class for easy use of JavaScript.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
2005-09-07 01:52:45 +00:00
|
|
|
* JavascriptHelper encloses all methods needed while working with JavaScript.
|
2005-08-21 06:49:02 +00:00
|
|
|
*
|
|
|
|
* @package cake
|
2005-10-09 01:56:21 +00:00
|
|
|
* @subpackage cake.cake.libs.view.helpers
|
|
|
|
* @since CakePHP v 0.10.0.1076
|
2005-08-21 06:49:02 +00:00
|
|
|
*/
|
2005-09-19 01:16:05 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
class JavascriptHelper extends Helper
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
|
|
|
|
var $_cachedEvents = array();
|
|
|
|
var $_cacheEvents = false;
|
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a JavaScript script tag.
|
|
|
|
*
|
|
|
|
* @param string $script The JavaScript to be wrapped in SCRIPT tags.
|
|
|
|
* @return string The full SCRIPT element, with the JavaScript inside it.
|
|
|
|
*/
|
2005-08-21 06:49:02 +00:00
|
|
|
function codeBlock($script)
|
|
|
|
{
|
2005-09-18 13:25:20 +00:00
|
|
|
return sprintf($this->tags['javascriptblock'], $script);
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a JavaScript include tag (SCRIPT element)
|
|
|
|
*
|
|
|
|
* @param string $url URL to JavaScript file.
|
|
|
|
* @return string
|
|
|
|
*/
|
2005-08-21 06:49:02 +00:00
|
|
|
function link($url)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
if(strpos($url, ".") === false) $url .= ".js";
|
2005-10-09 01:56:21 +00:00
|
|
|
return sprintf($this->tags['javascriptlink'], $this->webroot . JS_URL . $url);
|
2005-09-17 02:22:07 +00:00
|
|
|
}
|
|
|
|
|
2005-12-27 03:33:44 +00:00
|
|
|
/**
|
|
|
|
* Returns a JavaScript include tag for an externally-hosted script
|
|
|
|
*
|
|
|
|
* @param string $url URL to JavaScript file.
|
|
|
|
* @return string
|
|
|
|
*/
|
2005-09-17 02:22:07 +00:00
|
|
|
function linkOut($url)
|
|
|
|
{
|
2005-10-03 04:48:00 +00:00
|
|
|
if(strpos($url, ".") === false) $url .= ".js";
|
|
|
|
return sprintf($this->tags['javascriptlink'], $url);
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
2005-08-21 20:01:32 +00:00
|
|
|
|
|
|
|
/**
|
2006-01-12 16:55:46 +00:00
|
|
|
* Escape carriage returns and single and double quotes for JavaScript segments.
|
|
|
|
*
|
2005-08-21 20:01:32 +00:00
|
|
|
* @param string $script string that might have javascript elements
|
|
|
|
* @return string escaped string
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function escapeScript ($script)
|
|
|
|
{
|
|
|
|
$script = str_replace(array("\r\n","\n","\r"),'\n', $script);
|
|
|
|
$script = str_replace(array('"', "'"), array('\"', "\\'"), $script);
|
|
|
|
return $script;
|
|
|
|
}
|
2005-08-21 20:01:32 +00:00
|
|
|
|
2006-01-12 16:55:46 +00:00
|
|
|
/**
|
|
|
|
* Escape a string to be JavaScript friendly.
|
|
|
|
*
|
|
|
|
* List of escaped ellements:
|
|
|
|
* + "\r\n" => '\n'
|
|
|
|
* + "\r" => '\n'
|
|
|
|
* + "\n" => '\n'
|
|
|
|
* + '"' => '\"'
|
|
|
|
* + "'" => "\\'"
|
|
|
|
*
|
|
|
|
* @param string $script String that needs to get escaped.
|
|
|
|
* @return string Escaped string.
|
|
|
|
*/
|
|
|
|
function escapeString($string)
|
|
|
|
{
|
|
|
|
$escape = array(
|
|
|
|
"\r\n" => '\n',
|
|
|
|
"\r" => '\n',
|
|
|
|
"\n" => '\n',
|
|
|
|
'"' => '\"',
|
|
|
|
"'" => "\\'"
|
|
|
|
);
|
|
|
|
|
|
|
|
return str_replace(array_keys($escape), array_values($escape), $string);
|
|
|
|
}
|
2005-08-21 20:01:32 +00:00
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* Attach an event to an element. Used with the Prototype library.
|
2006-01-12 16:55:46 +00:00
|
|
|
*
|
2005-08-21 20:01:32 +00:00
|
|
|
* @param string $object Object to be observed
|
|
|
|
* @param string $event event to observe
|
|
|
|
* @param string $observer function to call
|
|
|
|
* @param boolean $useCapture default true
|
|
|
|
* @return boolean true on success
|
|
|
|
*/
|
2005-10-03 04:48:00 +00:00
|
|
|
function event ($object, $event, $observer, $useCapture = true)
|
|
|
|
{
|
|
|
|
if($useCapture == true)
|
|
|
|
{
|
|
|
|
$useCapture = "true";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$useCapture = "false";
|
|
|
|
}
|
|
|
|
|
|
|
|
$b = "Event.observe($object, '$event', $observer, $useCapture);";
|
|
|
|
if($this->_cacheEvents === true)
|
|
|
|
{
|
|
|
|
$this->_cachedEvents[] = $b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->codeBlock($b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache JavaScript events created with event()
|
2006-01-12 16:55:46 +00:00
|
|
|
*
|
2005-10-03 04:48:00 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
function cacheEvents ()
|
|
|
|
{
|
|
|
|
$this->_cacheEvents = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write cached JavaScript events
|
2006-01-12 16:55:46 +00:00
|
|
|
*
|
2005-10-03 04:48:00 +00:00
|
|
|
* @return string A single code block of all cached JavaScript events created with event()
|
|
|
|
*/
|
|
|
|
function writeEvents ()
|
|
|
|
{
|
|
|
|
$this->_cacheEvents = false;
|
|
|
|
return $this->codeBlock("\n" . implode("\n", $this->_cachedEvents) . "\n");
|
|
|
|
}
|
2005-08-21 20:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2005-09-07 01:52:45 +00:00
|
|
|
* Includes the Prototype Javascript library (and anything else) inside a single script tag.
|
2006-01-12 16:55:46 +00:00
|
|
|
*
|
2005-08-21 20:01:32 +00:00
|
|
|
* Note: The recommended approach is to copy the contents of
|
2005-11-02 03:52:22 +00:00
|
|
|
* javascripts into your application's
|
2005-08-21 20:01:32 +00:00
|
|
|
* public/javascripts/ directory, and use @see javascriptIncludeTag() to
|
|
|
|
* create remote script links.
|
|
|
|
* @return string script with all javascript in /javascripts folder
|
|
|
|
*/
|
2005-12-27 03:33:44 +00:00
|
|
|
function includeScript ($script = "")
|
|
|
|
{
|
|
|
|
if($script == "")
|
|
|
|
{
|
|
|
|
$files = scandir(JS);
|
|
|
|
$javascript = '';
|
|
|
|
foreach($files as $file)
|
|
|
|
{
|
|
|
|
if (substr($file, -3) == '.js')
|
|
|
|
{
|
|
|
|
$javascript .= file_get_contents(JS."{$file}") . "\n\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$javascript = file_get_contents(JS."$script.js") . "\n\n";
|
|
|
|
}
|
|
|
|
return $this->codeBlock("\n\n" . $javascript);
|
|
|
|
}
|
2005-08-21 20:01:32 +00:00
|
|
|
|
2005-08-21 06:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|