Implementing wrapped callbacks in Mootools. Fixing implementation in Prototype. Adding JsBaseEngineHelper::_processOptions() to reduce duplicate code.

Test cases appended and updated.
This commit is contained in:
mark_story 2009-07-29 00:05:27 -04:00
parent 75026715d1
commit 24299cf943
5 changed files with 182 additions and 84 deletions

View file

@ -880,11 +880,6 @@ class JsBaseEngineHelper extends AppHelper {
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 @@ 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 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,23 +931,40 @@ class JsBaseEngineHelper extends AppHelper {
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] . '}';
}
return $options;
}
/**
* 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
*

View file

@ -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 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
$this->selection = '$$("' . $selector . '")';
return $this;
}
/**
* Add an event to the script cache. Operates on the currently selected elements.
*
@ -113,6 +160,7 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
$out = $this->selection . ".addEvent(\"{$type}\", $callback);";
return $out;
}
/**
* Create a domReady event. This is a special event in many libraries
*
@ -123,6 +171,7 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
$this->selection = 'window';
return $this->event('domready', $functionBody, array('stop' => false));
}
/**
* Create an iteration over the current selection result.
*
@ -133,6 +182,7 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
function each($callback) {
return $this->selection . '.each(function (item, index) {' . $callback . '});';
}
/**
* Trigger an Effect.
*
@ -172,6 +222,7 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
}
return $this->selection . '.' . $effect . ';';
}
/**
* Create an new Request.
*
@ -201,16 +252,17 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
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 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
* @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 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
* @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 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
$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 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
$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 @@ class MootoolsEngineHelper extends JsBaseEngineHelper {
$this->selection = $slider;
return $out;
}
/**
* Serialize the form attached to $selector.
*

View file

@ -76,20 +76,32 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @var array
**/
var $_callbackArguments = array(
'onSlide' => 'event',
'onChange' => 'event',
'onHover' => 'event',
'onDrop' => 'event',
'slider' => array(
'onSlide' => 'value',
'onChange' => 'value',
),
'drag' => array(
'onStart' => 'event',
'change' => 'event',
'onDrag' => 'event',
'onUpdate' => '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 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
$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 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @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 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @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 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
* @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 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
$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 file

@ -168,7 +168,8 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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,7 +177,8 @@ class MooEngineHelperTestCase extends CakeTestCase {
$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);
@ -187,7 +189,8 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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);
@ -200,10 +203,21 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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);
@ -256,11 +272,21 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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,6 +300,7 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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);
@ -286,10 +313,21 @@ class MooEngineHelperTestCase extends CakeTestCase {
'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 file

@ -329,7 +329,7 @@ class PrototypeEngineHelperTestCase extends CakeTestCase {
'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);
}