2007-01-28 02:16:55 -05:00
|
|
|
require 'action_view/helpers/javascript_helper'
|
2008-11-24 13:14:45 -05:00
|
|
|
require 'active_support/json'
|
2005-11-14 17:28:47 -05:00
|
|
|
|
|
|
|
module ActionView
|
|
|
|
module Helpers
|
2010-01-30 16:20:00 -05:00
|
|
|
# Provides a set of helpers for calling Scriptaculous JavaScript
|
2005-11-14 17:28:47 -05:00
|
|
|
# functions, including those which create Ajax controls and visual effects.
|
|
|
|
#
|
2010-01-30 16:20:00 -05:00
|
|
|
# To be able to use these helpers, you must include the Prototype
|
|
|
|
# JavaScript framework and the Scriptaculous JavaScript library in your
|
2005-11-14 17:28:47 -05:00
|
|
|
# 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 http://script.aculo.us for more information on
|
|
|
|
# using these helpers in your application.
|
|
|
|
module ScriptaculousHelper
|
2005-12-25 13:53:22 -05:00
|
|
|
unless const_defined? :TOGGLE_EFFECTS
|
|
|
|
TOGGLE_EFFECTS = [:toggle_appear, :toggle_slide, :toggle_blind]
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-11-14 17:28:47 -05:00
|
|
|
# Returns a JavaScript snippet to be used on the Ajax callbacks for
|
|
|
|
# starting visual effects.
|
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# If no +element_id+ is given, it assumes "element" which should be a local
|
2010-01-30 16:20:00 -05:00
|
|
|
# variable in the generated JavaScript execution context. This can be
|
2008-05-25 07:29:00 -04:00
|
|
|
# used for example with +drop_receiving_element+:
|
2005-11-14 17:28:47 -05:00
|
|
|
#
|
2007-09-28 10:18:47 -04:00
|
|
|
# <%= drop_receiving_element (...), :loading => visual_effect(:fade) %>
|
2005-11-14 17:28:47 -05:00
|
|
|
#
|
2010-01-30 16:20:00 -05:00
|
|
|
# This would fade the element that was dropped on the drop receiving
|
2005-11-14 17:28:47 -05:00
|
|
|
# element.
|
|
|
|
#
|
2008-05-02 09:45:23 -04:00
|
|
|
# For toggling visual effects, you can use <tt>:toggle_appear</tt>, <tt>:toggle_slide</tt>, and
|
|
|
|
# <tt>:toggle_blind</tt> which will alternate between appear/fade, slidedown/slideup, and
|
2005-12-25 13:53:22 -05:00
|
|
|
# blinddown/blindup respectively.
|
|
|
|
#
|
2005-11-14 17:28:47 -05:00
|
|
|
# You can change the behaviour with various options, see
|
|
|
|
# http://script.aculo.us for more documentation.
|
|
|
|
def visual_effect(name, element_id = false, js_options = {})
|
2009-04-23 03:08:40 -04:00
|
|
|
element = element_id ? ActiveSupport::JSON.encode(element_id) : "element"
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2006-02-27 05:37:59 -05:00
|
|
|
js_options[:queue] = if js_options[:queue].is_a?(Hash)
|
|
|
|
'{' + js_options[:queue].map {|k, v| k == :limit ? "#{k}:#{v}" : "#{k}:'#{v}'" }.join(',') + '}'
|
|
|
|
elsif js_options[:queue]
|
|
|
|
"'#{js_options[:queue]}'"
|
|
|
|
end if js_options[:queue]
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2007-09-02 19:51:27 -04:00
|
|
|
[:endcolor, :direction, :startcolor, :scaleMode, :restorecolor].each do |option|
|
|
|
|
js_options[option] = "'#{js_options[option]}'" if js_options[option]
|
|
|
|
end
|
|
|
|
|
2005-12-25 13:53:22 -05:00
|
|
|
if TOGGLE_EFFECTS.include? name.to_sym
|
|
|
|
"Effect.toggle(#{element},'#{name.to_s.gsub(/^toggle_/,'')}',#{options_for_javascript(js_options)});"
|
|
|
|
else
|
|
|
|
"new Effect.#{name.to_s.camelize}(#{element},#{options_for_javascript(js_options)});"
|
|
|
|
end
|
2005-11-14 17:28:47 -05:00
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-11-14 17:28:47 -05:00
|
|
|
# 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:
|
2008-05-25 07:29:00 -04:00
|
|
|
#
|
2005-11-14 17:28:47 -05:00
|
|
|
# <%= sortable_element("my_list", :url => { :action => "order" }) %>
|
|
|
|
#
|
2010-01-30 16:20:00 -05:00
|
|
|
# In the example, the action gets a "my_list" array parameter
|
|
|
|
# containing the values of the ids of elements the sortable consists
|
2005-11-14 17:28:47 -05:00
|
|
|
# of, in the current order.
|
|
|
|
#
|
2006-10-22 20:01:51 -04:00
|
|
|
# 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.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2007-12-05 10:27:01 -05:00
|
|
|
# Additional +options+ are:
|
2006-10-22 20:01:51 -04:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:format</tt> - A regular expression to determine what to send as the
|
|
|
|
# serialized id to the server (the default is <tt>/^[^_]*_(.*)$/</tt>).
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:constraint</tt> - Whether to constrain the dragging to either
|
|
|
|
# <tt>:horizontal</tt> or <tt>:vertical</tt> (or false to make it unconstrained).
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:overlap</tt> - Calculate the item overlap in the <tt>:horizontal</tt>
|
|
|
|
# or <tt>:vertical</tt> direction.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:tag</tt> - Which children of the container element to treat as
|
|
|
|
# sortable (default is <tt>li</tt>).
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:containment</tt> - Takes an element or array of elements to treat as
|
|
|
|
# potential drop targets (defaults to the original target element).
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-09-13 15:28:01 -04:00
|
|
|
# * <tt>:only</tt> - A CSS class name or array of class names used to filter
|
2008-05-25 07:29:00 -04:00
|
|
|
# out child elements as candidates.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:scroll</tt> - Determines whether to scroll the list during drag
|
|
|
|
# operations if the list runs past the visual border.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:tree</tt> - 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.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:hoverclass</tt> - If set, the Droppable will have this additional CSS class
|
2010-01-30 16:20:00 -05:00
|
|
|
# when an accepted Draggable is hovered over it.
|
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:handle</tt> - 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.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:ghosting</tt> - Clones the element and drags the clone, leaving
|
|
|
|
# the original in place until the clone is dropped (default is <tt>false</tt>).
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:dropOnEmpty</tt> - 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
|
|
|
|
# is <tt>false</tt>).
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:onChange</tt> - 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.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:onUpdate</tt> - 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.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2007-12-05 10:01:47 -05:00
|
|
|
# See http://script.aculo.us for more documentation.
|
2005-11-14 17:28:47 -05:00
|
|
|
def sortable_element(element_id, options = {})
|
2006-02-26 09:20:21 -05:00
|
|
|
javascript_tag(sortable_element_js(element_id, options).chop!)
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2006-02-26 09:20:21 -05:00
|
|
|
def sortable_element_js(element_id, options = {}) #:nodoc:
|
2009-04-23 03:08:40 -04:00
|
|
|
options[:with] ||= "Sortable.serialize(#{ActiveSupport::JSON.encode(element_id)})"
|
2005-11-14 17:28:47 -05:00
|
|
|
options[:onUpdate] ||= "function(){" + remote_function(options) + "}"
|
|
|
|
options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) }
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-11-14 17:28:47 -05:00
|
|
|
[:tag, :overlap, :constraint, :handle].each do |option|
|
|
|
|
options[option] = "'#{options[option]}'" if options[option]
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2005-11-14 17:28:47 -05:00
|
|
|
options[:containment] = array_or_string_for_javascript(options[:containment]) if options[:containment]
|
|
|
|
options[:only] = array_or_string_for_javascript(options[:only]) if options[:only]
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
%(Sortable.create(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
|
2005-11-14 17:28:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Makes the element with the DOM ID specified by +element_id+ draggable.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# <%= draggable_element("my_image", :revert => true)
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2005-11-14 17:28:47 -05:00
|
|
|
# You can change the behaviour with various options, see
|
2006-02-26 09:20:21 -05:00
|
|
|
# http://script.aculo.us for more documentation.
|
2005-11-14 17:28:47 -05:00
|
|
|
def draggable_element(element_id, options = {})
|
2006-02-26 09:20:21 -05:00
|
|
|
javascript_tag(draggable_element_js(element_id, options).chop!)
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2006-02-26 09:20:21 -05:00
|
|
|
def draggable_element_js(element_id, options = {}) #:nodoc:
|
2009-04-23 03:08:40 -04:00
|
|
|
%(new Draggable(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
|
2005-11-14 17:28:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Makes the element with the DOM ID specified by +element_id+ receive
|
2008-05-25 07:29:00 -04:00
|
|
|
# dropped draggable elements (created by +draggable_element+).
|
2010-01-30 16:20:00 -05:00
|
|
|
# and make an AJAX call. By default, the action called gets the DOM ID
|
2005-11-14 17:28:47 -05:00
|
|
|
# of the element as parameter.
|
|
|
|
#
|
|
|
|
# Example:
|
2010-01-30 16:20:00 -05:00
|
|
|
# <%= drop_receiving_element("my_cart", :url =>
|
2005-11-14 17:28:47 -05:00
|
|
|
# { :controller => "cart", :action => "add" }) %>
|
|
|
|
#
|
|
|
|
# You can change the behaviour with various options, see
|
|
|
|
# http://script.aculo.us for more documentation.
|
2008-03-05 05:53:04 -05:00
|
|
|
#
|
|
|
|
# Some of these +options+ include:
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:accept</tt> - Set this to a string or an array of strings describing the
|
2010-01-30 16:20:00 -05:00
|
|
|
# allowable CSS classes that the +draggable_element+ must have in order
|
2008-05-25 07:29:00 -04:00
|
|
|
# to be accepted by this +drop_receiving_element+.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:confirm</tt> - Adds a confirmation dialog. Example:
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# :confirm => "Are you sure you want to do this?"
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:hoverclass</tt> - If set, the +drop_receiving_element+ will have
|
|
|
|
# this additional CSS class when an accepted +draggable_element+ is
|
2010-01-30 16:20:00 -05:00
|
|
|
# hovered over it.
|
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:onDrop</tt> - Called when a +draggable_element+ is dropped onto
|
2010-01-30 16:20:00 -05:00
|
|
|
# this element. Override this callback with a JavaScript expression to
|
2008-07-16 08:00:36 -04:00
|
|
|
# change the default drop behaviour. Example:
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# :onDrop => "function(draggable_element, droppable_element, event) { alert('I like bananas') }"
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# 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.
|
2010-01-30 16:20:00 -05:00
|
|
|
#
|
2008-05-25 07:29:00 -04:00
|
|
|
# * <tt>:with</tt> - A JavaScript expression specifying the parameters for
|
|
|
|
# the XMLHttpRequest. Any expressions should return a valid URL query string.
|
2005-11-14 17:28:47 -05:00
|
|
|
def drop_receiving_element(element_id, options = {})
|
2006-02-26 09:20:21 -05:00
|
|
|
javascript_tag(drop_receiving_element_js(element_id, options).chop!)
|
|
|
|
end
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2006-02-26 09:20:21 -05:00
|
|
|
def drop_receiving_element_js(element_id, options = {}) #:nodoc:
|
2005-11-14 17:28:47 -05:00
|
|
|
options[:with] ||= "'id=' + encodeURIComponent(element.id)"
|
|
|
|
options[:onDrop] ||= "function(element){" + remote_function(options) + "}"
|
|
|
|
options.delete_if { |key, value| PrototypeHelper::AJAX_OPTIONS.include?(key) }
|
2006-02-26 09:20:21 -05:00
|
|
|
|
2010-01-30 16:20:00 -05:00
|
|
|
options[:accept] = array_or_string_for_javascript(options[:accept]) if options[:accept]
|
2005-11-14 17:28:47 -05:00
|
|
|
options[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass]
|
2010-01-30 16:20:00 -05:00
|
|
|
|
2008-02-15 19:07:52 -05:00
|
|
|
# Confirmation happens during the onDrop callback, so it can be removed from the options
|
|
|
|
options.delete(:confirm) if options[:confirm]
|
|
|
|
|
2009-04-23 03:08:40 -04:00
|
|
|
%(Droppables.add(#{ActiveSupport::JSON.encode(element_id)}, #{options_for_javascript(options)});)
|
2005-11-14 17:28:47 -05:00
|
|
|
end
|
2010-01-30 17:13:49 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
def array_or_string_for_javascript(option)
|
|
|
|
if option.kind_of?(Array)
|
|
|
|
"['#{option.join('\',\'')}']"
|
|
|
|
elsif !option.nil?
|
|
|
|
"'#{option}'"
|
|
|
|
end
|
|
|
|
end
|
2005-11-14 17:28:47 -05:00
|
|
|
end
|
2010-01-30 17:02:26 -05:00
|
|
|
|
|
|
|
module PrototypeHelper
|
|
|
|
class JavaScriptGenerator
|
|
|
|
module GeneratorMethods
|
|
|
|
# Starts a script.aculo.us visual effect. See
|
|
|
|
# ActionView::Helpers::ScriptaculousHelper for more information.
|
|
|
|
def visual_effect(name, id = nil, options = {})
|
|
|
|
record @context.send(:visual_effect, name, id, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a script.aculo.us sortable element. Useful
|
|
|
|
# to recreate sortable elements after items get added
|
|
|
|
# or deleted.
|
|
|
|
# See ActionView::Helpers::ScriptaculousHelper for more information.
|
|
|
|
def sortable(id, options = {})
|
|
|
|
record @context.send(:sortable_element_js, id, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a script.aculo.us draggable element.
|
|
|
|
# See ActionView::Helpers::ScriptaculousHelper for more information.
|
|
|
|
def draggable(id, options = {})
|
|
|
|
record @context.send(:draggable_element_js, id, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a script.aculo.us drop receiving element.
|
|
|
|
# See ActionView::Helpers::ScriptaculousHelper for more information.
|
|
|
|
def drop_receiving(id, options = {})
|
|
|
|
record @context.send(:drop_receiving_element_js, id, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-11-14 17:28:47 -05:00
|
|
|
end
|
|
|
|
end
|