diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index d1c2aaf860b..dcb231306af 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -32,6 +32,7 @@ class JsHelper extends AppHelper { * Whether or not you want scripts to be buffered or output. * * @var boolean + * @access public */ var $bufferScripts = true; @@ -39,6 +40,7 @@ class JsHelper extends AppHelper { * helpers * * @var array + * @access public */ var $helpers = array('Html', 'Form'); @@ -47,6 +49,7 @@ class JsHelper extends AppHelper { * * @var array * @see JsHelper::set() + * @access private */ var $__jsVars = array(); @@ -54,6 +57,7 @@ class JsHelper extends AppHelper { * Scripts that are queued for output * * @var array + * @access private */ var $__bufferedScripts = array(); @@ -69,6 +73,7 @@ class JsHelper extends AppHelper { * The javascript variable created by set() variables. * * @var string + * @access public */ var $setVariable = APP_DIR; @@ -76,8 +81,8 @@ class JsHelper extends AppHelper { * Constructor - determines engine helper * * @param array $settings Settings array contains name of engine helper. - * @access public * @return void + * @access public */ function __construct($settings = array()) { $className = 'Jquery'; @@ -112,8 +117,8 @@ function __construct($settings = array()) { * * @param string $method Method to be called * @param array $params Parameters for the method being called. - * @access public * @return mixed Depends on the return of the dispatched method, or it could be an instance of the EngineHelper + * @access public */ function call__($method, $params) { if (isset($this->{$this->__engineName}) && method_exists($this->{$this->__engineName}, $method)) { @@ -157,6 +162,7 @@ function call__($method, $params) { * @param array $options Options to use for encoding JSON. See JsBaseEngineHelper::object() for more details. * @return string encoded JSON * @deprecated Remove when support for PHP4 and Object::object are removed. + * @access public */ function object($data = array(), $options = array()) { return $this->{$this->__engineName}->object($data, $options); @@ -190,6 +196,7 @@ function value($val, $quoteString = true) { * * @param array $options options for the code block * @return string completed javascript tag. + * @access public */ function writeBuffer($options = array()) { $defaults = array('onDomReady' => true, 'inline' => true, 'cache' => false, 'clear' => true, 'safe' => true); @@ -220,10 +227,18 @@ function writeBuffer($options = array()) { /** * Write a script to the cached scripts. * + * @param string $script Script string to add to the buffer. + * @param boolean $top If true the script will be added to the top of the + * buffered scripts array. If false the bottom. * @return void + * @access public */ - function buffer($script) { - $this->__bufferedScripts[] = $script; + function buffer($script, $top = false) { + if ($top) { + array_unshift($this->__bufferedScripts, $script); + } else { + $this->__bufferedScripts[] = $script; + } } /** @@ -231,6 +246,7 @@ function buffer($script) { * * @param boolean $clear Whether or not to clear the script caches (default true) * @return array Array of scripts added to the request. + * @access public */ function getBuffer($clear = true) { $this->_createVars(); @@ -246,11 +262,12 @@ function getBuffer($clear = true) { * Generates the object string for variables passed to javascript. * * @return string + * @access public */ function _createVars() { if (!empty($this->__jsVars)) { $setVar = (strpos($this->setVariable, '.')) ? $this->setVariable : 'window.' . $this->setVariable; - $this->buffer($setVar . ' = ' . $this->object($this->__jsVars) . ';'); + $this->buffer($setVar . ' = ' . $this->object($this->__jsVars) . ';', true); } } @@ -271,6 +288,7 @@ function _createVars() { * @param mixed $url Mixed either a string URL or an cake url array. * @param array $options Options for both the HTML element and Js::request() * @return string Completed link. If buffering is disabled a script tag will be returned as well. + * @access public */ function link($title, $url = null, $options = array()) { if (!isset($options['id'])) { @@ -303,9 +321,10 @@ function link($title, $url = null, $options = array()) { * 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 mixed $one - * @param mixed $two + * @param mixed $one Either an array of variables to set, or the name of the variable to set. + * @param mixed $two If $one is a string, $two is the value for that key. * @return void + * @access public */ function set($one, $two = null) { $data = null; @@ -335,6 +354,7 @@ function set($one, $two = null) { * @param string $title The display text of the submit button. * @param array $options Array of options to use. * @return string Completed submit button. + * @access public */ function submit($caption = null, $options = array()) { if (!isset($options['id'])) { @@ -377,6 +397,7 @@ function submit($caption = null, $options = array()) { * @param array $options Options to filter. * @param array $additional Array of additional keys to extract and include in the return options array. * @return array Array of options for non-js. + * @access public */ function _getHtmlOptions(&$options, $additional = array()) { $htmlKeys = array_merge(array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title'), $additional); @@ -425,6 +446,7 @@ class JsBaseEngineHelper extends AppHelper { * for end user use though. * * @var array + * @access protected */ var $_optionMap = array(); @@ -433,6 +455,7 @@ class JsBaseEngineHelper extends AppHelper { * This allows specific 'end point' methods to be automatically buffered by the JsHelper. * * @var array + * @access public */ var $bufferedMethods = array('event', 'sortable', 'drag', 'drop', 'slider'); @@ -440,6 +463,7 @@ class JsBaseEngineHelper extends AppHelper { * Contains a list of callback names -> default arguments. * * @var array + * @access protected */ var $_callbackArguments = array(); @@ -456,8 +480,8 @@ function __construct() { * Create an alert message in Javascript * * @param string $message Message you want to alter. - * @access public * @return string completed alert() + * @access public */ function alert($message) { return 'alert("' . $this->escape($message) . '");'; @@ -469,6 +493,7 @@ function alert($message) { * @param mixed $url * @param array $options * @return string completed redirect in javascript + * @access public */ function redirect($url = null) { return 'window.location = "' . Router::url($url) . '";'; @@ -478,8 +503,8 @@ function redirect($url = null) { * Create a confirm() message * * @param string $message Message you want confirmed. - * @access public * @return string completed confirm() + * @access public */ function confirm($message) { return 'confirm("' . $this->escape($message) . '");'; @@ -490,8 +515,8 @@ function confirm($message) { * function scope. * * @param string $message Message to use in the confirm dialog. + * @return string completed confirm with return script * @access public - * @return string */ function confirmReturn($message) { $out = 'var _confirm = ' . $this->confirm($message); @@ -504,8 +529,8 @@ function confirmReturn($message) { * * @param string $message Message you want to prompt. * @param string $default Default message - * @access public * @return string completed prompt() + * @access public */ function prompt($message, $default = '') { return 'prompt("' . $this->escape($message) . '", "' . $this->escape($default) . '");'; @@ -633,7 +658,9 @@ function escape($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 + * @access protected */ function _utf8ToHex($string) { $length = strlen($string); @@ -729,6 +756,7 @@ function _utf8ToHex($string) { * * @param string $selector The selector that is targeted * @return object instance of $this. Allows chained methods. + * @access public */ function get($selector) { trigger_error(sprintf(__('%s does not have get() implemented', true), get_class($this)), E_USER_WARNING); @@ -747,6 +775,7 @@ function get($selector) { * @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 + * @access public */ function event($type, $callback, $options = array()) { trigger_error(sprintf(__('%s does not have event() implemented', true), get_class($this)), E_USER_WARNING); @@ -757,6 +786,7 @@ function event($type, $callback, $options = array()) { * * @param string $functionBody The code to run on domReady * @return string completed domReady method + * @access public */ function domReady($functionBody) { trigger_error(sprintf(__('%s does not have domReady() implemented', true), get_class($this)), E_USER_WARNING); @@ -794,6 +824,7 @@ function each($callback) { * @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. + * @access public */ function effect($name, $options) { trigger_error(sprintf(__('%s does not have effect() implemented', true), get_class($this)), E_USER_WARNING); @@ -823,6 +854,7 @@ function effect($name, $options) { * @param mixed $url Array or String URL to target with the request. * @param array $options Array of options. See above for cross library supported options * @return string XHR request. + * @access public */ function request($url, $options = array()) { trigger_error(sprintf(__('%s does not have request() implemented', true), get_class($this)), E_USER_WARNING); @@ -846,6 +878,7 @@ function request($url, $options = array()) { * * @param array $options Options array see above. * @return string Completed drag script + * @access public */ function drag($options = array()) { trigger_error(sprintf(__('%s does not have drag() implemented', true), get_class($this)), E_USER_WARNING); @@ -867,6 +900,7 @@ function drag($options = array()) { * - `leave` - Event fired when a drag is removed from a drop zone without being dropped. * * @return string Completed drop script + * @access public */ function drop($options = array()) { trigger_error(sprintf(__('%s does not have drop() implemented', true), get_class($this)), E_USER_WARNING); @@ -891,6 +925,7 @@ function drop($options = array()) { * * @param array $options Array of options for the sortable. See above. * @return string Completed sortable script. + * @access public */ function sortable() { trigger_error(sprintf(__('%s does not have sortable() implemented', true), get_class($this)), E_USER_WARNING); @@ -914,6 +949,7 @@ function sortable() { * - `complete` - Fired when the user stops sliding the handle * * @return string Completed slider script + * @access public */ function slider() { trigger_error(sprintf(__('%s does not have slider() implemented', true), get_class($this)), E_USER_WARNING); @@ -932,6 +968,7 @@ function slider() { * * @param array $options options for serialization generation. * @return string completed form serialization script + * @access public */ function serializeForm() { trigger_error( @@ -991,8 +1028,8 @@ function _mapOptions($method, $options) { * @param string $method Name of the method you are preparing callbacks for. * @param array $options Array of options being parsed * @param string $callbacks Additional Keys that contain callbacks - * @access protected * @return array Array of options with callbacks added. + * @access protected */ function _prepareCallbacks($method, $options, $callbacks = array()) { $wrapCallbacks = true; @@ -1029,6 +1066,7 @@ function _prepareCallbacks($method, $options, $callbacks = array()) { * @param string $method Name of method processing options for. * @param array $options Array of options to process. * @return string Parsed options string. + * @access protected */ function _processOptions($method, $options) { $options = $this->_mapOptions($method, $options); diff --git a/cake/tests/cases/libs/view/helpers/js.test.php b/cake/tests/cases/libs/view/helpers/js.test.php index 2afc552494c..8e0dd04f803 100644 --- a/cake/tests/cases/libs/view/helpers/js.test.php +++ b/cake/tests/cases/libs/view/helpers/js.test.php @@ -231,9 +231,6 @@ function testWriteScriptsNoFile() { $view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/'))); $result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false)); } - function getTests() { - return array('start', 'startCase', 'testWriteBufferNotInline', 'endCase', 'end'); - } /** * test that writing the buffer with inline = false includes a script tag. @@ -490,8 +487,24 @@ function testSet() { $expected = 'Application.variables = {"loggedIn":true,"height":"tall","color":"purple"};'; $this->assertEqual($result[0], $expected); } -} +/** + * test that vars set with Js->set() go to the top of the buffered scripts list. + * + * @return void + */ + function testSetVarsAtTopOfBufferedScripts() { + $this->Js->set(array('height' => 'tall', 'color' => 'purple')); + $this->Js->alert('hey you!', array('buffer' => true)); + $this->Js->confirm('Are you sure?', array('buffer' => true)); + $result = $this->Js->getBuffer(false); + + $expected = 'window.app = {"height":"tall","color":"purple"};'; + $this->assertEqual($result[0], $expected); + $this->assertEqual($result[1], 'alert("hey you!");'); + $this->assertEqual($result[2], 'confirm("Are you sure?");'); + } +} /** * JsBaseEngine Class Test case