mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Making set vars with Js->set go to the top of the buffered scripts array. Tests added.
Updating doc blocks for JsHelper and JsBaseEngineHelper. Fixes #131
This commit is contained in:
parent
42fa6ff04a
commit
5274dce9f1
2 changed files with 67 additions and 16 deletions
|
@ -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 @@ class JsHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsHelper extends AppHelper {
|
|||
* @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 @@ class JsHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsHelper extends AppHelper {
|
|||
/**
|
||||
* 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 @@ class JsHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsHelper extends AppHelper {
|
|||
* 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 @@ class JsHelper extends AppHelper {
|
|||
* @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 @@ class JsHelper extends AppHelper {
|
|||
* 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 @@ class JsHelper extends AppHelper {
|
|||
* @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 @@ class JsHelper extends AppHelper {
|
|||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* 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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* 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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* 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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
/**
|
||||
* 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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* - `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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
*
|
||||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* - `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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
*
|
||||
* @param array $options options for serialization generation.
|
||||
* @return string completed form serialization script
|
||||
* @access public
|
||||
*/
|
||||
function serializeForm() {
|
||||
trigger_error(
|
||||
|
@ -991,8 +1028,8 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* @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 @@ class JsBaseEngineHelper extends AppHelper {
|
|||
* @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);
|
||||
|
|
|
@ -231,9 +231,6 @@ class JsHelperTestCase extends CakeTestCase {
|
|||
$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 @@ CODE;
|
|||
$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
|
||||
|
|
Loading…
Add table
Reference in a new issue