Skip to content
Browse files

Added JavascriptHelper#draggable_element and JavascriptHelper#drop_re…

…ceiving_element to facilitate easy dragging and dropping through the script.aculo.us libraries #1578 [Thomas Fuchs]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1606 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent c206071 commit 50f0a736242e65408c06b62b0fbd68e2ea349c23 @dhh dhh committed
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added JavascriptHelper#draggable_element and JavascriptHelper#drop_receiving_element to facilitate easy dragging and dropping through the script.aculo.us libraries #1578 [Thomas Fuchs]
+
* Added that UrlHelper#mail_to will now also encode the default link title #749 [f.svehla@gmail.com]
* Removed the default option of wrap=virtual on FormHelper#text_area to ensure XHTML compatibility #1300 [thomas@columbus.rr.com]
View
2 actionpack/lib/action_controller/auto_complete.rb
@@ -29,7 +29,7 @@ module ClassMethods
def auto_complete_for(object, method, options = {})
define_method("auto_complete_for_#{object}_#{method}") do
find_options = {
- :conditions => [ "LOWER(#{method}) LIKE ?", '%' + request.raw_post.downcase + '%' ],
+ :conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method] + '%' ],
:order => "#{method} ASC",
:limit => 10 }.merge!(options)
View
57 actionpack/lib/action_view/helpers/javascript_helper.rb
@@ -18,7 +18,8 @@ module JavaScriptHelper
unless const_defined? :CALLBACKS
CALLBACKS =
[:uninitialized, :loading, :loaded, :interactive, :complete, :failure].push((100..599).to_a).flatten
- AJAX_OPTIONS = [ :url, :asynchronous, :method, :insertion, :form, :with, :update ].concat(CALLBACKS)
+ AJAX_OPTIONS = [ :before, :after, :condition, :url, :asynchronous, :method,
+ :insertion, :position, :form, :with, :update, :script ].concat(CALLBACKS)
JAVASCRIPT_PATH = File.join(File.dirname(__FILE__), 'javascripts')
end
@@ -276,8 +277,7 @@ def observe_form(form_id, options = {})
# Defaults to field_id + '_auto_complete'
# <tt>:with</tt>:: A JavaScript expression specifying the
# parameters for the XMLHttpRequest. This defaults
- # to 'value', which in the evaluated context
- # refers to the new field value.
+ # to 'fieldname=value'.
# <tt>:indicator</tt>:: Specifies the DOM ID of an elment which will be
# displayed while autocomplete is running.
def auto_complete_field(field_id, options = {})
@@ -287,8 +287,8 @@ def auto_complete_field(field_id, options = {})
function << "'#{url_for(options[:url])}'"
js_options = {}
- js_options[:callback] = "function(element, value) { return #{options[:with]} }" if options[:with]
- js_options[:indicator] = "'#{options[:indicator]}'" if options[:indicator]
+ js_options[:callback] = "function(element, value) { return #{options[:with]} }" if options[:with]
+ js_options[:indicator] = "'#{options[:indicator]}'" if options[:indicator]
function << (', ' + options_for_javascript(js_options) + ')')
javascript_tag(function)
@@ -334,10 +334,19 @@ def text_field_with_auto_complete(object, method, tag_options = {}, completion_o
# :url => { :action => "reload" },
# :complete => visual_effect(:highlight, "posts", :duration => 0.5 )
#
+ # 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_receving_element (...), :loading => visual_effect(:fade) %>
+ #
+ # This would fade the element that was dropped on the drop receiving element.
+ #
# You can change the behaviour with various options, see
# http://script.aculo.us for more documentation.
- def visual_effect(name, element_id, js_options = {})
- "new Effect.#{name.to_s.capitalize}('#{element_id}',#{options_for_javascript(js_options)});"
+ def visual_effect(name, element_id = false, js_options = {})
+ element = element_id ? "'#{element_id}'" : "element"
+ "new Effect.#{name.to_s.capitalize}(#{element},#{options_for_javascript(js_options)});"
end
# Makes the element with the DOM ID specified by +element_id+ sortable
@@ -361,6 +370,38 @@ def sortable_element(element_id, options = {})
javascript_tag("Sortable.create('#{element_id}', #{options_for_javascript(options)})")
end
+
+ # 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
+ # http://script.aculo.us for more documentation.
+ def draggable_element(element_id, options = {})
+ javascript_tag("new Draggable('#{element_id}', #{options_for_javascript(options)})")
+ end
+
+ # 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
+ # http://script.aculo.us for more documentation.
+ def drop_receiving_element(element_id, options = {})
+ options[:with] ||= "'id=' + encodeURIComponent(element.id)"
+ options[:onDrop] ||= "function(element){" + remote_function(options) + "}"
+ options.delete_if { |key, value| AJAX_OPTIONS.include?(key) }
+
+ options[:accept] = "'#{options[:accept]}'" if options[:accept]
+ options[:hoverclass] = "'#{options[:hoverclass]}'" if options[:hoverclass]
+
+ javascript_tag("Droppables.add('#{element_id}', #{options_for_javascript(options)})")
+ end
# Escape carrier returns and single and double quotes for JavaScript segments.
def escape_javascript(javascript)
@@ -384,7 +425,7 @@ def options_for_ajax(options)
js_options['asynchronous'] = options[:type] != :synchronous
js_options['method'] = method_option_to_s(options[:method]) if options[:method]
js_options['insertion'] = "Insertion.#{options[:position].to_s.camelize}" if options[:position]
- js_options['evalScripts'] = options[:script] == true if options[:script]
+ js_options['evalScripts'] = options[:script].nil? || options[:script]
if options[:form]
js_options['parameters'] = 'Form.serialize(this)'
View
2 actionpack/lib/action_view/helpers/javascripts/controls.js
@@ -116,7 +116,7 @@ Ajax.Autocompleter.prototype = (new Ajax.Base()).extend({
this.startIndicator();
this.options.parameters = this.options.callback ?
this.options.callback(this.element, Form.Element.getValue(this.element)) :
- Form.Element.getValue(this.element);
+ Form.Element.serialize(this.element);
new Ajax.Request(this.url, this.options);
} else {
this.active = false;

0 comments on commit 50f0a73

Please sign in to comment.
Something went wrong with that request. Please try again.