|
@@ -20,56 +20,54 @@ use Tools\Utility\Multibyte;
|
|
|
*/
|
|
*/
|
|
|
class JsHelper extends Helper {
|
|
class JsHelper extends Helper {
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Whether or not you want scripts to be buffered or output.
|
|
|
|
|
- *
|
|
|
|
|
- * @var bool
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Whether or not you want scripts to be buffered or output.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var bool
|
|
|
|
|
+ */
|
|
|
public $bufferScripts = true;
|
|
public $bufferScripts = true;
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Helper dependencies
|
|
|
|
|
- *
|
|
|
|
|
- * @var array
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Helper dependencies
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var array
|
|
|
|
|
+ */
|
|
|
public $helpers = ['Html', 'Form'];
|
|
public $helpers = ['Html', 'Form'];
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Variables to pass to Javascript.
|
|
|
|
|
- *
|
|
|
|
|
- * @var array
|
|
|
|
|
- * @see JsHelper::set()
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Variables to pass to Javascript.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var array
|
|
|
|
|
+ */
|
|
|
protected $_jsVars = [];
|
|
protected $_jsVars = [];
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Scripts that are queued for output
|
|
|
|
|
- *
|
|
|
|
|
- * @var array
|
|
|
|
|
- * @see JsHelper::buffer()
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Scripts that are queued for output
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var array
|
|
|
|
|
+ */
|
|
|
protected $_bufferedScripts = [];
|
|
protected $_bufferedScripts = [];
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * The javascript variable created by set() variables.
|
|
|
|
|
- *
|
|
|
|
|
- * @var string
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * The javascript variable created by set() variables.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @var string
|
|
|
|
|
+ */
|
|
|
public $setVariable = 'app';
|
|
public $setVariable = 'app';
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * 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
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 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 = [], $options = []) {
|
|
public function object($data = [], $options = []) {
|
|
|
$defaultOptions = [
|
|
$defaultOptions = [
|
|
|
'prefix' => '', 'postfix' => '',
|
|
'prefix' => '', 'postfix' => '',
|
|
@@ -79,15 +77,14 @@ class JsHelper extends Helper {
|
|
|
return $options['prefix'] . json_encode($data) . $options['postfix'];
|
|
return $options['prefix'] . json_encode($data) . $options['postfix'];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * 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 bool $quoteString If false, leaves string values unquoted
|
|
|
|
|
- * @param string $key Key name.
|
|
|
|
|
- * @return string a JavaScript-safe/JSON representation of $val
|
|
|
|
|
- */
|
|
|
|
|
- public function value($val = [], $quoteString = null, $key = 'value') {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 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 bool $quoteString If false, leaves string values unquoted
|
|
|
|
|
+ * @return string a JavaScript-safe/JSON representation of $val
|
|
|
|
|
+ */
|
|
|
|
|
+ public function value($val = [], $quoteString = true) {
|
|
|
if ($quoteString === null) {
|
|
if ($quoteString === null) {
|
|
|
$quoteString = true;
|
|
$quoteString = true;
|
|
|
}
|
|
}
|
|
@@ -102,7 +99,6 @@ class JsHelper extends Helper {
|
|
|
$val = ($val === true) ? 'true' : 'false';
|
|
$val = ($val === true) ? 'true' : 'false';
|
|
|
break;
|
|
break;
|
|
|
case (is_int($val)):
|
|
case (is_int($val)):
|
|
|
- $val = $val;
|
|
|
|
|
break;
|
|
break;
|
|
|
case (is_float($val)):
|
|
case (is_float($val)):
|
|
|
$val = sprintf("%.11f", $val);
|
|
$val = sprintf("%.11f", $val);
|
|
@@ -116,28 +112,28 @@ class JsHelper extends Helper {
|
|
|
return $val;
|
|
return $val;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Escape a string to be JSON friendly.
|
|
|
|
|
- *
|
|
|
|
|
- * List of escaped elements:
|
|
|
|
|
- *
|
|
|
|
|
- * - "\r" => '\n'
|
|
|
|
|
- * - "\n" => '\n'
|
|
|
|
|
- * - '"' => '\"'
|
|
|
|
|
- *
|
|
|
|
|
- * @param string $string String that needs to get escaped.
|
|
|
|
|
- * @return string Escaped string.
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Escape a string to be JSON friendly.
|
|
|
|
|
+ *
|
|
|
|
|
+ * List of escaped elements:
|
|
|
|
|
+ *
|
|
|
|
|
+ * - "\r" => '\n'
|
|
|
|
|
+ * - "\n" => '\n'
|
|
|
|
|
+ * - '"' => '\"'
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $string String that needs to get escaped.
|
|
|
|
|
+ * @return string Escaped string.
|
|
|
|
|
+ */
|
|
|
public function escape($string) {
|
|
public function escape($string) {
|
|
|
return $this->_utf8ToHex($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
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Encode a string into JSON. Converts and escapes necessary characters.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $string The string that needs to be utf8->hex encoded
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
protected function _utf8ToHex($string) {
|
|
protected function _utf8ToHex($string) {
|
|
|
$length = strlen($string);
|
|
$length = strlen($string);
|
|
|
$return = '';
|
|
$return = '';
|
|
@@ -227,26 +223,26 @@ class JsHelper extends Helper {
|
|
|
return $return;
|
|
return $return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Writes all Javascript generated so far to a code block or
|
|
|
|
|
- * caches them to a file and returns a linked script. If no scripts have been
|
|
|
|
|
- * buffered this method will return null. If the request is an XHR(ajax) request
|
|
|
|
|
- * onDomReady will be set to false. As the dom is already 'ready'.
|
|
|
|
|
- *
|
|
|
|
|
- * ### Options
|
|
|
|
|
- *
|
|
|
|
|
- * - `inline` - Set to true to have scripts output as a script block inline
|
|
|
|
|
- * if `cache` is also true, a script link tag will be generated. (default true)
|
|
|
|
|
- * - `cache` - Set to true to have scripts cached to a file and linked in (default false)
|
|
|
|
|
- * - `clear` - Set to false to prevent script cache from being cleared (default true)
|
|
|
|
|
- * - `onDomReady` - wrap cached scripts in domready event (default true)
|
|
|
|
|
- * - `safe` - if an inline block is generated should it be wrapped in <![CDATA[ ... ]]> (default true)
|
|
|
|
|
- *
|
|
|
|
|
- * @param array $options options for the code block
|
|
|
|
|
- * @return mixed Completed javascript tag if there are scripts, if there are no buffered
|
|
|
|
|
- * scripts null will be returned.
|
|
|
|
|
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::writeBuffer
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Writes all Javascript generated so far to a code block or
|
|
|
|
|
+ * caches them to a file and returns a linked script. If no scripts have been
|
|
|
|
|
+ * buffered this method will return null. If the request is an XHR(ajax) request
|
|
|
|
|
+ * onDomReady will be set to false. As the dom is already 'ready'.
|
|
|
|
|
+ *
|
|
|
|
|
+ * ### Options
|
|
|
|
|
+ *
|
|
|
|
|
+ * - `inline` - Set to true to have scripts output as a script block inline
|
|
|
|
|
+ * if `cache` is also true, a script link tag will be generated. (default true)
|
|
|
|
|
+ * - `cache` - Set to true to have scripts cached to a file and linked in (default false)
|
|
|
|
|
+ * - `clear` - Set to false to prevent script cache from being cleared (default true)
|
|
|
|
|
+ * - `onDomReady` - wrap cached scripts in domready event (default true)
|
|
|
|
|
+ * - `safe` - if an inline block is generated should it be wrapped in <![CDATA[ ... ]]> (default true)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param array $options options for the code block
|
|
|
|
|
+ * @return mixed Completed javascript tag if there are scripts, if there are no buffered
|
|
|
|
|
+ * scripts null will be returned.
|
|
|
|
|
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::writeBuffer
|
|
|
|
|
+ */
|
|
|
public function writeBuffer($options = []) {
|
|
public function writeBuffer($options = []) {
|
|
|
$domReady = false;
|
|
$domReady = false;
|
|
|
$defaults = [
|
|
$defaults = [
|
|
@@ -274,15 +270,15 @@ class JsHelper extends Helper {
|
|
|
return $return;
|
|
return $return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Write a script to the buffered scripts.
|
|
|
|
|
- *
|
|
|
|
|
- * @param string $script Script string to add to the buffer.
|
|
|
|
|
- * @param bool $top If true the script will be added to the top of the
|
|
|
|
|
- * buffered scripts array. If false the bottom.
|
|
|
|
|
- * @return void
|
|
|
|
|
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::buffer
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Write a script to the buffered scripts.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $script Script string to add to the buffer.
|
|
|
|
|
+ * @param bool $top If true the script will be added to the top of the
|
|
|
|
|
+ * buffered scripts array. If false the bottom.
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::buffer
|
|
|
|
|
+ */
|
|
|
public function buffer($script, $top = false) {
|
|
public function buffer($script, $top = false) {
|
|
|
if ($top) {
|
|
if ($top) {
|
|
|
array_unshift($this->_bufferedScripts, $script);
|
|
array_unshift($this->_bufferedScripts, $script);
|
|
@@ -291,13 +287,13 @@ class JsHelper extends Helper {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Get all the buffered scripts
|
|
|
|
|
- *
|
|
|
|
|
- * @param bool $clear Whether or not to clear the script caches (default true)
|
|
|
|
|
- * @return array Array of scripts added to the request.
|
|
|
|
|
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::getBuffer
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Get all the buffered scripts
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param bool $clear Whether or not to clear the script caches (default true)
|
|
|
|
|
+ * @return array Array of scripts added to the request.
|
|
|
|
|
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::getBuffer
|
|
|
|
|
+ */
|
|
|
public function getBuffer($clear = true) {
|
|
public function getBuffer($clear = true) {
|
|
|
$this->_createVars();
|
|
$this->_createVars();
|
|
|
$scripts = $this->_bufferedScripts;
|
|
$scripts = $this->_bufferedScripts;
|
|
@@ -308,11 +304,11 @@ class JsHelper extends Helper {
|
|
|
return $scripts;
|
|
return $scripts;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Generates the object string for variables passed to javascript and adds to buffer
|
|
|
|
|
- *
|
|
|
|
|
- * @return void
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Generates the object string for variables passed to javascript and adds to buffer
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ */
|
|
|
protected function _createVars() {
|
|
protected function _createVars() {
|
|
|
if (!empty($this->_jsVars)) {
|
|
if (!empty($this->_jsVars)) {
|
|
|
$setVar = (strpos($this->setVariable, '.')) ? $this->setVariable : 'window.' . $this->setVariable;
|
|
$setVar = (strpos($this->setVariable, '.')) ? $this->setVariable : 'window.' . $this->setVariable;
|
|
@@ -320,16 +316,16 @@ class JsHelper extends Helper {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * Pass variables into Javascript. Allows you to set variables that will be
|
|
|
|
|
- * output when the buffer is fetched with `JsHelper::getBuffer()` or `JsHelper::writeBuffer()`
|
|
|
|
|
- * The Javascript variable used to output set variables can be controlled with `JsHelper::$setVariable`
|
|
|
|
|
- *
|
|
|
|
|
- * @param string|array $one Either an array of variables to set, or the name of the variable to set.
|
|
|
|
|
- * @param string|array $two If $one is a string, $two is the value for that key.
|
|
|
|
|
- * @return void
|
|
|
|
|
- * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::set
|
|
|
|
|
- */
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Pass variables into Javascript. Allows you to set variables that will be
|
|
|
|
|
+ * output when the buffer is fetched with `JsHelper::getBuffer()` or `JsHelper::writeBuffer()`
|
|
|
|
|
+ * The Javascript variable used to output set variables can be controlled with `JsHelper::$setVariable`
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string|array $one Either an array of variables to set, or the name of the variable to set.
|
|
|
|
|
+ * @param string|array $two If $one is a string, $two is the value for that key.
|
|
|
|
|
+ * @return void
|
|
|
|
|
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/js.html#JsHelper::set
|
|
|
|
|
+ */
|
|
|
public function set($one, $two = null) {
|
|
public function set($one, $two = null) {
|
|
|
$data = null;
|
|
$data = null;
|
|
|
if (is_array($one)) {
|
|
if (is_array($one)) {
|
|
@@ -342,7 +338,7 @@ class JsHelper extends Helper {
|
|
|
$data = [$one => $two];
|
|
$data = [$one => $two];
|
|
|
}
|
|
}
|
|
|
if (!$data) {
|
|
if (!$data) {
|
|
|
- return false;
|
|
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
$this->_jsVars = array_merge($this->_jsVars, $data);
|
|
$this->_jsVars = array_merge($this->_jsVars, $data);
|
|
|
}
|
|
}
|