Module: ActionView::Helpers::ScriptaculousHelper
- Included in:
- ActionView::Helpers
- Defined in:
- lib/action_view/helpers/scriptaculous_helper.rb
Overview
Provides a set of helpers for calling Scriptaculous JavaScript functions, including those which create Ajax controls and visual effects.
To be able to use these helpers, you must include the Prototype JavaScript framework and the Scriptaculous JavaScript library in your pages. See the documentation for ActionView::Helpers::JavaScriptHelper for more information on including the necessary JavaScript.
The Scriptaculous helpers’ behavior can be tweaked with various options.
See the documentation at script.aculo.us for more information on using these helpers in your application.
Constant Summary collapse
- TOGGLE_EFFECTS =
[:toggle_appear, :toggle_slide, :toggle_blind]
Instance Method Summary collapse
-
#draggable_element(element_id, options = {}) ⇒ Object
Makes the element with the DOM ID specified by
element_id
draggable. -
#draggable_element_js(element_id, options = {}) ⇒ Object
:nodoc:.
-
#drop_receiving_element(element_id, options = {}) ⇒ Object
Makes the element with the DOM ID specified by
element_id
receive dropped draggable elements (created bydraggable_element
). -
#drop_receiving_element_js(element_id, options = {}) ⇒ Object
:nodoc:.
-
#sortable_element(element_id, options = {}) ⇒ Object
Makes the element with the DOM ID specified by
element_id
sortable by drag-and-drop and make an Ajax call whenever the sort order has changed. -
#sortable_element_js(element_id, options = {}) ⇒ Object
:nodoc:.
-
#visual_effect(name, element_id = false, js_options = {}) ⇒ Object
Returns a JavaScript snippet to be used on the Ajax callbacks for starting visual effects.
Instance Method Details
#draggable_element(element_id, options = {}) ⇒ Object
Makes the element with the DOM ID specified by element_id
draggable.
Example:
<%= draggable_element("my_image", :revert => true)
You can change the behaviour with various options, see script.aculo.us for more documentation.
158 159 160 |
# File 'lib/action_view/helpers/scriptaculous_helper.rb', line 158 def draggable_element(element_id, = {}) javascript_tag(draggable_element_js(element_id, ).chop!) end |
#draggable_element_js(element_id, options = {}) ⇒ Object
:nodoc:
162 163 164 |
# File 'lib/action_view/helpers/scriptaculous_helper.rb', line 162 def draggable_element_js(element_id, = {}) #:nodoc: %(new Draggable(#{ActiveSupport::JSON.encode(element_id)}, #{()});) end |
#drop_receiving_element(element_id, options = {}) ⇒ Object
Makes the element with the DOM ID specified by element_id
receive dropped draggable elements (created by draggable_element
). and make an AJAX call. By default, the action called gets the DOM ID of the element as parameter.
Example:
<%= drop_receiving_element("my_cart", :url =>
{ :controller => "cart", :action => "add" }) %>
You can change the behaviour with various options, see script.aculo.us for more documentation.
Some of these options
include:
-
:accept
- Set this to a string or an array of strings describing the allowable CSS classes that thedraggable_element
must have in order to be accepted by thisdrop_receiving_element
. -
:confirm
- Adds a confirmation dialog. Example::confirm => "Are you sure you want to do this?"
-
:hoverclass
- If set, thedrop_receiving_element
will have this additional CSS class when an accepteddraggable_element
is hovered over it. -
:onDrop
- Called when adraggable_element
is dropped onto this element. Override this callback with a JavaScript expression to change the default drop behaviour. Example::onDrop => "function(draggable_element, droppable_element, event) { alert('I like bananas') }"
This callback gets three parameters: The Draggable element, the Droppable element and the Event object. You can extract additional information about the drop - like if the Ctrl or Shift keys were pressed - from the Event object.
-
:with
- A JavaScript expression specifying the parameters for the XMLHttpRequest. Any expressions should return a valid URL query string.
203 204 205 |
# File 'lib/action_view/helpers/scriptaculous_helper.rb', line 203 def drop_receiving_element(element_id, = {}) javascript_tag(drop_receiving_element_js(element_id, ).chop!) end |
#drop_receiving_element_js(element_id, options = {}) ⇒ Object
:nodoc:
207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/action_view/helpers/scriptaculous_helper.rb', line 207 def drop_receiving_element_js(element_id, = {}) #:nodoc: [:with] ||= "'id=' + encodeURIComponent(element.id)" [:onDrop] ||= "function(element){" + remote_function() + "}" .delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) } [:accept] = array_or_string_for_javascript([:accept]) if [:accept] [:hoverclass] = "'#{[:hoverclass]}'" if [:hoverclass] # Confirmation happens during the onDrop callback, so it can be removed from the options .delete(:confirm) if [:confirm] %(Droppables.add(#{ActiveSupport::JSON.encode(element_id)}, #{()});) end |
#sortable_element(element_id, options = {}) ⇒ Object
Makes the element with the DOM ID specified by element_id
sortable by drag-and-drop and make an Ajax call whenever the sort order has changed. By default, the action called gets the serialized sortable element as parameters.
Example:
<%= sortable_element("my_list", :url => { :action => "order" }) %>
In the example, the action gets a “my_list” array parameter containing the values of the ids of elements the sortable consists of, in the current order.
Important: For this to work, the sortable elements must have id attributes in the form “string_identifier”. For example, “item_1”. Only the identifier part of the id attribute will be serialized.
Additional options
are:
-
:format
- A regular expression to determine what to send as the serialized id to the server (the default is/^[^]*(.*)$/
). -
:constraint
- Whether to constrain the dragging to either:horizontal
or:vertical
(or false to make it unconstrained). -
:overlap
- Calculate the item overlap in the:horizontal
or:vertical
direction. -
:tag
- Which children of the container element to treat as sortable (default isli
). -
:containment
- Takes an element or array of elements to treat as potential drop targets (defaults to the original target element). -
:only
- A CSS class name or array of class names used to filter out child elements as candidates. -
:scroll
- Determines whether to scroll the list during drag operations if the list runs past the visual border. -
:tree
- Determines whether to treat nested lists as part of the main sortable list. This means that you can create multi-layer lists, and not only sort items at the same level, but drag and sort items between levels. -
:hoverclass
- If set, the Droppable will have this additional CSS class when an accepted Draggable is hovered over it. -
:handle
- Sets whether the element should only be draggable by an embedded handle. The value may be a string referencing a CSS class value (as of script.aculo.us V1.5). The first child/grandchild/etc. element found within the element that has this CSS class value will be used as the handle. -
:ghosting
- Clones the element and drags the clone, leaving the original in place until the clone is dropped (default isfalse
). -
:dropOnEmpty
- If true the Sortable container will be made into a Droppable, that can receive a Draggable (as according to the containment rules) as a child element when there are no more elements inside (default isfalse
). -
:onChange
- Called whenever the sort order changes while dragging. When dragging from one Sortable to another, the callback is called once on each Sortable. Gets the affected element as its parameter. -
:onUpdate
- Called when the drag ends and the Sortable’s order is changed in any way. When dragging from one Sortable to another, the callback is called once on each Sortable. Gets the container as its parameter.
See script.aculo.us for more documentation.
132 133 134 |
# File 'lib/action_view/helpers/scriptaculous_helper.rb', line 132 def sortable_element(element_id, = {}) javascript_tag(sortable_element_js(element_id, ).chop!) end |
#sortable_element_js(element_id, options = {}) ⇒ Object
:nodoc:
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/action_view/helpers/scriptaculous_helper.rb', line 136 def sortable_element_js(element_id, = {}) #:nodoc: [:with] ||= "Sortable.serialize(#{ActiveSupport::JSON.encode(element_id)})" [:onUpdate] ||= "function(){" + remote_function() + "}" .delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) } [:tag, :overlap, :constraint, :handle].each do |option| [option] = "'#{[option]}'" if [option] end [:containment] = array_or_string_for_javascript([:containment]) if [:containment] [:only] = array_or_string_for_javascript([:only]) if [:only] %(Sortable.create(#{ActiveSupport::JSON.encode(element_id)}, #{()});) end |
#visual_effect(name, element_id = false, js_options = {}) ⇒ Object
Returns a JavaScript snippet to be used on the Ajax callbacks for starting visual effects.
If no element_id
is given, it assumes “element” which should be a local variable in the generated JavaScript execution context. This can be used for example with drop_receiving_element
:
<%= drop_receiving_element (...), :loading => visual_effect(:fade) %>
This would fade the element that was dropped on the drop receiving element.
For toggling visual effects, you can use :toggle_appear
, :toggle_slide
, and :toggle_blind
which will alternate between appear/fade, slidedown/slideup, and blinddown/blindup respectively.
You can change the behaviour with various options, see script.aculo.us for more documentation.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/action_view/helpers/scriptaculous_helper.rb', line 41 def visual_effect(name, element_id = false, = {}) element = element_id ? ActiveSupport::JSON.encode(element_id) : "element" [:queue] = if [:queue].is_a?(Hash) '{' + [:queue].map {|k, v| k == :limit ? "#{k}:#{v}" : "#{k}:'#{v}'" }.join(',') + '}' elsif [:queue] "'#{[:queue]}'" end if [:queue] [:endcolor, :direction, :startcolor, :scaleMode, :restorecolor].each do |option| [option] = "'#{[option]}'" if [option] end if TOGGLE_EFFECTS.include? name.to_sym "Effect.toggle(#{element},'#{name.to_s.gsub(/^toggle_/,'')}',#{()});" else "new Effect.#{name.to_s.camelize}(#{element},#{()});" end end |