Skip to content

Commit

Permalink
Adding drag and drop() methods to prototype.
Browse files Browse the repository at this point in the history
Adding test for option pass through on _mapOptions(). Ensures that
correctly named options do not get munged.
  • Loading branch information
markstory committed Apr 11, 2009
1 parent 646cfd7 commit 02bb4e7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cake/libs/view/helpers/js.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
**/
Expand Down
53 changes: 52 additions & 1 deletion cake/libs/view/helpers/prototype_engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
)
);
/**
Expand Down Expand Up @@ -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 . ');';
}
}
?>
3 changes: 3 additions & 0 deletions cake/tests/cases/libs/view/helpers/js.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions cake/tests/cases/libs/view/helpers/prototype_engine.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
?>

0 comments on commit 02bb4e7

Please sign in to comment.