Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding drag and drop() methods to prototype.

Adding test for option pass through on _mapOptions(). Ensures that
correctly named options do not get munged.
  • Loading branch information...
commit 02bb4e71f3c08dd82d9df8a8b5dbc048a9c5b8f0 1 parent 646cfd7
@markstory markstory authored
View
4 cake/libs/view/helpers/js.php
@@ -295,7 +295,9 @@ class JsBaseEngineHelper extends AppHelper {
**/
var $selection;
/**
- * Collection of option maps.
+ * Collection of option maps. Option maps allow other helpers to use generic names for engine
+ * callbacks and options. Allowing uniform code access for all engine types. Their use is optional
+ * for end user use though.
*
* @var array
**/
View
53 cake/libs/view/helpers/prototype_engine.php
@@ -47,6 +47,18 @@ class PrototypeEngineHelper extends JsBaseEngineHelper {
'sort' => 'onDrag',
'complete' => 'onDrop',
'distance' => 'snap',
+ ),
+ 'drag' => array(
+ 'snapGrid' => 'snap',
+ 'container' => 'constraint',
+ 'stop' => 'onEnd',
+ 'start' => 'onStart',
+ 'drag' => 'onDrag',
+ ),
+ 'drop' => array(
+ 'hover' => 'onHover',
+ 'drop' => 'onDrop',
+ 'hoverClass' => 'hoverclass',
)
);
/**
@@ -198,7 +210,46 @@ function sortable($options = array()) {
$options = $this->_mapOptions('sortable', $options);
$callbacks = array('onStart', 'change', 'onDrag', 'onDrop');
$options = $this->_parseOptions($options, $callbacks);
- return 'var jsSortable = Sortable.create(' . $this->selection . ', {' . $options . '});';
+ if (!empty($options)) {
+ $options = ', {' . $options . '}';
+ }
+ return 'var jsSortable = Sortable.create(' . $this->selection . $options . ');';
+ }
+/**
+ * Create a Draggable element.
+ *
+ * #### Note: Requires scriptaculous to be loaded.
+ *
+ * @param array $options Array of options for the draggable.
+ * @return string Completed draggable script.
+ * @see JsHelper::draggable() for options list.
+ **/
+ function drag($options = array()) {
+ $options = $this->_mapOptions('drag', $options);
+ $callbacks = array('onStart', 'change', 'onDrag', 'onEnd');
+ $options = $this->_parseOptions($options, $callbacks);
+ if (!empty($options)) {
+ $options = ', {' . $options . '}';
+ }
+ return 'var jsDrag = new Draggable(' . $this->selection . $options . ');';
+ }
+/**
+ * Create a Droppable element.
+ *
+ * #### Note: Requires scriptaculous to be loaded.
+ *
+ * @param array $options Array of options for the droppable.
+ * @return string Completed draggable script.
+ * @see JsHelper::droppable() for options list.
+ **/
+ function drop($options = array()) {
+ $options = $this->_mapOptions('drop', $options);
+ $callbacks = array('onHover', 'onDrop');
+ $options = $this->_parseOptions($options, $callbacks);
+ if (!empty($options)) {
+ $options = ', {' . $options . '}';
+ }
+ return 'Droppables.add(' . $this->selection . $options . ');';
}
}
?>
View
3  cake/tests/cases/libs/view/helpers/js.test.php
@@ -402,6 +402,9 @@ function testOptionMapping() {
$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
+
+ $result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
+ $this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
}
/**
* test that option parsing escapes strings and saves what is supposed to be saved.
View
31 cake/tests/cases/libs/view/helpers/prototype_engine.test.php
@@ -225,5 +225,36 @@ function testSortable() {
$expected = 'var jsSortable = Sortable.create($("myList"), {onDrag:onSort, onDrop:onComplete, onStart:onStart, snap:5});';
$this->assertEqual($result, $expected);
}
+/**
+ * test drag() method
+ *
+ * @return void
+ **/
+ function testDrag() {
+ $this->Proto->get('#element');
+ $result = $this->Proto->drag(array(
+ 'start' => 'onStart',
+ 'drag' => 'onDrag',
+ 'stop' => 'onStop',
+ 'snapGrid' => array(10, 10),
+ ));
+ $expected = 'var jsDrag = new Draggable($("element"), {onDrag:onDrag, onEnd:onStop, onStart:onStart, snap:[10,10]});';
+ $this->assertEqual($result, $expected);
+ }
+/**
+ * test drop() method
+ *
+ * @return void
+ **/
+ function testDrop() {
+ $this->Proto->get('#element');
+ $result = $this->Proto->drop(array(
+ 'hover' => 'onHover',
+ 'drop' => 'onDrop',
+ 'accept' => '.drag-me'
+ ));
+ $expected = 'Droppables.add($("element"), {accept:".drag-me", onDrop:onDrop, onHover:onHover});';
+ $this->assertEqual($result, $expected);
+ }
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.