Drag and Drop with script.aculo.us and Rails - Draggable options
(Page 3 of 4 )
As with the Effect.* methods,Draggable.initializetakes a JavaScript hash of options to customize their behavior. Thedraggable_elementhelper takes a Ruby hash and converts it to JavaScript.
revert, if set totrue, causes the element to return back to its original location after being dragged. The value can also be a function, which will get called when a drag ends, to determine whether the element should be reverted. For example:
<div id="revertDIV" class="green box">revert</div>
<%= draggable_element :revertDIV, :revert => true %>
<div id="functionRevertDIV" class="green box">function revert</div>
<%= draggable_element :functionRevertDIV,
:revert => "function(el){
return Position.page(el)[0] > 100; }" %>
In the second example,:revertis a function that is passed a reference to the element when the dragging stops. In this case, it reverts the drag only if the position of the element is more than 100 pixels from the left edge of the window.
ghosting, if set totrue, will clone when a drag starts, leaving the original in place until the drag ends. For example:
<div id="ghostingDIV" class="green box">ghosting</div>
<%= draggable_element :ghostingDIV, :ghosting => true %>
handleallows for a subelement to be used as the handle—the part that can be clicked on to start the drag. The value should be a JavaScript expression that will evaluate to an element ID, or an element reference. For example:
<div id="handleDIV" class="green box">
<span id="myHandle">handle</span>
</div>
<%= draggable_element :handleDIV, :handle => "'myHandle'" %>
Note thatmyHandleis in two sets of quotes—that’s because it’s a JavaScript expression that needs to evaluate to a string.
changecan be set to a function that will be called every time the draggable is moved while dragging. The callback function gets the draggable as a parameter. For example:
<div id="changeDIV" class="green box">change</div>
<%= draggable_element :changeDIV, :change => "function(draggable) {
draggable.element.innerHTML=draggable. currentDelta();
}" %>
constraint, if set tohorizontalorvertical, will constrain the element to that dimension. It is evaluated as a JavaScript expression, so specifying a DOM element ID requires two sets of quote marks. For example:
<div id="constraintDIV" class="green box">constraint</div
>