From 9fac0b92c62855e174f723db02cccbc2dfd0fda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Lorenzo=20Rodri=CC=81guez?= Date: Tue, 21 Dec 2010 22:01:38 -0430 Subject: [PATCH] Extracting JsBaseEngineHelper to its own file, fixing some JsHelper related test cases --- lib/Cake/View/Helper/JqueryEngineHelper.php | 2 +- lib/Cake/View/Helper/JsBaseEngineHelper.php | 652 ++++++++++++++++++ lib/Cake/View/Helper/JsHelper.php | 635 +---------------- lib/Cake/View/Helper/MootoolsEngineHelper.php | 2 +- .../View/Helper/PrototypeEngineHelper.php | 2 +- .../tests/cases/libs/view/helpers/js.test.php | 1 + .../view/helpers/mootools_engine.test.php | 2 +- 7 files changed, 659 insertions(+), 637 deletions(-) create mode 100644 lib/Cake/View/Helper/JsBaseEngineHelper.php diff --git a/lib/Cake/View/Helper/JqueryEngineHelper.php b/lib/Cake/View/Helper/JqueryEngineHelper.php index bc3d90423..17e226d6f 100644 --- a/lib/Cake/View/Helper/JqueryEngineHelper.php +++ b/lib/Cake/View/Helper/JqueryEngineHelper.php @@ -23,7 +23,7 @@ * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -App::uses('JsHelper', 'View/Helper'); +App::uses('AppHelper', 'View/Helper'); App::uses('JsBaseEngineHelper', 'View/Helper'); class JqueryEngineHelper extends JsBaseEngineHelper { diff --git a/lib/Cake/View/Helper/JsBaseEngineHelper.php b/lib/Cake/View/Helper/JsBaseEngineHelper.php new file mode 100644 index 000000000..8d52f6939 --- /dev/null +++ b/lib/Cake/View/Helper/JsBaseEngineHelper.php @@ -0,0 +1,652 @@ + default arguments. + * + * @var array + * @access protected + */ + protected $_callbackArguments = array(); + +/** + * Constructor. + * + * @return void + */ + function __construct() { + $this->useNative = function_exists('json_encode'); + } + +/** + * Create an `alert()` message in Javascript + * + * @param string $message Message you want to alter. + * @return string completed alert() + */ + public function alert($message) { + return 'alert("' . $this->escape($message) . '");'; + } + +/** + * Redirects to a URL. Creates a window.location modification snippet + * that can be used to trigger 'redirects' from Javascript. + * + * @param mixed $url + * @param array $options + * @return string completed redirect in javascript + */ + public function redirect($url = null) { + return 'window.location = "' . Router::url($url) . '";'; + } + +/** + * Create a `confirm()` message + * + * @param string $message Message you want confirmed. + * @return string completed confirm() + */ + public function confirm($message) { + return 'confirm("' . $this->escape($message) . '");'; + } + +/** + * Generate a confirm snippet that returns false from the current + * function scope. + * + * @param string $message Message to use in the confirm dialog. + * @return string completed confirm with return script + */ + public function confirmReturn($message) { + $out = 'var _confirm = ' . $this->confirm($message); + $out .= "if (!_confirm) {\n\treturn false;\n}"; + return $out; + } + +/** + * Create a `prompt()` Javascript function + * + * @param string $message Message you want to prompt. + * @param string $default Default message + * @return string completed prompt() + */ + public function prompt($message, $default = '') { + return 'prompt("' . $this->escape($message) . '", "' . $this->escape($default) . '");'; + } + +/** + * Generates a JavaScript object in JavaScript Object Notation (JSON) + * from an array. Will use native JSON encode method if available, and $useNative == true + * + * ### Options: + * + * - `prefix` - String prepended to the returned data. + * - `postfix` - String appended to the returned data. + * + * @param array $data Data to be converted. + * @param array $options Set of options, see above. + * @return string A JSON code block + */ + public function object($data = array(), $options = array()) { + $defaultOptions = array( + 'prefix' => '', 'postfix' => '', + ); + $options = array_merge($defaultOptions, $options); + + if (is_object($data)) { + $data = get_object_vars($data); + } + + $out = $keys = array(); + $numeric = true; + + if ($this->useNative && function_exists('json_encode')) { + $rt = json_encode($data); + } else { + if (is_null($data)) { + return 'null'; + } + if (is_bool($data)) { + return $data ? 'true' : 'false'; + } + if (is_array($data)) { + $keys = array_keys($data); + } + + if (!empty($keys)) { + $numeric = (array_values($keys) === array_keys(array_values($keys))); + } + + foreach ($data as $key => $val) { + if (is_array($val) || is_object($val)) { + $val = $this->object($val); + } else { + $val = $this->value($val); + } + if (!$numeric) { + $val = '"' . $this->value($key, false) . '":' . $val; + } + $out[] = $val; + } + + if (!$numeric) { + $rt = '{' . join(',', $out) . '}'; + } else { + $rt = '[' . join(',', $out) . ']'; + } + } + $rt = $options['prefix'] . $rt . $options['postfix']; + return $rt; + } + +/** + * Converts a PHP-native variable of any type to a JSON-equivalent representation + * + * @param mixed $val A PHP variable to be converted to JSON + * @param boolean $quoteStrings If false, leaves string values unquoted + * @return string a JavaScript-safe/JSON representation of $val + */ + public function value($val, $quoteString = true) { + switch (true) { + case (is_array($val) || is_object($val)): + $val = $this->object($val); + break; + case ($val === null): + $val = 'null'; + break; + case (is_bool($val)): + $val = ($val === true) ? 'true' : 'false'; + break; + case (is_int($val)): + $val = $val; + break; + case (is_float($val)): + $val = sprintf("%.11f", $val); + break; + default: + $val = $this->escape($val); + if ($quoteString) { + $val = '"' . $val . '"'; + } + break; + } + return $val; + } + +/** + * Escape a string to be JSON friendly. + * + * List of escaped elements: + * + * - "\r" => '\n' + * - "\n" => '\n' + * - '"' => '\"' + * + * @param string $script String that needs to get escaped. + * @return string Escaped string. + */ + public function escape($string) { + return $this->_utf8ToHex($string); + } + +/** + * Encode a string into JSON. Converts and escapes necessary characters. + * + * @param string $string The string that needs to be utf8->hex encoded + * @return void + */ + protected function _utf8ToHex($string) { + $length = strlen($string); + $return = ''; + for ($i = 0; $i < $length; ++$i) { + $ord = ord($string{$i}); + switch (true) { + case $ord == 0x08: + $return .= '\b'; + break; + case $ord == 0x09: + $return .= '\t'; + break; + case $ord == 0x0A: + $return .= '\n'; + break; + case $ord == 0x0C: + $return .= '\f'; + break; + case $ord == 0x0D: + $return .= '\r'; + break; + case $ord == 0x22: + case $ord == 0x2F: + case $ord == 0x5C: + $return .= '\\' . $string{$i}; + break; + case (($ord >= 0x20) && ($ord <= 0x7F)): + $return .= $string{$i}; + break; + case (($ord & 0xE0) == 0xC0): + if ($i + 1 >= $length) { + $i += 1; + $return .= '?'; + break; + } + $charbits = $string{$i} . $string{$i + 1}; + $char = Multibyte::utf8($charbits); + $return .= sprintf('\u%04s', dechex($char[0])); + $i += 1; + break; + case (($ord & 0xF0) == 0xE0): + if ($i + 2 >= $length) { + $i += 2; + $return .= '?'; + break; + } + $charbits = $string{$i} . $string{$i + 1} . $string{$i + 2}; + $char = Multibyte::utf8($charbits); + $return .= sprintf('\u%04s', dechex($char[0])); + $i += 2; + break; + case (($ord & 0xF8) == 0xF0): + if ($i + 3 >= $length) { + $i += 3; + $return .= '?'; + break; + } + $charbits = $string{$i} . $string{$i + 1} . $string{$i + 2} . $string{$i + 3}; + $char = Multibyte::utf8($charbits); + $return .= sprintf('\u%04s', dechex($char[0])); + $i += 3; + break; + case (($ord & 0xFC) == 0xF8): + if ($i + 4 >= $length) { + $i += 4; + $return .= '?'; + break; + } + $charbits = $string{$i} . $string{$i + 1} . $string{$i + 2} . $string{$i + 3} . $string{$i + 4}; + $char = Multibyte::utf8($charbits); + $return .= sprintf('\u%04s', dechex($char[0])); + $i += 4; + break; + case (($ord & 0xFE) == 0xFC): + if ($i + 5 >= $length) { + $i += 5; + $return .= '?'; + break; + } + $charbits = $string{$i} . $string{$i + 1} . $string{$i + 2} . $string{$i + 3} . $string{$i + 4} . $string{$i + 5}; + $char = Multibyte::utf8($charbits); + $return .= sprintf('\u%04s', dechex($char[0])); + $i += 5; + break; + } + } + return $return; + } + +/** + * Create javascript selector for a CSS rule + * + * @param string $selector The selector that is targeted + * @return object instance of $this. Allows chained methods. + */ + abstract public function get($selector); + +/** + * Add an event to the script cache. Operates on the currently selected elements. + * + * ### Options + * + * - `wrap` - Whether you want the callback wrapped in an anonymous function. (defaults to true) + * - `stop` - Whether you want the event to stopped. (defaults to true) + * + * @param string $type Type of event to bind to the current dom id + * @param string $callback The Javascript function you wish to trigger or the function literal + * @param array $options Options for the event. + * @return string completed event handler + */ + abstract public function event($type, $callback, $options = array()); + +/** + * Create a domReady event. This is a special event in many libraries + * + * @param string $functionBody The code to run on domReady + * @return string completed domReady method + */ + abstract public function domReady($functionBody); + +/** + * Create an iteration over the current selection result. + * + * @param string $callback The function body you wish to apply during the iteration. + * @return string completed iteration + */ + abstract public function each($callback); + +/** + * Trigger an Effect. + * + * ### Supported Effects + * + * The following effects are supported by all core JsEngines + * + * - `show` - reveal an element. + * - `hide` - hide an element. + * - `fadeIn` - Fade in an element. + * - `fadeOut` - Fade out an element. + * - `slideIn` - Slide an element in. + * - `slideOut` - Slide an element out. + * + * ### Options + * + * - `speed` - Speed at which the animation should occur. Accepted values are 'slow', 'fast'. Not all effects use + * the speed option. + * + * @param string $name The name of the effect to trigger. + * @param array $options Array of options for the effect. + * @return string completed string with effect. + */ + abstract public function effect($name, $options = array()); + +/** + * Make an XHR request + * + * ### Event Options + * + * - `complete` - Callback to fire on complete. + * - `success` - Callback to fire on success. + * - `before` - Callback to fire on request initialization. + * - `error` - Callback to fire on request failure. + * + * ### Options + * + * - `method` - The method to make the request with defaults to GET in more libraries + * - `async` - Whether or not you want an asynchronous request. + * - `data` - Additional data to send. + * - `update` - Dom id to update with the content of the request. + * - `type` - Data type for response. 'json' and 'html' are supported. Default is html for most libraries. + * - `evalScripts` - Whether or not