Permalink
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...
1 parent 7502671 commit 24299cf943d0489d7c085ccf0356d45ca6a6d980 @markstory markstory committed Jul 29, 2009
@@ -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,24 +931,41 @@ 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] . '}';
}
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
*
* @param array $parameters Array of parameters to convert to a query string
@@ -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,20 +340,19 @@ 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 . '}';
}
$out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
$this->selection = $slider;
return $out;
}
+
/**
* Serialize the form attached to $selector.
*
@@ -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 . '}';
}
Oops, something went wrong.

0 comments on commit 24299cf

Please sign in to comment.