Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementing wrapped callbacks in Mootools. Fixing implementation in …

…Prototype. Adding JsBaseEngineHelper::_processOptions() to reduce duplicate code.

Test cases appended and updated.
  • Loading branch information...
commit 24299cf943d0489d7c085ccf0356d45ca6a6d980 1 parent 7502671
@markstory markstory authored
View
38 cake/libs/view/helpers/js.php
@@ -880,11 +880,6 @@ function serializeForm() {
function _parseOptions($options, $safeKeys = array()) {
$out = array();
$safeKeys = array_flip($safeKeys);
- $wrapCallbacks = true;
- if (isset($options['wrapCallbacks'])) {
- $wrapCallbacks = $options['wrapCallbacks'];
- unset($options['wrapCallbacks']);
- }
foreach ($options as $key => $value) {
if (!is_int($value) && !isset($safeKeys[$key])) {
$value = $this->value($value);
@@ -923,11 +918,11 @@ 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 Keys that contain callbacks
+ * @param string $callbacks Additional Keys that contain callbacks
* @access protected
* @return array Array of options with callbacks added.
**/
- function _prepareCallbacks($options, $callbacks = array()) {
+ function _prepareCallbacks($method, $options, $callbacks = array()) {
$wrapCallbacks = true;
if (isset($options['wrapCallbacks'])) {
$wrapCallbacks = $options['wrapCallbacks'];
@@ -936,17 +931,19 @@ function _prepareCallbacks($options, $callbacks = array()) {
if (!$wrapCallbacks) {
return $options;
}
+ $callbackOptions = array();
+ if (isset($this->_callbackArguments[$method])) {
+ $callbackOptions = $this->_callbackArguments[$method];
+ }
+ $callbacks = array_unique(array_merge(array_keys($callbackOptions), (array)$callbacks));
foreach ($callbacks as $callback) {
- if (!isset($options[$callback])) {
+ if (empty($options[$callback])) {
continue;
}
$args = null;
- if (isset($this->_callbackArguments[$callback])) {
- $args = $this->_callbackArguments[$callback];
- }
- if (empty($args)) {
- continue;
+ if (!empty($callbackOptions[$callback])) {
+ $args = $callbackOptions[$callback];
}
$options[$callback] = 'function (' . $args . ') {' . $options[$callback] . '}';
}
@@ -954,6 +951,21 @@ function _prepareCallbacks($options, $callbacks = array()) {
}
/**
+ * Conveinence wrapper method for all common option processing steps.
+ * Runs _mapOptions, _prepareCallbacks, and _parseOptions in order.
+ *
+ * @param string $method Name of method processing options for.
+ * @param array $options Array of options to process.
+ * @return string Parsed options string.
+ **/
+ function _processOptions($method, $options) {
+ $options = $this->_mapOptions($method, $options);
+ $options = $this->_prepareCallbacks($method, $options);
+ $options = $this->_parseOptions($options, array_keys($this->_callbackArguments[$method]));
+ return $options;
+ }
+
+/**
* Convert an array of data into a query string
*
* @param array $parameters Array of parameters to convert to a query string
View
84 cake/libs/view/helpers/mootools_engine.php
@@ -66,6 +66,52 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
'step' => 'steps'
)
);
+
+/**
+ * Contains a list of callback names -> default arguments.
+ *
+ * @var array
+ **/
+ var $_callbackArguments = array(
+ 'slider' => array(
+ 'onTick' => 'position',
+ 'onChange' => 'step',
+ 'onComplete' => 'event'
+ ),
+ 'request' => array(
+ 'onRequest' => '',
+ 'onComplete' => '',
+ 'onCancel' => '',
+ 'onSuccess' => 'responseText, responseXML',
+ 'onFailure' => 'xhr',
+ 'onException' => 'headerName, value',
+ ),
+ 'drag' => array(
+ 'onBeforeStart' => 'element',
+ 'onStart' => 'element',
+ 'onSnap' => 'element',
+ 'onDrag' => 'element, event',
+ 'onComplete' => 'element, event',
+ 'onCancel' => 'element',
+ ),
+ 'drop' => array(
+ 'onBeforeStart' => 'element',
+ 'onStart' => 'element',
+ 'onSnap' => 'element',
+ 'onDrag' => 'element, event',
+ 'onComplete' => 'element, event',
+ 'onCancel' => 'element',
+ 'onDrop' => 'element, droppable, event',
+ 'onLeave' => 'element, droppable',
+ 'onEnter' => 'element, droppable',
+ ),
+ 'sortable' => array(
+ 'onStart' => 'element, clone',
+ 'onSort' => 'element, clone',
+ 'onComplete' => 'element',
+ )
+ );
+
/**
* Create javascript selector for a CSS rule
*
@@ -86,6 +132,7 @@ function get($selector) {
$this->selection = '$$("' . $selector . '")';
return $this;
}
+
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
@@ -113,6 +160,7 @@ function event($type, $callback, $options = array()) {
$out = $this->selection . ".addEvent(\"{$type}\", $callback);";
return $out;
}
+
/**
* Create a domReady event. This is a special event in many libraries
*
@@ -123,6 +171,7 @@ function domReady($functionBody) {
$this->selection = 'window';
return $this->event('domready', $functionBody, array('stop' => false));
}
+
/**
* Create an iteration over the current selection result.
*
@@ -133,6 +182,7 @@ function domReady($functionBody) {
function each($callback) {
return $this->selection . '.each(function (item, index) {' . $callback . '});';
}
+
/**
* Trigger an Effect.
*
@@ -172,9 +222,10 @@ function effect($name, $options = array()) {
}
return $this->selection . '.' . $effect . ';';
}
+
/**
* Create an new Request.
- *
+ *
* Requires `Request`. If you wish to use 'update' key you must have ```Request.HTML```
* if you wish to do Json requests you will need ```JSON``` and ```Request.JSON```.
*
@@ -201,16 +252,17 @@ function request($url, $options = array()) {
unset($options['data']);
}
$options['url'] = $url;
- $callbacks = array('onComplete', 'onFailure', 'onRequest', 'onSuccess', 'onCancel', 'onException');
+ $options = $this->_prepareCallbacks('request', $options);
if (isset($options['dataExpression'])) {
$callbacks[] = 'data';
unset($options['dataExpression']);
} elseif (!empty($data)) {
$data = $this->object($data);
}
- $options = $this->_parseOptions($options, $callbacks);
+ $options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
return "var jsRequest = new Request$type({{$options}}).send($data);";
}
+
/**
* Create a sortable element.
*
@@ -221,11 +273,10 @@ function request($url, $options = array()) {
* @see JsHelper::sortable() for options list.
**/
function sortable($options = array()) {
- $options = $this->_mapOptions('sortable', $options);
- $callbacks = array('onStart', 'onSort', 'onComplete');
- $options = $this->_parseOptions($options, $callbacks);
+ $options = $this->_processOptions('sortable', $options);
return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
}
+
/**
* Create a Draggable element.
*
@@ -236,11 +287,10 @@ function sortable($options = array()) {
* @see JsHelper::drag() for options list.
**/
function drag($options = array()) {
- $options = $this->_mapOptions('drag', $options);
- $callbacks = array('onBeforeStart', 'onStart', 'onSnap', 'onDrag', 'onComplete');
- $options = $this->_parseOptions($options, $callbacks);
+ $options = $this->_processOptions('drag', $options);
return $this->selection . '.makeDraggable({' . $options . '});';
}
+
/**
* Create a Droppable element.
*
@@ -267,16 +317,15 @@ function drop($options = array()) {
$this->get($options['drag']);
unset($options['drag']);
- $options = $this->_mapOptions('drop', $options);
- $options = $this->_mapOptions('drag', $options);
- $callbacks = array('onBeforeStart', 'onStart', 'onSnap', 'onDrag', 'onComplete', 'onDrop',
- 'onLeave', 'onEnter', 'droppables');
-
- $optionString = $this->_parseOptions($options, $callbacks);
+ $options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
+ $options = $this->_prepareCallbacks('drop', $options);
+ $safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
+ $optionString = $this->_parseOptions($options, $safe);
$out = $this->selection . '.makeDraggable({' . $optionString . '});';
$this->selection = $options['droppables'];
return $out;
}
+
/**
* Create a slider control
*
@@ -291,13 +340,11 @@ function slider($options = array()) {
$this->get($options['handle']);
unset($options['handle']);
- $callbacks = array('onStart', 'onTick', 'onChange', 'onComplete');
- $options = $this->_mapOptions('slider', $options);
if (isset($options['min']) && isset($options['max'])) {
$options['range'] = array($options['min'], $options['max']);
unset($options['min'], $options['max']);
}
- $optionString = $this->_parseOptions($options, $callbacks);
+ $optionString = $this->_processOptions('slider', $options);
if (!empty($optionString)) {
$optionString = ', {' . $optionString . '}';
}
@@ -305,6 +352,7 @@ function slider($options = array()) {
$this->selection = $slider;
return $out;
}
+
/**
* Serialize the form attached to $selector.
*
View
70 cake/libs/view/helpers/prototype_engine.php
@@ -76,20 +76,32 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @var array
**/
var $_callbackArguments = array(
- 'onSlide' => 'event',
- 'onChange' => 'event',
- 'onHover' => 'event',
- 'onDrop' => 'event',
- 'onStart' => 'event',
- 'change' => 'event',
- 'onDrag' => 'event',
- 'onUpdate' => 'event',
- 'onEnd' => 'event',
- 'onCreate' => 'transport',
- 'onComplete' => 'transport',
- 'onFailure' => 'response, jsonHeader',
- 'onRequest' => 'transport',
- 'onSuccess' => 'response, jsonHeader'
+ 'slider' => array(
+ 'onSlide' => 'value',
+ 'onChange' => 'value',
+ ),
+ 'drag' => array(
+ 'onStart' => 'event',
+ 'onDrag' => 'event',
+ 'change' => 'draggable',
+ 'onEnd' => 'event',
+ ),
+ 'drop' => array(
+ 'onHover' => 'draggable, droppable, event',
+ 'onDrop' => 'draggable, droppable, event',
+ ),
+ 'request' => array(
+ 'onCreate' => 'transport',
+ 'onComplete' => 'transport',
+ 'onFailure' => 'response, jsonHeader',
+ 'onRequest' => 'transport',
+ 'onSuccess' => 'response, jsonHeader'
+ ),
+ 'sortable' => array(
+ 'onStart' => 'element',
+ 'onChange' => 'element',
+ 'onUpdate' => 'element',
+ ),
);
/**
@@ -223,13 +235,14 @@ function request($url, $options = array()) {
$type = '.Updater';
unset($options['update'], $options['type']);
}
- $callbacks = array('onCreate', 'onComplete', 'onFailure', 'onRequest', 'onSuccess');
- $options = $this->_prepareCallbacks($options, $callbacks);
+ $safe = array();
if (isset($options['dataExpression'])) {
- $callbacks[] = 'parameters';
+ $safe[] = 'parameters';
unset($options['dataExpression']);
}
- $options = $this->_parseOptions($options, $callbacks);
+ $safe = array_merge($safe, array_keys($this->_callbackArguments['request']));
+ $options = $this->_prepareCallbacks('request', $options, $safe);
+ $options = $this->_parseOptions($options, $safe);
if (!empty($options)) {
$options = ', {' . $options . '}';
}
@@ -245,10 +258,7 @@ function request($url, $options = array()) {
* @see JsHelper::sortable() for options list.
**/
function sortable($options = array()) {
- $options = $this->_mapOptions('sortable', $options);
- $callbacks = array('onStart', 'change', 'onDrag', 'onDrop', 'onChange', 'onUpdate', 'onEnd');
- $options = $this->_prepareCallbacks($options, $callbacks);
- $options = $this->_parseOptions($options, $callbacks);
+ $options = $this->_processOptions('sortable', $options);
if (!empty($options)) {
$options = ', {' . $options . '}';
}
@@ -264,10 +274,7 @@ function sortable($options = array()) {
* @see JsHelper::draggable() for options list.
**/
function drag($options = array()) {
- $options = $this->_mapOptions('drag', $options);
- $callbacks = array('onStart', 'change', 'onDrag', 'onEnd');
- $options = $this->_prepareCallbacks($options, $callbacks);
- $options = $this->_parseOptions($options, $callbacks);
+ $options = $this->_processOptions('drag', $options);
if (!empty($options)) {
$options = ', {' . $options . '}';
}
@@ -286,10 +293,7 @@ function drag($options = array()) {
* @see JsHelper::droppable() for options list.
**/
function drop($options = array()) {
- $options = $this->_mapOptions('drop', $options);
- $callbacks = array('onHover', 'onDrop');
- $options = $this->_prepareCallbacks($options, $callbacks);
- $options = $this->_parseOptions($options, $callbacks);
+ $options = $this->_processOptions('drop', $options);
if (!empty($options)) {
$options = ', {' . $options . '}';
}
@@ -309,15 +313,11 @@ function slider($options = array()) {
$this->get($options['handle']);
unset($options['handle']);
- $callbacks = array('onSlide', 'onChange');
- $options = $this->_mapOptions('slider', $options);
- $options = $this->_prepareCallbacks($options, $callbacks);
-
if (isset($options['min']) && isset($options['max'])) {
$options['range'] = array($options['min'], $options['max']);
unset($options['min'], $options['max']);
}
- $optionString = $this->_parseOptions($options, $callbacks);
+ $optionString = $this->_processOptions('slider', $options);
if (!empty($optionString)) {
$optionString = ', {' . $optionString . '}';
}
View
72 cake/tests/cases/libs/view/helpers/mootools_engine.test.php
@@ -49,23 +49,23 @@ function testSelector() {
$result = $this->Moo->get('#content');
$this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, '$("content")');
-
+
$result = $this->Moo->get('a .remove');
$this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, '$$("a .remove")');
-
+
$result = $this->Moo->get('document');
$this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, "$(document)");
-
+
$result = $this->Moo->get('window');
$this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, "$(window)");
-
+
$result = $this->Moo->get('ul');
$this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, '$$("ul")');
-
+
$result = $this->Moo->get('#some_long-id.class');
$this->assertEqual($result, $this->Moo);
$this->assertEqual($this->Moo->selection, '$$("#some_long-id.class")');
@@ -158,7 +158,7 @@ function testRequest() {
$result = $this->Moo->request(array('controller' => 'posts', 'action' => 'view', 1));
$expected = 'var jsRequest = new Request({url:"\\/posts\\/view\\/1"}).send();';
$this->assertEqual($result, $expected);
-
+
$result = $this->Moo->request('/posts/view/1', array('update' => 'content'));
$expected = 'var jsRequest = new Request.HTML({update:"content", url:"\\/posts\\/view\\/1"}).send();';
$this->assertEqual($result, $expected);
@@ -168,7 +168,8 @@ function testRequest() {
'complete' => 'doSuccess',
'error' => 'handleError',
'type' => 'json',
- 'data' => array('name' => 'jim', 'height' => '185cm')
+ 'data' => array('name' => 'jim', 'height' => '185cm'),
+ 'wrapCallbacks' => false
));
$expected = 'var jsRequest = new Request.JSON({method:"post", onComplete:doSuccess, onFailure:handleError, url:"\\/people\\/edit\\/1"}).send({"name":"jim","height":"185cm"});';
$this->assertEqual($result, $expected);
@@ -176,22 +177,24 @@ function testRequest() {
$result = $this->Moo->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doSuccess',
- 'update' => '#update-zone'
+ 'update' => '#update-zone',
+ 'wrapCallbacks' => false
));
$expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doSuccess, update:"update-zone", url:"\\/people\\/edit\\/1"}).send();';
$this->assertEqual($result, $expected);
-
+
$result = $this->Moo->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doComplete',
'success' => 'doSuccess',
'error' => 'doFailure',
'before' => 'doBefore',
- 'update' => 'update-zone'
+ 'update' => 'update-zone',
+ 'wrapCallbacks' => false
));
$expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doComplete, onFailure:doFailure, onRequest:doBefore, onSuccess:doSuccess, update:"update-zone", url:"\\/people\\/edit\\/1"}).send();';
$this->assertEqual($result, $expected);
-
+
$result = $this->Moo->request('/people/edit/1', array(
'method' => 'post',
'complete' => 'doComplete',
@@ -200,10 +203,21 @@ function testRequest() {
'before' => 'doBefore',
'update' => 'update-zone',
'dataExpression' => true,
- 'data' => '$("foo").toQueryString()'
+ 'data' => '$("foo").toQueryString()',
+ 'wrapCallbacks' => false
));
$expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doComplete, onFailure:doFailure, onRequest:doBefore, onSuccess:doSuccess, update:"update-zone", url:"\\/people\\/edit\\/1"}).send($("foo").toQueryString());';
$this->assertEqual($result, $expected);
+
+ $result = $this->Moo->request('/people/edit/1', array(
+ 'method' => 'post',
+ 'before' => 'doBefore',
+ 'success' => 'doSuccess',
+ 'complete' => 'doComplete',
+ 'update' => '#update-zone',
+ ));
+ $expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:function () {doComplete}, onRequest:function () {doBefore}, onSuccess:function (responseText, responseXML) {doSuccess}, update:"update-zone", url:"\\/people\\/edit\\/1"}).send();';
+ $this->assertEqual($result, $expected);
}
/**
* test sortable list generation
@@ -218,6 +232,7 @@ function testSortable() {
'start' => 'onStart',
'complete' => 'onStop',
'sort' => 'onSort',
+ 'wrapCallbacks' => false
));
$expected = 'var jsSortable = new Sortables($("myList"), {constrain:"parent", onComplete:onStop, onSort:onSort, onStart:onStart, snap:5});';
$this->assertEqual($result, $expected);
@@ -233,7 +248,8 @@ function testDrag() {
'start' => 'onStart',
'drag' => 'onDrag',
'stop' => 'onStop',
- 'snapGrid' => array(10,10)
+ 'snapGrid' => array(10,10),
+ 'wrapCallbacks' => false
));
$expected = '$("drag-me").makeDraggable({onComplete:onStop, onDrag:onDrag, onStart:onStart, snap:[10,10]});';
$this->assertEqual($result, $expected);
@@ -251,16 +267,26 @@ function testDrop() {
'leave' => 'onLeave',
'hover' => 'onHover',
));
-
+
$result = $this->Moo->drop(array(
'drop' => 'onDrop',
'leave' => 'onLeave',
'hover' => 'onHover',
- 'drag' => '#my-drag'
+ 'drag' => '#my-drag',
+ 'wrapCallbacks' => false
));
$expected = '$("my-drag").makeDraggable({droppables:$("drop-me"), onDrop:onDrop, onEnter:onHover, onLeave:onLeave});';
$this->assertEqual($result, $expected);
$this->assertEqual($this->Moo->selection, '$("drop-me")');
+
+ $result = $this->Moo->drop(array(
+ 'drop' => 'onDrop',
+ 'leave' => 'onLeave',
+ 'hover' => 'onHover',
+ 'drag' => '#my-drag',
+ ));
+ $expected = '$("my-drag").makeDraggable({droppables:$("drop-me"), onDrop:function (element, droppable, event) {onDrop}, onEnter:function (element, droppable) {onHover}, onLeave:function (element, droppable) {onLeave}});';
+ $this->assertEqual($result, $expected);
}
/**
* test slider generation
@@ -274,11 +300,12 @@ function testSlider() {
'complete' => 'onComplete',
'change' => 'onChange',
'direction' => 'horizontal',
+ 'wrapCallbacks' => false
));
$expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:onChange, onComplete:onComplete});';
$this->assertEqual($result, $expected);
$this->assertEqual($this->Moo->selection, '$("slider")');
-
+
$this->Moo->get('#slider');
$result = $this->Moo->slider(array(
'handle' => '#my-handle',
@@ -286,10 +313,21 @@ function testSlider() {
'change' => 'onChange',
'direction' => 'horizontal',
'min' => 10,
- 'max' => 40
+ 'max' => 40,
+ 'wrapCallbacks' => false
));
$expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:onChange, onComplete:onComplete, range:[10,40]});';
$this->assertEqual($result, $expected);
+
+ $this->Moo->get('#slider');
+ $result = $this->Moo->slider(array(
+ 'handle' => '#my-handle',
+ 'complete' => 'complete;',
+ 'change' => 'change;',
+ 'direction' => 'horizontal',
+ ));
+ $expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:function (step) {change;}, onComplete:function (event) {complete;}});';
+ $this->assertEqual($result, $expected);
}
/**
* test the serializeForm implementation.
View
2  cake/tests/cases/libs/view/helpers/prototype_engine.test.php
@@ -329,7 +329,7 @@ function testSlider() {
'complete' => 'complete();',
'value' => 4
));
- $expected = 'var jsSlider = new Control.Slider($("handle"), $("element"), {onChange:function (event) {complete();}, onSlide:function (event) {change();}, sliderValue:4});';
+ $expected = 'var jsSlider = new Control.Slider($("handle"), $("element"), {onChange:function (value) {complete();}, onSlide:function (value) {change();}, sliderValue:4});';
$this->assertEqual($result, $expected);
}
Please sign in to comment.
Something went wrong with that request. Please try again.